package com.sunxung.factoring.service.sys.systemMsg.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.DateUtil;
import com.sunxung.factoring.component.util.ThreadLocalUtil;
import com.sunxung.factoring.component.util.ValidatorUtil;
import com.sunxung.factoring.dict.impl.MsgTypeDict;
import com.sunxung.factoring.entity.GridPage;
import com.sunxung.factoring.entity.ValidatorEntity.ValidatorType;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.entity.sys.Org;
import com.sunxung.factoring.entity.sys.User;
import com.sunxung.factoring.entity.sys.systemMsg.MsgReadState;
import com.sunxung.factoring.entity.sys.systemMsg.SystemMsg;
import com.sunxung.factoring.entity.sys.systemMsg.SystemMsgReadDetail;
import com.sunxung.factoring.entity.sys.systemMsg.SystemMsgUnreadNum;
import com.sunxung.factoring.entity.sys.systemMsg.SystemMsgUserRel;
import com.sunxung.factoring.entity.sys.systemMsg.vo.FlowableMsgTips;
import com.sunxung.factoring.entity.sys.systemMsg.vo.SearchSystemMsg;
import com.sunxung.factoring.mapper.sys.systemMsg.SystemMsgMapper;
import com.sunxung.factoring.service.sys.DictionaryService;
import com.sunxung.factoring.service.sys.OrgService;
import com.sunxung.factoring.service.sys.UserService;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import com.sunxung.factoring.service.sys.systemMsg.SystemMsgService;

@Service
public class SystemMsgServiceImpl implements SystemMsgService {

    @Autowired
    private SystemMsgMapper systemMsgMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private DictionaryService dicService;
    @Autowired
    private OrgService orgService;
    @Autowired
    private FlowableService flowableService;

    @Override
    public GridPage<SystemMsg> findSentList(SearchSystemMsg search) {
        PageHelper.startPage(search.getPage(), search.getRows()).size();
        search.setSenderId(ThreadLocalUtil.getUser().getId());
        search.setRemove(false);
        List<SystemMsg> list = systemMsgMapper.findSendMsgBySearch(search);
        Integer count = systemMsgMapper.countSendMsgBySearch(search);
        if (null != list && list.size() > 0) {
            for (SystemMsg systemMsg : list) {
                // 统计消息的已读人数，未读人数
                Integer read = countBySystemMsgId(systemMsg.getId(), true); // 已读
                Integer unread = countBySystemMsgId(systemMsg.getId(), false); // 未读
                systemMsg.setReadingSituation("已读" + read + "人，未读" + unread + "人");
            }
        }
        return new GridPage<SystemMsg>(search.getRows(), search.getPage(), count, list);
    }

    @Override
    @Transactional
    public SystemMsg add(SystemMsg systemMsg) {
        List<User> userList = systemMsg.getUserList();
        List<Long> departmentList = systemMsg.getDepartmentList();
        List<Long> userIdList = new ArrayList<Long>(); // 用户ID集合
        if (null != systemMsg && (null == userList || userList.size() < 1)
                && (null == departmentList || departmentList.size() < 1)) {
            throw new ValidatorException(CodeUtil.FAIL, "发送范围不能为空");
        }
        systemMsg.setRemove(false);
        systemMsg.setSenderUser(ThreadLocalUtil.getUser());
        // 判断当前登录用户是否是超级管理员，是则是系统消息，否则是用户消息
        if (ThreadLocalUtil.getUser().getId().equals(ConstantConversionUtil.ADMIN_ID)) {
            // 系统消息
            systemMsg.setMsgType(dicService.getByCode(MsgTypeDict.ChildEnum.SYSTEMMESSAGE.getCode()));
        } else {
            // 用户消息
            systemMsg.setMsgType(dicService.getByCode(MsgTypeDict.ChildEnum.USERMESSAGE.getCode()));
        }
        // 判断消息发送类型，是即时，还是定时
        if (ConstantConversionUtil.IMMEDIATE.equals(systemMsg.getSendType())) {
            // 即时
            systemMsg.setPushState(true);
            systemMsg.setPushTime(new Date());
        } else {
            // 定时
            systemMsg.setPushState(false);
        }
        ValidatorUtil.validator(ValidatorType.ADD, systemMsg);
        // 向消息管理表添加数据
        systemMsgMapper.add(systemMsg);
        // 循环将用户集合中的用户ID添加到用户ID集合中
        if (null != systemMsg && null != userList && userList.size() > 0) {
            for (User user : userList) {
                userIdList.add(user.getId());
            }
        }
        // 循环部门ID，并查询部门下所有员工及子部门中所有员工，添加到用户ID集合中
        if (null != departmentList && departmentList.size() > 0) {
            for (Long orgId : departmentList) {
                Org org = orgService.get(orgId);
                List<Long> userIds = orgService.findUserIdByOrgCode(org.getInternalCode());
                if (null != userIds && userIds.size() > 0)
                    userIdList.addAll(userIds);
            }
        }
        // 利用hashSet集合不重复的特性，去除重复用户ID
        userIdList = new ArrayList(new HashSet(userIdList));
        if (null != userIdList && userIdList.size() > 0) {
            for (Long userId : userIdList) {
                // 向用户消息关联表添加数据
                addSystemMsgUser(systemMsg.getId(), userId, false, false);
            }
        }
        return systemMsg;
    }

    @Override
    public SystemMsg get(Long id) {
        return systemMsgMapper.get(id);
    }

    @Override
    @Transactional
    public boolean delete(Long id) {
        return systemMsgMapper.delete(id) > 0;
    }

    @Override
    @Transactional
    public void addSystemMsgUser(Long systemMsgId, Long receiverUserId, boolean readState, boolean receiverRemove) {
        // 根据消息ID和接收人ID查询中间表，判断同一条消息是否已经发送过该用户，以防止同一条消息发送同一个人多次的重复数据
        Integer count = systemMsgMapper.countBySystemMsgIdAndReceiverUserId(systemMsgId, receiverUserId);
        if (count <= 0) {
            systemMsgMapper.addSystemMsgUser(systemMsgId, receiverUserId, readState, receiverRemove);
        }
    }

    @Override
    @Transactional
    public boolean deleteBySystemMsgUserId(Long id, Long receiverUserId) {
        return systemMsgMapper.deleteBySystemMsgUserId(id, receiverUserId) > 0;
    }

    @Override
    @Transactional
    public boolean updateReadState(MsgReadState msgRead) {
        boolean flag = true;
        if (null == msgRead || null == msgRead.getMsgIds() || msgRead.getMsgIds().size() < 1) {
            throw new ValidatorException(CodeUtil.FAIL, "请选择操作的数据");
        }
        User user = ThreadLocalUtil.getUser();
        for (Long id : msgRead.getMsgIds()) {
            flag = systemMsgMapper.updateReadState(id, user.getId(), msgRead.getReadState()) > 0;
            if (!flag)
                return flag;
        }
        return flag;
    }

    @Override
    public Integer countBySystemMsgId(Long systemMsgId, Boolean readState) {
        return systemMsgMapper.countBySystemMsgId(systemMsgId, readState);
    }

    @Override
    public SystemMsg getSentMsgView(Long id) {
        SystemMsg msg = get(id);
        List<User> users = new ArrayList<User>();
        List<Long> list = systemMsgMapper.findReceiverUserIdBysystemMsgId(id, null);
        if (null != list && list.size() > 0) {
            for (Long userId : list) {
                User user = autoReceiverUser(userId);
                users.add(user);
            }
        }
        msg.setUserList(users);
        return msg;
    }

    public User autoReceiverUser(Long userId) {
        User user = userService.get(userId);
        return user;
    }

    @Override
    public SystemMsgReadDetail getSystemMsgReadDetail(Long id) {
        SystemMsgReadDetail readDetail = new SystemMsgReadDetail();
        // 已读
        List<User> readUsers = new ArrayList<User>();
        List<Long> readUserlist = systemMsgMapper.findReceiverUserIdBysystemMsgId(id, true);
        if (null != readUserlist && readUserlist.size() > 0) {
            for (Long userId : readUserlist) {
                User user = autoReceiverUser(userId);
                readUsers.add(user);
            }
        }
        // 未读
        List<User> unReadUsers = new ArrayList<User>();
        List<Long> unreadUserlist = systemMsgMapper.findReceiverUserIdBysystemMsgId(id, false);
        if (null != unreadUserlist && unreadUserlist.size() > 0) {
            for (Long userId : unreadUserlist) {
                User user = autoReceiverUser(userId);
                unReadUsers.add(user);
            }
        }
        readDetail.setReadNum(readUserlist.size());
        readDetail.setUnReadNum(unreadUserlist.size());
        readDetail.setReadUserList(readUsers);
        readDetail.setUnReadUserList(unReadUsers);
        return readDetail;
    }

    @Override
    @Transactional
    public boolean deleteSentMsg(MsgReadState msgRead) {
        boolean flag = true;
        if (null != msgRead && null != msgRead.getMsgIds() && msgRead.getMsgIds().size() > 0) {
            for (Long id : msgRead.getMsgIds()) {
                SystemMsg msg = get(id);
                // 判断消息发送状态
                if (null != msg) {
                    if (msg.getPushState()) {
                        // 已发送状态，获取消息与其他用户的关联信息，若所有接收人都删除该消息，则删除该消息；若其中有人未删除该消息，则该消息逻辑删除
                        // Integer count = countBySystemMsgId(msg.getId(), null);
                        List<SystemMsgUserRel> list = findBySystemMsgId(msg.getId());
                        boolean removeAll = true;
                        if (null != list && list.size() > 0) {
                            for (SystemMsgUserRel rel : list) {
                                if (null != rel.getReceiverRemove() && !rel.getReceiverRemove()) {
                                    removeAll = false;
                                    continue;
                                }
                                deleteBySystemMsgUserId(rel.getSystemMsgId(), rel.getReceiverUserId());
                            }
                            if (removeAll) {
                                // 删除消息
                                flag = delete(msg.getId());
                                if (!flag)
                                    return flag;
                            } else {
                                // 软删消息
                                flag = systemMsgMapper.updateSystemMsgRemove(msg.getId(), true) > 0;
                                if (!flag)
                                    return flag;
                            }
                        }
                    } else {
                        // 待发送状态,则删除消息及用户消息关联数据
                        flag = systemMsgMapper.deleteSystemMsgUserByMsgId(msg.getId()) > 0;
                        if (!flag)
                            return flag;
                        flag = delete(msg.getId());
                        if (!flag)
                            return flag;
                    }
                }
            }
        }
        return flag;
    }

    @Override
    public GridPage<SystemMsg> findMessageManageList(SearchSystemMsg search) {
        PageHelper.startPage(search.getPage(), search.getRows()).size();
        search.setReceiverUserId(ThreadLocalUtil.getUser().getId());
        search.setPushState(true);
        search.setReceiverRemove(false);
        List<SystemMsg> list = systemMsgMapper.findBySearch(search);
        Integer count = systemMsgMapper.countBySearch(search);
        if (null != list && list.size() > 0) {
            for (SystemMsg msg : list) {
                autoSenderUser(msg);
                if (null != msg.getSenderUser() && StringUtil.isNotEmpty(msg.getSenderUser().getName())) {
                    msg.setSenderName(msg.getSenderUser().getName());
                }
                autoMsgType(msg);
            }
        }
        return new GridPage<SystemMsg>(search.getRows(), search.getPage(), count, list);
    }

    public void autoSenderUser(SystemMsg systemMsg) {
        if (null != systemMsg && null != systemMsg.getSenderUser()) {
            User user = userService.get(systemMsg.getSenderUser().getId());
            if (null != user) {
                systemMsg.setSenderUser(user);
                systemMsg.setSenderName(user.getName());
            }
        }
    }

    public void autoMsgType(SystemMsg systemMsg) {
        if (null != systemMsg && null != systemMsg.getMsgType()) {
            Dictionary dic = dicService.get(systemMsg.getMsgType().getId());
            if (null != dic)
                systemMsg.setMsgType(dic);
        }
    }

    @Override
    public SystemMsg getMsgDetail(Long id) {
        SystemMsg systemMsg = get(id);
        autoSenderUser(systemMsg);
        return systemMsg;
    }

    @Override
    @Transactional
    public boolean deleteMsg(MsgReadState msgRead) {
        if (null == msgRead || null == msgRead.getMsgIds()) {
            throw new ValidatorException(CodeUtil.FAIL, "请选择要删除的数据");
        }
        boolean flag = true;
        User user = ThreadLocalUtil.getUser();
        for (Long id : msgRead.getMsgIds()) {
            SystemMsg msg = get(id);
            Dictionary msgType = dicService.get(msg.getMsgType().getId());
            // 用户消息，系统消息（用户手动发送的消息在发件人已发送列表中能查看，故存在软删）
            if (msgType.getCode().equals(MsgTypeDict.ChildEnum.SYSTEMMESSAGE.getCode())
                    || msgType.getCode().equals(MsgTypeDict.ChildEnum.USERMESSAGE.getCode())) {
                // 判断该条消息是否已经被发件人删除,是则删除，否则逻辑删除
                if (null != msg.getRemove() && msg.getRemove()) {
                    // 删除关联数据
                    flag = deleteBySystemMsgUserId(id, user.getId());
                    if (!flag)
                        return flag;
                    // 判断该条消息是否被其他用户关联,如果没有则进一步判断
                    if (countBySystemMsgId(id, null) <= 0) {
                        flag = delete(id);
                        if (!flag)
                            return flag;
                    }
                } else {
                    // 逻辑删除关联数据
                    updateReceiverRemoveByMsgId(id, true, user.getId());
                }
            } else {
                // 其它系统自动发送的消息（在已发送列表中无法查看，故直接删除）
                // 删除关联数据
                deleteBySystemMsgUserId(id, user.getId());
                if (countBySystemMsgId(id, null) <= 0) {
                    flag = delete(id);
                    if (!flag)
                        return flag;
                }
            }
        }
        return flag;
    }

    @Override
    @Transactional
    public SystemMsg sendFlowMsgImmediately(String title, String content, List<Long> receiverUserIds, Long businessId,
            String businessType, boolean handleMsg, String taskId) {
        if (null == receiverUserIds || receiverUserIds.size() < 0) {
            throw new ValidatorException(CodeUtil.FAIL, "消息接收者不能为空");
        }
        // 去重
        receiverUserIds = receiverUserIds.stream().distinct().collect(Collectors.toCollection(ArrayList::new));
        SystemMsg systemMsg = new SystemMsg();
        systemMsg.setTitle(title);
        systemMsg.setContent(content);
        systemMsg.setBusinessId(businessId);
        systemMsg.setBusinessType(dicService.getByCode(businessType));
        if (handleMsg) {
            // 待办消息
            systemMsg.setMsgType(dicService.getByCode(MsgTypeDict.ChildEnum.PENDINGNEWS.getCode()));
        } else {
            // 知晓消息
            systemMsg.setMsgType(dicService.getByCode(MsgTypeDict.ChildEnum.KNOWNEWS.getCode()));
        }
        systemMsg.setTaskId(taskId);
        systemMsg.setSenderName("系统");
        systemMsg.setSendType(ConstantConversionUtil.IMMEDIATE);
        systemMsg.setPushState(true);
        systemMsg.setPushTime(new Date());
        systemMsg.setRemove(false);
        if (systemMsg.getBusinessType() == null || systemMsg.getBusinessType().getId() == null) {
            throw new ValidatorException(CodeUtil.FAIL, "业务类型不能为空");
        }
        if (null != receiverUserIds && receiverUserIds.size() > 0) {
            ValidatorUtil.validator(ValidatorType.ADD, systemMsg);
            systemMsgMapper.add(systemMsg);
            for (Long receiverUserId : receiverUserIds) {
                // 向用户消息关联表添加数据
                addSystemMsgUser(systemMsg.getId(), receiverUserId, false, false);
            }
        }
        return systemMsg;
    }
    
    @Override
    public SystemMsgUnreadNum getUnreadMessageNum() {
        SystemMsgUnreadNum unreadNum = new SystemMsgUnreadNum();
        SearchSystemMsg search = new SearchSystemMsg();
        search.setReceiverUserId(ThreadLocalUtil.getUser().getId());
        search.setRemove(false);
        search.setReadState(false);
        search.setPushState(true);
        search.setReceiverRemove(false);
        // 待办消息
        search.setMsgType(dicService.getByCode(MsgTypeDict.ChildEnum.PENDINGNEWS.getCode()));
        Integer pendingNewsNumber = systemMsgMapper.countBySearch(search);
        // 知晓消息
        search.setMsgType(dicService.getByCode(MsgTypeDict.ChildEnum.KNOWNEWS.getCode()));
        Integer knowNewsNumber = systemMsgMapper.countBySearch(search);
        // 正常催收消息
        search.setMsgType(dicService.getByCode(MsgTypeDict.ChildEnum.NORMALCOLLECTION.getCode()));
        Integer normalCollection = systemMsgMapper.countBySearch(search);
        // 逾期催收
        search.setMsgType(dicService.getByCode(MsgTypeDict.ChildEnum.OVERDUECOLLECTION.getCode()));
        Integer overdueCollection = systemMsgMapper.countBySearch(search);
        // 信保提醒
        search.setMsgType(dicService.getByCode(MsgTypeDict.ChildEnum.LETTERGUARANTEEREMINDER.getCode()));
        Integer letterGuaranteereMinder = systemMsgMapper.countBySearch(search);
        // 用户消息
        search.setMsgType(dicService.getByCode(MsgTypeDict.ChildEnum.USERMESSAGE.getCode()));
        Integer userMessage = systemMsgMapper.countBySearch(search);
        // 系统消息
        search.setMsgType(dicService.getByCode(MsgTypeDict.ChildEnum.SYSTEMMESSAGE.getCode()));
        Integer systemMessageNumber = systemMsgMapper.countBySearch(search);
        unreadNum.setTotalNumber(pendingNewsNumber + systemMessageNumber + knowNewsNumber + normalCollection
                + overdueCollection + letterGuaranteereMinder + userMessage);
        unreadNum.setPendingNewsNumber(pendingNewsNumber);
        unreadNum.setSystemMessageNumber(systemMessageNumber);
        return unreadNum;
    }

    @Override
    @Transactional
    public void sentTimingMessage() {
        // 查询所有待发送的定时消息
        Date startTime = DateUtil.addMinute(new Date(), -10);
        startTime = DateUtil.stringToDate(DateUtil.dateToString(startTime, "yyyy-MM-dd HH:mm"), "yyyy-MM-dd HH:mm");
        Date endTime = DateUtil.stringToDate(DateUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm"), "yyyy-MM-dd HH:mm");
        List<Long> ids = systemMsgMapper.findTimingMessage(ConstantConversionUtil.TIMING, startTime, endTime);
        if (null != ids && ids.size() > 0) {
            for (Long id : ids) {
                // 修改消息状态
                systemMsgMapper.updatePushStateById(id, true);
            }
        }
    }

    @Override
    @Transactional
    public Integer updateReceiverRemoveByMsgId(Long systemMsgId, Boolean receiverRemove, Long userId) {
        return systemMsgMapper.updateReceiverRemoveByMsgId(systemMsgId, receiverRemove, userId);
    }

    @Override
    public List<SystemMsgUserRel> findBySystemMsgId(Long systemMsgId) {
        return systemMsgMapper.findBySystemMsgId(systemMsgId);
    }

    @Override
    public FlowableMsgTips existByTaskId(String taskId) {
        HistoricTaskInstance task = flowableService.getHistoricByTaskId(taskId);
        if (task == null || StringUtil.isEmpty(task.getAssignee())) {
            return new FlowableMsgTips(false, "参数错误，无法获取该待办业务数据!");
        }
        if (task.getAssignee().equals(ThreadLocalUtil.getUserId().toString())) {
            if (null != task.getEndTime()) {
                return new FlowableMsgTips(false, "您已处理过该业务，请勿重复处理!");
            }
            return new FlowableMsgTips(true, null);
        } else {
            return new FlowableMsgTips(false, "该业务已移交给他人处理，您无权限操作!");
        }
    }

    @Transactional
    @Override
    public SystemMsg sendBusinessMsgImmediately(String title, String content, List<Long> receiverUserIds,
            Long businessId, String businessType, String msgType, String taskId) {
        if (null == receiverUserIds || receiverUserIds.size() < 0) {
            throw new ValidatorException(CodeUtil.FAIL, "消息接收者不能为空");
        }
        // 去重
        receiverUserIds = receiverUserIds.stream().distinct().collect(Collectors.toCollection(ArrayList::new));
        SystemMsg systemMsg = new SystemMsg();
        systemMsg.setTitle(title);
        systemMsg.setContent(content);
        systemMsg.setBusinessId(businessId);
        systemMsg.setBusinessType(dicService.getByCode(businessType));
        systemMsg.setMsgType(dicService.getByCode(msgType));
        systemMsg.setTaskId(taskId);
        systemMsg.setSenderName("系统");
        systemMsg.setSendType(ConstantConversionUtil.IMMEDIATE);
        systemMsg.setPushState(true);
        systemMsg.setPushTime(new Date());
        systemMsg.setRemove(false);
        if (systemMsg.getBusinessType() == null || systemMsg.getBusinessType().getId() == null) {
            throw new ValidatorException(CodeUtil.FAIL, "业务类型不能为空");
        }
        if (null != receiverUserIds && receiverUserIds.size() > 0) {
            ValidatorUtil.validator(ValidatorType.ADD, systemMsg);
            systemMsgMapper.add(systemMsg);
            for (Long receiverUserId : receiverUserIds) {
                // 向用户消息关联表添加数据
                addSystemMsgUser(systemMsg.getId(), receiverUserId, false, false);
            }
        }
        return systemMsg;
    }
    
    @Override
    @Transactional
    public SystemMsg sendBusinessMsgTiming(String title, String content, List<Long> receiverUserIds, Long businessId,
            String businessType, String msgType, String taskId, Date pushTime) {
    	if (null == receiverUserIds || receiverUserIds.size() < 0) {
            throw new ValidatorException(CodeUtil.FAIL, "消息接收者不能为空");
        }
        // 去重
        receiverUserIds = receiverUserIds.stream().distinct().collect(Collectors.toCollection(ArrayList::new));
        SystemMsg systemMsg = new SystemMsg();
        systemMsg.setTitle(title);
        systemMsg.setContent(content);
        systemMsg.setBusinessId(businessId);
        systemMsg.setBusinessType(dicService.getByCode(businessType));
        systemMsg.setMsgType(dicService.getByCode(msgType));
        systemMsg.setTaskId(taskId);
        systemMsg.setSenderName("系统");
        systemMsg.setSendType(ConstantConversionUtil.TIMING);
        systemMsg.setPushState(false);
        systemMsg.setPushTime(pushTime);
        systemMsg.setRemove(false);
        if (systemMsg.getBusinessType() == null || systemMsg.getBusinessType().getId() == null) {
            throw new ValidatorException(CodeUtil.FAIL, "业务类型不能为空");
        }
        if (null != receiverUserIds && receiverUserIds.size() > 0) {
            ValidatorUtil.validator(ValidatorType.ADD, systemMsg);
            systemMsgMapper.add(systemMsg);
            for (Long receiverUserId : receiverUserIds) {
                // 向用户消息关联表添加数据
                addSystemMsgUser(systemMsg.getId(), receiverUserId, false, false);
            }
        }
        return systemMsg;
    }

    @Override
    public List<Long> findKnownewsByBusiness(Long businessId, String businessType) {
        if (null == businessId || StringUtil.isEmpty(businessType)) {
            throw new ValidatorException(CodeUtil.FAIL, "业务ID和业务类型为空");
        }
        Dictionary businessTypeDict = dicService.getByCode(businessType);
        if (null == businessTypeDict || null == businessTypeDict.getId()) {
            throw new ValidatorException(CodeUtil.FAIL, "获取业务类型数据字典时结果为空！");
        }
        Dictionary msgTypeDict = dicService.getByCode(MsgTypeDict.ChildEnum.KNOWNEWS.getCode());
        if (null == msgTypeDict || null == msgTypeDict.getId()) {
            throw new ValidatorException(CodeUtil.FAIL, "获取“知晓者”消息类型数据字典结果为空");
        }
        List<SystemMsg> msgList = systemMsgMapper.findKnownewsByBusiness(businessId, businessTypeDict.getId(),
                msgTypeDict.getId());
        if (null != msgList && msgList.size() > 0) {
            List<Long> knowerIds = new ArrayList<>();
            for (SystemMsg msg : msgList) {
                knowerIds.addAll(systemMsgMapper.findReceiverUserIdBysystemMsgId(msg.getId(), null));
            }
            return knowerIds.parallelStream().distinct().collect(Collectors.toList());
        }
        return null;
    }
}
