wisdomisite-java/src/main/java/com/zhgd/xmgl/async/AsyncAiAnalyse.java

182 lines
7.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.zhgd.xmgl.async;
import com.zhgd.mqtt.bean.PushPayload;
import com.zhgd.mqtt.server.IMqttSender;
import com.zhgd.xmgl.constant.Cts;
import com.zhgd.xmgl.modules.basicdata.constant.DictionaryConstant;
import com.zhgd.xmgl.modules.basicdata.entity.CompanyConfig;
import com.zhgd.xmgl.modules.basicdata.entity.DictionaryItem;
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
import com.zhgd.xmgl.modules.basicdata.mapper.SystemUserMapper;
import com.zhgd.xmgl.modules.basicdata.service.ICompanyConfigService;
import com.zhgd.xmgl.modules.basicdata.service.ISystemUserService;
import com.zhgd.xmgl.modules.basicdata.service.impl.DictionaryItemServiceImpl;
import com.zhgd.xmgl.modules.basicdata.service.impl.NoticeServiceImpl;
import com.zhgd.xmgl.modules.video.entity.AiAnalyseHardWareAlarmRecord;
import com.zhgd.xmgl.push.service.UniPushService;
import com.zhgd.xmgl.util.MapBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @program: wisdomSite
* @description: 异步AI分析数据处理
* @author: Mr.Peng
* @create: 2022-04-20 14:09
**/
@Slf4j
@Component
public class AsyncAiAnalyse {
@Autowired
private IMqttSender mqttPushClient;
@Autowired
private SystemUserMapper systemUserMapper;
@Autowired
private ISystemUserService systemUserService;
@Autowired
private ICompanyConfigService companyConfigService;
@Autowired
private UniPushService uniPushService;
@Autowired
private DictionaryItemServiceImpl dictionaryItemService;
@Lazy
@Autowired
private NoticeServiceImpl noticeService;
@Value("${mqtt-scope}")
private String scope;
/**
* @param record
*/
@Async("taskExecutor")
public void sendAiAnalyse(AiAnalyseHardWareAlarmRecord record, String type) {
try {
String title = getTitleByAlarmType(record.getAlarmType(), record.getProjectSn());
title = title == null ? "其他类型" : title;
String msg = title + ",位置" + record.getLocation();
List<SystemUser> systemUserList;
if (type.equals(Cts.PROJECT_LEVEL)) {
systemUserList = systemUserService.getSystemUsersBySn(new MapBuilder<String, Object>().put(Cts.SN, record.getProjectSn()).put(Cts.QUERY_TYPE, Cts.PROJECT_LEVEL).build());
} else {
systemUserList = systemUserService.getSystemUsersBySn(new MapBuilder<String, Object>().put(Cts.SN, record.getProjectSn()).put(Cts.QUERY_TYPE, Cts.PROJECT_LEVEL_AND_CHILDREN).build());
}
title = "AI分析" + title;
//向项目管理员和子账号推送通知
if (systemUserList.size() > 0) {
for (SystemUser systemUser : systemUserList) {
noticeService.addUserNotice(systemUser.getUserId(), msg, title, "8");
}
sendAppNotice(record.getProjectSn(), title, msg, systemUserList, "/pages/potentialRisk/potentialRisk?id=" + record.getId());
}
} catch (Exception e) {
log.error("error", e);
}
}
public String getTitleByAlarmType(Integer alarmType, String projectSn) {
DictionaryItem dict = dictionaryItemService.getDict(DictionaryConstant.AI_ANALYSE_HARD_WARE_ALARM_RECORD_TYPE, alarmType + "", projectSn);
return dict != null ? dict.getName() : null;
}
/**
* 向手机端推送通知
*
* @param projectSn
* @param title
* @param msg
* @param systemUserList
*/
public void sendAppNotice(String projectSn, String title, String msg, List<SystemUser> systemUserList, String payload) {
try {
CompanyConfig companyConfig = companyConfigService.getCompanyConfigByProjectSn(projectSn);
log.info("companyConfig-------------发送开关" + (companyConfig == null ? "" : companyConfig.getAppNoticeType()));
if (companyConfig != null && companyConfig.getAppNoticeType() != null && companyConfig.getAppNoticeType() == 1) {
for (SystemUser systemUser : systemUserList) {
if (systemUser.getAccountType() == 5 || systemUser.getAccountType() == 6) {
log.info("发送-----" + systemUser.getAccount() + "消息:" + msg);
uniPushService.pushToSingleByAlias(systemUser.getClientId(), title, msg, payload);
}
}
}
} catch (Exception e) {
log.error("error", e);
}
}
/**
* 责任人收到AI警告后推送消息给整改责任人
*
* @param record AI警告消息
* @param systemUserList 需要推送的人员集合
*/
public void sendCorrectAnalyse(AiAnalyseHardWareAlarmRecord record, List<SystemUser> systemUserList) {
String msg = "你有一条待整改消息,位置-----" + record.getLocation();
String title = getTitleByAlarmType(record.getAlarmType(), record.getProjectSn());
if (CollectionUtils.isNotEmpty(systemUserList)) {
for (SystemUser systemUser : systemUserList) {
log.info(systemUser.getAccount() + "有一条消息,位置-----" + record.getLocation());
PushPayload pushMessage = PushPayload.getPushPayloadBuider().setAccountId(systemUser.getUserId().toString())
.setTitle(title)
.setContent(msg)
.setType("8")
.setItemType(record.getAlarmType().toString()).bulid();
String kdTopic = scope + systemUser.getUserId();
mqttPushClient.sendToMqtt(kdTopic, pushMessage.toString());
}
sendAppNotice(record.getProjectSn(), title, msg, systemUserList, "/pages/potentialRisk/potentialRisk?id=" + record.getId());
}
}
/**
* 推送巡检信息到责任人
*/
public void sendMsgGotoPeople(List<SystemUser> systemUserList) {
String msg = "您有一条日常巡检信息待处理请及时处理";
String title = "日常巡检";
if (CollectionUtils.isNotEmpty(systemUserList)) {
for (SystemUser systemUser : systemUserList) {
log.info(systemUser.getAccount() + "有一条消息");
PushPayload pushMessage = PushPayload.getPushPayloadBuider().setAccountId(systemUser.getUserId().toString())
.setTitle(title)
.setContent(msg)
.setType("8")
.setItemType("日常巡检".toString()).bulid();
String kdTopic = scope + systemUser.getUserId();
mqttPushClient.sendToMqtt(kdTopic, pushMessage.toString());
}
sendMsgToApp(title, msg, systemUserList, "/pages/projectEnd/dailyCheck/index");
}
}
/**
* 巡检消息推送到责任人
*
* @param title
* @param msg
* @param systemUserList
* @param payload
*/
private void sendMsgToApp(String title, String msg, List<SystemUser> systemUserList, String payload) {
try {
for (SystemUser systemUser : systemUserList) {
if (systemUser.getAccountType() == 5 || systemUser.getAccountType() == 6) {
log.info("发送-----" + systemUser.getAccount() + "消息:" + msg);
uniPushService.pushToSingleByAlias(systemUser.getClientId(), title, msg, payload);
}
}
} catch (Exception e) {
log.error("error", e);
}
}
}