Переглянути джерело

批量绑定人员和设备接口

yanliming 5 роки тому
батько
коміт
740280b68b

+ 52 - 0
web/src/main/java/com/jpsoft/smart/modules/base/controller/PersonDeviceRelationController.java

@@ -1,6 +1,7 @@
 package com.jpsoft.smart.modules.base.controller;
 
 import com.github.pagehelper.Page;
+import com.github.pagehelper.util.StringUtil;
 import com.jpsoft.smart.modules.base.entity.DeviceInfo;
 import com.jpsoft.smart.modules.base.service.DeviceInfoService;
 import com.jpsoft.smart.modules.common.utils.PojoUtils;
@@ -75,6 +76,57 @@ public class PersonDeviceRelationController {
         return msgResult;
     }
 
+
+    @ApiOperation(value="人员与设备批量绑定")
+    @PostMapping("batchBindDevice")
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "personId",value = "人员ID", required = true,paramType = "query"),
+            @ApiImplicitParam(name = "devices",value = "设备ID(多个设备)", required = true, paramType = "query")
+    })
+    public MessageResult<Integer> batchBindDevice(Long personId,String devices,@RequestAttribute String subject){
+        MessageResult<Integer> msgResult = new MessageResult<>();
+        int affectCount = 0;
+
+        try {
+            if(StringUtil.isNotEmpty(devices)){
+               String[] deviceArray = devices.split(",");
+
+               for (String deviceId:deviceArray) {
+
+                   PersonDeviceRelation item = personDeviceRelationService.findByDeviceIdAndPersonId(deviceId,personId);
+
+                   if(item == null) {
+                       item = new PersonDeviceRelation();
+                       item.setId(UUID.randomUUID().toString());
+                       item.setDeviceId(deviceId);
+                       item.setPersonId(personId);
+                       item.setDelFlag(false);
+                       item.setCreateBy(subject);
+                       item.setCreateTime(new Date());
+
+                       affectCount += personDeviceRelationService.insert(item);
+                   }
+               }
+            }
+
+            if (affectCount > 0) {
+                msgResult.setResult(true);
+                msgResult.setData(affectCount);
+            } else {
+                msgResult.setResult(false);
+                msgResult.setMessage("数据库添加失败");
+            }
+        }
+        catch(Exception ex){
+            logger.error(ex.getMessage(),ex);
+
+            msgResult.setResult(false);
+            msgResult.setMessage(ex.getMessage());
+        }
+
+        return msgResult;
+    }
+
     @ApiOperation(value="获取人员和设备绑定信息")
     @GetMapping("edit/{id}")
     public MessageResult<PersonDeviceRelation> edit(@PathVariable("id") String id){