From f498c77c01ea479fa67663286fbbb3cf8b66e689 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期二, 19 七月 2022 18:34:20 +0800
Subject: [PATCH] 优化设备 调整为cmd 模式

---
 service-common/src/main/java/com/java110/common/cmd/machine/ListMachineCmd.java |  256 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 243 insertions(+), 13 deletions(-)

diff --git a/service-common/src/main/java/com/java110/common/cmd/machine/ListMachineCmd.java b/service-common/src/main/java/com/java110/common/cmd/machine/ListMachineCmd.java
index a707d78..274acf4 100644
--- a/service-common/src/main/java/com/java110/common/cmd/machine/ListMachineCmd.java
+++ b/service-common/src/main/java/com/java110/common/cmd/machine/ListMachineCmd.java
@@ -20,12 +20,26 @@
 import com.java110.core.context.ICmdDataFlowContext;
 import com.java110.core.event.cmd.Cmd;
 import com.java110.core.event.cmd.CmdEvent;
+import com.java110.dto.RoomDto;
+import com.java110.dto.community.CommunityDto;
+import com.java110.dto.community.CommunityLocationDto;
+import com.java110.dto.unit.FloorAndUnitDto;
+import com.java110.intf.common.IMachineInnerServiceSMO;
 import com.java110.intf.common.IMachineV1InnerServiceSMO;
+import com.java110.intf.community.*;
 import com.java110.utils.exception.CmdException;
+import com.java110.utils.util.Assert;
 import com.java110.utils.util.BeanConvertUtil;
+import com.java110.utils.util.DateUtil;
+import com.java110.utils.util.StringUtil;
 import com.java110.vo.ResultVo;
+import com.java110.vo.api.machine.ApiMachineDataVo;
+import com.java110.vo.api.machine.ApiMachineVo;
 import org.springframework.beans.factory.annotation.Autowired;
 import com.java110.dto.machine.MachineDto;
+
+import java.text.ParseException;
+import java.util.Calendar;
 import java.util.List;
 import java.util.ArrayList;
 import org.springframework.http.ResponseEntity;
@@ -44,37 +58,253 @@
  * 娓╅Θ鎻愮ず锛氬鏋滄偍瀵规鏂囦欢杩涜淇敼 璇蜂笉瑕佸垹闄ゅ師鏈変綔鑰呭強娉ㄩ噴淇℃伅锛岃琛ュ厖鎮ㄧ殑 淇敼鐨勫師鍥犱互鍙婅仈绯婚偖绠卞涓�
  * // modify by 寮犱笁 at 2021-09-12 绗�10琛屽湪鏌愮鍦烘櫙涓嬪瓨鍦ㄦ煇绉峛ug 闇�瑕佷慨澶嶏紝娉ㄩ噴10鑷�20琛� 鍔犲叆 20琛岃嚦30琛�
  */
-@Java110Cmd(serviceCode = "machine.listMachine")
+@Java110Cmd(serviceCode = "machine.listMachines")
 public class ListMachineCmd extends Cmd {
 
   private static Logger logger = LoggerFactory.getLogger(ListMachineCmd.class);
     @Autowired
-    private IMachineV1InnerServiceSMO machineV1InnerServiceSMOImpl;
+    private IMachineInnerServiceSMO machineInnerServiceSMOImpl;
+
+    @Autowired
+    private ICommunityInnerServiceSMO communityInnerServiceSMOImpl;
+
+    @Autowired
+    private IFloorInnerServiceSMO floorInnerServiceSMOImpl;
+
+    @Autowired
+    private IUnitInnerServiceSMO unitInnerServiceSMOImpl;
+
+    @Autowired
+    private IRoomInnerServiceSMO roomInnerServiceSMOImpl;
+
+    @Autowired
+    private ICommunityLocationInnerServiceSMO communityLocationInnerServiceSMOImpl;
 
     @Override
     public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) {
         super.validatePageInfo(reqJson);
+
+        Assert.jsonObjectHaveKey(reqJson, "communityId", "璇锋眰鎶ユ枃涓湭鍖呭惈灏忓尯淇℃伅");
     }
 
     @Override
     public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
 
-           MachineDto machineDto = BeanConvertUtil.covertBean(reqJson, MachineDto.class);
+        MachineDto machineDto = BeanConvertUtil.covertBean(reqJson, MachineDto.class);
 
-           int count = machineV1InnerServiceSMOImpl.queryMachinesCount(machineDto);
+        int count = machineInnerServiceSMOImpl.queryMachinesCount(machineDto);
 
-           List<MachineDto> machineDtos = null;
+        List<ApiMachineDataVo> machines = null;
 
-           if (count > 0) {
-               machineDtos = machineV1InnerServiceSMOImpl.queryMachines(machineDto);
-           } else {
-               machineDtos = new ArrayList<>();
-           }
+        if (count > 0) {
+            List<MachineDto> machineDtos = machineInnerServiceSMOImpl.queryMachines(machineDto);
+            // 鍒锋柊 浣嶇疆淇℃伅
+            //refreshMachines(machineDtos);
+            refreshMachineLocation(machineDtos);
+            machines = BeanConvertUtil.covertBeanList(machineDtos, ApiMachineDataVo.class);
+        } else {
+            machines = new ArrayList<>();
+        }
 
-           ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) reqJson.getInteger("row")), count, machineDtos);
+        ApiMachineVo apiMachineVo = new ApiMachineVo();
 
-           ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
+        apiMachineVo.setTotal(count);
+        apiMachineVo.setRecords((int) Math.ceil((double) count / (double) reqJson.getInteger("row")));
+        apiMachineVo.setMachines(machines);
 
-           cmdDataFlowContext.setResponseEntity(responseEntity);
+        ResponseEntity<String> responseEntity = new ResponseEntity<String>(JSONObject.toJSONString(apiMachineVo), HttpStatus.OK);
+
+        cmdDataFlowContext.setResponseEntity(responseEntity);
+
+    }
+
+    private void refreshMachineLocation(List<MachineDto> machines) {
+        for (MachineDto machineDto : machines) {
+            freshMachineStateName(machineDto);
+            getMachineLocation(machineDto);
+        }
+    }
+
+    private void freshMachineStateName(MachineDto machineDto) {
+        String heartbeatTime = machineDto.getHeartbeatTime();
+        try {
+            if (StringUtil.isEmpty(heartbeatTime)) {
+                machineDto.setStateName(machineDto.getStateName() + ";璁惧绂荤嚎");
+            } else {
+                Calendar calendar = Calendar.getInstance();
+                calendar.setTime(DateUtil.getDateFromString(heartbeatTime, DateUtil.DATE_FORMATE_STRING_A));
+                calendar.add(Calendar.MINUTE, 2);
+                if (calendar.getTime().getTime() <= DateUtil.getCurrentDate().getTime()) {
+                    machineDto.setStateName(machineDto.getStateName() + ";璁惧绂荤嚎");
+                } else {
+                    machineDto.setStateName(machineDto.getStateName() + ";璁惧鍦ㄧ嚎");
+                }
+            }
+        } catch (ParseException e) {
+            e.printStackTrace();
+            machineDto.setStateName(machineDto.getStateName() + ";璁惧绂荤嚎");
+        }
+    }
+
+    private void refreshMachines(List<MachineDto> machines) {
+
+        //鎵归噺澶勭悊 灏忓尯
+        refreshCommunitys(machines);
+
+        //鎵归噺澶勭悊鍗曞厓淇℃伅
+        refreshUnits(machines);
+
+        //鎵归噺澶勭悊 鎴垮眿淇℃伅
+        refreshRooms(machines);
+
+        //浣嶇疆鏈垎閰嶆椂
+        refreshOther(machines);
+
+    }
+
+    private void getMachineLocation(MachineDto machineDto) {
+
+        CommunityLocationDto communityLocationDto = new CommunityLocationDto();
+        communityLocationDto.setCommunityId(machineDto.getCommunityId());
+        communityLocationDto.setLocationId(machineDto.getLocationTypeCd());
+        List<CommunityLocationDto> communityLocationDtos = communityLocationInnerServiceSMOImpl.queryCommunityLocations(communityLocationDto);
+
+        if (communityLocationDtos == null || communityLocationDtos.size() < 1) {
+            machineDto.setLocationType(machineDto.getLocationTypeCd());
+            return;
+        }
+
+        machineDto.setLocationType(communityLocationDtos.get(0).getLocationType());
+        machineDto.setLocationObjName(communityLocationDtos.get(0).getLocationName());
+    }
+
+    /**
+     * 鑾峰彇鎵归噺灏忓尯
+     *
+     * @param machines 璁惧淇℃伅
+     * @return 鎵归噺userIds 淇℃伅
+     */
+    private void refreshCommunitys(List<MachineDto> machines) {
+        List<String> communityIds = new ArrayList<String>();
+        List<MachineDto> tmpMachineDtos = new ArrayList<>();
+        for (MachineDto machineDto : machines) {
+            getMachineLocation(machineDto);
+            if (!"2000".equals(machineDto.getLocationType())
+                    && !"3000".equals(machineDto.getLocationType())
+                    && !"4000".equals(machineDto.getLocationType())
+            ) {
+                communityIds.add(machineDto.getLocationObjId());
+                tmpMachineDtos.add(machineDto);
+            }
+        }
+
+        if (communityIds.size() < 1) {
+            return;
+        }
+        String[] tmpCommunityIds = communityIds.toArray(new String[communityIds.size()]);
+
+        CommunityDto communityDto = new CommunityDto();
+        communityDto.setCommunityIds(tmpCommunityIds);
+        //鏍规嵁 userId 鏌ヨ鐢ㄦ埛淇℃伅
+        List<CommunityDto> communityDtos = communityInnerServiceSMOImpl.queryCommunitys(communityDto);
+
+        for (MachineDto machineDto : tmpMachineDtos) {
+            for (CommunityDto tmpCommunityDto : communityDtos) {
+                if (machineDto.getLocationObjId().equals(tmpCommunityDto.getCommunityId())) {
+                    machineDto.setLocationObjName(tmpCommunityDto.getName() + " " + machineDto.getLocationTypeName());
+                }
+            }
+        }
+    }
+
+
+    /**
+     * 鑾峰彇鎵归噺鍗曞厓
+     *
+     * @param machines 璁惧淇℃伅
+     * @return 鎵归噺userIds 淇℃伅
+     */
+    private void refreshUnits(List<MachineDto> machines) {
+        List<String> unitIds = new ArrayList<String>();
+        List<MachineDto> tmpMachineDtos = new ArrayList<>();
+        for (MachineDto machineDto : machines) {
+            getMachineLocation(machineDto);
+            if ("2000".equals(machineDto.getLocationType())) {
+                unitIds.add(machineDto.getLocationObjId());
+                tmpMachineDtos.add(machineDto);
+            }
+        }
+
+        if (unitIds.size() < 1) {
+            return;
+        }
+        String[] tmpUnitIds = unitIds.toArray(new String[unitIds.size()]);
+
+        FloorAndUnitDto floorAndUnitDto = new FloorAndUnitDto();
+        floorAndUnitDto.setUnitIds(tmpUnitIds);
+        //鏍规嵁 userId 鏌ヨ鐢ㄦ埛淇℃伅
+        List<FloorAndUnitDto> unitDtos = unitInnerServiceSMOImpl.getFloorAndUnitInfo(floorAndUnitDto);
+
+        for (MachineDto machineDto : tmpMachineDtos) {
+            for (FloorAndUnitDto tmpUnitDto : unitDtos) {
+                if (machineDto.getLocationObjId().equals(tmpUnitDto.getUnitId())) {
+                    machineDto.setLocationObjName(tmpUnitDto.getFloorNum() + "鏍�" + tmpUnitDto.getUnitNum() + "鍗曞厓");
+                    BeanConvertUtil.covertBean(tmpUnitDto, machineDto);
+                }
+            }
+        }
+    }
+
+    /**
+     * 鑾峰彇鎵归噺鍗曞厓
+     *
+     * @param machines 璁惧淇℃伅
+     * @return 鎵归噺userIds 淇℃伅
+     */
+    private void refreshRooms(List<MachineDto> machines) {
+        List<String> roomIds = new ArrayList<String>();
+        List<MachineDto> tmpMachineDtos = new ArrayList<>();
+        for (MachineDto machineDto : machines) {
+            getMachineLocation(machineDto);
+            if ("3000".equals(machineDto.getLocationType())) {
+                roomIds.add(machineDto.getLocationObjId());
+                tmpMachineDtos.add(machineDto);
+            }
+        }
+        if (roomIds.size() < 1) {
+            return;
+        }
+        String[] tmpRoomIds = roomIds.toArray(new String[roomIds.size()]);
+
+        RoomDto roomDto = new RoomDto();
+        roomDto.setRoomIds(tmpRoomIds);
+        roomDto.setCommunityId(machines.get(0).getCommunityId());
+        //鏍规嵁 userId 鏌ヨ鐢ㄦ埛淇℃伅
+        List<RoomDto> roomDtos = roomInnerServiceSMOImpl.queryRooms(roomDto);
+
+        for (MachineDto machineDto : tmpMachineDtos) {
+            for (RoomDto tmpRoomDto : roomDtos) {
+                if (machineDto.getLocationObjId().equals(tmpRoomDto.getRoomId())) {
+                    machineDto.setLocationObjName(tmpRoomDto.getFloorNum() + "鏍�" + tmpRoomDto.getUnitNum() + "鍗曞厓" + tmpRoomDto.getRoomNum() + "瀹�");
+                    BeanConvertUtil.covertBean(tmpRoomDto, machineDto);
+                }
+            }
+        }
+    }
+
+    /**
+     * 鑾峰彇鎵归噺鍗曞厓
+     *
+     * @param machines 璁惧淇℃伅
+     * @return 鎵归噺userIds 淇℃伅
+     */
+    private void refreshOther(List<MachineDto> machines) {
+        for (MachineDto machineDto : machines) {
+
+            if ("4000".equals(machineDto.getLocationTypeCd())) {
+                machineDto.setLocationObjName("鏈垎閰�");
+            }
+        }
+
     }
 }

--
Gitblit v1.8.0