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.enums.ESystemType;
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.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.Arrays;
import java.util.List;

/**
 * 公告、站内信
 */
@RestController
@RequestMapping("/api/notice")
public class ApiNoticeController extends BaseController {

    @Autowired
    private IBdNoticeService noticeService;

    /**
     * 首页显示公告、站内信
     *
     * @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);
        // 查询直销和全部的
        noticeVO.setSystemTypeList(Arrays.asList(SecurityUtils.getSystemType(), ESystemType.ALL.getValue()));

        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);
        // 查询直销和全部的
        noticeVO.setSystemTypeList(Arrays.asList(SecurityUtils.getSystemType(), ESystemType.ALL.getValue()));
        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())
                .systemTypeList(Arrays.asList(SecurityUtils.getSystemType(), ESystemType.ALL.getValue())).build();
        Integer noticeNum = noticeService.showIndexNoticeCount(noticeVO);
        //查询站内信数
        BdNoticeVO stationVO = BdNoticeVO.builder().pkCountry(pkCountry).pkMember(SecurityUtils.getUserId())
                .websiteType(websiteType).functionType(ENoticeFunctionType.MAIL.getValue()).build();
        Integer stationNum = noticeService.showIndexMailCount(stationVO);
        BdNoticeCountVO noticeCountVO = BdNoticeCountVO.builder()
                .noticeNum(noticeNum).stationNum(stationNum).build();
        return AjaxResult.success(noticeCountVO);
    }

    /**
     * 会员首页站内信未读条数
     *
     * @return
     */
    @GetMapping("index-mail-count")
    public AjaxResult indexMailCount() {
        Integer pkCountry = SecurityUtils.getPkCountry();
        Integer websiteType = ENoticeWebsiteType.MEMBER.getValue();
        //查询公告数
        BdNoticeVO noticeVO = BdNoticeVO.builder().pkCountry(pkCountry)
                .websiteType(websiteType).functionType(ENoticeFunctionType.MAIL.getValue())
                .pkMember(SecurityUtils.getUserId()).build();
        return AjaxResult.success(noticeService.showIndexMailCount(noticeVO));
    }

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


    /**
     * 首页显示站内信弹窗
     *
     * @return TableDataInfo
     */
    @GetMapping("index-pop-mail")
    public AjaxResult indexMailPopScreen() {
        return AjaxResult.success(noticeService.indexMailPopScreen(SecurityUtils.getUserId(), SecurityUtils.getPkCountry()));
    }
}
