lihui001 před 3 roky
rodič
revize
1bd3d304e1
18 změnil soubory, kde provedl 256 přidání a 101 odebrání
  1. 1 1
      zoniot-water/pom.xml
  2. 8 5
      zoniot-water/zoniot-water-api/src/main/java/com/zcxk/water/api/dto/AlarmTypeDTO.java
  3. 51 0
      zoniot-water/zoniot-water-api/src/main/java/com/zcxk/water/api/dto/DeviceAddUpdateDto.java
  4. 17 0
      zoniot-water/zoniot-water-api/src/main/java/com/zcxk/water/api/enums/WaterErrorEnum.java
  5. 4 0
      zoniot-water/zoniot-water-api/src/main/java/com/zcxk/water/api/vo/DeviceVo.java
  6. 0 15
      zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/entity/AlarmRule.java
  7. 6 2
      zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/entity/AlarmType.java
  8. 4 0
      zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/entity/DeviceEntity.java
  9. 3 0
      zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/entity/DeviceTypeEntity.java
  10. 1 1
      zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/mapper/AlarmRuleMapper.xml
  11. 4 31
      zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/mapper/AlarmTypeMapper.xml
  12. 3 2
      zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/mapper/DeviceMapper.xml
  13. 2 1
      zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/mapper/DeviceTypeMapper.xml
  14. 2 2
      zoniot-water/zoniot-water-web/pom.xml
  15. 4 3
      zoniot-water/zoniot-water-web/src/main/java/com/zcxk/water/web/controller/DeviceController.java
  16. 4 3
      zoniot-water/zoniot-water-web/src/main/java/com/zcxk/water/web/service/DeviceService.java
  17. 70 16
      zoniot-water/zoniot-water-web/src/main/java/com/zcxk/water/web/service/impl/AlarmTypeServiceImpl.java
  18. 72 19
      zoniot-water/zoniot-water-web/src/main/java/com/zcxk/water/web/service/impl/DeviceServiceImpl.java

+ 1 - 1
zoniot-water/pom.xml

@@ -52,7 +52,7 @@
         </dependency>
        <!-- <dependency>
             <groupId>com.zcxk</groupId>
-            <artifactId>zoniot-iot-sync</artifactId>
+            <artifactId>zoniot-service-sync</artifactId>
         </dependency>-->
     </dependencies>
 </project>

+ 8 - 5
zoniot-water/zoniot-water-api/src/main/java/com/zcxk/water/api/dto/AlarmTypeDTO.java

@@ -22,15 +22,18 @@ public class AlarmTypeDTO {
     @ApiModelProperty(value="设备ID")
     private Long deviceId;
 
-    @ApiModelProperty(value="场景类型")
+    @ApiModelProperty(value="物联网产品ID")
+    private Integer iotProductId;
+
+    @ApiModelProperty(value="物联网设备ID")
+    private Long iotDeviceId;
+
+    @ApiModelProperty(value="场景类型SceneTypeEnum:1.水源 2.水厂 3.泵站 4.管网")
     private Integer sceneType;
 
     @ApiModelProperty(value="名称")
     private String name;
 
-    @ApiModelProperty(value="code")
-    private String code;
-
     @ApiModelProperty(value="描述")
     private String desc;
 
@@ -40,7 +43,7 @@ public class AlarmTypeDTO {
     @ApiModelProperty(value = "有效时间段,14:00:00-15:00:00,多个时间段以逗号拼接")
     private String times;
 
-    @ApiModelProperty(value="告警分类 1:业务报警 2:设备故障")
+    @ApiModelProperty(value="告警分类 1:设备告警,2:离线告警")
     private Integer alarmCategory;
 
     @ApiModelProperty(value="是否启用")

+ 51 - 0
zoniot-water/zoniot-water-api/src/main/java/com/zcxk/water/api/dto/DeviceAddUpdateDto.java

@@ -0,0 +1,51 @@
+package com.zcxk.water.api.dto;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+
+
+/**
+ * @description 设备信息dto
+ * @author  andy
+ * @data 2021
+ */
+@Data
+@ApiModel("设备信息")
+public class DeviceAddUpdateDto implements Serializable {
+
+    private static final long serialVersionUID = 6722509154948828769L;
+
+    @ApiModelProperty(value = "主键id")
+    private Long id;
+
+    @ApiModelProperty(value = "设备编号")
+    private String deviceCode;
+
+    @ApiModelProperty(value = "设备名称")
+    private String deviceName;
+
+    @ApiModelProperty(value = "设备类型")
+    private Integer deviceTypeId;
+
+    @ApiModelProperty(value = "所属公司")
+    private Integer companyOrgId;
+
+    @ApiModelProperty(value = "所属部门")
+    private Integer deptOrgId;
+
+    @ApiModelProperty(value = "经度")
+    private BigDecimal pointX;
+
+    @ApiModelProperty(value = "纬度")
+    private BigDecimal pointY;
+
+    @ApiModelProperty(value = "地址")
+    private String address;
+
+    @ApiModelProperty(value = "备注")
+    private String remark;
+}

+ 17 - 0
zoniot-water/zoniot-water-api/src/main/java/com/zcxk/water/api/enums/WaterErrorEnum.java

@@ -20,6 +20,11 @@ public enum WaterErrorEnum implements RespCode {
      */
     UPDATE_FAIL(202002, "更新失败。"),
 
+    /**
+     * 未找到相关数据
+     */
+    NOT_FOUND(202003, "未找到相关数据"),
+
     /**
      * 所选场景类型已经被引用,不能被删除
      */
@@ -78,6 +83,18 @@ public enum WaterErrorEnum implements RespCode {
 
     ALARM_NAME_ALREADY_EXISTS(500017, "该告警名称已存在,请修改"),
 
+
+    IOT_SYNC_ADD_ALARM_ERROR(203001, "同步添加物联网告警信息失败"),
+
+    IOT_SYNC_UPDATE_ALARM_ERROR(203002, "同步更新物联网告警信息失败"),
+
+    IOT_SYNC_DELETE_ALARM_ERROR(203003, "同步删除物联网告警信息失败"),
+
+    IOT_SYNC_ENABLED_ALARM_ERROR(203004, "同步删除物联网告警信息失败"),
+
+
+
+
     ;
 
     /**

+ 4 - 0
zoniot-water/zoniot-water-api/src/main/java/com/zcxk/water/api/vo/DeviceVo.java

@@ -24,6 +24,10 @@ public class DeviceVo  {
 
     @ApiModelProperty(value = "主键id")
     private Long id;
+
+    @ApiModelProperty(value = "物联网设备Id")
+    private Long iotDeviceId;
+
     @ApiModelProperty(value = "租户标识")
     private String tenantId;
 

+ 0 - 15
zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/entity/AlarmRule.java

@@ -51,19 +51,4 @@ public class AlarmRule {
     @ApiModelProperty(value="状态",hidden = true)
     private Boolean status;
 
-    @JsonIgnore
-    @ApiModelProperty(value="创建时间",hidden = true)
-    private LocalDateTime createDate;
-
-    @JsonIgnore
-    @ApiModelProperty(value="创建人",hidden = true)
-    private String createBy;
-
-    @JsonIgnore
-    @ApiModelProperty(value="更新时间",hidden = true)
-    private LocalDateTime updateDate;
-
-    @JsonIgnore
-    @ApiModelProperty(value="更新人",hidden = true)
-    private String updateBy;
 }

+ 6 - 2
zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/entity/AlarmType.java

@@ -6,6 +6,7 @@ import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
 import java.time.LocalDateTime;
+import java.util.Date;
 
 
 /**
@@ -31,6 +32,9 @@ public class AlarmType {
     @ApiModelProperty(value="站点id")
     private String tenantId;
 
+    @ApiModelProperty(value="客户Id")
+    private Long customerId;
+
     @ApiModelProperty(value="产品id")
     private Integer productId;
 
@@ -70,7 +74,7 @@ public class AlarmType {
 
     @JsonIgnore
     @ApiModelProperty(value="创建时间")
-    private LocalDateTime createDate;
+    private Date createDate;
 
     @JsonIgnore
     @ApiModelProperty(value="创建人")
@@ -78,7 +82,7 @@ public class AlarmType {
 
     @JsonIgnore
     @ApiModelProperty(value="更新时间")
-    private LocalDateTime updateDate;
+    private Date updateDate;
 
     @JsonIgnore
     @ApiModelProperty(value="更新人")

+ 4 - 0
zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/entity/DeviceEntity.java

@@ -25,6 +25,10 @@ public class DeviceEntity implements Serializable {
 
     @ApiModelProperty(value = "主键id")
     private Long id;
+
+    @ApiModelProperty(value = "物联网设备Id")
+    private Long iotDeviceId;
+
     @ApiModelProperty(value = "租户标识")
     private String tenantId;
 

+ 3 - 0
zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/entity/DeviceTypeEntity.java

@@ -25,6 +25,9 @@ public class DeviceTypeEntity implements Serializable {
     @ApiModelProperty(value = "主键")
     private Long id;
 
+    @ApiModelProperty(value = "物联网产品Id")
+    private Long iotProductId;
+
     /** 厂商名称 */
     @ApiModelProperty(value = "厂商名称")
     private String manufacturerName;

+ 1 - 1
zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/mapper/AlarmRuleMapper.xml

@@ -179,7 +179,7 @@
   </insert>
 
   <update id="delByAlarmTypeId">
-    update sms_alarm_rule set status = 0, update_date = NOW() where status = 1 and alarm_type_id = #{alarmTypeId}
+    update sms_alarm_rule set status = 0 where status = 1 and alarm_type_id = #{alarmTypeId}
   </update>
 
   <select id="findByAlarmTypeId" resultType="com.zcxk.water.api.vo.AlarmRuleVo">

+ 4 - 31
zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/mapper/AlarmTypeMapper.xml

@@ -2,31 +2,6 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.zcxk.water.core.dao.AlarmTypeMapper">
 
-    <resultMap id="BaseResultMap" type="com.zcxk.water.core.entity.AlarmType">
-      <!--@mbg.generated-->
-      <!--@Table sms_alarm_type-->
-      <id column="id" property="id" />
-      <result column="tenant_id" property="tenantId" />
-      <result column="product_id" property="productId" />
-      <result column="company_org_id" property="companyOrgId" />
-      <result column="dept_org_id" property="deptOrgId" />
-      <result column="device_id" property="deviceId" />
-      <result column="scene_type" property="sceneType" />
-      <result column="name" property="name" />
-      <result column="days" property="days" />
-      <result column="times" property="times" />
-      <result column="code" property="code" />
-      <result column="desc" property="desc" />
-      <result column="alarm_category" property="alarmCategory" />
-      <result column="enabled" property="enabled" />
-      <result column="alarm_rule_num" property="alarmRuleNum" />
-      <result column="status" property="status" />
-      <result column="create_date" property="createDate" />
-      <result column="create_by" property="createBy" />
-      <result column="update_date" property="updateDate" />
-      <result column="update_by" property="updateBy" />
-    </resultMap>
-
   <sql id="permissionCondition">
     <if test="userCondition.tenantId != null">and sat.tenant_id = #{userCondition.tenantId}</if>
     <if test="userCondition.userType!=null and userCondition.userType!=-999 and userCondition.userType!=-9999 and  userCondition.programItems != null and userCondition.programItems.size() > 0">
@@ -61,7 +36,7 @@
     <sql id="Base_Column_List">
       <!--@mbg.generated-->
       id, tenant_id, company_org_id, dept_org_id, product_id, `name`, code, `desc`, alarm_category, enabled,
-      alarm_rule_num, days, times,`status`, create_date, create_by, update_date, update_by
+      alarm_rule_num, days, times,`status`, create_date, create_by, update_date, update_by, iot_alarm_id
     </sql>
     <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zcxk.water.core.entity.AlarmType" useGeneratedKeys="true">
       <!--@mbg.generated-->
@@ -196,9 +171,6 @@
         <if test="productId != null">
           product_id = #{productId},
         </if>
-        <if test="alarmProperty != null">
-          alarm_property = #{alarmProperty},
-        </if>
         <if test="name != null">
           `name` = #{name},
         </if>
@@ -236,10 +208,11 @@
       where id = #{id}
     </update>
 
-    <select id="findById" resultMap="BaseResultMap">
+    <select id="findById" resultType="com.zcxk.water.core.entity.AlarmType">
       select
           <include refid="Base_Column_List" />
-      from sms_alarm_type where status = 1  and id = #{id}
+      from sms_alarm_type
+      where  id = #{id} and status = 1
     </select>
 
     <select id="getAlarmTypeList" resultType="com.zcxk.water.api.vo.AlarmTypeVo">

+ 3 - 2
zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/mapper/DeviceMapper.xml

@@ -25,6 +25,7 @@
             ,t.MANUFACTURER_ID as "manufacturerId"
             ,t.DEVICE_TYPE as "deviceType"
             ,t.DEVICE_MODE as "deviceMode"
+            ,d.iot_device_id
     </sql>
     <sql id="deviceJoins">
         left join sms_device_type t on d.DEVICE_TYPE_ID = t.id
@@ -354,12 +355,12 @@
         <if test="companyOrgId != null and companyOrgId != 0">COMPANY_ORG_ID , </if>
         <if test="deptOrgId != null and deptOrgId != 0">DEPT_ORG_ID ,</if>
         <if test="sceneId != null and sceneId != 0">SCENE_ID ,</if>
-        POINT_X ,  POINT_Y ,  ENABLE_STATE ,  LAST_UPDATE_TIME ,  ADDRESS ,  REMARK ,  STATUS ,  DATE_CREATE ,  CREATE_BY ,  DATE_UPDATE ,  UPDATE_BY )
+        POINT_X ,  POINT_Y ,  ENABLE_STATE ,  LAST_UPDATE_TIME ,  ADDRESS ,  REMARK ,  STATUS ,  DATE_CREATE ,  CREATE_BY ,  DATE_UPDATE ,  UPDATE_BY, IOT_DEVICE_ID)
         VALUES ( #{tenantId} ,#{deviceCode} ,#{deviceName}  ,#{deviceTypeId} ,
         <if test="companyOrgId != null and companyOrgId != 0">#{companyOrgId}  , </if>
         <if test="deptOrgId != null and deptOrgId != 0"> #{deptOrgId} , </if>
         <if test="sceneId != null and sceneId != 0">#{sceneId} , </if>
-        #{pointX} ,#{pointY} ,#{enableState} ,#{lastUpdateTime} ,#{address} ,#{remark} ,#{status} ,#{dateCreate} ,#{createBy} ,#{dateUpdate} ,#{updateBy})
+        #{pointX} ,#{pointY} ,#{enableState} ,#{lastUpdateTime} ,#{address} ,#{remark} ,#{status} ,#{dateCreate} ,#{createBy} ,#{dateUpdate} ,#{updateBy}, #{iotDeviceId})
    </insert>
 
     <!-- 批量新增 -->

+ 2 - 1
zoniot-water/zoniot-water-core/src/main/java/com/zcxk/water/core/mapper/DeviceTypeMapper.xml

@@ -16,7 +16,8 @@
          a.date_create as "dateCreate" ,
          a.create_by as "createBy" ,
          a.date_update as "dateUpdate" ,
-         a.update_by as "updateBy" 
+         a.update_by as "updateBy",
+         a.IOT_PRODUCT_ID
      </sql>
 
     <!--  根据主键获取实体   -->

+ 2 - 2
zoniot-water/zoniot-water-web/pom.xml

@@ -43,10 +43,10 @@
             <groupId>com.zcxk</groupId>
             <artifactId>zoniot-water-core</artifactId>
         </dependency>
-        <!--<dependency>
+        <dependency>
             <groupId>com.zcxk</groupId>
             <artifactId>zoniot-iot-sync</artifactId>
-        </dependency>-->
+        </dependency>
         <dependency>
             <groupId>com.zcxk</groupId>
             <artifactId>zoniot-core-common</artifactId>

+ 4 - 3
zoniot-water/zoniot-water-web/src/main/java/com/zcxk/water/web/controller/DeviceController.java

@@ -3,6 +3,7 @@ package com.zcxk.water.web.controller;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.OrderItem;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.zcxk.water.api.dto.DeviceAddUpdateDto;
 import com.zcxk.water.api.dto.query.DevicePageDto;
 import com.zcxk.water.api.dto.query.DeviceQueryDto;
 import com.zcxk.water.api.vo.DeviceVo;
@@ -107,14 +108,14 @@ public class DeviceController {
     @RequestMapping(value="add" , method = RequestMethod.POST)
     @ApiOperation(value = "新增设备信息")
     @LogAnnotation(module = "【设备管理】新增")
-    public AjaxMessage<Integer> add( @ApiParam(value = "设备信息") @RequestBody DeviceEntity deviceEntity){
-        return AjaxMessage.success(deviceService.add(deviceEntity));
+    public AjaxMessage<Integer> add( @ApiParam(value = "设备信息") @RequestBody DeviceAddUpdateDto dto){
+        return AjaxMessage.success(deviceService.add(dto));
     }
 
     @RequestMapping(value="edit" , method = RequestMethod.POST)
     @ApiOperation(value = "修改设备信息")
     @LogAnnotation(module = "【设备管理】修改")
-    public AjaxMessage<Integer> edit( @ApiParam(value = "设备信息") @RequestBody DeviceEntity deviceEntity){
+    public AjaxMessage<Integer> edit( @ApiParam(value = "设备信息") @RequestBody DeviceAddUpdateDto deviceEntity){
         return AjaxMessage.success(deviceService.update(deviceEntity));
     }
 

+ 4 - 3
zoniot-water/zoniot-water-web/src/main/java/com/zcxk/water/web/service/DeviceService.java

@@ -1,6 +1,7 @@
 package com.zcxk.water.web.service;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.zcxk.water.api.dto.DeviceAddUpdateDto;
 import com.zcxk.water.api.dto.DeviceDto;
 import com.zcxk.water.api.vo.DeviceVo;
 import com.zcxk.water.core.entity.DeviceEntity;
@@ -27,10 +28,10 @@ public interface DeviceService {
     * 添加设备
     * @author Andy
     * @date 9:13 2021/8/18
-    * @param deviceEntity:
+    * @param dto:
     * @return java.lang.Integer
     **/
-    Integer add(DeviceEntity deviceEntity);
+    Integer add(DeviceAddUpdateDto dto);
 
     /**
     * 单条删除设备
@@ -57,7 +58,7 @@ public interface DeviceService {
     * @param deviceEntity:
     * @return java.lang.Integer
     **/
-    Integer update(DeviceEntity deviceEntity);
+    Integer update(DeviceAddUpdateDto deviceEntity);
 
     /**
     * 分页查询

+ 70 - 16
zoniot-water/zoniot-water-web/src/main/java/com/zcxk/water/web/service/impl/AlarmTypeServiceImpl.java

@@ -3,10 +3,14 @@ package com.zcxk.water.web.service.impl;
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.zcxk.core.common.enums.StatusEnum;
 import com.zcxk.core.common.exception.BusinessException;
 import com.zcxk.core.common.util.BeanCopyUtils;
 import com.zcxk.core.oauth2.pojo.LoginUser;
 import com.zcxk.core.oauth2.util.UserUtil;
+import com.zcxk.iot.sync.dto.AlarmDataDTO;
+import com.zcxk.iot.sync.dto.AlarmOfflineDTO;
+import com.zcxk.iot.sync.service.SyncAlarmService;
 import com.zcxk.water.api.dto.AlarmRuleDTO;
 import com.zcxk.water.api.dto.AlarmTypeDTO;
 import com.zcxk.water.api.dto.AlarmTypePageDto;
@@ -17,9 +21,11 @@ import com.zcxk.water.core.dao.AlarmRuleMapper;
 import com.zcxk.water.core.dao.AlarmTypeMapper;
 import com.zcxk.water.core.entity.AlarmRule;
 import com.zcxk.water.core.entity.AlarmType;
+import com.zcxk.water.web.enums.AlarmCategoryEnum;
 import com.zcxk.water.web.service.AlarmTypeService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
@@ -27,6 +33,7 @@ import org.springframework.util.CollectionUtils;
 import javax.annotation.Resource;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 /**
@@ -43,8 +50,8 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
     @Resource
     private AlarmRuleMapper alarmRuleMapper;
 
-    //@Autowired
-    //private SyncAlarmService syncAlarmService;
+    @Autowired
+    private SyncAlarmService syncAlarmService;
 
     @Override
     public int insertSelective(AlarmType record) {
@@ -76,20 +83,29 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
     @Override
     @Transactional(rollbackFor = Exception.class)
     public void add(AlarmTypeDTO dto) {
-        Long iotAlarmId;
         log.info("begin addAlarmType alarmType:" + JSON.toJSONString(dto));
+        Long iotAlarmId = null;
         LoginUser loginUser = UserUtil.getCurrentUser();
         AlarmType alarmType = BeanCopyUtils.copy(dto, AlarmType.class);
         setUserInfo(loginUser, alarmType, dto.getRules());
         // 设备告警
-        /*if (AlarmCategoryEnum.DATA_ALARM.getType() == dto.getAlarmCategory()) {
-            iotAlarmId = syncAlarmService.addDataWaringConfig(null);
+        if (AlarmCategoryEnum.DATA_ALARM.getType() == dto.getAlarmCategory()) {
+            AlarmDataDTO dataDTO = BeanCopyUtils.copy(dto, AlarmDataDTO.class);
+            dataDTO.setCustomerId(loginUser.getCustomerId());
+            dataDTO.setDeviceId(dto.getIotDeviceId());
+            dataDTO.setProductId(dto.getIotProductId());
+            iotAlarmId = syncAlarmService.addDataWaringConfig(dataDTO);
         } else if (AlarmCategoryEnum.OFFLINE_ALARM.getType() == dto.getAlarmCategory()) {
-            iotAlarmId = syncAlarmService.addOfflineConfig(null);
-        } else {
-            throw BusinessException.builder(500, "未匹配");
+            AlarmOfflineDTO dataDTO = BeanCopyUtils.copy(dto, AlarmOfflineDTO.class);
+            dataDTO.setCustomerId(loginUser.getCustomerId());
+            dataDTO.setDeviceId(dto.getIotDeviceId());
+            dataDTO.setProductId(dto.getIotProductId());
+            iotAlarmId = syncAlarmService.addOfflineConfig(dataDTO);
+        }
+        if (iotAlarmId == null || iotAlarmId == 0) {
+            throw BusinessException.builder(WaterErrorEnum.IOT_SYNC_ADD_ALARM_ERROR);
         }
-        alarmType.setIotAlarmId(iotAlarmId);*/
+        alarmType.setIotAlarmId(iotAlarmId);
         int result = this.insertSelective(alarmType);
         // 批量加入告警规则
         batchInsertAlarmRule(alarmType, dto.getRules());
@@ -102,16 +118,39 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
     public void edit(AlarmTypeDTO dto) {
         log.info("begin editAlarmType alarmType:" + JSON.toJSONString(dto));
         LoginUser loginUser = UserUtil.getCurrentUser();
+        Long iotAlarmId = null;
+        AlarmType alarm = alarmTypeMapper.findById(dto.getId());
+        if (alarm == null) {
+            throw BusinessException.builder(WaterErrorEnum.NOT_FOUND);
+        }
         if (!nameUnique(dto.getId(),  loginUser.getTenantId(), dto.getName())){
             throw BusinessException.builder(WaterErrorEnum.ALARM_NAME_ALREADY_EXISTS);
         }
         AlarmType alarmType =  BeanCopyUtils.copy(dto, AlarmType.class);
         if (dto.getRules() != null){
             alarmType.setAlarmRuleNum(dto.getRules().size());
-        }else {
+        } else {
             alarmType.setAlarmRuleNum(0);
         }
-        alarmType.setUpdateDate(LocalDateTime.now());
+        if (AlarmCategoryEnum.DATA_ALARM.getType() == dto.getAlarmCategory()) {
+            AlarmDataDTO dataDTO = BeanCopyUtils.copy(dto, AlarmDataDTO.class);
+            dataDTO.setCustomerId(loginUser.getCustomerId());
+            dataDTO.setDeviceId(dto.getIotDeviceId());
+            dataDTO.setProductId(dto.getIotProductId());
+            dataDTO.setRuleId(alarm.getIotAlarmId());
+            iotAlarmId = syncAlarmService.updateDataWaringConfig(dataDTO);
+        } else if (AlarmCategoryEnum.OFFLINE_ALARM.getType() == dto.getAlarmCategory()) {
+            AlarmOfflineDTO dataDTO = BeanCopyUtils.copy(dto, AlarmOfflineDTO.class);
+            dataDTO.setCustomerId(loginUser.getCustomerId());
+            dataDTO.setDeviceId(dto.getIotDeviceId());
+            dataDTO.setProductId(dto.getIotProductId());
+            dataDTO.setRuleId(alarm.getIotAlarmId());
+            iotAlarmId = syncAlarmService.updateOfflineConfig(dataDTO);
+        }
+        if (iotAlarmId == null || iotAlarmId == 0) {
+            throw BusinessException.builder(WaterErrorEnum.IOT_SYNC_UPDATE_ALARM_ERROR);
+        }
+        alarmType.setUpdateDate(new Date());
         alarmType.setUpdateBy(loginUser.getUsername());
         int result = this.updateByPrimaryKeySelective(alarmType);
         // 告警规则
@@ -125,9 +164,20 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
     public void delete(Integer id) {
         log.info("begin deleteAlarmType id=" + id);
         AlarmType alarmType =  alarmTypeMapper.findById(id);
+        if (alarmType == null) {
+            throw BusinessException.builder(WaterErrorEnum.NOT_FOUND);
+        }
         alarmType.setId(id);
         alarmType.setStatus(false);
-
+        Long iotAlarmId = null;
+        if (AlarmCategoryEnum.DATA_ALARM.getType() == alarmType.getAlarmCategory()) {
+            iotAlarmId = syncAlarmService.deleteDataWaringConfig(alarmType.getIotAlarmId());
+        } else if (AlarmCategoryEnum.OFFLINE_ALARM.getType() == alarmType.getAlarmCategory()) {
+            iotAlarmId = syncAlarmService.deleteOfflineConfig(alarmType.getIotAlarmId());
+        }
+        if (iotAlarmId == null || iotAlarmId == 0) {
+            throw BusinessException.builder(WaterErrorEnum.IOT_SYNC_DELETE_ALARM_ERROR);
+        }
         int result = this.updateByPrimaryKeySelective(alarmType);
         AlarmType dto = new AlarmType();
         dto.setId(id);
@@ -141,8 +191,11 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
     public void updateEnabled(Integer id) {
         AlarmType alarmType = alarmTypeMapper.findById(id);
         if (alarmType == null) {
-            log.info("异常类型不存在");
-            return;
+            throw BusinessException.builder(WaterErrorEnum.NOT_FOUND);
+        }
+        Long data = syncAlarmService.enable(alarmType.getIotAlarmId());
+        if (data == null || data == 0) {
+            throw BusinessException.builder(WaterErrorEnum.IOT_SYNC_ENABLED_ALARM_ERROR);
         }
         alarmType.setId(id);
         alarmType.setEnabled(!alarmType.getEnabled());
@@ -209,6 +262,7 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
             }
             alarmRule.setExpression(expression);
             AlarmRule rule = BeanCopyUtils.copy(alarmRule, AlarmRule.class);
+            rule.setStatus(true);
             alarmRuleList.add(rule);
         }
         alarmRuleMapper.batchInsert(alarmRuleList);
@@ -236,9 +290,9 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
         alarmType.setDeptOrgId(loginUser.getDepartmentId());
         alarmType.setStatus(true);
         alarmType.setEnabled(true);
-        alarmType.setCreateDate(LocalDateTime.now());
+        alarmType.setCreateDate(new Date());
         alarmType.setCreateBy(loginUser.getUsername());
-        alarmType.setUpdateDate(LocalDateTime.now());
+        alarmType.setUpdateDate(new Date());
         alarmType.setUpdateBy(loginUser.getUsername());
     }
 

+ 72 - 19
zoniot-water/zoniot-water-web/src/main/java/com/zcxk/water/web/service/impl/DeviceServiceImpl.java

@@ -1,24 +1,27 @@
 package com.zcxk.water.web.service.impl;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.zcxk.core.common.exception.BusinessException;
+import com.zcxk.core.common.util.BeanCopyUtils;
+import com.zcxk.core.oauth2.pojo.LoginUser;
+import com.zcxk.core.oauth2.util.UserUtil;
+import com.zcxk.core.utils.util.RedisUtils;
+import com.zcxk.iot.sync.dto.DeviceDTO;
+import com.zcxk.iot.sync.service.SyncDeviceService;
+import com.zcxk.water.api.dto.DeviceAddUpdateDto;
+import com.zcxk.water.api.dto.DeviceDto;
 import com.zcxk.water.api.enums.DeviceStatusEnum;
 import com.zcxk.water.api.vo.DeviceVo;
 import com.zcxk.water.core.dao.DeviceMapper;
-import com.zcxk.water.api.dto.DeviceDto;
 import com.zcxk.water.core.entity.DeviceEntity;
+import com.zcxk.water.core.entity.DeviceTypeEntity;
 import com.zcxk.water.web.service.DeviceService;
-import com.zcxk.core.oauth2.pojo.LoginUser;
-import com.zcxk.core.oauth2.util.UserUtil;
-import com.zcxk.core.utils.util.RedisUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -35,14 +38,28 @@ public class DeviceServiceImpl implements DeviceService {
     @Autowired
     private RedisUtils redisUtils;
 
+    @Autowired
+    private SyncDeviceService syncDeviceService;
+
+    @Autowired
+    private DeviceTypeService deviceTypeService;
+
     @Override
     public DeviceVo selectById(Long id) {
         return deviceMapper.selectById(id);
     }
 
     @Override
-    public Integer add(DeviceEntity deviceEntity) {
+    public Integer add(DeviceAddUpdateDto dto) {
         LoginUser loginUser = UserUtil.getCurrentUser();
+        // 同步到物联网
+        DeviceDTO deviceDTO = this.covertDeviceDTO(dto, loginUser.getCustomerId());
+        Long iotDeviceId = syncDeviceService.addDevice(deviceDTO);
+        if (iotDeviceId== null || iotDeviceId == 0){
+            throw BusinessException.builder(500, "同步物联网失败");
+        }
+        DeviceEntity deviceEntity = BeanCopyUtils.copy(dto, DeviceEntity.class);
+        deviceEntity.setIotDeviceId(iotDeviceId);
         deviceEntity.setUpdateBy(loginUser.getUsername());
         deviceEntity.setDateUpdate(new Date());
         deviceEntity.setCreateBy(loginUser.getUsername());
@@ -56,27 +73,47 @@ public class DeviceServiceImpl implements DeviceService {
     @Override
     public Integer delete(Long id) {
         DeviceVo deviceDto = deviceMapper.selectById(id);
-        if(deviceDto != null && StringUtils.isNotBlank(deviceDto.getDeviceCode())){
-            redisUtils.del(("sms_water_"+deviceDto.getDeviceCode()).getBytes());
+        // 同步到物联网
+        Long iotDeviceId = syncDeviceService.deleteDevice(deviceDto.getIotDeviceId());
+        if (iotDeviceId== null || iotDeviceId == 0) {
+            throw BusinessException.builder(500, "同步物联网删除失败");
+        }
+        if (deviceDto != null && StringUtils.isNotBlank(deviceDto.getDeviceCode())){
+            redisUtils.del(("sms_water_" + deviceDto.getDeviceCode()).getBytes());
         }
         return deviceMapper.deleteById(id);
     }
 
     @Override
     public Integer batchDelete(List<Long> ids) {
-        List<DeviceVo> deviceDtos = deviceMapper.selectByIds(ids);
-        for(DeviceVo deviceDto : deviceDtos){
-            if(deviceDto != null && StringUtils.isNotBlank(deviceDto.getDeviceCode())){
-                redisUtils.del(("sms_water_"+deviceDto.getDeviceCode()).getBytes());
+        List<String> listCode = new ArrayList<>();
+        List<DeviceVo> deviceVos = deviceMapper.selectByIds(ids);
+        for (DeviceVo deviceDto : deviceVos) {
+            // 物联网删除
+            Long iotDeviceId = syncDeviceService.deleteDevice(deviceDto.getIotDeviceId());
+            if (iotDeviceId == null || iotDeviceId == 0 ){
+                throw BusinessException.builder(500, "同步物联网删除失败,删除成功的设备编号:" + listCode.toString());
             }
+            if (deviceDto != null && StringUtils.isNotBlank(deviceDto.getDeviceCode())){
+                redisUtils.del(("sms_water_" + deviceDto.getDeviceCode()).getBytes());
+            }
+            listCode.add(deviceDto.getDeviceCode());
         }
         return deviceMapper.batchDelete(ids);
     }
     @Override
-    public Integer update(DeviceEntity deviceEntity) {
+    public Integer update(DeviceAddUpdateDto dto) {
+        DeviceEntity deviceEntity = BeanCopyUtils.copy(dto, DeviceEntity.class);
         DeviceVo deviceDto = deviceMapper.selectById(deviceEntity.getId());
-        if(deviceDto != null && StringUtils.isNotBlank(deviceDto.getDeviceCode())){
-            redisUtils.del(("sms_water_"+deviceDto.getDeviceCode()).getBytes());
+        // 同步到物联网
+        DeviceDTO deviceDTO = this.covertDeviceDTO(dto, null);
+        deviceDTO.setId(deviceDto.getIotDeviceId());
+        Long iotDeviceId = syncDeviceService.updateDevice(deviceDTO);
+        if (iotDeviceId== null || iotDeviceId == 0) {
+            throw BusinessException.builder(500, "同步更新物联网失败");
+        }
+        if (deviceDto != null && StringUtils.isNotBlank(deviceDto.getDeviceCode())){
+            redisUtils.del(("sms_water_" + deviceDto.getDeviceCode()).getBytes());
         }
         LoginUser loginUser = UserUtil.getCurrentUser();
         deviceEntity.setUpdateBy(loginUser.getUsername());
@@ -155,7 +192,6 @@ public class DeviceServiceImpl implements DeviceService {
         return deviceMapper.selectListByScene(deviceDto);
     }
 
-
     @Override
     public List<DeviceVo> selectListBySceneId(DeviceDto deviceDto) {
         LoginUser loginUser = UserUtil.getCurrentUser();
@@ -166,4 +202,21 @@ public class DeviceServiceImpl implements DeviceService {
         deviceDto.setPermissonType(loginUser.getPermissonType());
         return deviceMapper.selectListBySceneId(deviceDto);
     }
+
+    private DeviceDTO covertDeviceDTO(DeviceAddUpdateDto deviceEntity, Long customerId){
+        DeviceDTO deviceDTO = new DeviceDTO();
+        DeviceTypeEntity deviceTypeEntity = deviceTypeService.findDeviceTypeById((long)deviceEntity.getDeviceTypeId());
+        if (deviceTypeEntity == null || deviceTypeEntity.getIotProductId() == null || deviceTypeEntity.getIotProductId() == 0L){
+            throw BusinessException.builder(500, "找不到物联网产品Id");
+        }
+        deviceDTO.setProductId(deviceTypeEntity.getIotProductId());
+        deviceDTO.setCustomerId(customerId);
+        deviceDTO.setDeviceName(deviceEntity.getDeviceName());
+        deviceDTO.setLongitude(deviceEntity.getPointX());
+        deviceDTO.setLatitude(deviceEntity.getPointY());
+        deviceDTO.setLocationDesc(deviceEntity.getAddress());
+        deviceDTO.setDeviceNo(deviceEntity.getDeviceCode());
+        return deviceDTO;
+    }
+
 }