From f89c8d19d96fdbff3081ba4ac48ea3e72168bc59 Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期五, 20 五月 2022 15:52:44 +0800
Subject: [PATCH] 日志

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightTaskService.java |  535 ++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 454 insertions(+), 81 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..7f487a5 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,21 @@
 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.object.BaseConditionVO;
 import com.sandu.common.service.impl.BaseServiceImpl;
+import com.sandu.common.util.SpringContextHolder;
+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 +23,32 @@
 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.StoreOperationRecordsUtils;
+import com.sandu.ximon.admin.utils.TaskOrderUtil;
+import com.sandu.ximon.dao.bo.LightTaskStatusAndPole;
+import com.sandu.ximon.dao.domain.LightEnergyData;
 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.math.BigDecimal;
 import java.time.LocalDateTime;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
 
 /**
  * @author chenjiantian
@@ -33,13 +56,15 @@
  * 璺伅浠诲姟鎿嶄綔
  */
 @Service
+@Slf4j
 @AllArgsConstructor
 public class LightTaskService extends BaseServiceImpl<LightTaskMapper, LightTask> {
 
-
-    private final LightTaskQuartzService lightTaskQuartzService;
     private final PoleService poleService;
     private final LightTaskPoleRelationService lightTaskPoleRelationService;
+    private final LightTaskMapper lightTaskMapper;
+    private final ClientService clientService;
+    private final LightEnergyDataService lightEnergyDataService;
 
     /**
      * 鏂板璺伅浠诲姟
@@ -49,73 +74,76 @@
         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());
+        if (SecurityUtils.getClientId() != null) {
+            lightTask.setClientId(clientService.getClientId());
+            lightTask.setUserId(SecurityUtils.getUserId());
+        }
         lightTask.setTaskName(param.getTaskName());
-        lightTask.setWeek(param.getWeek());
+        lightTask.setWeek(week);
+        lightTask.setLightAdress(param.getLightAddress());
         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("娣诲姞瀹氭椂浠诲姟澶辫触");
+
+
+        /**
+         * 鏂板璺伅浠诲姟鏃ュ織璁板綍寮�濮�
+         */
+        List<String> poleCodeList = new ArrayList<>();
+        if (CollectionUtil.isNotEmpty(param.getPoleIdList())) {
+            List<Pole> poleList = SpringContextHolder.getBean(PoleService.class).listByIds(param.getPoleIdList());
+            if (CollectionUtil.isNotEmpty(poleList)) {
+                poleCodeList = poleList.stream().map(Pole::getDeviceCode).collect(Collectors.toList());
+            }
         }
-        // 娣诲姞缁戝畾鐏潌
-        if (!lightTaskPoleRelationService.updateLightTaskPoleRelation(lightTask.getTaskId(), param.getPoleIdList())) {
-            throw new BusinessException("缁戝畾鐏潌澶辫触");
-        }
+        String content = "{浠诲姟ID锛�" + lightTask.getTaskId()
+                + "锛� 浠诲姟鍚嶏細" + lightTask.getTaskName()
+                + "}锛寋鍐呭抚鎸囦护" + lightTask.getFramePayload()
+                + "锛� 鐏潌ID锛�" + param.getPoleIdList().toString()
+                + "锛� 鎺у埗鐨勭伅澶村湴鍧�锛�" + param.getLightAddress()
+                + " }";
+        StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "鏂板璺伅浠诲姟", content);
+        /**
+         * 鏂板璺伅浠诲姟鏃ュ織璁板綍缁撴潫
+         */
 
         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(), param.getLightAddress());
+
+            // 娣诲姞缁戝畾鐏潌
+            if (!lightTaskPoleRelationService.saveBatch(lightTaskPoleRelationList)) {
+                throw new BusinessException("缁戝畾鐏潌澶辫触");
             }
+
+            // 涓�涓伅鏉嗗彧鑳戒娇鐢ㄤ竴涓换鍔★紝鏂颁换鍔¤瑕嗙洊鏃т换鍔�
+            lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getPoleId, poleIdList).ne(LightTaskPoleRelation::getTaskId, lightTask.getTaskId()));
+
+            /**
+             * 涓嬪彂璺伅浠诲姟鏃ュ織璁板綍寮�濮�
+             */
+
+            String content1 = "{浠诲姟ID锛�" + lightTask.getTaskId()
+                    + "锛� 浠诲姟鍚嶏細" + lightTask.getTaskName()
+                    + "}," + " 鐏潌ID锛�" + param.getPoleIdList().toString()
+                    + " }";
+            StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "涓嬪彂璺伅浠诲姟", content1);
+            /**
+             * 涓嬪彂璺伅浠诲姟鏃ュ織璁板綍缁撴潫
+             */
         }
         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<LightTask> 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);
-        return list(wrapper);
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -128,42 +156,240 @@
             throw new BusinessException("鎵句笉鍒拌矾鐏换鍔�");
         }
 
-        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)) {
+        //璁板綍浠诲姟缂栬緫鍣ㄥ墠鐏潌ID
+        List<LightTaskStatusAndPole> oldLightTaskStatusAndPoles = lightTaskPoleRelationService.listPoleAndStatusIdByTaskId(taskId);
+
+
+        int week = 0;
+        for (Integer w : param.getWeekList()) {
+            week |= w;
+        }
+
+        LightTask newLightTask = new LightTask();
+        newLightTask.setTaskName(param.getTaskName());
+        if (SecurityUtils.getClientId() != null) {
+            lightTask.setClientId(clientService.getClientId());
+            lightTask.setUserId(SecurityUtils.getUserId());
+        }
+        newLightTask.setWeek(week);
+        newLightTask.setCreateUser(SecurityUtils.getUsername());
+        newLightTask.setControlOrder(param.getControlOrder());
+        newLightTask.setOpenOrder(param.getOpenOrder());
+        newLightTask.setCloseOrder(param.getCloseOrder());
+        newLightTask.setLightAdress(param.getLightAddress());
+        newLightTask.setUpdateTime(LocalDateTime.now());
+        newLightTask.setFramePayload(buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week));
+
+        if (!save(newLightTask)) {
             throw new BusinessException("缂栬緫浠诲姟澶辫触");
         }
 
-        List<LightTaskQuartz> list = lightTaskQuartzService.updateLightTaskQuartz(lightTask);
-        if (CollectionUtil.isEmpty(list)) {
-            throw new BusinessException("缂栬緫瀹氭椂浠诲姟澶辫触");
+        /**
+         * 缂栬緫璺伅浠诲姟鏃ュ織璁板綍寮�濮�
+         */
+        List<String> poleCodeList = new ArrayList<>();
+        if (CollectionUtil.isNotEmpty(param.getPoleIdList())) {
+            List<Pole> poleList = SpringContextHolder.getBean(PoleService.class).listByIds(param.getPoleIdList());
+            if (CollectionUtil.isNotEmpty(poleList)) {
+                poleCodeList = poleList.stream().map(Pole::getDeviceCode).collect(Collectors.toList());
+            }
         }
+        String content = "{浠诲姟ID锛�" + newLightTask.getTaskId()
+                + "锛� 浠诲姟鍚嶏細" + newLightTask.getTaskName()
+                + "}锛寋鍐呭抚鎸囦护" + newLightTask.getFramePayload()
+                + "锛� 鐏潌ID锛�" + param.getPoleIdList().toString()
+                + "锛� 鎺у埗鐨勭伅澶村湴鍧�锛�" + param.getLightAddress()
+                + " }";
+        StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "缂栬緫璺伅浠诲姟", content);
+        /**
+         * 缂栬緫璺伅浠诲姟鏃ュ織璁板綍缁撴潫
+         */
 
-        // 娣诲姞缁戝畾鐏潌
-        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();
+            List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(newLightTask, poleIdList, lightTask.getFramePayload(), param.getLightAddress());
+            //鐘舵�佷笉涓�0 鏂板缓浠诲姟
+            lightTaskPoleRelationList.forEach(
+                    taskResult -> {
+                        //淇濆瓨涓嬪彂澶辫触鐨勪换鍔″叧鑱斿叧绯伙紝鐘舵�佷负O鎴愬姛锛屽叾浠栫姸鎬侀兘涓哄け璐�
+                        if (taskResult.getIssueStatus() == 0) {
+                            for (LightTaskStatusAndPole bean : oldLightTaskStatusAndPoles) {
+                                if (bean.getId().equals(taskResult.getPoleId())) {
+                                    oldLightTaskStatusAndPoles.remove(bean);
+                                    break;
+                                }
+                            }
+                        }
+                    }
+            );
+            //淇敼鐏潌浠诲姟缁戝畾鍏崇郴琛�
+            for (LightTaskPoleRelation bean : lightTaskPoleRelationList) {
+                //鎴愬姛鎵嶄慨鏀癸紝澶辫触涓嶅彉
+                if (bean.getIssueStatus() == 0) {
 
-            // rrpc 鍙戠敓瀹氭椂鍛戒护
-            for (String deviceCode : deviceCodeList) {
-                sendTimeRRpc(framePayload, deviceCode);
+                    LightTaskPoleRelation one = lightTaskPoleRelationService.getOne(Wrappers.lambdaQuery(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getPoleId, bean.getPoleId()));
+                    if (one != null) {
+                        one.setTaskId(newLightTask.getTaskId());
+                        lightTaskPoleRelationService.updateById(one);
+                    } else {
+                        lightTaskPoleRelationService.save(bean);
+                    }
+                }
             }
+
+            List<Long> failPoleId = new ArrayList<>();
+            for (LightTaskStatusAndPole bean1 : oldLightTaskStatusAndPoles) {
+                failPoleId.add(bean1.getId());
+            }
+            //鎵ц鍏崇伅
+            String framePayloadClose = "7f0000007f173b00";
+
+            List<LightTaskPoleRelation> lightTaskPoleRelations = new ArrayList<>();
+            if (!failPoleId.isEmpty()) {
+                lightTaskPoleRelations = sendControllerFrame(lightTask, failPoleId, framePayloadClose, lightTask.getLightAdress());
+            }
+
+            //鐘舵�佷笉涓�0 鏂板缓浠诲姟
+            lightTaskPoleRelations.forEach(
+                    taskResult -> {
+                        //淇濆瓨涓嬪彂澶辫触鐨勪换鍔″叧鑱斿叧绯伙紝鐘舵�佷负O鎴愬姛锛屽叾浠栫姸鎬侀兘涓哄け璐�
+                        if (taskResult.getIssueStatus() == 0) {
+                            for (LightTaskStatusAndPole bean : oldLightTaskStatusAndPoles) {
+                                if (bean.getId().equals(taskResult.getPoleId())) {
+                                    oldLightTaskStatusAndPoles.remove(bean);
+                                    lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class)
+                                            .eq(LightTaskPoleRelation::getTaskId, lightTask.getTaskId()).eq(LightTaskPoleRelation::getPoleId, bean.getId()));
+                                    break;
+                                }
+                            }
+                        }
+                    }
+            );
+
+
+            //浠诲姟椤哄埄瀹屾垚
+            if (oldLightTaskStatusAndPoles.size() == 0) {
+                removeById(lightTask);
+            }
+
+
+//            // 娣诲姞缁戝畾鐏潌
+//            if (!lightTaskPoleRelationService.saveBatch(lightTaskPoleRelationList)) {
+//                throw new BusinessException("缁戝畾鐏潌澶辫触");
+//            }
+//
+//            // 涓�涓伅鏉嗗彧鑳戒娇鐢ㄤ竴涓换鍔★紝鏂颁换鍔¤瑕嗙洊鏃т换鍔�
+//            lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getPoleId, poleIdList).ne(LightTaskPoleRelation::getTaskId, lightTask.getTaskId()));
+
+            /**
+             * 涓嬪彂璺伅浠诲姟鏃ュ織璁板綍寮�濮�
+             */
+
+            String content1 = "{浠诲姟ID锛�" + lightTask.getTaskId()
+                    + "锛� 浠诲姟鍚嶏細" + lightTask.getTaskName()
+                    + "}," + " 鐏潌ID锛�" + param.getPoleIdList().toString()
+                    + " }";
+            StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "涓嬪彂璺伅浠诲姟", content1);
+            /**
+             * 涓嬪彂璺伅浠诲姟鏃ュ織璁板綍缁撴潫
+             */
         }
 
         return true;
+    }
+
+    /**
+     * 鍙戦�佺伅鎺ц姹�
+     *
+     * @param framePayload 鐏帶鍙傛暟
+     * @param deviceCode   璁惧鍚�
+     * @return 杩斿洖甯�
+     */
+    public A5LightTimerRespInnerFrame sendTimeRRpc(String framePayload, String deviceCode, String lightAddress) {
+        IRequestFrame requestFrame = FrameBuilder.builderA5().innerFrame(new A5LightTimerReqInnerFrame(framePayload, lightAddress))
+                .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(BaseConditionVO conditionVO, String keyword) {
+        LambdaQueryWrapper<LightTask> wrapper = Wrappers.lambdaQuery(LightTask.class);
+        if (StrUtil.isNotBlank(keyword)) {
+            wrapper.like(LightTask::getTaskName, keyword);
+        }
+        //涓嶆槸瓒呯闇�瑕佺瓫閫�
+        if (SecurityUtils.getClientId() != null) {
+            wrapper.eq(LightTask::getClientId, SecurityUtils.getUserId()).or(w -> {
+                w.eq(LightTask::getUserId, SecurityUtils.getUserId());
+            });
+        }
+
+        if (conditionVO != null) {
+            PageHelper.startPage(conditionVO.getPageNo(), conditionVO.getPageSize());
+        }
+        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;
+    }
+
+    /**
+     * 鎵ц涓殑璺伅浠诲姟
+     *
+     * @return
+     */
+    public List<LightTaskDto> listTask() {
+        Long clientId = SecurityUtils.getClientId();
+        List<LightTask> lightTasks = lightTaskMapper.listLightTask(clientId);
+        Page<LightTaskDto> page = new Page<>();
+        BeanUtils.copyProperties(lightTasks, page);
+        for (LightTask lightTask : lightTasks) {
+            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, String lightAddress) {
+        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(), lightAddress);
+                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)
@@ -173,21 +399,168 @@
             throw new BusinessException("鎵句笉鍒颁换鍔′俊鎭�");
         }
 
+        List<LightTaskPoleRelation> list = lightTaskPoleRelationService.list(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getTaskId, taskIdList));
+        if (list.size() != 0) {
+            throw new BusinessException("鍒犻櫎浠诲姟澶辫触锛屽垹闄ょ殑浠诲姟鏈夌伅鏉嗘鍦ㄤ娇鐢�");
+        }
+
         // 鍒犻櫎浠诲姟
         if (!removeByIds(taskIdList)) {
             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));
+                }
+        );
+
+        /**
+         * 鍒犻櫎鎺х伅浠诲姟鏃ュ織璁板綍寮�濮�
+         */
+        String content = "{鎺х伅浠诲姟id锛�" + taskIdList + " }";
+        StoreOperationRecordsUtils.storeOperationData(null, null, "鍒犻櫎鎺х伅浠诲姟", content);
+        /**
+         * 鍒犻櫎鎺х伅浠诲姟鏃ュ織璁板綍缁撴潫
+         */
+        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, lightTask.getLightAdress());
+
+        /**
+         * 涓嬪彂璺伅浠诲姟鏃ュ織璁板綍寮�濮�
+         */
+        List<String> poleCodeList = new ArrayList<>();
+        List<Pole> list = SpringContextHolder.getBean(PoleService.class).list(Wrappers.lambdaQuery(Pole.class).eq(Pole::getId, param.getPoleId()));
+        if (CollectionUtil.isNotEmpty(list)) {
+            poleCodeList = list.stream().map(Pole::getDeviceCode).collect(Collectors.toList());
+        }
+        String content = "{浠诲姟ID锛�" + lightTask.getTaskId()
+                + "锛� 浠诲姟鍚嶏細" + lightTask.getTaskName()
+                + "}," + " 鐏潌ID锛�" + param.getPoleId()
+                + " }";
+        StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "涓嬪彂璺伅浠诲姟", content);
+        /**
+         * 涓嬪彂璺伅浠诲姟鏃ュ織璁板綍缁撴潫
+         */
+
+
+        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;
     }
+
+
+    //璁$畻鍔熺巼鍙婅兘鑰�
+    public void energy() {
+        List<LightTask> lightTasks = list(Wrappers.lambdaQuery(LightTask.class));
+        for (LightTask lightTask : lightTasks) {
+            List<LightTaskPoleRelation> list = SpringContextHolder.getBean(LightTaskPoleRelationService.class)
+                    .list(Wrappers.lambdaQuery(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getTaskId, lightTask.getTaskId()));
+
+            LightTaskDto lightTaskDto = new LightTaskDto();
+            BeanUtils.copyProperties(lightTask, lightTaskDto);
+            lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(lightTask.getWeek()));
+
+            BigDecimal energySaving = SpringContextHolder.getBean(LightService.class).jisuan(lightTaskDto);
+            BigDecimal energy = SpringContextHolder.getBean(LightService.class).jisuanEnergy(lightTaskDto);
+
+            //鑾峰彇鏄ㄥぉ鐨勬槦鏈熸暟
+            LocalDateTime now = LocalDateTime.now();
+            LocalDateTime yesterday = now.minusDays(1);
+
+            LightEnergyData lightEnergyData = new LightEnergyData();
+            lightEnergyData.setTaskId(lightTask.getTaskId());
+            lightEnergyData.setYtdTime(yesterday);
+            lightEnergyData.setEnergySaving(energySaving);
+            lightEnergyData.setEnergy(energy);
+            lightEnergyData.setUserId(lightTask.getUserId());
+            lightEnergyData.setClientId(lightTask.getClientId());
+
+            System.out.println(lightEnergyData + "---------------");
+            //ID   浠诲姟id
+        }
+    }
+
 }

--
Gitblit v1.9.3