lin 4 سال پیش
والد
کامیت
44e33bcd46
23فایلهای تغییر یافته به همراه274 افزوده شده و 53 حذف شده
  1. 12 0
      smart-city-intf/src/main/java/com/zcxk/smartcity/data/access/dao/DeviceCommandMapper.java
  2. 30 0
      smart-city-intf/src/main/java/com/zcxk/smartcity/data/access/service/impl/DeviceValveRecordServiceImpl.java
  3. 8 0
      smart-city-intf/src/main/resources/mapper/DeviceCommandMapper.xml
  4. 12 0
      smart-city-platform/src/main/java/com/bz/smart_city/controller/DeviceController.java
  5. 3 3
      smart-city-platform/src/main/java/com/bz/smart_city/controller/system/CommandController.java
  6. 4 1
      smart-city-platform/src/main/java/com/bz/smart_city/dao/DeviceCommandMapper.java
  7. 1 1
      smart-city-platform/src/main/java/com/bz/smart_city/entity/DeviceCommand.java
  8. 3 1
      smart-city-platform/src/main/java/com/bz/smart_city/excel/ResolverExcelTemplate.java
  9. 1 0
      smart-city-platform/src/main/java/com/bz/smart_city/excel/model/ExcelData.java
  10. 54 13
      smart-city-platform/src/main/java/com/bz/smart_city/excel/resolver/CommandResolverExcelService.java
  11. 10 3
      smart-city-platform/src/main/java/com/bz/smart_city/rabbitmq/listener/CommandReceiver.java
  12. 12 0
      smart-city-platform/src/main/java/com/bz/smart_city/rabbitmq/model/RabbitCommandParamData.java
  13. 6 1
      smart-city-platform/src/main/java/com/bz/smart_city/service/DeviceCommandService.java
  14. 1 0
      smart-city-platform/src/main/java/com/bz/smart_city/service/DeviceService.java
  15. 41 13
      smart-city-platform/src/main/java/com/bz/smart_city/service/impl/DeviceCommandServiceImpl.java
  16. 26 4
      smart-city-platform/src/main/java/com/bz/smart_city/service/impl/DeviceServiceImpl.java
  17. 36 11
      smart-city-platform/src/main/java/com/bz/smart_city/service/importfile/AsyncTaskImportService.java
  18. 2 0
      smart-city-platform/src/main/resources/application-dev.properties
  19. 2 1
      smart-city-platform/src/main/resources/application-prd.properties
  20. 2 1
      smart-city-platform/src/main/resources/application-sit.properties
  21. BIN
      smart-city-platform/src/main/resources/excel/orderDeviceTemplate20210421.xlsx
  22. 7 0
      smart-city-platform/src/main/resources/mapper/DeviceCommandMapper.xml
  23. 1 0
      smart-city-platform/src/main/resources/mapper/DeviceCommandTaskMapper.xml

+ 12 - 0
smart-city-intf/src/main/java/com/zcxk/smartcity/data/access/dao/DeviceCommandMapper.java

@@ -0,0 +1,12 @@
+package com.zcxk.smartcity.data.access.dao;
+
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+
+@Mapper
+public interface DeviceCommandMapper {
+
+
+    int updateCommandStatus(@Param("commandId") String commandId, @Param("status") Integer status, @Param("message") String message);
+}

+ 30 - 0
smart-city-intf/src/main/java/com/zcxk/smartcity/data/access/service/impl/DeviceValveRecordServiceImpl.java

@@ -1,10 +1,13 @@
 package com.zcxk.smartcity.data.access.service.impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.zcxk.smartcity.data.access.dao.DeviceCommandMapper;
 import com.zcxk.smartcity.data.access.dao.DeviceValveRecordMapper;
 import com.zcxk.smartcity.data.access.entity.DeviceValveRecord;
 import com.zcxk.smartcity.data.access.service.DeviceValveRecordService;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import java.time.LocalDateTime;
@@ -14,6 +17,8 @@ public class DeviceValveRecordServiceImpl implements DeviceValveRecordService {
 
     @Resource
     private DeviceValveRecordMapper deviceValveRecordMapper;
+    @Resource
+    private DeviceCommandMapper deviceCommandMapper;
 
     @Override
     public int insertSelective(DeviceValveRecord record) {
@@ -29,10 +34,35 @@ public class DeviceValveRecordServiceImpl implements DeviceValveRecordService {
     }
 
     @Override
+    @Transactional
     public void updateCommandStatus(JSONObject jsonObject) {
         String commandId =  jsonObject.getString("relationId");
         String commandStatus =  jsonObject.getString("status");
         String message =  jsonObject.getString("message");
         deviceValveRecordMapper.updateCommandStatus(commandId,commandStatus,message);
+
+        deviceCommandMapper.updateCommandStatus(commandId,convertStatus(commandStatus),message);
+    }
+
+    private Integer convertStatus(String commandStatus){
+        //⚫ PENDING 表示缓存未下发
+        //⚫ EXPIRED 表示命令已经过期
+        //⚫ SUCCESSFUL 表示命令已经成功执行
+        //⚫ FAILED 表示命令执行失败
+        //⚫ TIMEOUT 表示命令下发执行超时
+        //⚫ CANCELED 表示命令已经被撤销执行
+        //⚫ DELIVERED 表示命令已送达设备
+        //⚫ SENT 表示命令正在下发中
+
+        //0:发送、1:超时、2:成功、3:失败
+        if(StringUtils.equalsIgnoreCase("PENDING",commandStatus))return 0;
+        if(StringUtils.equalsIgnoreCase("EXPIRED",commandStatus))return 2;
+        if(StringUtils.equalsIgnoreCase("SUCCESSFUL",commandStatus))return 2;
+        if(StringUtils.equalsIgnoreCase("FAILED",commandStatus))return 3;
+        if(StringUtils.equalsIgnoreCase("TIMEOUT",commandStatus))return 1;
+        if(StringUtils.equalsIgnoreCase("CANCELED",commandStatus))return 3;
+        if(StringUtils.equalsIgnoreCase("DELIVERED",commandStatus))return 2;
+        if(StringUtils.equalsIgnoreCase("SENT",commandStatus))return 0;
+        return null;
     }
 }

+ 8 - 0
smart-city-intf/src/main/resources/mapper/DeviceCommandMapper.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.zcxk.smartcity.data.access.dao.DeviceCommandMapper">
+
+    <update id="updateCommandStatus">
+        update sc_device_command set command_status = #{status},remark=#{message},finish_date=NOW(),date_update=NOW() where command_id = #{commandId}
+    </update>
+</mapper>

+ 12 - 0
smart-city-platform/src/main/java/com/bz/smart_city/controller/DeviceController.java

@@ -314,6 +314,18 @@ public class DeviceController {
         return new AjaxMessage(ResultStatus.OK);
     }
 
+    @ResponseBody
+    @PostMapping("queryValve")
+    @ApiOperation(value = "查询阀门执行状态")
+    public AjaxMessage queryValve(
+            @ApiParam(value = "设备id", required = true) @RequestParam(required = true) Long deviceId,
+            @ApiParam(value = "阀门状态 0:关阀 1:开阀", required = true) @RequestParam(required = true) Integer valveStatus
+    ) {
+        //0:关阀 1:开阀 2:多次操作 3:可执行
+        Integer status =deviceService.queryValve(deviceId, valveStatus);
+        return new AjaxMessage(ResultStatus.OK,status);
+    }
+
     @ResponseBody
     @PostMapping("batchSetValve/{valveStatus}")
     @ApiOperation(value = "批量开关阀门")

+ 3 - 3
smart-city-platform/src/main/java/com/bz/smart_city/controller/system/CommandController.java

@@ -31,9 +31,9 @@ public class CommandController {
     @GetMapping("/getPage")
     @ApiOperation(value = "查询指令分页")
     public AjaxMessage<Pagination<DeviceCommandDto>> getPage(
-            @ApiParam(value = "设备id", required = false) @RequestParam(required = false) Long deviceId,
-            @ApiParam(value = "查询日期,YYYYMMDD格式", required = false) @RequestParam Integer startDate,
-            @ApiParam(value = "查询日期,YYYYMMDD格式", required = false) @RequestParam Integer endDate,
+            @ApiParam(value = "设备id", required = true) @RequestParam(required = true) Long deviceId,
+            @ApiParam(value = "查询日期,YYYYMMDD格式", required = true) @RequestParam Integer startDate,
+            @ApiParam(value = "查询日期,YYYYMMDD格式", required = true) @RequestParam Integer endDate,
             @ApiParam(value = "页数,非必传,默认第一页", required = false, defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNum,
             @ApiParam(value = "条数,非必传,默认15条", required = false, defaultValue = "15") @RequestParam(required = false, defaultValue = "15") int pageSize
     ){

+ 4 - 1
smart-city-platform/src/main/java/com/bz/smart_city/dao/DeviceCommandMapper.java

@@ -5,6 +5,7 @@ import com.bz.smart_city.entity.DeviceCommand;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
+import java.time.LocalDateTime;
 import java.util.List;
 
 @Mapper
@@ -13,5 +14,7 @@ public interface DeviceCommandMapper {
 
     int updateByPrimaryKeySelective(DeviceCommand record);
 
-    List<DeviceCommandDto> getList(@Param("siteId") Integer siteId, @Param("deviceId") Long deviceId);
+    List<DeviceCommandDto> getList(@Param("siteId") Integer siteId, @Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate, @Param("deviceId") Long deviceId);
+
+    int countByDeviceId(@Param("deviceId") Long deviceId);
 }

+ 1 - 1
smart-city-platform/src/main/java/com/bz/smart_city/entity/DeviceCommand.java

@@ -20,7 +20,7 @@ public class DeviceCommand {
     @ApiModelProperty(value="指令类型 0:关阀 1:开阀")
     private Integer commandType;
 
-    @ApiModelProperty(value="指令状态 0:发送、1:已送达、2:超时、3:执行成功、4:执行失败")
+    @ApiModelProperty(value="指令状态 0:发送、1:超时、2:成功、3:失败")
     private Integer commandStatus;
 
     @ApiModelProperty(value="操作类型 1:批量指令下发、2:系统执行指令、3:手动触发指令")

+ 3 - 1
smart-city-platform/src/main/java/com/bz/smart_city/excel/ResolverExcelTemplate.java

@@ -27,13 +27,15 @@ public class ResolverExcelTemplate {
         this.handleExcelService = handleExcelService;
     }
 
-    public void resolver(Integer sysId, Integer customerId, Import record, String messageTitle){
+    public void resolver(Integer sysId, Integer customerId, Import record, Object var, String messageTitle){
 
         ExcelData data = new ExcelData();
         data.setSysId(sysId);
         data.setCustomerId(customerId);
         data.setRecord(record);
         data.setMessageTitle(messageTitle);
+        data.setVar(var);
+        data.getMessageContent().append(record.getImportFileName());
 
         InputStream inputStream = null;
         try {

+ 1 - 0
smart-city-platform/src/main/java/com/bz/smart_city/excel/model/ExcelData.java

@@ -18,4 +18,5 @@ public class ExcelData {
     private Boolean invalidTemplate = false;
     private AtomicInteger successTime = new AtomicInteger(0);
     private AtomicInteger failTime = new AtomicInteger(0);
+    private Object var;
 }

+ 54 - 13
smart-city-platform/src/main/java/com/bz/smart_city/excel/resolver/CommandResolverExcelService.java

@@ -1,7 +1,9 @@
 package com.bz.smart_city.excel.resolver;
 
+import com.alibaba.fastjson.JSON;
 import com.bz.smart_city.commom.util.DateTimeUtil;
 import com.bz.smart_city.commom.util.RedisUtil;
+import com.bz.smart_city.commom.util.UserUtil;
 import com.bz.smart_city.dao.CustomerMapper;
 import com.bz.smart_city.dao.DeviceCommandMapper;
 import com.bz.smart_city.dao.DeviceCommandTaskMapper;
@@ -12,6 +14,7 @@ import com.bz.smart_city.dto.assistant.InstallListDTO;
 import com.bz.smart_city.entity.*;
 import com.bz.smart_city.excel.ResolverExcelService;
 import com.bz.smart_city.excel.model.ExcelData;
+import com.bz.smart_city.rabbitmq.model.RabbitCommandParamData;
 import com.bz.smart_city.service.BuildingService;
 import com.bz.smart_city.service.CommunityService;
 import com.bz.smart_city.service.DeviceTypeService;
@@ -20,6 +23,7 @@ import com.bz.smart_city.service.DeviceCommandService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,6 +31,7 @@ import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
 import java.time.LocalDateTime;
+import java.util.List;
 
 @Slf4j
 @Component
@@ -59,6 +64,8 @@ public class CommandResolverExcelService implements ResolverExcelService {
         InstallListDTO dto = new InstallListDTO();
         dto.setSiteId(data.getRecord().getSiteId());
         dto.setInstallTime(LocalDateTime.now());
+        RabbitCommandParamData paramData = (RabbitCommandParamData) data.getVar();
+        log.info("paramData = {}", JSON.toJSONString(paramData));
         // 循环工作表Sheet
         for (int numSheet = 0; numSheet < data.getWorkbook().getNumberOfSheets(); numSheet++) {
             Sheet hssfSheet = data.getWorkbook().getSheetAt(numSheet);
@@ -80,7 +87,7 @@ public class CommandResolverExcelService implements ResolverExcelService {
             }
             if(dt.getIsValve()==0){
                 data.getMessageContent().append("导入失败,");
-                data.getMessageContent().append("该设备型号不支持阀控");
+                data.getMessageContent().append("设备型号不支持该指令");
                 continue;
             }
 
@@ -94,10 +101,15 @@ public class CommandResolverExcelService implements ResolverExcelService {
             }
 
             Integer valveStatus =  convertValveStatus(commandTypeCell.getStringCellValue().trim());
+            if (valveStatus == null) {
+                data.getMessageContent().append("导入失败,");
+                data.getMessageContent().append("无效指令");
+                continue;
+            }
             String taskNo = generateTaskNo();
 
             // 2,处理明细行数据
-            for (int rowNum = 6; rowNum < 207; rowNum++) {
+            for (int rowNum = 6; rowNum < 206; rowNum++) {
                 Row row = hssfSheet.getRow(rowNum);
                 if (row != null) {
 
@@ -105,10 +117,17 @@ public class CommandResolverExcelService implements ResolverExcelService {
                     Cell deviceNoCell = row.getCell(0);
                     Cell paramsCell = row.getCell(1);
                     Cell remarkCell = row.getCell(2);
-                    if(StringUtils.equals("", deviceNoCell.getStringCellValue().trim())) {
+                    if(deviceNoCell == null || StringUtils.equals("", deviceNoCell.getStringCellValue().trim())) {
                         continue;
                     }
-                    log.info("row deviceNo = {},params = {}",deviceNoCell.getStringCellValue(),paramsCell.getStringCellValue());
+                    deviceNoCell.setCellType(CellType.STRING);
+
+                    String params = null;
+                    if (paramsCell != null) {
+                        paramsCell.setCellType(CellType.STRING);
+                        params = paramsCell.getStringCellValue().trim();
+                    }
+                    log.info("row rowNum  = {} deviceNo = {} params = {}",rowNum,deviceNoCell.getStringCellValue(),params);
 
                     Device device = deviceMapper.findByDeviceNo(deviceNoCell.getStringCellValue().trim());
                     if (device == null) {
@@ -121,8 +140,19 @@ public class CommandResolverExcelService implements ResolverExcelService {
                         remarkCell.setCellValue("表型不匹配");
                         continue;
                     }
+                    Integer count = deviceCommandMapper.countByDeviceId(device.getId());
+                    if (count != null && count > 0) {
+                        data.getFailTime().incrementAndGet();
+                        remarkCell.setCellValue("指令执行中重复触发");
+                        continue;
+                    }
+                    if(!matchAuth(paramData.getCustomerIds(),device.getCustomerId())){
+                        data.getFailTime().incrementAndGet();
+                        remarkCell.setCellValue("无操作权限");
+                        continue;
+                    }
 
-                    save(device,paramsCell.getStringCellValue().trim(),valveStatus,taskNo,dt);
+                    save(device,params,valveStatus,taskNo,dt,paramData.getUsername());
 
 
 
@@ -148,14 +178,14 @@ public class CommandResolverExcelService implements ResolverExcelService {
         log.info("end CommandResolverExcelService");
     }
 
-    private void save(Device device,String params,Integer valveStatus,String taskNo,DeviceTypeDto deviceTypeDto){
+    private void save(Device device,String params,Integer valveStatus,String taskNo,DeviceTypeDto deviceTypeDto,String username){
         //1、执行开关阀门
         ValveCommandRequestDTO valveCommandRequest = new ValveCommandRequestDTO();
         valveCommandRequest.setValveStatus(String.valueOf(valveStatus));
         valveCommandRequest.setOperationType(1);
         valveCommandRequest.setPlatform(1);
 
-        Integer deviceCommandId = deviceCommandService.executeValveCommand(device,deviceTypeDto,valveCommandRequest);
+        Integer deviceCommandId = deviceCommandService.executeValveCommand(device,deviceTypeDto,valveCommandRequest,username);
 
 
         DeviceCommandTask task = new DeviceCommandTask();
@@ -164,8 +194,8 @@ public class CommandResolverExcelService implements ResolverExcelService {
         task.setStatus(1);
         task.setDateCreate(LocalDateTime.now());
         task.setDateUpdate(LocalDateTime.now());
-        task.setCreateBy("系统操作");
-        task.setUpdateBy("系统操作");
+        task.setCreateBy(username);
+        task.setUpdateBy(username);
         deviceCommandTaskMapper.insertSelective(task);
     }
 
@@ -190,11 +220,22 @@ public class CommandResolverExcelService implements ResolverExcelService {
 
 
     private Integer convertValveStatus(String valve){
-        if("开阀".equals(valve)) {
-            return 1;
-        } else {
-            return 0;
+        if("开阀".equals(valve)) return 1;
+        if("关阀".equals(valve)) return 0;
+        return null;
+    }
+
+    private Boolean matchAuth(List<Integer> customerIds,Integer customerId){
+        log.info("customerIds = {} customerId = {}",JSON.toJSONString(customerIds), customerId);
+        if (customerIds != null && customerIds.size() > 0) {
+            return checkCustomerId(customerIds,customerId);
         }
+        return true;
+    }
+
+
+    private boolean checkCustomerId(List<Integer> list, Integer customerId) {
+        return list.parallelStream().anyMatch(cId -> cId.equals(customerId));
     }
 
 

+ 10 - 3
smart-city-platform/src/main/java/com/bz/smart_city/rabbitmq/listener/CommandReceiver.java

@@ -1,11 +1,14 @@
 package com.bz.smart_city.rabbitmq.listener;
 
 import com.alibaba.fastjson.JSON;
+import com.bz.smart_city.rabbitmq.model.RabbitCommandParamData;
 import com.bz.smart_city.rabbitmq.model.RabbitDeviceParamData;
+import com.bz.smart_city.service.DeviceCommandService;
 import com.rabbitmq.client.Channel;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.amqp.core.Message;
 import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import java.io.IOException;
@@ -14,16 +17,20 @@ import java.io.IOException;
 @Component
 @Slf4j
 public class CommandReceiver {
-    //@RabbitListener(queues = {"${spring.rabbitmq.download-device-queue}"},containerFactory = "platformListenerContainerFactory")
+
+    @Autowired
+    private DeviceCommandService deviceCommandService;
+
+    @RabbitListener(queues = {"${spring.rabbitmq.command-task-queue}"},containerFactory = "platformListenerContainerFactory")
     public void receiver(Channel channel, Message message) throws IOException {
         //String str = new String(body);
         try {
             String msg = new String(message.getBody());
             log.info("-----CommandReceiver msg-----,"  +msg);
-            RabbitDeviceParamData data = JSON.parseObject(msg, RabbitDeviceParamData.class);
+            RabbitCommandParamData data = JSON.parseObject(msg, RabbitCommandParamData.class);
             //log.info("-----DownloadDeviceReceiver params-----,"  +JSON.toJSONString(data));
             // 1,处理消息
-            //deviceService.executeDeviceListExcel(data);
+            deviceCommandService.executeRabbitCommand(data);
 
             // 2,确认消息消费成功 
             //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

+ 12 - 0
smart-city-platform/src/main/java/com/bz/smart_city/rabbitmq/model/RabbitCommandParamData.java

@@ -0,0 +1,12 @@
+package com.bz.smart_city.rabbitmq.model;
+
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class RabbitCommandParamData {
+    private Integer recordId;
+    private String username;
+    private List<Integer> customerIds;
+}

+ 6 - 1
smart-city-platform/src/main/java/com/bz/smart_city/service/DeviceCommandService.java

@@ -6,6 +6,7 @@ import com.bz.smart_city.dto.DeviceTypeDto;
 import com.bz.smart_city.dto.ValveCommandRequestDTO;
 import com.bz.smart_city.entity.Device;
 import com.bz.smart_city.entity.DeviceCommand;
+import com.bz.smart_city.rabbitmq.model.RabbitCommandParamData;
 
 import javax.servlet.http.HttpServletResponse;
 
@@ -22,7 +23,11 @@ public interface DeviceCommandService{
 
     Pagination<DeviceCommandDto> getPage(Long deviceId, Integer startDate, Integer endDate, int pageNum, int pageSize);
 
-    Integer executeValveCommand(Device device, DeviceTypeDto deviceTypeDto, ValveCommandRequestDTO valveCommandRequest);
+    Integer executeValveCommand(Device device, DeviceTypeDto deviceTypeDto, ValveCommandRequestDTO valveCommandRequest,String username);
 
     void getExcel(String deviceNo, String name, Integer commandType, Integer commandStatus, HttpServletResponse httpServletResponse);
+
+    Integer countByDeviceId(Long deviceId);
+
+    void executeRabbitCommand(RabbitCommandParamData data);
 }

+ 1 - 0
smart-city-platform/src/main/java/com/bz/smart_city/service/DeviceService.java

@@ -149,4 +149,5 @@ public interface DeviceService{
 
     Boolean setValveV2(Long aLong, String valve,Integer platform);
 
+    Integer queryValve(Long deviceId, Integer valveStatus);
 }

+ 41 - 13
smart-city-platform/src/main/java/com/bz/smart_city/service/impl/DeviceCommandServiceImpl.java

@@ -6,10 +6,16 @@ import com.bz.smart_city.commom.model.Pagination;
 import com.bz.smart_city.commom.util.ExcelUtil;
 import com.bz.smart_city.commom.util.UserUtil;
 import com.bz.smart_city.dao.DeviceCommandTaskMapper;
+import com.bz.smart_city.dao.ImportMapper;
 import com.bz.smart_city.dto.*;
 import com.bz.smart_city.entity.Device;
 import com.bz.smart_city.entity.DeviceValveRecord;
+import com.bz.smart_city.entity.Import;
+import com.bz.smart_city.excel.HandleExcelService;
+import com.bz.smart_city.excel.ResolverExcelTemplate;
 import com.bz.smart_city.excel.download.template.CommandDownloadExcelTemplate;
+import com.bz.smart_city.excel.resolver.CommandResolverExcelService;
+import com.bz.smart_city.rabbitmq.model.RabbitCommandParamData;
 import com.bz.smart_city.service.DeviceCommandService;
 import com.bz.smart_city.service.DeviceValveRecordService;
 import com.bz.smart_city.service.udip.EasylinkinUtils;
@@ -29,6 +35,7 @@ import com.bz.smart_city.dao.DeviceCommandMapper;
 import com.bz.smart_city.entity.DeviceCommand;
 
 import java.time.LocalDateTime;
+import java.time.LocalTime;
 import java.time.format.DateTimeFormatter;
 import java.util.List;
 
@@ -45,6 +52,8 @@ public class DeviceCommandServiceImpl implements DeviceCommandService {
     private DeviceCommandMapper deviceCommandMapper;
     @Resource
     private DeviceCommandTaskMapper deviceCommandTaskMapper;
+    @Resource
+    private ImportMapper importMapper;
 
     @Autowired
     private GdAgentUtils gdAgentUtils;
@@ -59,6 +68,11 @@ public class DeviceCommandServiceImpl implements DeviceCommandService {
     @Autowired
     private DeviceValveRecordService deviceValveRecordService;
 
+    @Autowired
+    private CommandResolverExcelService commandResolverExcelService;
+    @Autowired
+    private HandleExcelService handleExcelService;
+
     @Override
     public int insertSelective(DeviceCommand record) {
         return deviceCommandMapper.insertSelective(record);
@@ -85,8 +99,13 @@ public class DeviceCommandServiceImpl implements DeviceCommandService {
     @Override
     public Pagination<DeviceCommandDto> getPage(Long deviceId, Integer startDate, Integer endDate, int pageNum, int pageSize) {
         LoginUser loginUser = UserUtil.getCurrentUser();
+
+        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
+        LocalDateTime startLocalDate = LocalDateTime.parse(startDate+"000000",df);
+        LocalDateTime endLocalDate = LocalDateTime.parse(endDate+"235959",df);
+
         PageHelper.startPage(pageNum,pageSize);
-        List<DeviceCommandDto> list = deviceCommandMapper.getList(loginUser.getSiteId(),deviceId);
+        List<DeviceCommandDto> list = deviceCommandMapper.getList(loginUser.getSiteId(),startLocalDate,endLocalDate,deviceId);
         return new Pagination<>(list);
     }
 
@@ -123,10 +142,9 @@ public class DeviceCommandServiceImpl implements DeviceCommandService {
 
     private String getCommandStatusName(Integer commandStatus) {
         if(commandStatus==0) return "发送";
-        if(commandStatus==1) return "已送达";
-        if(commandStatus==2) return "超时";
-        if(commandStatus==3) return "执行成功";
-        if(commandStatus==4) return "执行失败";
+        if(commandStatus==1) return "超时";
+        if(commandStatus==2) return "执行成功";
+        if(commandStatus==3) return "执行失败";
         return null;
     }
 
@@ -137,7 +155,12 @@ public class DeviceCommandServiceImpl implements DeviceCommandService {
     }
 
     @Override
-    public Integer executeValveCommand(Device device, DeviceTypeDto deviceTypeDto, ValveCommandRequestDTO valveCommandRequest){
+    public Integer countByDeviceId(Long deviceId) {
+        return deviceCommandMapper.countByDeviceId(deviceId);
+    }
+
+    @Override
+    public Integer executeValveCommand(Device device, DeviceTypeDto deviceTypeDto, ValveCommandRequestDTO valveCommandRequest,String username){
         log.info("begin executeValveCommand");
         String commandId = null;
         Integer sendValveStatus = Integer.valueOf(valveCommandRequest.getValveStatus());
@@ -198,23 +221,28 @@ public class DeviceCommandServiceImpl implements DeviceCommandService {
         DeviceCommand deviceCommand = new DeviceCommand();
         deviceCommand.setSiteId(device.getSiteId());
         deviceCommand.setDeviceId(device.getId());
-        deviceCommand.setCommandType(sendValveStatus);
-        deviceCommand.setCommandStatus(executeResult?0:4);
-        deviceCommand.setOperationType(1);
+        deviceCommand.setCommandType(Integer.valueOf(valveCommandRequest.getValveStatus()));
+        deviceCommand.setCommandStatus(executeResult?0:3);
+        deviceCommand.setOperationType(valveCommandRequest.getOperationType());
         deviceCommand.setCommandId(commandId);
         deviceCommand.setIssueDate(LocalDateTime.now());
-        deviceCommand.setPlatform(1);
+        deviceCommand.setPlatform(valveCommandRequest.getPlatform());
         deviceCommand.setStatus(1);
         deviceCommand.setDateCreate(LocalDateTime.now());
         deviceCommand.setDateUpdate(LocalDateTime.now());
-        deviceCommand.setCreateBy("");
-        deviceCommand.setUpdateBy("");
+        deviceCommand.setCreateBy(username);
+        deviceCommand.setUpdateBy(username);
         deviceCommandMapper.insertSelective(deviceCommand);
 
         log.info("end executeValveCommand");
         return deviceCommand.getId();
     }
 
+    @Override
+    public void executeRabbitCommand(RabbitCommandParamData data) {
 
-
+        Import record = importMapper.findByObjId(data.getRecordId());
+        ResolverExcelTemplate template = new ResolverExcelTemplate(commandResolverExcelService,handleExcelService);
+        template.resolver(null,null,record,data,"批量指令下发-指令导入");
+    }
 }

+ 26 - 4
smart-city-platform/src/main/java/com/bz/smart_city/service/impl/DeviceServiceImpl.java

@@ -1425,8 +1425,8 @@ public  class DeviceServiceImpl implements DeviceService {
     }
 
     @Override
-    @Transactional
     public void setValve(Long deviceId, Integer valve) {
+        String username = UserUtil.getUsername()!=null?UserUtil.getUsername():"系统操作";
         Device device = deviceMapper.findByDeviceId(deviceId);
         DeviceTypeDto deviceTypeDto = deviceTypeMapper.getById(device.getDeviceType());
 
@@ -1438,11 +1438,33 @@ public  class DeviceServiceImpl implements DeviceService {
         valveCommandRequest.setPlatform(1);
 
         //1、执行开关阀门
-        deviceCommandService.executeValveCommand(device,deviceTypeDto,valveCommandRequest);
+        deviceCommandService.executeValveCommand(device,deviceTypeDto,valveCommandRequest,username);
+
+    }
+
+    @Override
+    public Integer queryValve(Long deviceId, Integer valveStatus) {
+
+        WaterMeterErrorDays waterMeter = waterMeterErrorDaysMapper.findByDeviceId(deviceId);
+        if (waterMeter != null) {
+            if(valveStatus.equals(waterMeter.getValveStatus())){
+                if("1".equals(waterMeter.getValveStatus().toString())){
+                    return 1;
+                }else {
+                    return 0;
+                }
+            }
+        }
+
+        Integer result = deviceCommandService.countByDeviceId(deviceId);
+        if (result != null && result > 0) {
+            return 2;
+        }
+        return 3;
 
     }
 
-    public Boolean setValveV2(Long deviceId, String valve,Integer platform) {
+    public Boolean setValveV2(Long deviceId, String valve, Integer platform) {
         log.info("setValveV2 deviceId={} valve={}",deviceId,valve);
         Device device = deviceMapper.findByDeviceId(deviceId);
         DeviceTypeDto deviceTypeDto = deviceTypeMapper.getById(device.getDeviceType());
@@ -1453,7 +1475,7 @@ public  class DeviceServiceImpl implements DeviceService {
         valveCommandRequest.setPlatform(platform);
 
         //1、执行开关阀门
-        Integer deviceCommandId = deviceCommandService.executeValveCommand(device,deviceTypeDto,valveCommandRequest);
+        Integer deviceCommandId = deviceCommandService.executeValveCommand(device,deviceTypeDto,valveCommandRequest,"系统操作");
 
         //2、更换阀门按钮状态
         //if(deviceCommandId !=null) replaceValveButtonStatus(deviceId,valve);

+ 36 - 11
smart-city-platform/src/main/java/com/bz/smart_city/service/importfile/AsyncTaskImportService.java

@@ -1,7 +1,9 @@
 package com.bz.smart_city.service.importfile;
 
+import com.alibaba.fastjson.JSON;
 import com.bz.smart_city.commom.exception.ServiceException;
 import com.bz.smart_city.commom.util.FileUtil;
+import com.bz.smart_city.commom.util.UserUtil;
 import com.bz.smart_city.commom.util.Util;
 import com.bz.smart_city.dao.*;
 import com.bz.smart_city.dao.pay.PayBaseAccountMapper;
@@ -16,6 +18,7 @@ import com.bz.smart_city.entity.pay.archives.PayBaseCustomerandmeterrela;
 import com.bz.smart_city.excel.HandleExcelService;
 import com.bz.smart_city.excel.ResolverExcelTemplate;
 import com.bz.smart_city.excel.resolver.*;
+import com.bz.smart_city.rabbitmq.model.RabbitCommandParamData;
 import com.bz.smart_city.service.*;
 import com.bz.smart_city.service.pay.PayBaseAccountService;
 import com.bz.smart_city.service.pay.PaySysDictService;
@@ -24,6 +27,8 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.springframework.amqp.rabbit.connection.SimpleResourceHolder;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Async;
@@ -96,6 +101,13 @@ public class AsyncTaskImportService {
     @Value("${service.domain}")
     private String domain;
 
+    @Value("${spring.rabbitmq.exchange}")
+    private String exchange;
+    @Value("${spring.rabbitmq.command-task-queue}")
+    private String queue;
+    @Autowired
+    private RabbitTemplate rabbitTemplate;
+
     protected String getFilePath() {
         String name = Util.createUUIDId();
         String fullPath = filesPath + "/import/" + name + ".xlsm";
@@ -107,10 +119,10 @@ public class AsyncTaskImportService {
         log.info("begin executeAsyncInstallTask!");
         if(sysId == 55){
             ResolverExcelTemplate template = new ResolverExcelTemplate(installRemoteMeterResolverExcelService,handleExcelService);
-            template.resolver(null,null,record,"新装水表管理-批量添加水表");
+            template.resolver(null,null,record, null,"新装水表管理-批量添加水表");
         }else {
             ResolverExcelTemplate template = new ResolverExcelTemplate(installResolverExcelService,handleExcelService);
-            template.resolver(null,null,record,"新装水表管理-批量添加水表");
+            template.resolver(null,null,record,null, "新装水表管理-批量添加水表");
         }
         log.info("begin executeAsyncInstallTask!");
     }
@@ -122,15 +134,15 @@ public class AsyncTaskImportService {
         Channel channel = channelMapper.findById(sysId);
         if(StringUtils.equals("photoelectricMeter",channel.getChannelCode())){
             ResolverExcelTemplate template = new ResolverExcelTemplate(gdDeviceResolverExcelService,handleExcelService);
-            template.resolver(sysId,null,record,"设备列表-批量添加");
+            template.resolver(sysId,null,record,null,"设备列表-批量添加");
         }else {
             //判断是否是水表场景
             if (wMeterType != null) {
                 ResolverExcelTemplate template = new ResolverExcelTemplate(waterDeviceResolverExcelService,handleExcelService);
-                template.resolver(sysId,null,record,"设备列表-批量添加");
+                template.resolver(sysId,null,record,null,"设备列表-批量添加");
             } else {
                 ResolverExcelTemplate template = new ResolverExcelTemplate(generalDeviceResolverExcelService,handleExcelService);
-                template.resolver(sysId,null,record,"设备列表-批量添加");
+                template.resolver(sysId,null,record,null,"设备列表-批量添加");
 
             }
         }
@@ -141,12 +153,12 @@ public class AsyncTaskImportService {
     @Async
     public void executeAsyncBuildingTask(Import record, Integer customerId, MultipartFile file){
         ResolverExcelTemplate template = new ResolverExcelTemplate(buildingResolverExcelService,handleExcelService);
-        template.resolver(null,customerId,record,"建筑列表-批量添加");
+        template.resolver(null,customerId,record,null,"建筑列表-批量添加");
     }
     @Async
     public void executeAsyncInstallPlanTask(Import record){
         ResolverExcelTemplate template = new ResolverExcelTemplate(installPlanResolverExcelService,handleExcelService);
-        template.resolver(null,null,record,"装表计划-批量添加");
+        template.resolver(null,null,record,null,"装表计划-批量添加");
     }
 
     /**
@@ -578,17 +590,30 @@ public class AsyncTaskImportService {
     @Async
     public void executeAsyncConcentratorTask(Import record) {
         ResolverExcelTemplate template = new ResolverExcelTemplate(concentratorResolveExcelService,handleExcelService);
-        template.resolver(null,null,record,"集中器列表-批量添加");
+        template.resolver(null,null,record,null,"集中器列表-批量添加");
     }
 
     @Async
     public void executeAsyncCollectorTask(Import record) {
         ResolverExcelTemplate template = new ResolverExcelTemplate(collectorResolveExcelService,handleExcelService);
-        template.resolver(null,null,record,"采集器列表-批量添加");
+        template.resolver(null,null,record,null,"采集器列表-批量添加");
     }
 
     public void executeAsyncCommandTask(Import record) {
-        ResolverExcelTemplate template = new ResolverExcelTemplate(commandResolverExcelService,handleExcelService);
-        template.resolver(null,null,record,"批量指令下发-指令导入");
+
+        RabbitCommandParamData data = new RabbitCommandParamData();
+        data.setRecordId(record.getId());
+        data.setUsername(UserUtil.getUsername()!=null?UserUtil.getUsername():"系统操作");
+        data.setCustomerIds(UserUtil.getCustomerIds());
+
+
+        String message = JSON.toJSONString(data);
+
+        //设置当前线程lookupKey,内部由ThreadLocal实现
+        SimpleResourceHolder.bind(rabbitTemplate.getConnectionFactory(), "platform");
+        //业务操作会根据线程中的lookupKey从routeConnectionFactory的targetConnectionFactories中选择对应的connectionFactory
+        rabbitTemplate.convertAndSend(exchange,queue, message);
+        //操作完以后记得解绑。不影响线程的后序其他工厂操作
+        SimpleResourceHolder.unbind(rabbitTemplate.getConnectionFactory());
     }
 }

+ 2 - 0
smart-city-platform/src/main/resources/application-dev.properties

@@ -191,5 +191,7 @@ platform_url=http://localhost:8321/user/getUniqId?appId={appId}&key={key}
 spring.rabbitmq.exchange=task-handler-exchange
 spring.rabbitmq.download-device-queue=download-device-queue
 spring.rabbitmq.download-install-queue=download-install-queue
+spring.rabbitmq.command-task-queue=command-task-queue
+
 #sentry.dsn=http://2384bbf9e2134af9b765dc8c10a2c08a@192.168.174.133:8080/1
 #sentry.exception-resolver-order=2147483647

+ 2 - 1
smart-city-platform/src/main/resources/application-prd.properties

@@ -193,4 +193,5 @@ platform_url=http://114.135.61.187:38081/user-auth/user/getUniqId?appId={appId}&
 
 spring.rabbitmq.exchange=task-handler-exchange
 spring.rabbitmq.download-device-queue=download-device-queue
-spring.rabbitmq.download-install-queue=download-install-queue
+spring.rabbitmq.download-install-queue=download-install-queue
+spring.rabbitmq.command-task-queue=command-task-queue

+ 2 - 1
smart-city-platform/src/main/resources/application-sit.properties

@@ -190,4 +190,5 @@ platform_appSecret=12345678
 platform_url=http://114.135.61.187:38081/user-auth/user/getUniqId?appId={appId}&key={key}
 spring.rabbitmq.exchange=task-handler-exchange
 spring.rabbitmq.download-device-queue=download-device-queue
-spring.rabbitmq.download-install-queue=download-install-queue
+spring.rabbitmq.download-install-queue=download-install-queue
+spring.rabbitmq.command-task-queue=command-task-queue

BIN
smart-city-platform/src/main/resources/excel/orderDeviceTemplate20210421.xlsx


+ 7 - 0
smart-city-platform/src/main/resources/mapper/DeviceCommandMapper.xml

@@ -192,5 +192,12 @@
     select * from sc_device_command
     where status = 1 and device_id = #{deviceId}
     <if test="siteId != null"> and site_id = #{siteId}</if>
+    <if test="startDate != null"> and issue_date <![CDATA[ >= ]]> #{startDate}</if>
+    <if test="endDate != null"> and issue_date <![CDATA[ <= ]]> #{endDate}</if>
+    order by issue_date desc
+  </select>
+
+  <select id="countByDeviceId" resultType="int">
+    select count(1) from sc_device_command where status = 1 and device_id = #{deviceId} and command_status = 0
   </select>
 </mapper>

+ 1 - 0
smart-city-platform/src/main/resources/mapper/DeviceCommandTaskMapper.xml

@@ -113,5 +113,6 @@
     <if test="name != null and name != ''"> AND ct.name LIKE concat('%',#{name},'%')</if>
     <if test="commandType != null"> and c.command_type = #{commandType}</if>
     <if test="commandStatus != null"> and c.command_status = #{commandStatus}</if>
+    order by ct.date_create desc
   </select>
 </mapper>