package com.ruoyi.cms.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.cms.domain.*;
import com.ruoyi.cms.domain.vo.NoticeTotal;
import com.ruoyi.cms.mapper.*;
import com.ruoyi.cms.service.IAppNoticeService;
import com.ruoyi.cms.util.notice.NoticeUtils;
import com.ruoyi.cms.util.StringUtil;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.utils.SiteSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 公告 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class AppNoticeServiceImpl implements IAppNoticeService
{
    @Autowired
    private AppNoticeMapper noticeMapper;
    @Autowired
    private JobRecommentMapper jobRecommentMapper;
    @Autowired
    private FairCollectionMapper fairCollectionMapper;
    @Autowired
    private AppNoticeMapper appNoticeMapper;
    @Autowired
    private JobFairMapper jobFairMapper;
    @Autowired
    private NoticeMapper noticeInfoMapper;
    @Autowired
    private AppUserMapper appUserMapper;

    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public AppNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<AppNotice> selectNoticeList(AppNotice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(AppNotice notice)
    {
        return noticeMapper.insertNotice(notice);
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(AppNotice notice)
    {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     * 
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId)
    {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     * 
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    @Override
    public void recommendJobCron() {
        List<AppUser> appUsers = appUserMapper.selectList(null);
        appUsers.forEach(appUser -> {
            if(appUser.getIsRecommend()==1){
                //todo complete  logic of recommend
                recommendJobForUser(appUser);
            }
        });
    }

    private void recommendJobForUser(AppUser appUser) {
        jobRecommentMapper.delete(Wrappers.<JobRecomment>lambdaQuery().eq(JobRecomment::getUserId, appUser.getUserId()));
        jobRecommentMapper.recommendJobForUser(appUser.getUserId());
        noticeInfoMapper.delete(Wrappers.<Notice>lambdaQuery().eq(Notice::getUserId, appUser.getUserId()).eq(Notice::getTitle,"职位上新"));
        Notice notice = new Notice();
        notice.setUserId(appUser.getUserId());
        notice.setTitle("职位上新");
        notice.setIsRead(NoticeUtils.NOTICE_WD);
        notice.setDate(new Date());
        //todo update number of job
        notice.setSubTitle("这里有10个岗位很适合你，快来看看吧");
        notice.setNotReadCount(10);
        noticeInfoMapper.insert(notice);
    }

    @Override
    public void fairCompanyCron() {
        List<AppUser> appUsers = appUserMapper.selectList(null);
        appUsers.forEach(appUser -> {
            if(appUser.getIsRecommend()==1){
                //todo complete  logic of recommend
                fairCompany(appUser);
            }
        });
    }

    private void fairCompany(AppUser appUser) {
        List<FairCollection> fairCollections = fairCollectionMapper.selectList(Wrappers.<FairCollection>lambdaQuery().eq(FairCollection::getUserId, appUser.getUserId()).orderByDesc(FairCollection::getCreateTime));

    }

    @Override
    public List<Notice> listNotRead(Integer isRead) {

        Long userId = SiteSecurityUtils.getUserId();
        List<Notice> sysNoticeList = noticeInfoMapper.selectList(Wrappers.<Notice>lambdaQuery().eq(Notice::getUserId, userId).eq(Notice::getRemark,"notice_bar").orderByDesc(Notice::getCreateTime));
        return sysNoticeList;
    }

    @Override
    public List<Job> recommend(String jobTitle) {
        return appNoticeMapper.recommend(jobTitle,SiteSecurityUtils.getUserId());
    }

    @Override
    public void sysNotice(String ids) {
        List<Long> longs = StringUtil.convertStringToLongList(ids);
        List<Notice> notices = noticeInfoMapper.selectBatchIds(longs);
        notices.forEach(notice -> {
            notice.setIsRead(NoticeUtils.NOTICE_YD);
            noticeInfoMapper.updateById(notice);
            });
        List<Notice> notices1 = noticeInfoMapper.selectList(Wrappers.<Notice>lambdaQuery().eq(Notice::getUserId, SiteSecurityUtils.getUserId()).eq(Notice::getRemark, "notice_bar").eq(Notice::getTitle, "系统通知").orderByDesc(Notice::getCreateTime).last("limit 1"));
        Notice notice = notices1.get(0);
        notice.setNotReadCount(notice.getNotReadCount() - longs.size());
        if(notice.getNotReadCount()==0){
            notice.setIsRead(NoticeUtils.NOTICE_YD);
        }
        noticeInfoMapper.updateById(notice);
    }

    @Override
    public void read(String ids) {
        List<Long> longs = StringUtil.convertStringToLongList(ids);
        List<Notice> notices = noticeInfoMapper.selectBatchIds(longs);
        notices.forEach(notice -> {
            notice.setIsRead(NoticeUtils.NOTICE_YD);
            notice.setNotReadCount(0);
            noticeInfoMapper.updateById(notice);
        });
    }

    @Override
    public List<Notice> sysNoticeList() {
        Long userId = SiteSecurityUtils.getUserId();
        List<Notice> sysNoticeList = noticeInfoMapper.selectList(Wrappers.<Notice>lambdaQuery().eq(Notice::getUserId, userId).isNotNull(Notice::getNoticeType).notIn(Notice::getNoticeType,"").orderByDesc(Notice::getCreateTime));
        return sysNoticeList;
    }

    @Override
    public List<Notice> selectListAppNotRead(Notice notice) {
        notice.setRemark(NoticeUtils.NOTICE_REMARK);
        notice.setIsRead(NoticeUtils.NOTICE_WD);
        //List<Notice> sysNoticeList = noticeInfoMapper.selectList(Wrappers.<Notice>lambdaQuery().eq(Notice::getUserId, notice.getUserId()).eq(Notice::getRemark,"notice_bar").eq(Notice::getIsRead,0).orderByDesc(Notice::getCreateTime));
        return noticeInfoMapper.getNoticlist(notice);
    }

    @Override
    public void readSysNotices(String ids,Long userId) {
        List<Long> longs = StringUtil.convertStringToLongList(ids);
        List<Notice> notices = noticeInfoMapper.selectBatchIds(longs);
        notices.forEach(notice -> {
            Notice upNotic=new Notice();
            upNotic.setNoticeId(notice.getNoticeId());
            upNotic.setIsRead(NoticeUtils.NOTICE_YD);
            noticeInfoMapper.updateById(upNotic);
        });
        List<Notice> notices1 = noticeInfoMapper.selectList(Wrappers.<Notice>lambdaQuery().eq(Notice::getUserId, userId).eq(Notice::getRemark, "notice_bar").orderByDesc(Notice::getCreateTime).last("limit 1"));
        if(notices1!=null&&notices1.size()>0){
            Notice parmNotice = notices1.get(0);
            Notice notice=new Notice();
            notice.setNoticeId(parmNotice.getNoticeId());
            notice.setNotReadCount(parmNotice.getNotReadCount()!=null&&parmNotice.getNotReadCount()!=0?parmNotice.getNotReadCount() - longs.size():0);
            if(notice.getNotReadCount()==0){
                notice.setIsRead(NoticeUtils.NOTICE_YD);
            }
            noticeInfoMapper.updateById(notice);
        }
    }

    @Override
    public List<Notice> selectListAppNotics(Notice notice) {
        notice.setRemark(NoticeUtils.NOTICE_REMARK);
        return noticeInfoMapper.getNoticlist(notice);
    }

    public List<Notice> selectListAppNotices(Notice notice){
        return noticeInfoMapper.getNoticlist(notice);
    }

    public NoticeTotal noticTotal(Notice notice){
        return noticeInfoMapper.noticTotal(notice);
    }

    public int removeNotice(Long[] ids){
        int t=noticeInfoMapper.deleteBatchIds(Arrays.asList(ids));
        return t;
    }
}

