SystemNoticeUtils.java 8.2 KB
package com.skua.modules.util;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.skua.modules.alarmtmp.vo.MeetNoticeVO;
import com.skua.modules.alarmtmp.vo.SystemNoticeVO;
import com.skua.common.constant.CommonSendStatus;
import com.skua.core.constant.CommonConstant;
import com.skua.core.util.UUIDGenerator;
import com.skua.core.util.push.IPushService;
import com.skua.core.util.push.MessageEntity;
import com.skua.core.util.push.PushMessageFactory;

import com.skua.modules.system.entity.SysAnnouncement;
import com.skua.modules.system.entity.SysAnnouncementSend;
import com.skua.modules.system.service.ISysAnnouncementSendService;
import com.skua.modules.system.service.ISysAnnouncementService;
import com.skua.modules.system.service.ISysDictService;
import com.skua.modules.system.service.ISysUserService;
import com.skua.modules.websocket.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SystemNoticeUtils
 * @Description 系统通知工具类
 * @Author yangsong
 * @Date 2021/4/21 15:58
 * @Version 1.0
 */
@Slf4j
@Component
public class SystemNoticeUtils {
    private static Map<String, String> tmp = Maps.newHashMap();

    static {
        tmp.put("重要报警", "H");//高
        tmp.put("一般报警", "M");//中
        tmp.put("提醒报警", "L");//低
    }

    @Autowired
    private ISysAnnouncementService sysAnnouncementService;
    @Autowired
    private ISysAnnouncementSendService sysAnnouncementSendService;
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private ISysUserService sysUserService;
    @Resource
    private WebSocket webSocket;

    public void dealNoticeMessage(SystemNoticeVO systemNoticeVO) {
        String userIds = systemNoticeVO.getUserIds();
        Date refDate = new Date();
        SysAnnouncement sysAnnouncement = new SysAnnouncement();
        sysAnnouncement.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
        sysAnnouncement.setSendStatus(CommonSendStatus.PUBLISHED_STATUS_1);//发布
        sysAnnouncement.setMsgType("ALARM");
        sysAnnouncement.setPriority(tmp.getOrDefault(systemNoticeVO.getAlarmRuleLevelName(), "H"));
        sysAnnouncement.setId(UUIDGenerator.creatUUID());
        sysAnnouncement.setSendTime(refDate);
        sysAnnouncement.setMsgCategory("3");
        //系统
        sysAnnouncement.setSender("系统");
        sysAnnouncement.setDepartId(systemNoticeVO.getDepartId());
        sysAnnouncement.setMsgContent(systemNoticeVO.getContent());
        //根据报警类型获取中文
        String alarmRuleType = systemNoticeVO.getAlarmRuleType();
        String alarmTypeName = sysDictService.queryDictTextByKey("alarm_type", alarmRuleType);
        sysAnnouncement.setTitile(alarmTypeName + "通知");
        sysAnnouncement.setAlarmType(alarmRuleType);
        sysAnnouncement.setUserIds(userIds);
        sysAnnouncementService.save(sysAnnouncement);
        // 2.插入用户通告阅读标记表记录
        String[] users = userIds.split(",");
        String anntId = sysAnnouncement.getId();
        for (int i = 0; i < users.length; i++) {
            SysAnnouncementSend announcementSend = new SysAnnouncementSend();
            announcementSend.setAnntId(anntId);
            announcementSend.setUserId(users[i]);
            announcementSend.setReadFlag(CommonConstant.NO_READ_FLAG);
            announcementSend.setReadTime(refDate);
            sysAnnouncementSendService.save(announcementSend);
        }
        // 发送webSocket
        JSONObject obj = new JSONObject();
        obj.put("cmd", "historyRecordRemind");
        obj.put("type", sysAnnouncement.getPriority());
        obj.put("msgId", sysAnnouncement.getId());
        obj.put("msgTxt", sysAnnouncement.getTitile());
        webSocket.sendMoreMessage(users, obj.toJSONString());
        log.info("系统通知发送成功" + userIds);

        //移动端消息推送
        //根据用户id查询cid
        List<String> cidList = sysUserService.getCidByUserIds(userIds);
        if (cidList != null && cidList.size() > 0) {
            log.info("APP通知发送开始" + userIds);
            try {
                MessageEntity messageEntity = new MessageEntity();
                messageEntity.setMessageTitle(alarmTypeName + "通知");
                messageEntity.setMessageBody(systemNoticeVO.getContent());
                messageEntity.setReceiveUserCid(cidList);
                messageEntity.setForwardTag("");
                IPushService appPushService = PushMessageFactory.getPushService("MOB");
                appPushService.pushMessage(messageEntity);
            } catch (Exception e) {
                log.info("APP通知发送错误" + e);
            }
            log.info("APP通知发送成功" + userIds + ":" + alarmTypeName + "通知" + systemNoticeVO.getContent());
        } else {
            log.info("APP通知没有cid" + userIds);
        }

    }

    /**
     * 会议通知
     * @param systemNoticeVO
     */
    public void meetNoticeMessage(SystemNoticeVO systemNoticeVO,String meetId,String title,String meetType) {
        String userIds = systemNoticeVO.getUserIds();
        Date refDate = new Date();
        SysAnnouncement sysAnnouncement = new SysAnnouncement();
        sysAnnouncement.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
        sysAnnouncement.setSendStatus(CommonSendStatus.PUBLISHED_STATUS_1);//发布
        sysAnnouncement.setMsgType("USER");
        sysAnnouncement.setPriority("M");
        sysAnnouncement.setId(UUIDGenerator.creatUUID());
        sysAnnouncement.setSendTime(refDate);
        sysAnnouncement.setMsgCategory("4");//会议通知
        //系统
        sysAnnouncement.setSender("系统");
        sysAnnouncement.setDepartId(systemNoticeVO.getDepartId());
        sysAnnouncement.setMsgContent(systemNoticeVO.getContent());
        //根据报警类型获取中文
        sysAnnouncement.setTitile(title+"[会议通知]");
        sysAnnouncement.setUserIds(userIds);
        sysAnnouncementService.save(sysAnnouncement);
        // 2.插入用户通告阅读标记表记录
        String[] users = userIds.split(",");
        String anntId = sysAnnouncement.getId();
        for (int i = 0; i < users.length; i++) {
            SysAnnouncementSend announcementSend = new SysAnnouncementSend();
            announcementSend.setAnntId(anntId);
            announcementSend.setUserId(users[i]);
            announcementSend.setReadFlag(CommonConstant.NO_READ_FLAG);
            announcementSend.setReadTime(refDate);
            sysAnnouncementSendService.save(announcementSend);
        }
        // 发送webSocket
        JSONObject obj = new JSONObject();
        obj.put("cmd", "meettingNotice");
        obj.put("type", sysAnnouncement.getPriority());
        obj.put("msgId", sysAnnouncement.getId());
        obj.put("meetRecordId", meetId);
        obj.put("meetType", meetType);
        obj.put("msgTxt", sysAnnouncement.getTitile());
        webSocket.sendMoreMessage(users, obj.toJSONString());
        log.info("系统通知发送成功" + userIds);

        //移动端消息推送
        //根据用户id查询cid
        List<String> cidList = sysUserService.getCidByUserIds(userIds);
        if (cidList != null && cidList.size() > 0) {
            log.info("APP通知发送开始" + userIds);
            try {
                MessageEntity messageEntity = new MessageEntity();
                messageEntity.setMessageTitle(title+"[会议通知]");
                messageEntity.setMessageBody(systemNoticeVO.getContent());
                messageEntity.setReceiveUserCid(cidList);
                messageEntity.setForwardTag("");
                IPushService appPushService = PushMessageFactory.getPushService("MOB");
                appPushService.pushMessage(messageEntity);
            } catch (Exception e) {
                log.info("APP通知发送错误" + e);
            }
            log.info("APP通知发送成功" + userIds + ":" + title + "[会议通知]" + systemNoticeVO.getContent());
        } else {
            log.info("APP通知没有cid" + userIds);
        }

    }
}