package com.xjrsoft.module.oa.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.oa.dto.AddNewsDto;
import com.xjrsoft.module.oa.dto.ChangeNewsDto;
import com.xjrsoft.module.oa.dto.DeleteNewsRelationDto;
import com.xjrsoft.module.oa.dto.NewsPageDto;
import com.xjrsoft.module.oa.dto.NewsRelationPageDto;
import com.xjrsoft.module.oa.dto.NewsReplyDto;
import com.xjrsoft.module.oa.dto.UpdateNewsDto;
import com.xjrsoft.module.oa.dto.UpdateNewsRelationDto;
import com.xjrsoft.module.oa.entity.News;
import com.xjrsoft.module.oa.entity.NewsAppendix;
import com.xjrsoft.module.oa.entity.NewsRelation;
import com.xjrsoft.module.oa.entity.NewsRelationConfig;
import com.xjrsoft.module.oa.service.INewsAppendixService;
import com.xjrsoft.module.oa.service.INewsRelationConfigService;
import com.xjrsoft.module.oa.service.INewsRelationService;
import com.xjrsoft.module.oa.service.INewsService;
import com.xjrsoft.module.oa.vo.NewsAppendixVo;
import com.xjrsoft.module.oa.vo.NewsCountTypeVo;
import com.xjrsoft.module.oa.vo.NewsPageVo;
import com.xjrsoft.module.oa.vo.NewsRelationConfigVo;
import com.xjrsoft.module.oa.vo.NewsRelationVo;
import com.xjrsoft.module.oa.vo.NewsVo;
import com.xjrsoft.module.organization.entity.Department;
import com.xjrsoft.module.organization.mapper.DepartmentMapper;
import com.xjrsoft.module.system.entity.File;
import com.xjrsoft.module.system.entity.SystemUpdateMessage;
import com.xjrsoft.module.system.entity.SystemUpdateMessageNotice;
import com.xjrsoft.module.system.service.ISystemUpdateMessageService;
import com.xjrsoft.module.system.vo.SystemUpdateMessagePageVo;
import com.xjrsoft.module.teacher.entity.XjrUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 新闻
 * @author zlf
 * @since 2022-06-16
 */
@RestController
@RequestMapping(GlobalConstant.OA_MODULE_PREFIX + "/news")
@Api(value = GlobalConstant.OA_MODULE_PREFIX + "/news", tags = "新闻")
@AllArgsConstructor
public class NewsController {

    private INewsService newsService;

    private INewsRelationService newsRelationService;
    private INewsAppendixService newsAppendixService;

    private DepartmentMapper departmentMapper;
    private INewsRelationConfigService newsRelationConfigService;
    private final ISystemUpdateMessageService systemUpdateMessageService;


    @GetMapping
    @ApiOperation(value = "获取新闻分页")
    public RT<PageOutput<NewsPageVo>> page(NewsPageDto dto) {
        Wrapper<News> wrapper = Wrappers.<News>query().lambda()
                .eq(News::getTypeId, dto.getType())
                .like(StrUtil.isNotBlank(dto.getKeyword()), News::getFullHead, dto.getKeyword())
                .select(News.class, x -> VoToColumnUtil.fieldsToColumns(NewsPageVo.class).contains(x.getProperty()))
                .orderByDesc(News::getSendEndDate);
        IPage<News> page = newsService.page(ConventPage.getPage(dto), wrapper);
        PageOutput<NewsPageVo> pageOutput = ConventPage.getPageOutput(page, NewsPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping("/manage-box")
    @ApiOperation(value = "获取管理新闻分页")
    public RT<PageOutput<NewsPageVo>> manageBox(NewsPageDto dto) {
        IPage<NewsPageVo> page = newsService.manageBox(dto);
        PageOutput<NewsPageVo> pageOutput = ConventPage.getPageOutput(page, NewsPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping("/receipt-box")
    @ApiOperation(value = "获取接受新闻分页")
    public RT<PageOutput<NewsPageVo>> receiptBox(NewsPageDto dto) {
        IPage<NewsPageVo> page = newsService.receiptBox(dto);
        PageOutput<NewsPageVo> pageOutput = ConventPage.getPageOutput(page, NewsPageVo.class);
        return RT.ok(pageOutput);
    }


    @PostMapping
    @ApiOperation(value = "新增新闻")
    public RT<Boolean> add(@RequestBody AddNewsDto addNewsDto) {
        return RT.ok(newsService.add(addNewsDto));
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "新闻详情")
    public RT<NewsVo> info(@RequestParam Long id, @RequestParam(required = false) Boolean isRead) {
        News news = newsService.getById(id);
        if (news == null) {
            RT.error("找不到此新闻！");
        }

        List<NewsRelationVo> newsRelationVoList = new ArrayList<>();
        MPJLambdaWrapper<NewsRelation> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .select(NewsRelation::getId)
                .selectAs(XjrUser::getName,NewsRelationVo::getUserName)
                .select(NewsRelation.class, x -> VoToColumnUtil.fieldsToColumns(NewsRelationVo.class).contains(x.getProperty()))
                .leftJoin(XjrUser.class,XjrUser::getId,NewsRelation::getUserId)
                .eq(NewsRelation::getNewsId , id)
                .disableSubLogicDel();

        if(ObjectUtil.isNotNull(news.getCreateUserId()) && StpUtil.getLoginIdAsLong() == news.getCreateUserId()){
            newsRelationVoList = newsRelationService.selectJoinList(NewsRelationVo.class, queryWrapper);
        }else{
            queryWrapper.eq(NewsRelation::getUserId , StpUtil.getLoginIdAsLong());
            NewsRelationVo newsRelationVo = newsRelationService.selectJoinOne(NewsRelationVo.class, queryWrapper);
            if(ObjectUtil.isNotNull(newsRelationVo)){
                newsRelationVoList.add(newsRelationVo);
            }
        }

        NewsVo newsVo = BeanUtil.toBean(news, NewsVo.class);
        newsVo.setRelationList(newsRelationVoList);

        Department department = departmentMapper.selectById(newsVo.getSendDeptId());
        if(department != null){
            newsVo.setSendDeptIdCN(department.getName());
        }

        if (BooleanUtils.isTrue(isRead)) {
//            NewsRelation newsRelation = new NewsRelation();
//            newsRelation.setNewsId(id);
//            newsRelation.setUserId(StpUtil.getLoginIdAsLong());
//            newsRelationService.remove(Wrappers.<NewsRelation>query().lambda()
//                    .eq(NewsRelation::getUserId, StpUtil.getLoginIdAsLong())
//                    .eq(NewsRelation::getNewsId, id));
//            newsRelationService.save(newsRelation);
        }



//        List<NewsRelationVo> newsRelationVoList = newsRelationService.selectJoinList(NewsRelationVo.class,
//                new MPJLambdaWrapper<NewsRelation>()
//                        .eq(NewsRelation::getNewsId, id)
//                        .select(NewsRelation::getId)
//                        .select(NewsRelation.class, x -> VoToColumnUtil.fieldsToColumns(NewsRelationVo.class).contains(x.getProperty()))
//                        .leftJoin(XjrUser.class, XjrUser::getId, NewsRelation::getUserId, ext -> ext.selectAs(XjrUser::getName, NewsRelationVo::getUserName)));
//        if (newsRelationVoList.size() > 0) {
//            newsVo.setRelationList(newsRelationVoList);
//        }

        List<NewsAppendixVo> newsAppendixVoList = newsAppendixService.selectJoinList(NewsAppendixVo.class,
                new MPJLambdaWrapper<NewsAppendix>().eq(NewsAppendix::getNewsId, id)
                        .select(NewsRelation::getId)
                        .select(NewsAppendix.class, x -> VoToColumnUtil.fieldsToColumns(NewsAppendixVo.class).contains(x.getProperty()))
                        .leftJoin(File.class, File::getFolderId, NewsAppendix::getFileId, ext -> ext.selectAs(File::getFileName, NewsAppendixVo::getFileName)
                                .selectAs(File::getFolderId, NewsAppendixVo::getFolderId)
                                .selectAs(File::getFileUrl, NewsAppendixVo::getFileUrl))
        );

        if (newsAppendixVoList.size() > 0) {
            newsVo.setAppendixList(newsAppendixVoList);
        }

        return RT.ok(newsVo);
    }

    @PutMapping
    @ApiOperation(value = "修改新闻")
    public RT<Boolean> update(@RequestBody UpdateNewsDto updateNewsDto) {
        //News news = BeanUtil.toBean(updateNewsDto, News.class);
        return RT.ok(newsService.update(updateNewsDto));
    }

    @DeleteMapping
    @ApiOperation(value = "删除新闻")
    public RT<Boolean> delete(@RequestBody List<Long> ids) {
        return RT.ok(newsService.delete(ids));
    }


    @PutMapping("/change-status")
    @ApiOperation(value = "发布或下架新闻")
    public RT<Boolean> changeStatus(@RequestBody ChangeNewsDto changeNewsDto) throws Exception {
        News xjrNews = newsService.getById(changeNewsDto.getId());
        if (changeNewsDto.getStatus() == null || xjrNews == null) {
            throw new Exception("参数异常");
        }
        if (changeNewsDto.getStatus().equals(xjrNews.getStatus())) {
            if (changeNewsDto.getStatus() == 2) {
                throw new Exception("新闻已发布!!!");
            } else if (changeNewsDto.getStatus() == 3) {
                throw new Exception("新闻已下架!!!");
            }
        }
        //将原来的修改enabled_mark改为修改status
        xjrNews.setStatus(changeNewsDto.getStatus());
        if(xjrNews.getStatus() == 2){
            //将发布时间改为当前时间
            xjrNews.setReleaseTime(LocalDateTime.now());
            //将张贴时间改为当前时间
            xjrNews.setSendStartDate(LocalDateTime.now());
            newsService.SendMessage(xjrNews.getId());
        }

        return RT.ok(newsService.updateById(xjrNews));
    }

    @PutMapping("/read")
    @ApiOperation(value = "读新闻")
    public RT<Boolean> read(@RequestBody List<Long> ids) {
        for (Long id : ids) {
            NewsRelation newsRelation = newsRelationService.getOne(
                    Wrappers.<NewsRelation>query().lambda()
                            .eq(NewsRelation::getNewsId, id)
                            .eq(NewsRelation::getUserId, StpUtil.getLoginIdAsLong()));
            if (newsRelation == null) {
                newsRelationService.save(new NewsRelation(){{
                    setNewsId(id);
                    setUserId(StpUtil.getLoginIdAsLong());
                    setReadMark(1);
                    setReadDate(LocalDateTime.now());
                }});
                continue;
            }
            newsRelationService.updateById(new NewsRelation(){{
                setId(newsRelation.getId());
                setReadMark(1);
                setReadDate(LocalDateTime.now());
            }});
        }
        return RT.ok(true);
    }

    @PutMapping("/reply")
    @ApiOperation(value = "新闻回复")
    public RT<Boolean> reply(@RequestBody NewsReplyDto newsReplyDto) {
        NewsRelation newsRelation = newsRelationService.getOne(
                Wrappers.<NewsRelation>query().lambda()
                        .eq(NewsRelation::getNewsId, newsReplyDto.getId())
                        .eq(NewsRelation::getUserId, StpUtil.getLoginIdAsLong()));
        if (newsRelation == null) {
            newsRelation = new NewsRelation();
            newsRelation.setNewsId(newsReplyDto.getId());
            newsRelation.setUserId(StpUtil.getLoginIdAsLong());
            newsRelation.setReadMark(1);
            newsRelation.setReadDate(LocalDateTime.now());
            newsRelation.setReplyContent(newsReplyDto.getReplyContent());
            return RT.ok(newsRelationService.save(newsRelation));
        }

        if (newsRelation.getReadMark() == null || newsRelation.getReadMark() == 0) {
            newsRelation.setReadMark(1);
            newsRelation.setReadDate(LocalDateTime.now());
        }
        newsRelation.setReplyContent(newsReplyDto.getReplyContent());
        return RT.ok(newsRelationService.updateById(newsRelation));
    }

    @GetMapping("/page-relation")
    @ApiOperation(value = "获取新闻阅读权限分页")
    public RT<PageOutput<NewsRelationVo>> pageRelation(NewsRelationPageDto dto) {

        IPage<NewsRelationVo> page = newsRelationService.selectJoinListPage(ConventPage.getPage(dto), NewsRelationVo.class,
                new MPJLambdaWrapper<NewsRelation>()
                        .eq(NewsRelation::getNewsId, dto.getNewsId())
                        .eq(ObjectUtil.isNotEmpty(dto.getReadMark()) && dto.getReadMark() > 0, NewsRelation::getReadMark, dto.getReadMark() == 1 ? 0 : 1)
                        .isNull(ObjectUtil.isNotEmpty(dto.getReplyMark()) && dto.getReplyMark() == 1, NewsRelation::getReplyContent)
                        .isNotNull(ObjectUtil.isNotEmpty(dto.getReplyMark()) && dto.getReplyMark() == 2, NewsRelation::getReplyContent)
                        .select(NewsRelation::getId)
                        .select(NewsRelation.class, x -> VoToColumnUtil.fieldsToColumns(NewsRelationVo.class).contains(x.getProperty()))
                        .leftJoin(XjrUser.class, XjrUser::getId, NewsRelation::getUserId, ext -> ext.selectAs(XjrUser::getName, NewsRelationVo::getUserName))
        );
        PageOutput<NewsRelationVo> pageOutput = ConventPage.getPageOutput(page);
        return RT.ok(pageOutput);
    }


    @GetMapping("/page-relation-config")
    @ApiOperation(value = "获取新闻权限分页")
    public RT<PageOutput<NewsRelationConfigVo>> pageRelationConfig(NewsRelationPageDto dto) {
        Wrapper<NewsRelationConfig> wrapper = Wrappers.<NewsRelationConfig>query().lambda()
                .eq(NewsRelationConfig::getNewsId, dto.getNewsId())
                .select(NewsRelationConfig.class, x -> VoToColumnUtil.fieldsToColumns(NewsRelationConfigVo.class).contains(x.getProperty()));

        IPage<NewsRelationConfig> page = newsRelationConfigService.page(ConventPage.getPage(dto), wrapper);
        PageOutput<NewsRelationConfigVo> pageOutput = ConventPage.getPageOutput(page, NewsRelationConfigVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping("/count-relation")
    @ApiOperation(value = "公告阅读权限统计")
    public RT<Long[]> countRelation(@RequestParam Long id) {
        Long total = newsRelationService.count(Wrappers.<NewsRelation>query().lambda().eq(NewsRelation::getNewsId, id));
        Long notRead = newsRelationService.count(Wrappers.<NewsRelation>query().lambda().eq(NewsRelation::getNewsId, id).eq(NewsRelation::getReadMark, 0));
        Long isRead = newsRelationService.count(Wrappers.<NewsRelation>query().lambda().eq(NewsRelation::getNewsId, id).eq(NewsRelation::getReadMark, 1));
        Long isReply = newsRelationService.count(Wrappers.<NewsRelation>query().lambda().eq(NewsRelation::getNewsId, id).isNotNull(NewsRelation::getReplyContent));
        Long notReply = newsRelationService.count(Wrappers.<NewsRelation>query().lambda().eq(NewsRelation::getNewsId, id).isNull(NewsRelation::getReplyContent));
        Long[] arrayRefVar = {total, notRead, isRead, isReply, notReply};
        return RT.ok(arrayRefVar);
    }

    @PutMapping("/update-relation")
    @ApiOperation(value = "设置新闻阅读权限")
    public RT<Boolean> updateRelation(@RequestBody UpdateNewsRelationDto updateRelationDto) {
        return RT.ok(newsService.updateRelation(updateRelationDto));
    }

    @DeleteMapping("/delete-relation")
    @ApiOperation(value = "删除新闻阅读权限")
    public RT<Boolean> deleteRelation(@RequestBody DeleteNewsRelationDto deleteNewsRelationDto) {
        return RT.ok(newsService.deleteRelation(deleteNewsRelationDto));
    }

    @GetMapping("/count-type")
    @ApiOperation(value = "未读类型统计")
    public RT<List<NewsCountTypeVo>> countType() {
        List<NewsCountTypeVo> newsCountTypeVoList = new ArrayList<>();
        Integer typeIds[] = {1, 2, 3, 4};
        for (Integer typeId : typeIds) {
            NewsCountTypeVo newsCountTypeVo = new NewsCountTypeVo();
            News news = new News();

            //新闻
            if(typeId == 1){
                Long notReadCount = newsService.count(Wrappers.<News>query().lambda()
                        .eq(News::getTypeId, typeId)
                        .eq(News::getStatus, 2)
                        .gt(News::getSendEndDate, LocalDateTime.now())
                        .eq(News::getWfStatus, 1)
                        .notInSql(News::getId, "select distinct news_id from xjr_oa_news_relation where read_mark = 1 and user_id = " + StpUtil.getLoginIdAsLong()));

                newsCountTypeVo.setTypeId(typeId);
                newsCountTypeVo.setTotal(notReadCount);

                news = newsService.getOne(Wrappers.<News>query().lambda()
                        .eq(News::getTypeId, typeId)
                        .eq(News::getStatus, 2)
                        .gt(News::getSendEndDate, LocalDateTime.now())
                        .eq(News::getWfStatus, 1)
                        //.notInSql(News::getId, "select distinct news_id from xjr_oa_news_relation where read_mark = 1 and user_id = " + StpUtil.getLoginIdAsLong())
                        .orderByDesc(News::getReleaseTime)
                        .last("limit 1"));
            }

            //公告
            if(typeId == 2){
                //公告发给全体的
                Long notReadCountProclamationToAll = newsService.count(Wrappers.<News>query().lambda()
                        .eq(News::getTypeId, 2)
                        .eq(News::getStatus, 2)
                        .eq(News::getSendRange, 1)
                        .notInSql(News::getId, "select distinct news_id from xjr_oa_news_relation where read_mark = 1 and user_id = " + StpUtil.getLoginIdAsLong())
                        .and(wp -> wp.gt(News::getSendEndDate, LocalDateTime.now())
                                .or()
                                .isNull(News::getSendEndDate)));

                News newsAll = newsService.getOne(Wrappers.<News>query().lambda()
                        .eq(News::getTypeId, 2)
                        .eq(News::getStatus, 2)
                        .eq(News::getSendRange, 1)
                        //.notInSql(News::getId, "select distinct news_id from xjr_oa_news_relation where read_mark = 1 and user_id = " + StpUtil.getLoginIdAsLong())
                        .and(wp -> wp.gt(News::getSendEndDate, LocalDateTime.now())
                                .or()
                                .isNull(News::getSendEndDate))
                        .orderByDesc(News::getReleaseTime)
                        .last("limit 1"));

                //公告发给非全体的
                Long notReadCountProclamation = newsRelationService.count(Wrappers.<NewsRelation>query().lambda()
                        .eq(NewsRelation::getReadMark, 0)
                        .eq(NewsRelation::getUserId, StpUtil.getLoginIdAsLong())
                        .inSql(NewsRelation::getNewsId, "select id from xjr_oa_news where delete_mark = 0 and type_id = 2 and (send_range = 2 or send_range = 3) AND (send_end_date > '" + LocalDateTime.now() + "' OR send_end_date IS NULL)"));

                MPJLambdaWrapper<News> queryNews = new MPJLambdaWrapper<>();
                queryNews
                        .leftJoin(NewsRelation.class, NewsRelation::getNewsId, News::getId)
                        .eq(News::getTypeId, typeId)
                        .eq(News::getStatus, 2)
                        .and(wp -> wp.eq(News::getSendRange, 2)
                                .or()
                                .eq(News::getSendRange, 3))
                        .and(wp -> wp.gt(News::getSendEndDate, LocalDateTime.now())
                                .or()
                                .isNull(News::getSendEndDate))
                        //.eq(NewsRelation::getReadMark, 0)
                        .eq(NewsRelation::getUserId, StpUtil.getLoginIdAsLong())
                        .orderByDesc(News::getReleaseTime)
                        .last("limit 1");
                News newsNotAll = newsService.selectJoinOne(News.class, queryNews);

                if(newsAll != null && newsNotAll != null){
                    if(newsAll.getReleaseTime().compareTo(newsNotAll.getReleaseTime()) < 0){
                        news = newsNotAll;
                    }else {
                        news = newsAll;
                    }
                }
                if(newsAll != null && newsNotAll == null){
                    news = newsAll;
                }

                if(newsAll == null && newsNotAll != null){
                    news = newsNotAll;
                }

                newsCountTypeVo.setTypeId(typeId);
                newsCountTypeVo.setTotal(notReadCountProclamationToAll + notReadCountProclamation);
            }

            if (news != null) {
                newsCountTypeVo.setTime(news.getReleaseTime());
                newsCountTypeVo.setTitle(news.getFullHead());
            }
            if(typeId == 4){
                long count = systemUpdateMessageService.count(
                        MPJWrappers.<SystemUpdateMessage>lambdaJoin()
                                .leftJoin(SystemUpdateMessageNotice.class, SystemUpdateMessageNotice::getSystemUpdateMessageId, SystemUpdateMessage::getId)
                                .eq(SystemUpdateMessageNotice::getUserId, StpUtil.getLoginIdAsLong())
                                .eq(SystemUpdateMessage::getDeleteMark, DeleteMark.NODELETE.getCode())
                                .eq(SystemUpdateMessage::getStatus, 1)
                                .eq(SystemUpdateMessageNotice::getStatus, 0)
                );
                newsCountTypeVo.setTypeId(typeId);
                newsCountTypeVo.setTotal(count);
            }
            newsCountTypeVoList.add(newsCountTypeVo);
        }
        return RT.ok(newsCountTypeVoList);
    }

    @GetMapping("/send-message")
    @ApiOperation(value = "发送消息")
    public RT<Boolean> sendMessage(@RequestParam Long id) {
        return RT.ok(newsService.SendMessage(id));
    }

    @GetMapping("/unread-count")
    @ApiOperation(value = "获取登录人未读消息数量")
    public RT<Long> unreadCount() {
        Long notReadCountNews = newsService.count(Wrappers.<News>query().lambda()
                .eq(News::getTypeId, 1)
                .eq(News::getStatus, 2)
                .gt(News::getSendEndDate, LocalDateTime.now())
                .eq(News::getWfStatus, 1)
                .notInSql(News::getId, "select distinct news_id from xjr_oa_news_relation where read_mark = 1 and user_id = " + StpUtil.getLoginIdAsLong()));

        //公告发给全体的
        Long notReadCountProclamationToAll = newsService.count(Wrappers.<News>query().lambda()
                .eq(News::getTypeId, 2)
                .eq(News::getStatus, 2)
                .eq(News::getSendRange, 1)
                .notInSql(News::getId, "select distinct news_id from xjr_oa_news_relation where read_mark = 1 and user_id = " + StpUtil.getLoginIdAsLong())
                .and(wp -> wp.gt(News::getSendEndDate, LocalDateTime.now())
                        .or()
                        .isNull(News::getSendEndDate)));

        //公告发给非全体的
        Long notReadCountProclamation = newsRelationService.count(Wrappers.<NewsRelation>query().lambda()
                .eq(NewsRelation::getReadMark, 0)
                .eq(NewsRelation::getUserId, StpUtil.getLoginIdAsLong())
                .inSql(NewsRelation::getNewsId, "select id from xjr_oa_news where delete_mark = 0 and type_id = 2 and status = 2 and (send_range = 2 or send_range = 3) AND (send_end_date > '" + LocalDateTime.now() + "' OR send_end_date IS NULL)"));

        //公告发给非全体的
        long systemUpdateMessageCount = systemUpdateMessageService.count(
                MPJWrappers.<SystemUpdateMessage>lambdaJoin()
                        .leftJoin(SystemUpdateMessageNotice.class, SystemUpdateMessageNotice::getSystemUpdateMessageId, SystemUpdateMessage::getId)
                        .eq(SystemUpdateMessageNotice::getUserId, StpUtil.getLoginIdAsLong())
                        .eq(SystemUpdateMessage::getDeleteMark, DeleteMark.NODELETE.getCode())
                        .eq(SystemUpdateMessage::getStatus, 1)
                        .eq(SystemUpdateMessageNotice::getStatus, 0)
        );

        Long notReadCount = notReadCountNews + notReadCountProclamationToAll + notReadCountProclamation + systemUpdateMessageCount;
        return RT.ok(notReadCount);
    }
}
