SysMessagePushService.java 6.8 KB
package com.skua.modules.util;

import com.skua.core.context.BaseContextHandler;
import com.skua.core.util.DateUtils;
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.alarmtmp.entity.AlarmRecordHistory;
import com.skua.modules.alarmtmp.entity.AlarmRuleLevelConfigTemplate;
import com.skua.modules.alarmtmp.enums.AlarmRecordStatusEnum;
import com.skua.modules.alarmtmp.service.AlarmRecordHistoryService;
import com.skua.modules.alarmtmp.service.AlarmRuleConfigService;
import com.skua.modules.flow.business.service.FlowBusinessService;
import com.skua.modules.flow.business.service.IFlowService;
import com.skua.modules.flow.core.constant.ProcessConstant;
import com.skua.modules.flow.core.entity.BladeFlow;
import com.skua.modules.flow.core.utils.FlowUtil;
import com.skua.modules.flow.support.Kv;
import com.skua.modules.flow.utils.Func;
import com.skua.modules.system.entity.SysUser;
import com.skua.modules.system.service.ISysDictService;
import com.skua.modules.system.service.ISysPermissionService;
import com.skua.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.TaskService;
import org.flowable.identitylink.api.IdentityLink;
import org.flowable.identitylink.api.IdentityLinkType;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 系统数据推送
 */
@Service
@Slf4j
public class SysMessagePushService {

    @Autowired
    private AlarmRecordHistoryService alarmRecordHistoryService;

    @Autowired
    AlarmRuleConfigService alarmRuleConfigService;
    @Autowired
    ISysPermissionService permissionService;
    @Autowired
    ISysUserService userService;
    @Autowired
    ISysDictService dictService;


    @Autowired
    private IFlowService flowService;
    @Autowired
    private FlowBusinessService flowBusinessService;
    @Autowired
    private TaskService taskService;

    /**
    * <pre>
    * 消息推送,不发起工作流
    * </pre>
     * @param recordHistory
    * @author Li Yuanyuan,2023/6/21 15:09
    * @Description: TODO(这里描述这个方法的需求变更情况)
    */
    public void pushAlarmRecordMsgNoProcess(AlarmRecordHistory recordHistory) {
        //只推送重要级别的告警
        if(recordHistory == null || !SysWarningLevelEnums.WARNING_LEVEL_1.getValue().equals(recordHistory.getAlarmRuleLevelName())){
            return;
        }

        Map<String, String> alarmTypeMap = dictService.getDictMapByCode("alarm_type");

        //获取报警数据对应的报警配置
        AlarmRuleLevelConfigTemplate alarmRuleLevelConfig = getAlarm(recordHistory);
        if (alarmRuleLevelConfig == null) return;

        //判断是否需要发送系统通知 (内容跟报警记录信息相同)
        if ("1".equals(alarmRuleLevelConfig.getIsSendNotice())) {
            //推送 app & web
            List<SysUser> sysUserList = userService.getUserByUserIds(alarmRuleLevelConfig.getAlarmNoticeRecipient());
            pushMsgNoProcess(recordHistory, alarmTypeMap, sysUserList);
        }


    }

    private void pushMsgNoProcess(AlarmRecordHistory recordHistory, Map<String, String> alarmTypeMap, List<SysUser> sysUserList) {
        List<String> userCidList = sysUserList.stream().map(SysUser::getCid).filter(v -> StringUtils.isNotBlank(v)).collect(Collectors.toList());
        List<String> userIdList = sysUserList.stream().map(SysUser::getId).collect(Collectors.toList());
        if (userCidList.size()>0||userIdList.size()>0) {
            MessageEntity messageEntity = new MessageEntity();
            messageEntity.setMessageTitle("发生数据报警");
            messageEntity.setMessageBody("报警信息");
            messageEntity.setReceiveUser(userIdList);
            messageEntity.setReceiveUserCid(userCidList);
            messageEntity.setSendUser(BaseContextHandler.getUserName());
            messageEntity.setForwardTag("historyRecordRemind");//消息提醒类型 保持唯一

            List<Map<String, Object>> extMapList = new ArrayList();
            //报警类型
            Map<String, Object> extMap = new HashMap<>();
            extMap.put("key","warningType");
            extMap.put("value",alarmTypeMap.get(recordHistory.getAlarmRuleType()));
            extMapList.add(extMap);
            //报警级别
            extMap = new HashMap<>();
            extMap.put("key","level");
            extMap.put("value", recordHistory.getAlarmRuleLevelName());
            extMapList.add(extMap);
            //报警时间
            extMap = new HashMap<>();
            extMap.put("key","recordTime");
            extMap.put("value", recordHistory.getRecordTime() == null ? "" : DateUtils.format(recordHistory.getRecordTime(),"yyyy-MM-dd HH:mm:ss"));
            extMapList.add(extMap);
            //报警id
            extMap = new HashMap<>();
            extMap.put("key","id");
            extMap.put("value",recordHistory.getId());
            extMapList.add(extMap);

            extMap = new HashMap<>();
            extMap.put("key","alarmType");
            extMap.put("value",recordHistory.getAlarmRuleType());
            extMapList.add(extMap);

            extMap = new HashMap<>();
            extMap.put("key","processInstanceId");
            extMap.put("value",recordHistory.getProcessInstanceId());
            extMapList.add(extMap);


            extMap = new HashMap<>();
            extMap.put("key","processDefinitionId");
            extMap.put("value",recordHistory.getProcessDefinitionId());
            extMapList.add(extMap);

            messageEntity.setExtMapList(extMapList);
            IPushService appPushService = PushMessageFactory.getPushService("MOB");
            IPushService webPushService = PushMessageFactory.getPushService("WEB");
            appPushService.pushMessage(messageEntity);
            webPushService.pushMessage(messageEntity);
        }
    }

    private AlarmRuleLevelConfigTemplate getAlarm(AlarmRecordHistory recordHistory) {
        List<AlarmRuleLevelConfigTemplate> alarmRuleList = alarmRuleConfigService.getAlarmConfigTemplateByAlarmType(recordHistory.getAlarmRuleType(), "af880d6a13404a67825e94bc0f2f3808");
        if (alarmRuleList == null) {
            return null;
        }
        for (AlarmRuleLevelConfigTemplate alarmRuleLevelConfigTemplate : alarmRuleList) {
            if (alarmRuleLevelConfigTemplate.getAlarmTemplateLevelName().equals(recordHistory.getAlarmRuleLevelName())) {
                return alarmRuleLevelConfigTemplate;
            }
        }
        return null;
    }

}