package com.ynfy.app.api.v1.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ynfy.app.api.v1.annoation.IgnoreAuth;
import com.ynfy.buss.article.service.IArticleService;
import com.ynfy.buss.banner.service.IBannerService;
import com.ynfy.buss.course.course.service.ICourseService;
import com.ynfy.buss.exam.exam.entity.Exam;
import com.ynfy.buss.exam.exam.service.IExamService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.TokenUtil;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.entity.SysAnnouncementSend;
import org.jeecg.modules.system.service.ISysAnnouncementSendService;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/api/v1/index")
public class ApiIndexController extends ApiBaseController {

    @Autowired
    private IBannerService bannerService;

    @Autowired
    private ISysAnnouncementService announcementService;

    @Autowired
    private ISysAnnouncementSendService announcementSendService;

    @Autowired
    private ICourseService courseService;

    @Autowired
    private IExamService examService;

    @Autowired
    private IArticleService articleService;

    @IgnoreAuth
    @PostMapping("/banner/list")
    public Result<?> list() {
        ;
        return Result.OK(bannerService.list());
    }

    /**
     * 查询通知公告
     *
     * @return
     */
    @IgnoreAuth
    @PostMapping("/announcement/list")
    public Result<?> announcementList() {
        String userId = TokenUtil.getUserId(TokenUtil.getToken(request));
        // 1.将系统消息补充到用户通告阅读标记表中
        LambdaQueryWrapper<SysAnnouncement> querySaWrapper = new LambdaQueryWrapper<SysAnnouncement>();
        //全部人员
        querySaWrapper.eq(SysAnnouncement::getMsgType, CommonConstant.MSG_TYPE_ALL);
        //未删除
        querySaWrapper.eq(SysAnnouncement::getDelFlag, CommonConstant.DEL_FLAG_0.toString());
        //已发布
        querySaWrapper.eq(SysAnnouncement::getSendStatus, CommonConstant.HAS_SEND);
        if (StringUtils.isNotBlank(userId)) {
            querySaWrapper.notInSql(SysAnnouncement::getId, "select annt_id from sys_announcement_send where user_id='" + userId + "'");
        }
        List<SysAnnouncement> announcements = announcementService.list(querySaWrapper);
        if (!CollectionUtils.isEmpty(announcements) && StringUtils.isNotBlank(userId)) {
            for (SysAnnouncement ann : announcements) {
                LambdaQueryWrapper<SysAnnouncementSend> query = new LambdaQueryWrapper<>();
                query.eq(SysAnnouncementSend::getAnntId, ann.getId());
                query.eq(SysAnnouncementSend::getUserId, userId);
                SysAnnouncementSend sendUser = announcementSendService.getOne(query);
                if (Objects.isNull(sendUser)) {
                    SysAnnouncementSend announcementSend = new SysAnnouncementSend();
                    announcementSend.setAnntId(ann.getId());
                    announcementSend.setUserId(userId);
                    announcementSend.setReadFlag(CommonConstant.NO_READ_FLAG);
                    announcementSendService.save(announcementSend);
                }
            }
        }

        SysAnnouncement announcement = new SysAnnouncement();
        announcement.setUserId(userId);
        List<SysAnnouncement> pageList = announcementService.listNoRead(announcement);
        return Result.OK(pageList);
    }

    /**
     * 查看通知公告
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/viewMessage")
    public Result<?> viewMessage(@RequestParam String id) {
        String userId = TokenUtil.getUserId(TokenUtil.getToken(request));
        SysAnnouncement announcement = announcementService.getById(id);

        if (!Objects.isNull(announcement)) {
            if (StringUtils.isNotBlank(userId)) {
                //阅读通知公告
                announcementService.readMessage(id, userId);
            }
            return Result.OK(announcement);
        }
        return null;
    }


    /**
     * 热门课程
     *
     * @return
     */
    @IgnoreAuth
    @GetMapping("/listHotCourse")
    public Result<?> listHotCourse(Integer limit) {
        return Result.OK(courseService.listHotCourse(limit));
    }

    /**
     * 最新考试
     *
     * @return
     */
    @IgnoreAuth
    @GetMapping("/listLatestExam")
    public Result<?> listLatestExam(Integer limit) {
        List<Exam> examList = examService.listLatestExam(limit, TokenUtil.getUserId(TokenUtil.getToken(request)));
        if (!CollectionUtils.isEmpty(examList)) {
            examList.forEach(item -> item.setAppShowTime(DateUtil.parseDate(DateUtil.formatDate(!Objects.isNull(item.getUpdateTime())
                    ? item.getUpdateTime() : item.getCreateTime()))));
        }
        return Result.OK(examList);
    }

    /**
     * 推荐资讯
     *
     * @return
     */
    @IgnoreAuth
    @GetMapping("/listRecommendArticle")
    public Result<?> listRecommendArticle(Integer limit) {
        return Result.OK(articleService.listRecommendArticle(limit));
    }

    /**
     * 查看资讯
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/viewArticle")
    public Result<?> viewArticle(@RequestParam String id) {
        return Result.OK(articleService.getById(id));
    }

}
