Explorar o código

Merge branch 'dev' into sky_v2

15928045575 %!s(int64=2) %!d(string=hai) anos
pai
achega
d641d4fbde

+ 28 - 0
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/consumer/MqttCallbackHandler.java

@@ -27,6 +27,7 @@ import com.sckw.slope.detection.model.dto.DevicesAlarm;
 import com.sckw.slope.detection.model.dto.DevicesItem;
 import com.sckw.slope.detection.model.dto.SystemDict;
 import com.sckw.slope.detection.model.vo.DeviceIntegrationVo;
+import com.sckw.slope.detection.model.vo.ProjectVo;
 import com.sckw.slope.detection.service.CommonService;
 import com.sckw.slope.detection.service.TdengineService;
 import com.sckw.slope.detection.service.api.DetectionApiService;
@@ -189,6 +190,33 @@ public class MqttCallbackHandler extends AbstractHandler {
                                     , smsTemplateConfig.getDataAlarm().getSignName()
                                     , templateParam);
                         }
+                        ProjectVo vo = projectDeviceMapper.selectProjectByDeviceId(device.getId());
+                        /**邮寄推送*/
+                        List<KwsAlarmInfo> emailList = alarmInfoRepository.list(new LambdaQueryWrapper<KwsAlarmInfo>()
+                                .eq(KwsAlarmInfo::getMountainId, mountainId)
+                                .eq(KwsAlarmInfo::getLevel, level.intValue())
+                                .eq(KwsAlarmInfo::getType, NumberConstant.TWO)
+                                .eq(KwsAlarmInfo::getDelFlag, NumberConstant.ZERO)
+                        );
+                        List<String> emailAddress = new ArrayList<>();
+                        if (!CollectionUtils.isEmpty(emailList)) {
+                            emailAddress = list.stream().map(KwsAlarmInfo::getValueDesc).distinct().collect(Collectors.toList());
+                        }
+                        if (!CollectionUtils.isEmpty(emailAddress)){
+                            Map<String, Object> templateParam = new HashMap<>();
+                            if (StringUtils.isBlank(time)) {
+                                LocalDateTime localDateTime = Instant.ofEpochSecond(ts).atZone(ZoneId.systemDefault()).toLocalDateTime();
+                                time = com.sckw.excel.utils.DateUtil.format(localDateTime);
+                            }
+                            templateParam.put("company", smsTemplateConfig.getDataAlarm().getSignName());
+                            templateParam.put("level", level.toString());
+                            templateParam.put("name", device.getName());
+                            templateParam.put("time", time);
+                            templateParam.put("project", vo.getProjectName());
+                            templateParam.put("value", itemValue);
+                            commonService.pushEmailMessage(emailAddress
+                                    , templateParam);
+                        }
                     }
                     //集成要素阈值告警
                     deviceIntegrationsAlarm(device, ts);

+ 27 - 5
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/controller/CommonController.java

@@ -11,6 +11,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 /**
  * @author lfdc
  * @description 公共字典查询
@@ -23,15 +28,16 @@ public class CommonController {
     @Autowired
     CommonService commonService;
 
-    @RequestMapping(value = "/getDict",method = RequestMethod.POST)
-    public HttpResult getDict(@RequestBody @Valid QueryDictTypePageReqVo queryDictTypePageReqVo){
-        return  HttpResult.ok(commonService.getDict(queryDictTypePageReqVo));
+    @RequestMapping(value = "/getDict", method = RequestMethod.POST)
+    public HttpResult getDict(@RequestBody @Valid QueryDictTypePageReqVo queryDictTypePageReqVo) {
+        return HttpResult.ok(commonService.getDict(queryDictTypePageReqVo));
     }
 
 
     @Autowired
-    SmsTemplateConfig  smsTemplateConfig;
-//
+    SmsTemplateConfig smsTemplateConfig;
+
+    //
 //    @RequestMapping(value = "/pushSmsMessageByDataAlarm",method = RequestMethod.GET)
 //    public HttpResult pushSmsMessageByDataAlarm(){
 //
@@ -70,4 +76,20 @@ public class CommonController {
 //                , templateParam);
 //        return  HttpResult.ok();
 //    }
+    @RequestMapping(value = "/pushEmailMessage", method = RequestMethod.GET)
+    public HttpResult pushEmailMessage() {
+        Map<String, Object> templateParam = new HashMap<>();
+        templateParam.put("company", smsTemplateConfig.getDataAlarm().getSignName());
+        templateParam.put("level", "一级");
+        templateParam.put("name", "设备名称");
+        templateParam.put("time", "2023-12-05 15:00:00");
+        templateParam.put("project", "第一个项目");
+        templateParam.put("value", "29.5645E");
+        List<String> emailAddress = new ArrayList<>();
+        emailAddress.add("1573035851@qq.com");
+        emailAddress.add("lengfaqiang96@163.com");
+        commonService.pushEmailMessage(emailAddress
+                , templateParam);
+        return HttpResult.ok();
+    }
 }

+ 8 - 0
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/dao/mysql/KwsProjectDeviceMapper.java

@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.sckw.slope.detection.model.dos.mysql.KwsProjectDevice;
 import com.sckw.slope.detection.model.dto.BaseItemDTO;
 import com.sckw.slope.detection.model.vo.IntegrationItemVO;
+import com.sckw.slope.detection.model.vo.ProjectVo;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
@@ -27,6 +28,13 @@ public interface KwsProjectDeviceMapper extends BaseMapper<KwsProjectDevice> {
 
     List<BaseItemDTO> selectDataByDeviceId(@Param("id") long id);
 
+    /**
+     * 设备id查项目数据
+     * @param id 设备id
+     * @return
+     */
+    ProjectVo selectProjectByDeviceId(@Param("deviceId") long id);
+
 
     List<IntegrationItemVO> selectIntegrationItemByProjectId(@Param("id") long id);
 

+ 11 - 0
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/model/param/SmsMessageParam.java

@@ -31,8 +31,19 @@ public class SmsMessageParam implements Serializable {
 
     @Data
     public static class Content implements Serializable{
+
+        /**
+         * sms 使用
+         */
         private String templateCode;
         private String signName;
         private Map<String,String> templateParam;
+
+
+        /**
+         * email 使用
+         */
+        private String subject;
+        private String text;
     }
 }

+ 4 - 0
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/model/vo/DeviceListBean.java

@@ -39,4 +39,8 @@ public class DeviceListBean implements Serializable {
      * 设备ID
      */
     private String deviceId;
+    /**
+     * 告警状态
+     */
+    private String alarmStatus;
 }

+ 16 - 11
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/service/CommonService.java

@@ -387,9 +387,10 @@ public class CommonService {
 
     /**
      * 推送sms短信
-     * @param pushToPhone 手机号
+     *
+     * @param pushToPhone   手机号
      * @param templateCode  模板编码
-     * @param signName  签名
+     * @param signName      签名
      * @param templateParam 请求参数
      */
     @Async
@@ -413,21 +414,25 @@ public class CommonService {
 
     /**
      * 推送邮箱
-     * @param pushToPhone
-     * @param templateCode
-     * @param signName
+     *
+     * @param emailAddress
      * @param templateParam
      */
     @Async
-    public void pushEmailMessage(List<String> pushToPhone, String templateCode, String signName, Map<String, String> templateParam) {
+    public void pushEmailMessage(List<String> emailAddress, Map<String, Object> templateParam) {
         SmsMessageParam param = new SmsMessageParam();
         param.setCode("");
-        param.setType(2);
-        param.setPushTo(pushToPhone);
+        param.setType(4);
+        param.setPushTo(emailAddress);
         SmsMessageParam.Content content = new SmsMessageParam.Content();
-        content.setTemplateCode(templateCode);
-        content.setSignName(signName);
-        content.setTemplateParam(templateParam);
+        String templateStr = "尊敬的${company}用户,在北京时间${time}监测到有${level}级告警被触发;\n" +
+                "设备名称:${name};\n" +
+                "所属项目:${project};\n" +
+                "监测数值:${value};\n" +
+                "请在收到邮件后尽快处理,或查阅操作手册文档,确认告警通知相关信息。";
+        String text = com.sckw.core.utils.StringUtils.replaceTextVar(templateStr, templateParam);
+        content.setSubject("设备阈值告警");
+        content.setText(text);
         param.setContent(content);
         Map<String, String> headerMap = new HashMap<>();
         headerMap.put("sign", new MD5Utils().getMd5("sign"));

+ 74 - 10
slope-modules/slope-detection/src/main/java/com/sckw/slope/detection/service/ProjectService.java

@@ -300,8 +300,13 @@ public class ProjectService {
                 KwsDevice kwsDevice = deviceMapper.selectByPrimaryKey(deviceRelation.getDeviceId());
                 projectDeviceDTO.setParentName(kwsDevice == null ? null : kwsDevice.getName());
             }
-            if (projectDeviceDTO.getId().equals(projectDeviceDTO.getProjectId())) {
-                projectDeviceDTO.setParentName(null);
+            if (StringUtils.isNotBlank(projectDeviceDTO.getParentId())) {
+                if (projectDeviceDTO.getId().equals(projectDeviceDTO.getParentId())) {
+                    projectDeviceDTO.setParentName(null);
+                } else {
+                    KwsDevice device = deviceMapper.selectById(Long.parseLong(projectDeviceDTO.getParentId()));
+                    projectDeviceDTO.setParentName(device == null ? null : device.getName());
+                }
             }
         }
         return PageRes.build(info, list);
@@ -1199,7 +1204,9 @@ public class ProjectService {
         //如果删除主设备  子设备也要删除
         //id集合
         List<Long> deleteList = new ArrayList<>();
+        List<String> deleteDiviceList = new ArrayList<>();
         if (deviceId.equals(deviceParentId)) {
+            deleteDiviceList.add(deviceId);
             List<KwsDeviceRelation> deviceRelationList = deviceRelationMapper.selectList(
                     new LambdaQueryWrapper<KwsDeviceRelation>()
                             .eq(KwsDeviceRelation::getDeviceId, deviceId)
@@ -1230,15 +1237,23 @@ public class ProjectService {
                 );
             }
         } else {
-            deleteList.add(Long.parseLong(deviceId));
+            deleteList.add(Long.parseLong(bindDeviceId));
+            deleteDiviceList.add(deviceId);
+            deleteDiviceList.add(bindDeviceId);
             deviceRelationMapper.update(null, new LambdaUpdateWrapper<KwsDeviceRelation>()
-                    .eq(KwsDeviceRelation::getMountainId, headerData.getMountainId())
+                    .eq(KwsDeviceRelation::getDeviceId, deviceParentId)
+                    .eq(KwsDeviceRelation::getRelateDeviceId, deviceId)
                     .set(KwsDeviceRelation::getDelFlag, 1)
-                    .in(KwsDeviceRelation::getRelateDeviceId, deleteList)
+                    .in(KwsDeviceRelation::getId, deleteList)
             );
+//            projectDeviceMapper.update(null,
+//                    new LambdaUpdateWrapper<KwsProjectDevice>()
+//                            .eq(KwsProjectDevice::getDeviceId,deviceId)
+//                            .eq(KwsProjectDevice::getProjectId,projectId)
+//            );
         }
         //矿山与设备解绑
-        if (!CollectionUtils.isEmpty(deleteList)) {
+        if (!CollectionUtils.isEmpty(deleteDiviceList)) {
             projectDeviceMapper.update(null, new LambdaUpdateWrapper<KwsProjectDevice>()
                     .set(KwsProjectDevice::getDelFlag, 1)
                     .set(KwsProjectDevice::getUpdateBy, headerData.getUpdateBy() == null ? null : Long.parseLong(headerData.getUpdateBy()))
@@ -1246,13 +1261,13 @@ public class ProjectService {
                     .eq(KwsProjectDevice::getDelFlag, 0)
                     .eq(KwsProjectDevice::getProjectId, projectId)
                     .eq(KwsProjectDevice::getMountainId, headerData.getMountainId())
-                    .in(KwsProjectDevice::getDeviceId, deviceId)
+                    .in(KwsProjectDevice::getDeviceId, deleteDiviceList)
             );
         }
         //设备修改状态
-        if (!CollectionUtils.isEmpty(deleteList)) {
+        if (!CollectionUtils.isEmpty(deleteDiviceList)) {
             deviceMapper.update(null, new LambdaUpdateWrapper<KwsDevice>()
-                    .in(KwsDevice::getId, deviceId)
+                    .in(KwsDevice::getId, deleteDiviceList)
                     .eq(KwsDevice::getMountainId, headerData.getMountainId())
                     .eq(KwsDevice::getDelFlag, 0)
                     .set(KwsDevice::getRelevanceLevel, 0)
@@ -1261,7 +1276,7 @@ public class ProjectService {
                     .set(KwsDevice::getUpdateBy, headerData.getUpdateBy() == null ? Long.parseLong(headerData.getCreateBy()) : Long.parseLong(headerData.getUpdateBy()))
             );
         }
-        if (!CollectionUtils.isEmpty(deleteList)) {
+        if (!CollectionUtils.isEmpty(deleteDiviceList)) {
             List<KwsDevice> kwsDevices = deviceMapper.selectList(new LambdaQueryWrapper<KwsDevice>().in(KwsDevice::getId, deviceId).eq(KwsDevice::getDelFlag, 0));
             if (!CollectionUtils.isEmpty(kwsDevices)) {
                 StringBuilder deviceName = new StringBuilder();
@@ -1406,6 +1421,9 @@ public class ProjectService {
                     bean.setLogicAlt(device.getLogicLat());
                     bean.setAlias(device.getAlias());
                     bean.setDeviceId(device.getDeviceId());
+                    String deviceId = device.getDeviceId();
+                    String alarmStatus = getAlarmStatus(deviceId);
+                    bean.setAlarmStatus(alarmStatus);
                     beans.add(bean);
                 });
                 vo.setDeviceList(beans);
@@ -1415,6 +1433,52 @@ public class ProjectService {
         return HttpResult.ok(returnList);
     }
 
+    private String getAlarmStatus(String deviceId) {
+        String alarmStatus = "0";
+        List<KwsThreshold> kwsThresholds = thresholdMapper.selectList(new LambdaQueryWrapper<KwsThreshold>()
+                .eq(KwsThreshold::getDeviceId, Long.parseLong(deviceId))
+                .eq(KwsThreshold::getDelFlag, NumberConstant.ZERO)
+        );
+        List<KwsAlarmDetail> deviceDetails = alarmDetailMapper.selectList(
+                new LambdaQueryWrapper<KwsAlarmDetail>()
+                        .in(KwsAlarmDetail::getPid, deviceId)
+                        .eq(KwsAlarmDetail::getDelFlag, NumberConstant.ZERO)
+                        .eq(KwsAlarmDetail::getStatus, NumberConstant.ZERO)
+        );
+        if (!CollectionUtils.isEmpty(deviceDetails)) {
+            alarmStatus = "1";
+        }
+        if (!CollectionUtils.isEmpty(kwsThresholds)) {
+            List<Long> longList = kwsThresholds.stream().map(KwsThreshold::getId).distinct().collect(Collectors.toList());
+            if (!CollectionUtils.isEmpty(longList) && longList.size() > 0) {
+                List<KwsAlarmDetail> details = alarmDetailMapper.selectList(
+                        new LambdaQueryWrapper<KwsAlarmDetail>()
+                                .in(KwsAlarmDetail::getPid, longList)
+                                .eq(KwsAlarmDetail::getDelFlag, NumberConstant.ZERO)
+                                .eq(KwsAlarmDetail::getStatus, NumberConstant.ZERO)
+                );
+                if (!CollectionUtils.isEmpty(details) && details.size() > 0) {
+                    List<Long> thresholdIds = details.stream().map(KwsAlarmDetail::getPid).collect(Collectors.toList());
+                    if (!CollectionUtils.isEmpty(thresholdIds)) {
+                        List<KwsThreshold> thresholds = thresholdMapper.selectList(
+                                new LambdaQueryWrapper<KwsThreshold>()
+                                        .in(KwsThreshold::getId, thresholdIds)
+                                        .eq(KwsThreshold::getDelFlag, NumberConstant.ZERO)
+                                        .eq(KwsThreshold::getStatus, NumberConstant.ZERO)
+                        );
+                        if (!CollectionUtils.isEmpty(thresholds)) {
+                            List<Integer> levelList = thresholds.stream().map(KwsThreshold::getLevel).sorted().distinct().collect(Collectors.toList());
+                            if (!CollectionUtils.isEmpty(levelList)) {
+                                alarmStatus = String.valueOf(levelList.get(0));
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        return alarmStatus;
+    }
+
     /**
      * 修改组网设备-关联设备-查询
      *

+ 11 - 0
slope-modules/slope-detection/src/main/resources/mapper/mysql/KwsProjectDeviceMapper.xml

@@ -225,4 +225,15 @@ WHERE
       AND d.del_flag =0
       and b.id = #{id}
     </select>
+
+    <select id="selectProjectByDeviceId" resultType="com.sckw.slope.detection.model.vo.ProjectVo">
+        SELECT
+            a.id AS projectId,
+            a.`name` AS projectName
+        FROM
+            kws_project a
+                LEFT JOIN kws_project_device b ON a.id = b.project_id
+        WHERE
+            b.id = #{deviceId}
+    </select>
 </mapper>

+ 11 - 11
slope-modules/slope-detection/src/main/resources/mapper/mysql/KwsProjectMapper.xml

@@ -98,17 +98,17 @@
     </select>
 
     <select id="selectDeviceList" resultType="com.sckw.slope.detection.model.vo.ProjectDeviceVO">
-        SELECT a.id           AS projectId,
-               a.`name`       AS projectName,
-               c.id           AS id,
-               c.`name`       AS NAME,
-               c.alias        AS alias,
-               d.device_type  AS deviceType,
-               d.manufacturer AS manufacturer,
-               d.`name`       AS deviceModelName,
-               c.create_time  AS createTime,
-               e.id           AS deviceRelationId,
-               e.device_id    AS parentId
+        SELECT DISTINCT e.id           AS deviceRelationId,
+                        e.device_id    AS parentId,
+                        a.id           AS projectId,
+                        a.`name`       AS projectName,
+                        c.id           AS id,
+                        c.`name`       AS NAME,
+                        c.alias        AS alias,
+                        d.device_type  AS deviceType,
+                        d.manufacturer AS manufacturer,
+                        d.`name`       AS deviceModelName,
+                        c.create_time  AS createTime
         FROM kws_project a
                  LEFT JOIN kws_project_device b
                            ON a.id = b.project_id