From c751933819729c429a16353dced879bdb8615395 Mon Sep 17 00:00:00 2001
From: liuhaonan <konodioda2333@vip.qq.com>
Date: 星期一, 21 三月 2022 15:06:33 +0800
Subject: [PATCH] LED熙讯---任务详情图片路径

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightTaskService.java |  263 ++++++++++++++++++++++++++++++++++------------------
 1 files changed, 172 insertions(+), 91 deletions(-)

diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightTaskService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightTaskService.java
index 10b12cc..d17178b 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightTaskService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightTaskService.java
@@ -1,7 +1,10 @@
 package com.sandu.ximon.admin.service;
 
 import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.collection.ListUtil;
 import cn.hutool.core.map.MapUtil;
+import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.core.util.HexUtil;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -10,6 +13,7 @@
 import com.sandu.common.execption.BusinessException;
 import com.sandu.common.service.impl.BaseServiceImpl;
 import com.sandu.ximon.admin.dto.LightTaskDto;
+import com.sandu.ximon.admin.dto.SingleLightOrderDto;
 import com.sandu.ximon.admin.manager.iot.frame.FrameBuilder;
 import com.sandu.ximon.admin.manager.iot.frame.IRequestFrame;
 import com.sandu.ximon.admin.manager.iot.frame.inner.request.A5LightTimerReqInnerFrame;
@@ -17,22 +21,28 @@
 import com.sandu.ximon.admin.manager.iot.rrpc.dto.WrapResponseCommonFrame;
 import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5OrderEnum;
 import com.sandu.ximon.admin.manager.iot.rrpc.mainboard.MainBoardInvokeSyncService;
+import com.sandu.ximon.admin.manager.iot.rrpc.util.SupplementUtils;
+import com.sandu.ximon.admin.param.LightTaskIssueParam;
 import com.sandu.ximon.admin.param.LightTaskParam;
 import com.sandu.ximon.admin.security.SecurityUtils;
 import com.sandu.ximon.admin.utils.TaskOrderUtil;
+import com.sandu.ximon.dao.bo.LightTaskStatusAndPole;
 import com.sandu.ximon.dao.domain.LightTask;
 import com.sandu.ximon.dao.domain.LightTaskPoleRelation;
-import com.sandu.ximon.dao.domain.LightTaskQuartz;
 import com.sandu.ximon.dao.domain.Pole;
+import com.sandu.ximon.dao.enums.DeviceRespStatusEnums;
 import com.sandu.ximon.dao.mapper.LightTaskMapper;
 import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.time.LocalDateTime;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 
 /**
  * @author chenjiantian
@@ -40,14 +50,12 @@
  * 璺伅浠诲姟鎿嶄綔
  */
 @Service
+@Slf4j
 @AllArgsConstructor
 public class LightTaskService extends BaseServiceImpl<LightTaskMapper, LightTask> {
 
-
-    private final LightTaskQuartzService lightTaskQuartzService;
     private final PoleService poleService;
     private final LightTaskPoleRelationService lightTaskPoleRelationService;
-
 
     /**
      * 鏂板璺伅浠诲姟
@@ -69,77 +77,26 @@
         lightTask.setOpenOrder(param.getOpenOrder());
         lightTask.setControlOrder(param.getControlOrder());
         lightTask.setCreateUser(SecurityUtils.getUsername());
+        lightTask.setFramePayload(buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week));
         if (!save(lightTask)) {
             throw new BusinessException("淇濆瓨璺伅浠诲姟澶辫触");
-        }
-        List<LightTaskQuartz> list = lightTaskQuartzService.addLightTaskQuartz(lightTask);
-        if (CollectionUtil.isEmpty(list)) {
-            throw new BusinessException("娣诲姞瀹氭椂浠诲姟澶辫触");
-        }
-        // 娣诲姞缁戝畾鐏潌
-        if (!lightTaskPoleRelationService.updateLightTaskPoleRelation(lightTask.getTaskId(), param.getPoleIdList())) {
-            throw new BusinessException("缁戝畾鐏潌澶辫触");
         }
 
         List<Long> poleIdList = param.getPoleIdList();
         if (CollectionUtil.isNotEmpty(poleIdList)) {
-            List<String> deviceCodeList = poleService.listDeviceCodeByIds(poleIdList);
-            if (CollectionUtil.isEmpty(deviceCodeList)) {
-                throw new BusinessException("鎵句笉鍒扮伅鏉唌ac锛屾棤娉曞彂閫佸畾鏃朵换鍔�");
-            }
-            // 鑾峰彇瀹氭椂鐏帶鍛戒护
-            String framePayload = list.get(0).getFramePayload();
 
-            // rrpc 鍙戠敓瀹氭椂鍛戒护
-            for (String deviceCode : deviceCodeList) {
-                sendTimeRRpc(framePayload, deviceCode);
+            List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, poleIdList, lightTask.getFramePayload());
+
+            // 娣诲姞缁戝畾鐏潌
+            if (!lightTaskPoleRelationService.saveBatch(lightTaskPoleRelationList)) {
+                throw new BusinessException("缁戝畾鐏潌澶辫触");
             }
+
+            // 涓�涓伅鏉嗗彧鑳戒娇鐢ㄤ竴涓换鍔★紝鏂颁换鍔¤瑕嗙洊鏃т换鍔�
+            lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getPoleId, poleIdList).ne(LightTaskPoleRelation::getTaskId, lightTask.getTaskId()));
         }
         return true;
     }
-
-    /**
-     * 鍙戦�佺伅鎺ц姹�
-     *
-     * @param framePayload 鐏帶鍙傛暟
-     * @param deviceCode   璁惧鍚�
-     * @return 杩斿洖甯�
-     */
-    public A5LightTimerRespInnerFrame sendTimeRRpc(String framePayload, String deviceCode) {
-        IRequestFrame requestFrame = FrameBuilder.builderA5().innerFrame(new A5LightTimerReqInnerFrame(framePayload))
-                .orderType(A5OrderEnum.REQUEST_LIGHT_DATA.getCode()).build();
-        WrapResponseCommonFrame<A5LightTimerRespInnerFrame> responseCommonFrame = MainBoardInvokeSyncService.getInstance()
-                .sendRRPC(deviceCode, requestFrame, A5LightTimerRespInnerFrame.class);
-        System.out.println("鍙戦�佺粨鏋�");
-        A5LightTimerRespInnerFrame responseInnerFrame = responseCommonFrame.getResponseInnerFrame();
-        System.out.println(responseInnerFrame.getResponseStatus());
-        return responseInnerFrame;
-    }
-
-    public List<LightTaskDto> listLightTask(int pageNo, int pageSize, String keyword) {
-        LambdaQueryWrapper<LightTask> wrapper = Wrappers.lambdaQuery(LightTask.class);
-        if (StrUtil.isNotBlank(keyword)) {
-            wrapper.like(LightTask::getTaskName, keyword);
-        }
-        Long clientId = SecurityUtils.getClientId();
-        if (clientId != null) {
-            wrapper.eq(LightTask::getClientId, clientId);
-        }
-
-        PageHelper.startPage(pageNo, pageSize);
-        List<LightTask> list = list(wrapper);
-
-        Page<LightTaskDto> page = new Page<>();
-        BeanUtils.copyProperties(list,page);
-        for (LightTask lightTask : list) {
-            LightTaskDto lightTaskDto = new LightTaskDto();
-            BeanUtils.copyProperties(lightTask,lightTaskDto);
-            lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(lightTask.getWeek()));
-            page.add(lightTaskDto);
-        }
-        return page;
-    }
-
 
     @Transactional(rollbackFor = Exception.class)
     public boolean updateLightTask(Long taskId, LightTaskParam param) {
@@ -160,36 +117,97 @@
         lightTask.setOpenOrder(param.getOpenOrder());
         lightTask.setCloseOrder(param.getCloseOrder());
         lightTask.setUpdateTime(LocalDateTime.now());
+        lightTask.setFramePayload(buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week));
 
         if (!updateById(lightTask)) {
             throw new BusinessException("缂栬緫浠诲姟澶辫触");
         }
 
-        List<LightTaskQuartz> list = lightTaskQuartzService.updateLightTaskQuartz(lightTask);
-        if (CollectionUtil.isEmpty(list)) {
-            throw new BusinessException("缂栬緫瀹氭椂浠诲姟澶辫触");
-        }
-
-        // 娣诲姞缁戝畾鐏潌
-        if (!lightTaskPoleRelationService.updateLightTaskPoleRelation(lightTask.getTaskId(), param.getPoleIdList())) {
-            throw new BusinessException("缁戝畾鐏潌澶辫触");
-        }
         List<Long> poleIdList = param.getPoleIdList();
         if (CollectionUtil.isNotEmpty(poleIdList)) {
-            List<String> deviceCodeList = poleService.listDeviceCodeByIds(poleIdList);
-            if (CollectionUtil.isEmpty(deviceCodeList)) {
-                throw new BusinessException("鎵句笉鍒扮伅鏉唌ac锛屾棤娉曞彂閫佸畾鏃朵换鍔�");
-            }
-            // 鑾峰彇瀹氭椂鐏帶鍛戒护
-            String framePayload = list.get(0).getFramePayload();
 
-            // rrpc 鍙戠敓瀹氭椂鍛戒护
-            for (String deviceCode : deviceCodeList) {
-                sendTimeRRpc(framePayload, deviceCode);
+            List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, poleIdList, lightTask.getFramePayload());
+
+            lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getTaskId, lightTask.getTaskId()));
+            // 娣诲姞缁戝畾鐏潌
+            if (!lightTaskPoleRelationService.saveBatch(lightTaskPoleRelationList)) {
+                throw new BusinessException("缁戝畾鐏潌澶辫触");
             }
+
+            // 涓�涓伅鏉嗗彧鑳戒娇鐢ㄤ竴涓换鍔★紝鏂颁换鍔¤瑕嗙洊鏃т换鍔�
+            lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getPoleId, poleIdList).ne(LightTaskPoleRelation::getTaskId, lightTask.getTaskId()));
         }
 
         return true;
+    }
+
+    /**
+     * 鍙戦�佺伅鎺ц姹�
+     *
+     * @param framePayload 鐏帶鍙傛暟
+     * @param deviceCode   璁惧鍚�
+     * @return 杩斿洖甯�
+     */
+    public A5LightTimerRespInnerFrame sendTimeRRpc(String framePayload, String deviceCode) {
+        IRequestFrame requestFrame = FrameBuilder.builderA5().innerFrame(new A5LightTimerReqInnerFrame(framePayload))
+                .orderType(A5OrderEnum.REQUEST_LIGHT_DATA.getCode()).build();
+        WrapResponseCommonFrame<A5LightTimerRespInnerFrame> responseCommonFrame = MainBoardInvokeSyncService.getInstance()
+                .sendRRPC(deviceCode, requestFrame, A5LightTimerRespInnerFrame.class);
+        return Optional.ofNullable(responseCommonFrame).map(WrapResponseCommonFrame::getResponseInnerFrame).orElse(null);
+    }
+
+    public List<LightTaskDto> listLightTask(int pageNo, int pageSize, String keyword) {
+        LambdaQueryWrapper<LightTask> wrapper = Wrappers.lambdaQuery(LightTask.class);
+        if (StrUtil.isNotBlank(keyword)) {
+            wrapper.like(LightTask::getTaskName, keyword);
+        }
+        Long clientId = SecurityUtils.getClientId();
+        if (clientId != null) {
+            wrapper.eq(LightTask::getClientId, clientId);
+        }
+
+        PageHelper.startPage(pageNo, pageSize);
+        List<LightTask> list = list(wrapper);
+
+        Page<LightTaskDto> page = new Page<>();
+        BeanUtils.copyProperties(list, page);
+        for (LightTask lightTask : list) {
+            LightTaskDto lightTaskDto = new LightTaskDto();
+            BeanUtils.copyProperties(lightTask, lightTaskDto);
+            lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(lightTask.getWeek()));
+            page.add(lightTaskDto);
+        }
+        return page;
+    }
+
+    // 姣忎釜璺伅鍙戦�佹帶鍒跺抚 杩斿洖鍏宠仈鍒楄〃
+    private List<LightTaskPoleRelation> sendControllerFrame(LightTask lightTask, List<Long> poleIdList, String framePayload) {
+        List<LightTaskPoleRelation> lightTaskPoleRelationList = new ArrayList<>();
+
+        List<Pole> poles = poleService.listByIds(poleIdList);
+        if (CollectionUtil.isEmpty(poles)) {
+            return null;
+        }
+
+        for (Pole pole : poles) {
+            LightTaskPoleRelation lightTaskPoleRelation = new LightTaskPoleRelation();
+            lightTaskPoleRelation.setPoleId(pole.getId());
+            lightTaskPoleRelation.setTaskId(lightTask.getTaskId());
+            // rrpc 鍙戠敓瀹氭椂鍛戒护
+            try {
+                A5LightTimerRespInnerFrame a5LightTimerRespInnerFrame = sendTimeRRpc(framePayload, pole.getDeviceCode());
+                if (a5LightTimerRespInnerFrame == null) {
+                    lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode());
+                } else {
+                    lightTaskPoleRelation.setIssueStatus(HexUtil.hexToInt(a5LightTimerRespInnerFrame.getResponseStatus()));
+                }
+            } catch (BusinessException e) {
+                lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode());
+            }
+
+            lightTaskPoleRelationList.add(lightTaskPoleRelation);
+        }
+        return lightTaskPoleRelationList;
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -209,21 +227,84 @@
             throw new BusinessException("鍒犻櫎缁戝畾鐏潌澶辫触");
         }
 
-        // 鍒犻櫎瀹氭椂鍣�
-        if (!lightTaskQuartzService.removeLightTaskQuartz(taskIdList)) {
-            throw new BusinessException("鍒犻櫎瀹氭椂鍣ㄥけ璐�");
-        }
-
         return true;
     }
 
+    /**
+     * 浠诲姟璇︽儏
+     */
     public Map<Object, Object> detailLightTask(Long taskId) {
         LightTask lightTask = getById(taskId);
-        if(lightTask == null){
+        if (lightTask == null) {
             throw new BusinessException("鎵句笉鍒颁换鍔�");
         }
-        List<Long> poleIdList = lightTaskPoleRelationService.listPoleIdByTaskId(taskId);
-        List<Pole> poles = poleService.listByIds(poleIdList);
-        return MapUtil.builder().put("task",lightTask).put("poles",poles).build();
+        List<LightTaskStatusAndPole> lightTaskStatusAndPoles = lightTaskPoleRelationService.listPoleAndStatusIdByTaskId(taskId);
+        return MapUtil.builder().put("task", lightTask).put("poles", lightTaskStatusAndPoles).build();
+    }
+
+    /**
+     * 鏍规嵁浠诲姟淇℃伅杞垚甯ц礋鑽�
+     *
+     * @return 璺伅瀹氭椂鍣ㄥ垪琛�
+     */
+    private String buildControlFramePayload(String openOrder, String closeOrder, String controlOrder, Integer week) {
+
+        // 娣诲姞寮�鐏畾鏃跺櫒
+        SingleLightOrderDto openLightOrder = TaskOrderUtil.parseLightOrder(openOrder);
+        SingleLightOrderDto closeLightOrder = TaskOrderUtil.parseLightOrder(closeOrder);
+        String[] controlOrderArray = StrUtil.split(controlOrder, LightTaskParam.REQUEST_ORDER_LENGTH);
+        Integer[] weekArrays = TaskOrderUtil.parseLightCronWeek2List(week).toArray(new Integer[0]);
+        if (openLightOrder == null || closeLightOrder == null) {
+            throw new BusinessException("瑙f瀽鍗曠伅鍛戒护澶辫触");
+        }
+        if (ArrayUtil.isEmpty(weekArrays)) {
+            throw new BusinessException("璇疯缃槦鏈�");
+        }
+
+        StringBuilder sb = new StringBuilder();
+        String weekHex = SupplementUtils.suppleZero(HexUtil.toHex(week), 2);
+
+        // 鎷兼帴寮�鐏懡浠�
+        sb.append(weekHex);
+        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(openLightOrder.getHour()), 2));
+        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(openLightOrder.getMinute()), 2));
+        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(openLightOrder.getBrightness()), 2));
+
+        // 鎷兼帴鍏崇伅鍛戒护
+        sb.append(weekHex);
+        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(closeLightOrder.getHour()), 2));
+        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(closeLightOrder.getMinute()), 2));
+        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(closeLightOrder.getBrightness()), 2));
+
+        // 鎷兼帴浜害鎺х伅鍛戒护
+        for (String controlOrderStr : controlOrderArray) {
+            SingleLightOrderDto controlLightOrder = TaskOrderUtil.parseLightOrder(controlOrderStr);
+            if (controlLightOrder != null) {
+                sb.append(weekHex);
+                sb.append(SupplementUtils.suppleZero(HexUtil.toHex(controlLightOrder.getHour()), 2));
+                sb.append(SupplementUtils.suppleZero(HexUtil.toHex(controlLightOrder.getMinute()), 2));
+                sb.append(SupplementUtils.suppleZero(HexUtil.toHex(controlLightOrder.getBrightness()), 2));
+            }
+        }
+
+        return sb.toString();
+    }
+
+    /**
+     * 涓嬪彂鍗曚釜鐏潌鐨勪换鍔�
+     */
+    public boolean issueLightTask(LightTaskIssueParam param) {
+        LightTask lightTask = getById(param.getTaskId());
+        if (lightTask == null) {
+            throw new BusinessException("鎵句笉鍒颁换鍔�");
+        }
+        String framePayload = buildControlFramePayload(lightTask.getOpenOrder(), lightTask.getCloseOrder(), lightTask.getControlOrder(), lightTask.getWeek());
+        List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, ListUtil.toList(param.getPoleId()), framePayload);
+        if (CollectionUtil.isNotEmpty(lightTaskPoleRelationList)) {
+            return lightTaskPoleRelationService.update(lightTaskPoleRelationList.get(0),
+                    Wrappers.lambdaUpdate(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getPoleId, param.getPoleId())
+                            .eq(LightTaskPoleRelation::getTaskId, param.getTaskId()));
+        }
+        return true;
     }
 }

--
Gitblit v1.9.3