wwh 2 лет назад
Родитель
Сommit
9e87ab4f2c
1 измененных файлов с 64 добавлено и 55 удалено
  1. 64 55
      src/main/java/com/huimv/admin/service/impl/EnvDeviceServiceImpl.java

+ 64 - 55
src/main/java/com/huimv/admin/service/impl/EnvDeviceServiceImpl.java

@@ -143,36 +143,44 @@ public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice
         BasePigpen basePigpen = basePigpenMapper.selectOne(basePigpenQueryWrapper);
         envDevice.setStageType(basePigpen.getStageCode());
 
-        QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
-        queryWrapper.eq("device_name", envDevice1.getDeviceName()).or().eq("farm_id", envDevice1.getFarmId())
-                .or().eq("device_code", envDevice1.getDeviceCode()).or().eq("unit_id",envDevice1.getUnitId());
-        EnvDevice device = envDeviceMapper.selectOne(queryWrapper);
-        if (ObjectUtil.isEmpty(device)) {
-            QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
-            queryWrapper1.eq("farm_id", envDevice1.getFarmId());
-            Integer integer = envDeviceMapper.selectCount(queryWrapper1);
-            if (integer == 0) {
-                envDevice.setDeviceSortCode("100" + 01);
-                addAll(envDevice);
+        QueryWrapper<EnvDevice> deviceQueryWrapper = new QueryWrapper<>();
+        deviceQueryWrapper.eq("farm_id", envDevice1.getFarmId()).eq("unit_id", envDevice1.getUnitId());
+        EnvDevice device1 = envDeviceMapper.selectOne(deviceQueryWrapper);//一个栋舍只能有一个设备
+        if (ObjectUtil.isEmpty(device1)) {
+            QueryWrapper<EnvDevice> queryWrapper = new QueryWrapper<>();
+            queryWrapper.eq("device_name", envDevice1.getDeviceName()).eq("farm_id", envDevice1.getFarmId())
+                    .or().eq("device_code", envDevice1.getDeviceCode());
+            EnvDevice device = envDeviceMapper.selectOne(queryWrapper);//名字和编码不能重复
+            if (ObjectUtil.isEmpty(device)) {
+                QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
+                queryWrapper1.eq("farm_id", envDevice1.getFarmId());
+                Integer integer = envDeviceMapper.selectCount(queryWrapper1);
+                if (integer == 0) {
+                    envDevice.setDeviceSortCode("100" + 01);
+                    addAll(envDevice);
+                } else {
+                    queryWrapper1.orderByDesc("id").last(" limit 1");
+                    EnvDevice envDevice2 = envDeviceMapper.selectOne(queryWrapper1);
+                    envDevice.setDeviceSortCode(envDevice2.getDeviceSortCode() + 1);
+                    addAll(envDevice);
+                }
+                envDeviceMapper.insert(envDevice);
+                return new Result(ResultCode.SUCCESS, "添加成功");
             } else {
-                queryWrapper1.orderByDesc("id").last(" limit 1");
-                EnvDevice envDevice2 = envDeviceMapper.selectOne(queryWrapper1);
-                envDevice.setDeviceSortCode(envDevice2.getDeviceSortCode() + 1);
-                addAll(envDevice);
+                return new Result(ResultCode.FAIL, "添加失败,设备名称或设备编码已存在");
             }
-            envDeviceMapper.insert(envDevice);
-
-            return new Result(ResultCode.SUCCESS, "添加成功");
         } else {
-            return new Result(ResultCode.FAIL, "添加失败,设备名称或设备编码已存在");
+            return new Result(ResultCode.FAIL, "添加失败,该栋舍已绑定设备");
         }
 
+
     }
+
     public void addAll(EnvDevice envDevice1) {
         for (int i = 1; i < 24; i++) {
             EnvDeviceRecentlyData data = new EnvDeviceRecentlyData();
             data.setDeviceCode(envDevice1.getDeviceSortCode());
-            data.setChipCode(envDevice1.getDeviceSortCode()+ "14" + i);
+            data.setChipCode(envDevice1.getDeviceSortCode() + "14" + i);
             data.setChipStatus(0);
             data.setChipType(i);
             if (i < 9) {
@@ -220,7 +228,7 @@ public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice
             if (i == 22) {
                 data.setChipName("室内光照");
             }
-            if (i==23) {
+            if (i == 23) {
                 data.setChipName("体感温度");
             }
             data.setFarmId(envDevice1.getFarmId());
@@ -244,7 +252,7 @@ public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice
 
         for (int i = 12; i < 15; i++) {
             EnvHumi humi = new EnvHumi();
-            humi.setChipCode(envDevice1.getDeviceSortCode() + "14"+i);//湿度
+            humi.setChipCode(envDevice1.getDeviceSortCode() + "14" + i);//湿度
             humi.setDeviceCode(envDevice1.getDeviceSortCode());
             humi.setUnitId(envDevice1.getUnitId().toString());
             humi.setFarmId(envDevice1.getFarmId());
@@ -280,7 +288,7 @@ public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice
         pressureMapper.insert(pressure);
 
 
-        for (int i = 1; i < envDevice1.getFengJiNum()+1; i++) {
+        for (int i = 1; i < envDevice1.getFengJiNum() + 1; i++) {
             EnvDeviceFan fan = new EnvDeviceFan();
             fan.setDeviceCode(envDevice1.getDeviceSortCode());
             fan.setChipCode(envDevice1.getDeviceSortCode() + "11" + i);
@@ -290,7 +298,7 @@ public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice
             fan.setOther1(String.valueOf(i));
             fanMapper.insert(fan);
         }
-        for (int i = 1; i < envDevice1.getXiaoChuangNum()+1; i++) {
+        for (int i = 1; i < envDevice1.getXiaoChuangNum() + 1; i++) {
             EnvDeviceFenestrule fenestrulen = new EnvDeviceFenestrule();
             fenestrulen.setDeviceCode(envDevice1.getDeviceSortCode());
             fenestrulen.setChipCode(envDevice1.getDeviceSortCode() + "12" + i);
@@ -305,19 +313,19 @@ public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice
             other.setDeviceCode(envDevice1.getDeviceSortCode());
             other.setChipCode(envDevice1.getDeviceSortCode() + "13" + i);
             other.setFarmId(envDevice1.getFarmId());
-            if (i==1) {
+            if (i == 1) {
                 other.setChipName("照明");
             }
-            if (i==2) {
+            if (i == 2) {
                 other.setChipName("加热");
             }
-            if (i==3) {
+            if (i == 3) {
                 other.setChipName("湿帘");
             }
-            if (i==4) {
+            if (i == 4) {
                 other.setChipName("喷雾");
             }
-            if (i==5) {
+            if (i == 5) {
                 other.setChipName("锅炉");
             }
 
@@ -332,9 +340,10 @@ public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice
     public static void main(String[] args) {
         String farm = "1000" + 01;
         System.out.println(farm);
-       String farm2=String.valueOf((Integer.valueOf(farm) + 9));
+        String farm2 = String.valueOf((Integer.valueOf(farm) + 9));
         System.out.println(farm2);
     }
+
     @Override
     public Result edit(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
         String farmId = paramsMap.get("farmId");
@@ -360,10 +369,10 @@ public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice
         envDevice.setRemark(remark);
 
         QueryWrapper<EnvDevice> queryWrapper1 = new QueryWrapper<>();
-        queryWrapper1.ne("id",id).and(i ->i.eq("device_name", DeviceName).or().eq("device_code", DeviceCode));
+        queryWrapper1.ne("id", id).and(i -> i.eq("device_name", DeviceName).or().eq("device_code", DeviceCode));
         if (ObjectUtil.isEmpty(envDeviceMapper.selectOne(queryWrapper1))) {
             envDeviceMapper.updateById(envDevice);
-            return new Result(ResultCode.SUCCESS,"修改成功");
+            return new Result(ResultCode.SUCCESS, "修改成功");
         } else {
             return new Result(ResultCode.FAIL, "设备名称或者设备编码已存在");
         }
@@ -570,8 +579,8 @@ public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice
             queryWrapper.eq("farm_id", farmId);
             queryWrapper.like("device_code", name).or().like("device_name", name);
         }
-        Page<EnvDeviceVo> page = new Page(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
-        IPage<EnvDeviceVo> list = envDeviceMapper.list(page,queryWrapper);
+        Page<EnvDeviceVo> page = new Page(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
+        IPage<EnvDeviceVo> list = envDeviceMapper.list(page, queryWrapper);
         return new Result(ResultCode.SUCCESS, list);
     }
 
@@ -655,33 +664,33 @@ public class EnvDeviceServiceImpl extends ServiceImpl<EnvDeviceMapper, EnvDevice
         String deviceCode = paramsMap.get("deviceCode");
         String farmId = paramsMap.get("farmId");
         Map map = new HashMap();
-        Integer sensorNum = envDeviceRecentlyDataMapper.selectCount(new QueryWrapper<EnvDeviceRecentlyData>().eq("device_code", deviceCode).eq("farm_Id",farmId));
-        Integer fanNum = fanMapper.selectCount(new QueryWrapper<EnvDeviceFan>().eq("device_code", deviceCode).eq("farm_Id",farmId));
-        Integer fenestruleNum = fenestruleMapper.selectCount(new QueryWrapper<EnvDeviceFenestrule>().eq("device_code", deviceCode).eq("farm_Id",farmId));
-        Integer lightNum = otherMapper.selectCount(new QueryWrapper<EnvDeviceOther>().eq("device_code", deviceCode).eq("other1", 1).eq("farm_Id",farmId));
-        Integer heatNum = otherMapper.selectCount(new QueryWrapper<EnvDeviceOther>().eq("device_code", deviceCode).eq("other1", 2).eq("farm_Id",farmId));
-        if (ObjectUtil.isNull(sensorNum)){
-            sensorNum =0;
+        Integer sensorNum = envDeviceRecentlyDataMapper.selectCount(new QueryWrapper<EnvDeviceRecentlyData>().eq("device_code", deviceCode).eq("farm_Id", farmId));
+        Integer fanNum = fanMapper.selectCount(new QueryWrapper<EnvDeviceFan>().eq("device_code", deviceCode).eq("farm_Id", farmId));
+        Integer fenestruleNum = fenestruleMapper.selectCount(new QueryWrapper<EnvDeviceFenestrule>().eq("device_code", deviceCode).eq("farm_Id", farmId));
+        Integer lightNum = otherMapper.selectCount(new QueryWrapper<EnvDeviceOther>().eq("device_code", deviceCode).eq("other1", 1).eq("farm_Id", farmId));
+        Integer heatNum = otherMapper.selectCount(new QueryWrapper<EnvDeviceOther>().eq("device_code", deviceCode).eq("other1", 2).eq("farm_Id", farmId));
+        if (ObjectUtil.isNull(sensorNum)) {
+            sensorNum = 0;
         }
-        if (ObjectUtil.isNull(fanNum)){
-            fanNum =0;
+        if (ObjectUtil.isNull(fanNum)) {
+            fanNum = 0;
         }
-        if (ObjectUtil.isNull(fenestruleNum)){
-            fenestruleNum =0;
+        if (ObjectUtil.isNull(fenestruleNum)) {
+            fenestruleNum = 0;
         }
-        if (ObjectUtil.isNull(lightNum)){
-            lightNum =0;
+        if (ObjectUtil.isNull(lightNum)) {
+            lightNum = 0;
         }
-        if (ObjectUtil.isNull(heatNum)){
-            heatNum =0;
+        if (ObjectUtil.isNull(heatNum)) {
+            heatNum = 0;
         }
-        map.put("sensorNum",sensorNum);
-        map.put("fanNum",fanNum);
-        map.put("fenestruleNum",fenestruleNum);
-        map.put("lightNum",lightNum);
-        map.put("heatNum",heatNum);
+        map.put("sensorNum", sensorNum);
+        map.put("fanNum", fanNum);
+        map.put("fenestruleNum", fenestruleNum);
+        map.put("lightNum", lightNum);
+        map.put("heatNum", heatNum);
 
-        return new Result(ResultCode.SUCCESS,map);
+        return new Result(ResultCode.SUCCESS, map);
     }
 
 //    //添加新的设备