Jelajahi Sumber

add 设备删除

A17404李放 3 tahun lalu
induk
melakukan
fd3a8f26fe

+ 3 - 0
nb-admin/src/main/resources/application.yml

@@ -72,6 +72,9 @@ mybatis-plus:
     enableSqlRunner: false
     dbConfig:
       idType: ASSIGN_ID
+      logic-delete-field: isDelete
+      logic-not-delete-value: 0
+      logic-delete-value: 1
   configuration:
     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
     default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler

+ 8 - 2
nb-admin/src/test/java/com/nb/admin/AliyunTest.java

@@ -73,8 +73,14 @@ public class AliyunTest {
 
     @Test
     public void test003(){
-        List<QueryDeviceResponse.DeviceInfo> deviceInfos = aliyunIotSdk.queryDevice();
-        System.out.println(deviceInfos.size());
+        BusDeviceEntity deviceEntity = new BusDeviceEntity();
+        deviceEntity.setDeviceId("572157323738029F");
+        deviceService.saveDevice(deviceEntity);
+    }
+
+    @Test
+    public void test004(){
+        deviceService.syncDeviceFromIot();
     }
 
 }

+ 12 - 12
nb-system/src/main/java/com/nb/bus/controller/BusDeviceController.java

@@ -161,18 +161,18 @@ public class BusDeviceController implements
 //        }
 //    }
 
-//    @PostMapping("/remove")
-//    @ApiImplicitParams({
-//            @ApiImplicitParam(name = "id",value = "主键id",required = true)
-//    })
-//    @SaCheckPermission("device:info:delete")
-//    @ApiOperation(value = "根据ID删除")
-//    public R delete(@RequestParam("id") String id) {
-//        if(CharSequenceUtil.isNullOrUndefined(String.valueOf(id))){
-//            return R.success();
-//        }
-//        return R.success(deviceService.removeIotById(id));
-//    }
+    @PostMapping("/remove")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "id",value = "主键id",required = true)
+    })
+    @SaCheckPermission("device:info:delete")
+    @ApiOperation(value = "根据ID删除")
+    public R delete(@RequestParam("id") String id) {
+        if(CharSequenceUtil.isNullOrUndefined(String.valueOf(id))){
+            return R.success();
+        }
+        return R.success(deviceService.removeIotById(id));
+    }
 
     /**
      * @author 龙三郎

+ 1 - 1
nb-system/src/main/java/com/nb/bus/entity/BusDeviceEntity.java

@@ -73,7 +73,7 @@ public class BusDeviceEntity extends TenantGenericEntity<String,String> {
 
 
     @TableField(fill = FieldFill.INSERT)
-    @TableLogic(value = "0",delval = "1")
+    @TableLogic
     private Integer isDelete;
 
     @ApiModelProperty(value = "设备在线状态,0未激活,1在线,2离线",accessMode = ApiModelProperty.AccessMode.READ_ONLY)

+ 8 - 2
nb-system/src/main/java/com/nb/bus/mapper/BusDeviceMapper.java

@@ -9,6 +9,10 @@ import com.nb.bus.service.dto.DeviceResult;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
 /**
  * @author lifang
  * @version 1.0.0
@@ -28,12 +32,14 @@ public interface BusDeviceMapper extends BaseMapper<BusDeviceEntity> {
 
     /**
      * 去掉逻辑删除标志
-     * @param deviceId
+     * @param ids
      * @return
      */
-    Integer notDelete(String deviceId);
+    Integer notDelete(@Param("ids") Collection<String> ids);
 
     IPage<DeviceResult> pageQuery(Page<DeviceResult> page,@Param("query") DeviceQuery query);
 
     DeviceResult view(@Param("id") String id);
+
+    List<BusDeviceEntity> ignoreLogicAll(@Param("ids") Set<String> pollDeviceIds);
 }

+ 14 - 10
nb-system/src/main/java/com/nb/bus/service/LocalBusDeviceService.java

@@ -21,6 +21,7 @@ import com.nb.bus.service.dto.DeviceQuery;
 import com.nb.bus.service.dto.DeviceResult;
 import com.nb.common.crud.BaseService;
 import com.nb.common.exception.CustomException;
+import com.nb.common.util.ExceptionUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
@@ -48,9 +49,6 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
     @Lazy
     private DeviceRegistry deviceRegistry;
 
-    @Autowired
-    private BusDeviceMapper deviceMapper;
-
     @Autowired
     private AliyunIotSdk aliyunIotSdk;
 
@@ -142,14 +140,15 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
      * 保存一个设备,系统中存在时就更新,不存在时则插入。
      * @param entity
      */
+    @Transactional(rollbackFor = Exception.class)
     public boolean saveDevice(BusDeviceEntity entity) {
         // 查询设备是否存在,无视逻辑删除
-        BusDeviceEntity device = deviceMapper.selectOneByDeviceId(entity.getDeviceId());
+        BusDeviceEntity device = this.baseMapper.selectOneByDeviceId(entity.getDeviceId());
         // 判断设备是否存在
         boolean isExists = Objects.nonNull(device);
         // 设备存在,且处于删除状态,首先去掉逻辑删除标志 ?? 逻辑删除后设备数据是否不再接收
         if (isExists && device.getIsDelete() == 1){
-            deviceMapper.notDelete(entity.getDeviceId());
+            this.baseMapper.notDelete(Collections.singleton(entity.getDeviceId()));
         }
         // 设备存在
         if (isExists){
@@ -160,7 +159,7 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
             try {
                 return this.save(entity);
             }catch (Exception e){
-                e.printStackTrace();
+                log.error("新增设备失败,【{}】",ExceptionUtil.getExceptionMsg(e));
             }
             return false;
 
@@ -243,7 +242,7 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
      * @param
      * @return int
      */
-    public void syncDeviceFromIot() throws ExecutionException, InterruptedException {
+    public void syncDeviceFromIot(){
         log.info("从阿里云同步所有设备信息");
         HashMap<String, BusDeviceEntity> deviceById = new HashMap<>();
         ArrayList<BusDeviceEntity> devices = new ArrayList<>();
@@ -262,7 +261,12 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
                 Set<String> pollDeviceIds = devices.stream().filter(info -> StrUtil.isNotBlank(info.getDeviceId()))
                         .map(BusDeviceEntity::getDeviceId)
                         .collect(Collectors.toSet());
-                List<BusDeviceEntity> existDevices = this.list(new QueryWrapper<BusDeviceEntity>().lambda().in(BusDeviceEntity::getDeviceId, pollDeviceIds));
+                List<BusDeviceEntity> existDevices=this.baseMapper.ignoreLogicAll(pollDeviceIds);
+                //将所有删除设备恢复
+                List<String> delDeviceIds = existDevices.stream().filter(device -> device.getIsDelete() == 1).map(BusDeviceEntity::getDeviceId).collect(Collectors.toList());
+                if (CollUtil.isNotEmpty(delDeviceIds)) {
+                    this.baseMapper.notDelete(delDeviceIds);
+                }
                 if (CollUtil.size(pollDeviceIds) != CollUtil.size(existDevices)) {
                     //有新的设备产生
                     Set<String> existDeviceIds = existDevices.stream()
@@ -280,9 +284,9 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
             })
                     .whenComplete((i,e)->{
                         if(e!=null){
-                            log.error("同步设备失败",e);
+                            log.error("同步设备失败:【{}】", ExceptionUtil.getExceptionMsg(e));
                         }
-                    });
+                    }).join();
         }
     }
 

+ 22 - 1
nb-system/src/main/resources/mapper/bus/BusDeviceMapper.xml

@@ -29,7 +29,15 @@
     <update id="notDelete">
         UPDATE bus_device
         SET is_delete = 0
-        WHERE device_id = #{deviceId}
+        <where>
+            <if test="ids != null and ids.size > 0">
+                and device_id in
+                <foreach item="deviceId" index="index" collection="ids" open="(" separator="," close=")">
+                    #{deviceId, jdbcType=VARCHAR}
+                </foreach>
+            </if>
+        </where>
+
     </update>
 
 
@@ -51,6 +59,7 @@
         d.tenant_id as tenant_id
         from (select * from bus_device
         <where>
+            is_delete=0
             <if test="query.deviceId!=null">
                 and device_id like concat('%',#{query.deviceId},'%')
             </if>
@@ -89,4 +98,16 @@
         on i.id=d.infusion_id
         order by d.create_time desc
     </select>
+
+    <select id="ignoreLogicAll" resultType="com.nb.bus.entity.BusDeviceEntity">
+        select  * from bus_device
+        <where>
+            <if test="ids != null and ids.size > 0">
+                and device_id in
+                <foreach item="deviceId" index="index" collection="ids" open="(" separator="," close=")">
+                    #{deviceId, jdbcType=VARCHAR}
+                </foreach>
+            </if>
+        </where>
+    </select>
 </mapper>