package com.hzs.system.base.controller.api;

import com.hzs.common.core.enums.ENoticeFunctionType;
import com.hzs.common.core.enums.ENoticeWebsiteType;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.system.base.BdNotice;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.service.IBdFeedbackService;
import com.hzs.system.base.service.IBdNoticeService;
import com.hzs.system.base.vo.BdNoticeCountVO;
import com.hzs.system.base.vo.BdNoticeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 公告、站内信
 *
 * @Description:
 * @Author: ljc
 * @Time: 2023/2/6 16:52
 * @Classname: ApiNoticeController
 * @Package_name: com.hzs.system.base.controller.api
 */
@RestController
@RequestMapping("/api/notice")
public class ApiNoticeController extends BaseController {

    @Autowired
    IBdNoticeService noticeService;

    @Autowired
    IBdFeedbackService bdFeedbackService;

    /**
     * 首页显示公告、站内信
     *
     * @param functionType 1：公告 2：站内信
     * @return TableDataInfo
     */
    @GetMapping("index-list")
    public TableDataInfo indexList(Integer functionType) {
        BdNoticeVO noticeVO = new BdNoticeVO();
        noticeVO.setPkCountry(SecurityUtils.getPkCountry());
        noticeVO.setPkMember(SecurityUtils.getUserId());
        noticeVO.setWebsiteType(ENoticeWebsiteType.MEMBER.getValue());
        noticeVO.setFunctionType(functionType);
        startPage();
        Object[] objects = noticeService.showIndexNotice(noticeVO);

        TableDataInfo tableDataInfo = getDataTable((List<BdNotice>) objects[0]);
        tableDataInfo.setRows((List<BdNoticeVO>) objects[1]);
        return tableDataInfo;
    }


    /**
     * 首页显示公告弹窗
     *
     * @return TableDataInfo
     */
    @GetMapping("index-pop-screen")
    public AjaxResult indexPopScreen(String grade, String awards) {
        BdNoticeVO noticeVO = new BdNoticeVO();
        noticeVO.setPkCountry(SecurityUtils.getPkCountry());
        noticeVO.setWebsiteType(ENoticeWebsiteType.MEMBER.getValue());
        noticeVO.setFunctionType(ENoticeFunctionType.NOTICE.getValue());
        noticeVO.setGrade(grade);
        noticeVO.setAwards(awards);
        return AjaxResult.success(noticeService.showIndexPopScreen(noticeVO));
    }

    /**
     * 会员首页 公告、站内信、意见反馈数
     *
     * @return
     */
    @GetMapping("index-count")
    public AjaxResult indexMessageCount() {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Integer websiteType = ENoticeWebsiteType.MEMBER.getValue();
        //查询公告数
        BdNoticeVO noticeVO = BdNoticeVO.builder().pkCountry(pkCountry)
                .websiteType(websiteType).functionType(ENoticeFunctionType.NOTICE.getValue()).build();
        Integer noticeNum = noticeService.showIndexNoticeCount(noticeVO);
        //查询意见反馈数
        Integer feedbackNum = bdFeedbackService.selectIndexFeedbackCount(pkCountry,SecurityUtils.getUserId());
        BdNoticeCountVO noticeCountVO = BdNoticeCountVO.builder()
                .noticeNum(noticeNum)
                .feedbackNum(feedbackNum).build();
        return AjaxResult.success(noticeCountVO);
    }

    @GetMapping("index-detail")
    public AjaxResult index(Long pkId) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long pkMember = SecurityUtils.getUserId();
        return AjaxResult.success(noticeService.showIndexNoticeDetail(pkId,pkCountry,pkMember));
    }
}
