Kaynağa Gözat

soc-modules-iot模块-诊断任务绑定操作,代码优化

humingshi-7@163.com 11 ay önce
ebeveyn
işleme
6894accc28

+ 1 - 1
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/controller/CameraDiagnoseMissionController.java

@@ -34,7 +34,7 @@ public class CameraDiagnoseMissionController {
     @ApiModelProperty("诊断任务绑定操作")
     @PostMapping({"/bindMission"})
     public AjaxResult bindMission(@RequestBody final DiagnoseMissionPageDto request) {
-        diagnoseMissionService.selectDiagnoseMissionPage(request);
+        diagnoseMissionService.camerasBind(request);
         return AjaxResult.success(null);
     }
 

+ 16 - 0
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/controller/CameraDiagnoseThresholdController.java

@@ -1,10 +1,12 @@
 package com.xunmei.iot.controller;
 
+import com.xunmei.common.core.web.domain.AjaxResult;
 import com.xunmei.common.core.web.page.TableDataInfo;
 import com.xunmei.iot.dto.cameraDiagnose.DiagnoseThresholdPageDto;
 import com.xunmei.iot.service.DiagnoseThresholdService;
 import com.xunmei.iot.vo.alarmData.AlarmTypeSelectedVO;
 import com.xunmei.iot.vo.sensor.DiagnoseThresholdPageVo;
+import io.swagger.annotations.ApiModelProperty;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.web.bind.annotation.*;
 import javax.annotation.Resource;
@@ -29,4 +31,18 @@ public class CameraDiagnoseThresholdController {
     public List<AlarmTypeSelectedVO> thresholdTypeList() {
         return diagnoseThresholdService.thresholdTypeList();
     }
+
+    @ApiModelProperty("诊断阈值绑定操作")
+    @PostMapping({"/bindThreshold"})
+    public AjaxResult bindThreshold(@RequestBody final DiagnoseThresholdPageDto request) {
+        diagnoseThresholdService.selectDiagnoseThresholdPage(request);
+        return AjaxResult.success(null);
+    }
+
+    @ApiModelProperty("诊断阈值解绑操作")
+    @PostMapping({"/unbindThreshold"})
+    public AjaxResult unbindThreshold(@RequestBody final DiagnoseThresholdPageDto request) {
+        diagnoseThresholdService.selectDiagnoseThresholdPage(request);
+        return AjaxResult.success(null);
+    }
 }

+ 4 - 0
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/dto/cameraDiagnose/DiagnoseThresholdPageDto.java

@@ -22,4 +22,8 @@ public class DiagnoseThresholdPageDto extends PageRequest {
     @ApiModelProperty(value = "阈值code")
     private String thresholdCode;
 
+
+    @ApiModelProperty(value = "需要绑定或解绑的阈值code数组")
+    private List<String> bindThresholdCodes;
+
 }

+ 14 - 0
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/mapper/DiagnoseMissionChMapper.java

@@ -2,7 +2,10 @@ package com.xunmei.iot.mapper;
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseMission;
+import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseMissionCh;
+import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseThresholdCh;
 import com.xunmei.iot.dto.cameraDiagnose.DiagnoseMissionPageDto;
+import com.xunmei.iot.vo.sensor.DiagnoseMissionBindVo;
 import com.xunmei.iot.vo.sensor.DiagnoseMissionPageVo;
 import org.apache.ibatis.annotations.Param;
 import org.springframework.stereotype.Repository;
@@ -11,5 +14,16 @@ import java.util.List;
 @Repository
 public interface DiagnoseMissionChMapper {
     Page<DiagnoseMissionPageVo> selectPageData(Page<DiagnoseMissionPageVo> pageRequest, @Param("request") DiagnoseMissionPageDto request);
+    Integer selectCamerasCount(@Param("request") DiagnoseMissionPageDto request);
+
+    List<DiagnoseMissionBindVo> selectCamerasBind(@Param("request") DiagnoseMissionPageDto request);
+    //批量插入-任务通道绑定
+
+    int batchInsertMissionCh(@Param("list") List<IotDeviceDiagnoseMissionCh> list);
+    //批量更新-任务通道绑定
+    int batchUpdateMissionCh(@Param("list") List<IotDeviceDiagnoseMissionCh> list);
+    //批量插入-默认阈值通道绑定
+    int batchInsertThresholdCh(@Param("list") List<IotDeviceDiagnoseThresholdCh> list);
+
     List<IotDeviceDiagnoseMission> selectAllDiagnoseMission();
 }

+ 4 - 1
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/mapper/DiagnoseThresholdChMapper.java

@@ -1,5 +1,6 @@
 package com.xunmei.iot.mapper;
 
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseThreshold;
 import com.xunmei.iot.dto.cameraDiagnose.DiagnoseThresholdPageDto;
@@ -10,7 +11,9 @@ import org.springframework.stereotype.Repository;
 import java.util.List;
 
 @Repository
-public interface DiagnoseThresholdChMapper {
+public interface DiagnoseThresholdChMapper  extends BaseMapper<IotDeviceDiagnoseThreshold> {
     Page<DiagnoseThresholdPageVo> selectPageData(Page<DiagnoseThresholdPageVo> pageRequest, @Param("request") DiagnoseThresholdPageDto request);
     List<IotDeviceDiagnoseThreshold> selectAllDiagnoseThreshold();
+
+    Integer selectBindCount(@Param("thresholdCode")String thresholdCode);
 }

+ 68 - 4
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/service/impl/DiagnoseMissionServiceImpl.java

@@ -1,19 +1,28 @@
 package com.xunmei.iot.service.impl;
 
 import cn.hutool.core.util.ObjectUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.google.common.collect.Lists;
 import com.xunmei.common.core.constant.CacheConstants;
+import com.xunmei.common.core.constant.Constants;
 import com.xunmei.common.core.constant.SecurityConstants;
 import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseMission;
+import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseMissionCh;
+import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseThreshold;
+import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseThresholdCh;
 import com.xunmei.common.core.enums.OrgTypeEnum;
+import com.xunmei.common.core.exception.ServiceException;
+import com.xunmei.common.core.utils.IDHelper;
 import com.xunmei.common.core.utils.StringUtils;
 import com.xunmei.common.core.web.page.TableDataInfo;
 import com.xunmei.common.redis.utils.RedisUtils;
 import com.xunmei.iot.dto.cameraDiagnose.DiagnoseMissionPageDto;
 import com.xunmei.iot.mapper.DiagnoseMissionChMapper;
+import com.xunmei.iot.mapper.DiagnoseThresholdChMapper;
 import com.xunmei.iot.service.DiagnoseMissionService;
 import com.xunmei.iot.vo.alarmData.AlarmTypeSelectedVO;
+import com.xunmei.iot.vo.sensor.DiagnoseMissionBindVo;
 import com.xunmei.iot.vo.sensor.DiagnoseMissionPageVo;
 import com.xunmei.system.api.RemoteOrgService;
 import com.xunmei.system.api.domain.SysOrg;
@@ -32,6 +41,8 @@ public class DiagnoseMissionServiceImpl implements DiagnoseMissionService {
 
     @Autowired
     private DiagnoseMissionChMapper diagnoseMissionChMapper;
+    @Autowired
+    private DiagnoseThresholdChMapper diagnoseThresholdChMapper;
 
     @Override
     public TableDataInfo<DiagnoseMissionPageVo> selectDiagnoseMissionPage(DiagnoseMissionPageDto request) {
@@ -113,13 +124,66 @@ public class DiagnoseMissionServiceImpl implements DiagnoseMissionService {
             request.setOrgId(null);
         }
 
-        List<IotDeviceDiagnoseMission> missions = diagnoseMissionChMapper.selectAllDiagnoseMission();
+        Integer camerasCount = diagnoseMissionChMapper.selectCamerasCount(request);
+        if (camerasCount == null || camerasCount == 0) {
+            String errorMsg = "绑定任务查询条件筛选结果为空!";
+            throw  new ServiceException(errorMsg);
+        }
 
-        Page<DiagnoseMissionPageVo> page = diagnoseMissionChMapper.selectPageData(request.getPageRequest(), request);
-        final List<SysOrgVO> cacheList = RedisUtils.getCacheList(CacheConstants.ORG_CACHE_LIST_KEY);
+        String bindMissionId = request.getBindMissionId();
+        QueryWrapper<IotDeviceDiagnoseThreshold> queryWrapper = new QueryWrapper();
+        queryWrapper.eq("type", 1);
+        queryWrapper.last(Constants.LIMIT1);
+        IotDeviceDiagnoseThreshold system = diagnoseThresholdChMapper.selectOne(queryWrapper);
+        //获取未绑定阈值的通道
+        List<IotDeviceDiagnoseThresholdCh> batchInsertsUnbindThresholdCh = Lists.newArrayList();
+        //获取要绑定的通道
+        List<IotDeviceDiagnoseMissionCh> batchInserts = Lists.newArrayList();
+        List<IotDeviceDiagnoseMissionCh> batchUpdates = Lists.newArrayList();
+
+        List<DiagnoseMissionBindVo> list = diagnoseMissionChMapper.selectCamerasBind(request);
+        for (DiagnoseMissionBindVo info :list) {
+            String beanId = info.getMissionId();
+            IotDeviceDiagnoseMissionCh chn = new IotDeviceDiagnoseMissionCh();
+            chn.setIotToken(info.getIotToken());
+            chn.setDeviceProduct(info.getDeviceProduct());
+            chn.setDvs(info.getDvs());
+            chn.setChannel(info.getChannel());
+            chn.setMissionId(bindMissionId);
+
+            if (StringUtils.isBlank(beanId)) {
+                chn.setId(IDHelper.id());
+                batchInserts.add(chn);
+            } else if (!beanId.equals(bindMissionId)) {
+                batchUpdates.add(chn);
+            }
+            if (StringUtils.isEmpty(info.getThresholdCodes())) {
+                IotDeviceDiagnoseThresholdCh thresholdCh = new IotDeviceDiagnoseThresholdCh();
+                thresholdCh.setThresholdCode(system.getThresholdCode());
+                thresholdCh.setIotToken(info.getIotToken());
+                thresholdCh.setDeviceProduct(info.getDeviceProduct());
+                thresholdCh.setDvs(info.getDvs());
+                thresholdCh.setChannel(info.getChannel());
+                thresholdCh.setId(IDHelper.id());
+                batchInsertsUnbindThresholdCh.add(thresholdCh);
+            }
+        }
 
-        for (DiagnoseMissionPageVo bean : page.getRecords()) {
+        if (batchInserts != null && !batchInserts.isEmpty()) {
+            diagnoseMissionChMapper.batchInsertMissionCh(batchInserts);
+        }
+        if (batchUpdates != null && !batchUpdates.isEmpty()) {
+            diagnoseMissionChMapper.batchUpdateMissionCh(batchUpdates);
+        }
 
+        //查询的通道如果未绑定阈值,绑定它们到默认阈值上
+        if (batchInsertsUnbindThresholdCh != null && !batchInsertsUnbindThresholdCh.isEmpty()) {
+            diagnoseMissionChMapper.batchInsertThresholdCh(batchInsertsUnbindThresholdCh);
         }
+
+        //更新默认阈值绑定数量
+        Integer cameraBindCount = diagnoseThresholdChMapper.selectBindCount(system.getThresholdCode());
+        system.setDignoseNumber(cameraBindCount);
+        diagnoseThresholdChMapper.updateById(system);
     }
 }

+ 13 - 0
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/vo/sensor/DiagnoseMissionBindVo.java

@@ -0,0 +1,13 @@
+package com.xunmei.iot.vo.sensor;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseMissionCh;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+@Data
+public class DiagnoseMissionBindVo extends IotDeviceDiagnoseMissionCh {
+    @JsonIgnore
+    @ApiModelProperty("机构路径")
+    private String thresholdCodes;
+}

+ 115 - 0
soc-modules/soc-modules-iot/src/main/resources/mapper/DiagnoseMissionChMapper.xml

@@ -44,6 +44,121 @@
 
     </select>
 
+    <select id="selectCamerasCount" resultType="java.lang.Integer">
+        select count(1) from
+        (
+        select s.iot_token,s.device_product,s.host_code dvs,s.device_code channel,
+        (select mission_id from iot_device_diagnose_mission_ch ch where ch.iot_token = s.iot_token
+        and ch.device_product=s.device_product and ch.dvs = s.host_code and ch.channel = s.device_code limit 1) mission_id
+        from iot_device_info s
+        inner join iot_device_info dvs on dvs.iot_token = s.iot_token
+        and dvs.device_product = s.device_product and dvs.device_code = s.host_code
+        and dvs.deleted = 0 and s.enable = 0 and dvs.device_type = 1
+        where s.deleted=0 and s.enable=0 and s.device_type = 2
+        <if test="request.orgId!= null">
+            and s.org_id=#{request.orgId}
+        </if>
+        <if test="request.orgPath!= null">
+            and s.org_path like CONCAT(#{request.orgPath}, '%')
+        </if>
+        <if test="request.deviceName != null and request.deviceName!= ''">
+            and s.device_name like CONCAT('%',#{request.deviceName},'%')
+        </if>
+        order by s.host_code ,cast(s.device_code as signed)
+        ) c where 1=1
+        <choose>
+            <when test=" request.state == null">
+            </when>
+            <when test=" request.state == 0">
+                and c.mission_id is null
+            </when>
+            <otherwise>
+                and c.mission_id is not null
+            </otherwise>
+        </choose>
+        <choose>
+            <when test=" request.missionCode != null and request.missionCode != '' ">
+                and c.mission_id = #{request.missionCode}
+            </when>
+            <otherwise>
+            </otherwise>
+        </choose>
+
+    </select>
+
+    <select id="selectCamerasBind" resultType="com.xunmei.iot.vo.sensor.DiagnoseMissionBindVo">
+        select c.* from
+        (
+        select s.iot_token,s.device_product,s.host_code dvs,s.device_code channel,
+        (select mission_id from iot_device_diagnose_mission_ch ch where ch.iot_token = s.iot_token
+        and ch.device_product=s.device_product and ch.dvs = s.host_code and ch.channel = s.device_code limit 1) mission_id,
+        (select group_concat(threshold_code) from iot_device_diagnose_threshold_ch ch where ch.iot_token = s.iot_token
+        and ch.device_product=s.device_product and ch.dvs = s.host_code and ch.channel = s.device_code) threshold_codes
+        from iot_device_info s
+        inner join iot_device_info dvs on dvs.iot_token = s.iot_token
+        and dvs.device_product = s.device_product and dvs.device_code = s.host_code
+        and dvs.deleted = 0 and s.enable = 0 and dvs.device_type = 1
+        where s.deleted=0 and s.enable=0 and s.device_type = 2
+        <if test="request.orgId!= null">
+            and s.org_id=#{request.orgId}
+        </if>
+        <if test="request.orgPath!= null">
+            and s.org_path like CONCAT(#{request.orgPath}, '%')
+        </if>
+        <if test="request.deviceName != null and request.deviceName!= ''">
+            and s.device_name like CONCAT('%',#{request.deviceName},'%')
+        </if>
+        order by s.host_code ,cast(s.device_code as signed)
+        ) c where 1=1
+        <choose>
+            <when test=" request.state == null">
+            </when>
+            <when test=" request.state == 0">
+                and c.mission_id is null
+            </when>
+            <otherwise>
+                and c.mission_id is not null
+            </otherwise>
+        </choose>
+        <choose>
+            <when test=" request.missionCode != null and request.missionCode != '' ">
+                and c.mission_id = #{request.missionCode}
+            </when>
+            <otherwise>
+            </otherwise>
+        </choose>
+
+    </select>
+
+
+    <insert id="batchInsertMissionCh" parameterType="java.util.List">
+        insert into iot_device_diagnose_mission_ch (id,mission_id,iot_token,device_product,dvs,channel)
+        values
+        <foreach collection="list" item="item" index="index" separator=",">
+            (#{item.id},#{item.missionId},#{item.iotToken},#{item.deviceProduct},#{item.dvs}, #{item.channel})
+        </foreach>
+    </insert>
+    <update id="batchUpdateMissionCh" parameterType="java.util.List">
+        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
+            update iot_device_diagnose_mission_ch
+            <set>
+                <if test="item.missionId != null">
+                    mission_id = #{item.missionId},
+                </if>
+            </set>
+            where iot_token = #{item.iotToken} and device_product = #{item.deviceProduct}
+            and dvs = #{item.dvs} and channel = #{item.channel}
+        </foreach>
+    </update>
+
+    <insert id="batchInsertThresholdCh" parameterType="java.util.List">
+        insert into iot_device_diagnose_threshold_ch (id,threshold_code, iot_token,device_product,dvs,channel)
+        values
+        <foreach collection="list" item="item" index="index" separator="," >
+            (#{item.id},#{item.thresholdCode},#{item.iotToken},#{item.deviceProduct},#{item.dvs}, #{item.channel})
+        </foreach>
+    </insert>
+
     <select id="selectAllDiagnoseMission" resultType="com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseMission">
         select * from iot_device_diagnose_mission
     </select>

+ 4 - 0
soc-modules/soc-modules-iot/src/main/resources/mapper/DiagnoseThresholdChMapper.xml

@@ -47,4 +47,8 @@
     <select id="selectAllDiagnoseThreshold" resultType="com.xunmei.common.core.domain.iot.domain.IotDeviceDiagnoseThreshold">
         select * from iot_device_diagnose_threshold
     </select>
+
+    <select id="selectBindCount" resultType="java.lang.Integer">
+        select count(1) from iot_device_diagnose_threshold_ch where threshold_code = #{thresholdCode}
+    </select>
 </mapper>