From f92e7a801cb5e59bd2391641eccad67036513b8a Mon Sep 17 00:00:00 2001
From: zhanzhiqin <895896009@qq.com>
Date: 星期五, 15 四月 2022 15:00:14 +0800
Subject: [PATCH] fix

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightTaskService.java |  270 +++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 197 insertions(+), 73 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 0db1ff1..1433673 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,12 +1,19 @@
 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;
+import com.github.pagehelper.Page;
 import com.github.pagehelper.PageHelper;
 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;
@@ -14,18 +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
@@ -33,11 +50,10 @@
  * 璺伅浠诲姟鎿嶄綔
  */
 @Service
+@Slf4j
 @AllArgsConstructor
 public class LightTaskService extends BaseServiceImpl<LightTaskMapper, LightTask> {
 
-
-    private final LightTaskQuartzService lightTaskQuartzService;
     private final PoleService poleService;
     private final LightTaskPoleRelationService lightTaskPoleRelationService;
 
@@ -49,40 +65,79 @@
         if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) {
             throw new BusinessException("鐏帶鍛戒护鏍煎紡涓嶆纭�");
         }
+        int week = 0;
+        for (Integer w : param.getWeekList()) {
+            week |= w;
+        }
         LightTask lightTask = new LightTask();
         lightTask.setClientId(SecurityUtils.getClientId());
         lightTask.setTaskName(param.getTaskName());
-        lightTask.setWeek(param.getWeek());
+        lightTask.setWeek(week);
         lightTask.setCloseOrder(param.getCloseOrder());
         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;
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    public boolean updateLightTask(Long taskId, LightTaskParam param) {
+        if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) {
+            throw new BusinessException("鐏帶鍛戒护鏍煎紡涓嶆纭�");
+        }
+        LightTask lightTask = getById(taskId);
+        if (lightTask == null) {
+            throw new BusinessException("鎵句笉鍒拌矾鐏换鍔�");
+        }
+        int week = 0;
+        for (Integer w : param.getWeekList()) {
+            week |= w;
+        }
+        lightTask.setTaskName(param.getTaskName());
+        lightTask.setWeek(week);
+        lightTask.setControlOrder(param.getControlOrder());
+        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<Long> poleIdList = param.getPoleIdList();
+        if (CollectionUtil.isNotEmpty(poleIdList)) {
+
+            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;
     }
 
@@ -98,13 +153,10 @@
                 .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;
+        return Optional.ofNullable(responseCommonFrame).map(WrapResponseCommonFrame::getResponseInnerFrame).orElse(null);
     }
 
-    public List<LightTask> listLightTask(int pageNo, int pageSize, String keyword) {
+    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);
@@ -115,55 +167,47 @@
         }
 
         PageHelper.startPage(pageNo, pageSize);
-        return list(wrapper);
+        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) {
-        if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) {
-            throw new BusinessException("鐏帶鍛戒护鏍煎紡涓嶆纭�");
-        }
-        LightTask lightTask = getById(taskId);
-        if (lightTask == null) {
-            throw new BusinessException("鎵句笉鍒拌矾鐏换鍔�");
+    // 姣忎釜璺伅鍙戦�佹帶鍒跺抚 杩斿洖鍏宠仈鍒楄〃
+    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;
         }
 
-        lightTask.setTaskName(param.getTaskName());
-        lightTask.setWeek(param.getWeek());
-        lightTask.setControlOrder(param.getControlOrder());
-        lightTask.setOpenOrder(param.getOpenOrder());
-        lightTask.setCloseOrder(param.getCloseOrder());
-        lightTask.setUpdateTime(LocalDateTime.now());
-
-        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();
-
+        for (Pole pole : poles) {
+            LightTaskPoleRelation lightTaskPoleRelation = new LightTaskPoleRelation();
+            lightTaskPoleRelation.setPoleId(pole.getId());
+            lightTaskPoleRelation.setTaskId(lightTask.getTaskId());
             // rrpc 鍙戠敓瀹氭椂鍛戒护
-            for (String deviceCode : deviceCodeList) {
-                sendTimeRRpc(framePayload, deviceCode);
+            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());
             }
-        }
 
-        return true;
+            lightTaskPoleRelationList.add(lightTaskPoleRelation);
+        }
+        return lightTaskPoleRelationList;
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -178,16 +222,96 @@
             throw new BusinessException("鍒犻櫎浠诲姟澶辫触");
         }
 
+        boolean del;
         // 鍒犻櫎缁戝畾鐏潌
-        if (!lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getTaskId, taskIdList))) {
-            throw new BusinessException("鍒犻櫎缁戝畾鐏潌澶辫触");
+//        if (!lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).notIn(LightTaskPoleRelation::getTaskId, taskIdList))) {
+//            throw new BusinessException("鏈壘鍒颁换鍔�/鐏潌缁戝畾鍏崇郴");
+//        }
+//        lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getTaskId, taskIdList));
+        taskIdList.forEach(
+                taskId->{
+                    lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getTaskId,taskId));
+                }
+        );
+
+        return true;
+    }
+
+    /**
+     * 浠诲姟璇︽儏
+     */
+    public Map<Object, Object> detailLightTask(Long taskId) {
+        LightTask lightTask = getById(taskId);
+        if (lightTask == null) {
+            throw new BusinessException("鎵句笉鍒颁换鍔�");
+        }
+        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("璇疯缃槦鏈�");
         }
 
-        // 鍒犻櫎瀹氭椂鍣�
-        if (!lightTaskQuartzService.removeLightTaskQuartz(taskIdList)) {
-            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