package com.hxhq.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxhq.business.domain.Notice;
import com.hxhq.business.dto.study.StudyListDto;
import com.hxhq.business.enums.NormalEnum;
import com.hxhq.business.enums.study.StudyTypeEnum;
import com.hxhq.business.form.study.StudySearchForm;
import com.hxhq.business.service.INoticeService;
import com.hxhq.business.service.IStudyService;
import com.hxhq.common.core.web.controller.BaseController;
import com.hxhq.common.core.web.domain.AjaxResult;
import com.hxhq.common.core.web.page.TableDataInfo;
import com.hxhq.common.security.annotation.RequiresPermissions;
import com.hxhq.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 首页Controller
 *
 * @author hxhq
 * @date 2025-12-24
 */
@RestController
@RequestMapping("/business/home")
public class HomeController extends BaseController
{
    @Autowired
    private IStudyService studyService;
    @Autowired
    private INoticeService noticeService;

    /**
     * 查询首页数量
     */
    @GetMapping("/count")
    public AjaxResult list()
    {
        return AjaxResult.success(studyService.queryHomeCount());
    }

    /**
     * 查询通知列表
     */
    @GetMapping("/noticeList")
    public TableDataInfo noticeService()
    {
        startPage();
        List<Notice> list = noticeService.queryList();
        TableDataInfo table = getDataTable(list);
        return table;
    }

    /**
     * 通知已读
     */
    @PostMapping("/noticeRead")
    public AjaxResult noticeRead(@RequestBody Notice notice)
    {
        Notice info = noticeService.getById(notice.getId());
        if(info!=null && info.getStatus().equals(NormalEnum.no.getValue())){
            info.setStatus(NormalEnum.yes.getValue());
            noticeService.updateById(info);
        }
        return AjaxResult.success();
    }

    /**
     * 查询未读通知数量
     */
    @GetMapping("/noticeCount")
    public AjaxResult noticeCount()
    {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", SecurityUtils.getUserId());
        queryWrapper.eq("status", NormalEnum.no.getValue());
        Map<String,Object> count = new HashMap<>(1);
        count.put("noticeCount",noticeService.count(queryWrapper));
        return AjaxResult.success(count);
    }
}
