package jnpf.notice.controller.app;


import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.model.notice.dto.FtbNoticeAnnouncementsReceiveDto;
import jnpf.model.notice.dto.MyNoticeAnnouncementsDetailDto;
import jnpf.model.notice.dto.MyNoticeAnnouncementsDto;
import jnpf.model.notice.dto.MyNoticeAnnouncementsMsgDto;
import jnpf.model.notice.enums.NoticeEnums;
import jnpf.model.notice.req.announcement.AppQueryMyAnnouncementListReq;
import jnpf.model.notice.req.announcement.QueryUserListReq;
import jnpf.model.personnels.req.employment.BatchByPrimaryIdReq;
import jnpf.notice.service.FtbNoticeAnnouncementsReceiveService;
import jnpf.notice.service.FtbNoticeAnnouncementsService;
import jnpf.util.FtbUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * app 我的公告模块
 *
 * @author xgl
 * @since 2024-05-08 09:51:26
 */
@RestController
@RequestMapping("/app/myNoticeAnnouncements")
public class AppMyNoticeAnnouncementsController {
    /**
     * 服务对象
     */
    @Autowired
    private FtbNoticeAnnouncementsService ftbNoticeAnnouncementsService;

    @Autowired
    private FtbNoticeAnnouncementsReceiveService ftbNoticeAnnouncementsReceiveService;

    /**
     * App查询最新的N条公告
     *
     * @param num 最新公告数据量
     * @return {@link ActionResult}<{@link MyNoticeAnnouncementsDto}>
     */
    @GetMapping("/queryLatest/{num}")
    public ActionResult<List<MyNoticeAnnouncementsDto>> queryLatest(@PathVariable("num") Integer num) {
        return ActionResult.success("查询成功", ftbNoticeAnnouncementsService.queryMyLatest(UserProvider.getLoginUserId(), num));
    }

    /**
     * 查询我的公告列表
     *
     * @param req
     * @return {@link ActionResult}<{@link MyNoticeAnnouncementsDto}>
     */
    @GetMapping("/queryList")
    public ActionResult<PageListVO<MyNoticeAnnouncementsDto>> pageLists(@Validated AppQueryMyAnnouncementListReq req) {
        PageInfo<MyNoticeAnnouncementsDto> pageVo = ftbNoticeAnnouncementsService.getAppMyPageList(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }

    /**
     * 查询公告详情
     *
     * @param announcementId 公告ID
     * @return {@link ActionResult}
     */
    @GetMapping("/get/{announcementId}")
    public ActionResult<MyNoticeAnnouncementsDetailDto> get(@PathVariable("announcementId") String announcementId) {
        return ActionResult.success("成功", ftbNoticeAnnouncementsService.getMyAnnouncementInfo(announcementId, UserProvider.getLoginUserId()));
    }

    /**
     * 查询公告中已读/未读人员列表
     *
     * @param req
     * @return {@link ActionResult}<{@link FtbNoticeAnnouncementsReceiveDto}>
     */
    @GetMapping("/queryUser")
    public ActionResult<PageListVO<FtbNoticeAnnouncementsReceiveDto>> queryUserList(@Validated QueryUserListReq req) {
        PageInfo<FtbNoticeAnnouncementsReceiveDto> pageVo = ftbNoticeAnnouncementsReceiveService.queryUserListForReadStatus(req, false);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }


    /**
     * 轮训我的未读公告
     *
     * @param num 公告数量,0查询所有 1：查询1条 2 查询2条
     * @return {@link ActionResult}
     */
    @GetMapping("/queryAllNoRead/{num}")
    public ActionResult<List<MyNoticeAnnouncementsMsgDto>> queryAllNoRead(@PathVariable("num") Integer num) {
        //0:全部 1：非必读，2：必读
        return ActionResult.success("成功", ftbNoticeAnnouncementsService.queryAllMyNoRead(UserProvider.getLoginUserId(), num, NoticeEnums.NeedReadStatus.REQUIRED.getCode()));
    }

    /**
     * 轮训我的未读公告
     *
     * @param num    公告数量,0查询所有 1：查询1条 2 查询2条
     * @param status 0-全部  1-非必读  2-必读
     * @return {@link ActionResult}
     */
    @GetMapping("/queryAllNoRead/{num}/{status}")
    public ActionResult<List<MyNoticeAnnouncementsMsgDto>> queryAllNoReadAndStatus(@PathVariable("num") Integer num, @PathVariable("status") Integer status) {
        //0:全部 1：非必读，2：必读
        return ActionResult.success("成功", ftbNoticeAnnouncementsService.queryAllMyNoRead(UserProvider.getLoginUserId(), num, status));
    }

    /**
     * 公告完成阅读接口
     *
     * @param announcementId 公告ID
     * @return {@link ActionResult}
     */
    @GetMapping("/completeRead/{announcementId}")
    public ActionResult<Boolean> completeRead(@PathVariable("announcementId") String announcementId) {
        ftbNoticeAnnouncementsService.completeRead(announcementId, UserProvider.getLoginUserId());
        return ActionResult.success("成功", true);
    }

    /**
     * 检测公告是否已读，返回未读列表
     *
     * @param req
     * @return
     */
    @PostMapping("/queryReadStatusForAnnouncementIds")
    public ActionResult<List<String>> queryReadStatusForAnnouncementIds(@RequestBody @Validated BatchByPrimaryIdReq req) {
        if (CollectionUtil.isEmpty(req.getIds())) {
            throw new IllegalArgumentException("参数错误");
        }
        return ActionResult.success("成功", ftbNoticeAnnouncementsReceiveService.checkIsRead(UserProvider.getLoginUserId(), req.getIds()));
    }

}

