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/LightService.java |  820 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 788 insertions(+), 32 deletions(-)

diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java
index 6469109..d4e9300 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java
@@ -1,14 +1,17 @@
 package com.sandu.ximon.admin.service;
 
 import cn.hutool.core.collection.CollectionUtil;
-import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.HexUtil;
 import cn.hutool.core.util.StrUtil;
+import com.aliyuncs.iot.model.v20180120.BatchGetDeviceStateResponse;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.pagehelper.PageHelper;
+import com.sandu.common.domain.CommonPage;
 import com.sandu.common.execption.BusinessException;
 import com.sandu.common.redis.RedisService;
 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.manager.iot.frame.A5Frame;
 import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5LightHeartbeatReportInnerFrame;
 import com.sandu.ximon.admin.manager.iot.frame.inner.request.A5LightBrightnessReqInnerFrame;
@@ -17,20 +20,30 @@
 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.param.LightControlParam;
+import com.sandu.ximon.admin.param.LightPowerSettingParam;
 import com.sandu.ximon.admin.param.LightRemarkParam;
 import com.sandu.ximon.admin.redis.LightKey;
 import com.sandu.ximon.admin.security.SecurityUtils;
+import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
+import com.sandu.ximon.admin.vo.ControlLightCommandVO;
+import com.sandu.ximon.admin.vo.EquipmentInfomation;
 import com.sandu.ximon.dao.bo.LightBo;
+import com.sandu.ximon.dao.bo.PoleTaskLightPowerBo;
 import com.sandu.ximon.dao.domain.Light;
 import com.sandu.ximon.dao.domain.LightReportData;
 import com.sandu.ximon.dao.domain.Pole;
 import com.sandu.ximon.dao.domain.PoleBinding;
 import com.sandu.ximon.dao.enums.DeviceRespStatusEnums;
 import com.sandu.ximon.dao.mapper.LightMapper;
+import com.sandu.ximon.dao.mapper.LightTaskMapper;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
+import java.math.BigDecimal;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -93,23 +106,47 @@
 
         // 鑾峰彇鏈�杩戠殑涓婃姤鏃堕棿
         List<String> deviceCodeList = listLight.stream().map(Light::getDeviceCode).collect(Collectors.toList());
+        //鎷嗗垎list
+        List<List<String>> split = CollectionUtil.split(deviceCodeList, 100);
+
+        List<BatchGetDeviceStateResponse.DeviceStatus> deviceStatuses = null;
+        for (List<String> splist : split) {
+            deviceStatuses = MainBoardInvokeSyncService.getInstance().batchGetDeviceState(splist);
+            if (deviceStatuses != null) {
+                for (LightBo lightBo : listLight) {
+                    for (BatchGetDeviceStateResponse.DeviceStatus deviceStatus : deviceStatuses) {
+
+                        if (lightBo.getDeviceCode() != null && lightBo.getDeviceCode().equals(deviceStatus.getDeviceName())) {
+                            if ("ONLINE".equals(deviceStatus.getStatus())) {
+                                lightBo.setOnlineStatus(0);
+                            } else if ("OFFLINE".equals(deviceStatus.getStatus())) {
+                                lightBo.setOnlineStatus(1);
+                            } else {
+                                lightBo.setOnlineStatus(2);
+                            }
+
+                        }
+                    }
+
+                }
+            }
+        }
         if (CollectionUtil.isNotEmpty(deviceCodeList)) {
             List<LightReportData> reportDataList = lightReportDataService.getNewestReportByDeviceCode(deviceCodeList);
             for (LightBo lightBo : listLight) {
                 deviceCodeList.forEach(code -> {
-                            PoleBinding bind = bindingService.getPoleIdByMac(code);
-                            if (bind != null && lightBo.getDeviceCode().equals(bind.getDeviceCode())) {
-                                Long poleId = bind.getPoleId();
-                                Pole pole = poleService.getById(poleId);
-                                lightBo.setPoleId(pole.getId());
-                                lightBo.setPoleCode(pole.getDeviceCode());
-                                lightBo.setPoleName(pole.getPoleName());
-                            }
-                        }
-                );
+                    PoleBinding bind = bindingService.getPoleIdByMac(code);
+                    if (bind != null && lightBo.getDeviceCode().equals(bind.getDeviceCode())) {
+                        Long poleId = bind.getPoleId();
+                        Pole pole = poleService.getById(poleId);
+                        lightBo.setPoleId(pole.getId());
+                        lightBo.setPoleCode(pole.getDeviceCode());
+                        lightBo.setPoleName(pole.getPoleName());
+                    }
+                });
                 for (LightReportData lightReportData : reportDataList) {
                     if (StrUtil.equals(lightBo.getDeviceCode(), lightReportData.getDeviceCode())) {
-                        lightBo.setReportTime(lightReportData.getCreateTime());
+                        lightBo.setReportTime(lightReportData.getCreateTime1());
                         break;
                     }
                 }
@@ -136,6 +173,33 @@
         return one;
     }
 
+    /**
+     * 棣栭〉鐏潌缁戝畾淇℃伅
+     *
+     * @param deviceCode
+     * @return
+     */
+    public EquipmentInfomation getLightInfo(String deviceCode) {
+        EquipmentInfomation equipmentInfo = new EquipmentInfomation();
+        equipmentInfo.setEquipmentType("鍗曠伅");
+        if (deviceCode == null || deviceCode.trim().length() == 0) {
+            return equipmentInfo;
+        }
+        Light one = getOne(Wrappers.<Light>lambdaQuery().eq(Light::getDeviceCode, deviceCode));
+        if (one != null) {
+            equipmentInfo.setEquipmentMac(one.getDeviceCode());
+            equipmentInfo.setEquipmentCreateTime(one.getCreateTime());
+        }
+
+        Object o = redisService.get(LightKey.REPORT_MAC.key(deviceCode));
+        if (o != null) {
+            equipmentInfo.setEquipmentState("鍦ㄧ嚎");
+        } else {
+            equipmentInfo.setEquipmentState("绂荤嚎");
+        }
+        return equipmentInfo;
+    }
+
     public boolean addRemark(LightRemarkParam param) {
         Light light = getById(param.getLightId());
         if (light == null) {
@@ -159,13 +223,16 @@
 
         List<Map<String, Object>> resultList = new ArrayList<>();
         for (LightControlParam param : paramList) {
-            A5LightBrightnessReqInnerFrame lightControlFrame = new A5LightBrightnessReqInnerFrame(param.getBrightness());
+            A5LightBrightnessReqInnerFrame lightControlFrame = new A5LightBrightnessReqInnerFrame(param.getBrightness(), param.getLightAddress());
             A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_LIGHT_DATA.getCode(), lightControlFrame);
             Map<String, Object> map = new HashMap<>();
             try {
                 map.put("deviceCode", param.getDeviceCode());
-                WrapResponseCommonFrame<A5LightBrightnessRespInnerFrame> frame = MainBoardInvokeSyncService.getInstance()
-                        .sendRRPC(param.getDeviceCode(), a5Frame, A5LightBrightnessRespInnerFrame.class);
+                WrapResponseCommonFrame<A5LightBrightnessRespInnerFrame> frame
+                        = MainBoardInvokeSyncService.getInstance().sendRRPC(param.getDeviceCode(), a5Frame, A5LightBrightnessRespInnerFrame.class);
+                //瀛樺偍鎺у埗甯ф寚浠�
+                StoreOperationRecordsUtils.storeInnerFrameData(param.getDeviceCode(), "鍗曠伅甯�-浜害鎺у埗", a5Frame, frame);
+
                 if (frame == null) {
                     map.put("status", DeviceRespStatusEnums.OTHER_ERROR.getCode());
                     resultList.add(map);
@@ -188,6 +255,20 @@
             }
         }
 
+        /**
+         * 鏈嶅姟绔壒閲忔帶鐏棩蹇楄褰曞紑濮�
+         */
+        String content = "{鎺х伅璇锋眰锛�" + paramList.toString()
+                + "锛� 鎺х伅缁撴灉锛�" + resultList.toString() + "}";
+        List<String> codeList = new ArrayList<>();
+        for (LightControlParam bean : paramList) {
+            codeList.add(bean.getDeviceCode());
+        }
+        StoreOperationRecordsUtils.storeOperationData(codeList, null, "鏈嶅姟绔壒閲忔帶鐏�", content);
+        /**
+         * 鏈嶅姟绔壒閲忔帶鐏棩蹇楄褰曠粨鏉�
+         */
+
         return resultList;
     }
 
@@ -196,27 +277,669 @@
      *
      * @return
      */
-    public Map controlEnergySaving() {
-        Long clientId = SecurityUtils.getClientId();
-        List<LightBo> listLight = baseMapper.listLight(clientId, null);
-        List<String> deviceCodeList = listLight.stream().map(LightBo::getDeviceCode).collect(Collectors.toList());
+    public List controlEnergySaving() {
+        //鑾峰彇鍒版鍦ㄦ墽琛岀殑浠诲姟鍒楄〃
+        List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask();
+        LocalDateTime now = LocalDateTime.now();
 
-        //鑾峰彇鐢ㄦ埛鏈�杩�7澶╃殑鍗曠伅鏁版嵁
-        List<LightReportData> reportDataList = lightReportDataService.list(Wrappers.<LightReportData>lambdaQuery()
-                .in(LightReportData::getDeviceCode, deviceCodeList)
-                .ge(LightReportData::getCreateTime, DateUtil.offsetDay(new Date(), -7))
-                .orderByDesc(LightReportData::getCreateTime));
+        List<LightTaskDto> list1 = new ArrayList<>();
+        List<LightTaskDto> list2 = new ArrayList<>();
+        List<LightTaskDto> list3 = new ArrayList<>();
+        List<LightTaskDto> list4 = new ArrayList<>();
+        List<LightTaskDto> list5 = new ArrayList<>();
+        List<LightTaskDto> list6 = new ArrayList<>();
+        List<LightTaskDto> list7 = new ArrayList<>();
 
-
-        if (CollectionUtil.isEmpty(reportDataList)) {
-            throw new BusinessException("娌℃湁璺伅鏁版嵁");
+        for (LightTaskDto lightTaskDto : lightTaskDtos) {
+            List<Integer> weekList = lightTaskDto.getWeekList();
+            for (Integer one : weekList) {
+                switch (one) {
+                    case 1:
+                        list1.add(lightTaskDto);
+                        break;
+                    case 2:
+                        list2.add(lightTaskDto);
+                        break;
+                    case 4:
+                        list3.add(lightTaskDto);
+                        break;
+                    case 8:
+                        list4.add(lightTaskDto);
+                        break;
+                    case 16:
+                        list5.add(lightTaskDto);
+                        break;
+                    case 32:
+                        list6.add(lightTaskDto);
+                        break;
+                    case 64:
+                        list7.add(lightTaskDto);
+                        break;
+                    default:
+                }
+            }
         }
-        Map map = new HashMap();
-        map.put("7", reportDataList);
 
 
-        return map;
+        //鑾峰彇褰撳墠鏄熸湡鍑� 1-7
+
+        int currentWeekValue = now.getDayOfWeek().getValue();
+        Map map;
+        List<Map> temp = new ArrayList<>();
+        //鑾峰彇鏃ユ湡 绮剧‘鍒板ぉ
+        LocalDateTime localDateTime = now.with(LocalTime.MIN);
+
+
+        for (int i = 1; i < 8; i++) {
+            currentWeekValue--;
+            if (currentWeekValue < 1) {
+                currentWeekValue = 7;
+            }
+            map = new LinkedHashMap();
+            map.put("time", localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+            map.put("value", getlist(currentWeekValue));
+            temp.add(map);
+        }
+
+
+        return temp;
     }
+
+
+    /**
+     * 鍗曠伅鑳借��
+     *
+     * @return
+     */
+    public List controlEnergy() {
+        //鑾峰彇鍒版鍦ㄦ墽琛岀殑浠诲姟鍒楄〃
+        List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask();
+        LocalDateTime now = LocalDateTime.now();
+
+        List<LightTaskDto> list1 = new ArrayList<>();
+        List<LightTaskDto> list2 = new ArrayList<>();
+        List<LightTaskDto> list3 = new ArrayList<>();
+        List<LightTaskDto> list4 = new ArrayList<>();
+        List<LightTaskDto> list5 = new ArrayList<>();
+        List<LightTaskDto> list6 = new ArrayList<>();
+        List<LightTaskDto> list7 = new ArrayList<>();
+
+        for (LightTaskDto lightTaskDto : lightTaskDtos) {
+            List<Integer> weekList = lightTaskDto.getWeekList();
+            System.out.println(weekList + "===========================");
+            for (Integer one : weekList) {
+                switch (one) {
+                    case 1:
+                        list1.add(lightTaskDto);
+                        break;
+                    case 2:
+                        list2.add(lightTaskDto);
+                        break;
+                    case 4:
+                        list3.add(lightTaskDto);
+                        break;
+                    case 8:
+                        list4.add(lightTaskDto);
+                        break;
+                    case 16:
+                        list5.add(lightTaskDto);
+                        break;
+                    case 32:
+                        list6.add(lightTaskDto);
+                        break;
+                    case 64:
+                        list7.add(lightTaskDto);
+                        break;
+                    default:
+                }
+            }
+        }
+
+        Integer currentWeekValue = now.getDayOfWeek().getValue();
+        Map map;
+        List<Map> temp = new ArrayList<>();
+        //鑾峰彇褰撳墠鏃ユ湡  鏍煎紡涓簓yyy-MM-dd
+        LocalDateTime localDateTime = now.with(LocalTime.MIN);
+
+
+        for (int i = 1; i < 8; i++) {
+            currentWeekValue--;
+            if (currentWeekValue < 1) {
+                currentWeekValue = 7;
+            }
+            map = new LinkedHashMap();
+            map.put("time", localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+            map.put("value", getlistEnergy(currentWeekValue));
+            temp.add(map);
+        }
+
+
+        return temp;
+    }
+
+
+    public BigDecimal getlist(Integer week) {
+
+        Long clientId = SecurityUtils.getClientId();
+//        List<LightBo> listLight = baseMapper.listLight(clientId, null);
+//        List<String> deviceCodeList = listLight.stream().map(LightBo::getDeviceCode).collect(Collectors.toList());
+
+        //鑾峰彇鍒版鍦ㄦ墽琛岀殑浠诲姟鍒楄〃
+        List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask();
+        LocalDateTime now = LocalDateTime.now();
+
+        List<LightTaskDto> list1 = new ArrayList<>();
+        List<LightTaskDto> list2 = new ArrayList<>();
+        List<LightTaskDto> list3 = new ArrayList<>();
+        List<LightTaskDto> list4 = new ArrayList<>();
+        List<LightTaskDto> list5 = new ArrayList<>();
+        List<LightTaskDto> list6 = new ArrayList<>();
+        List<LightTaskDto> list7 = new ArrayList<>();
+
+        for (LightTaskDto lightTaskDto : lightTaskDtos) {
+            List<Integer> weekList = lightTaskDto.getWeekList();
+            System.out.println(weekList + "===========================");
+            for (Integer one : weekList) {
+                switch (one) {
+                    case 1:
+                        list1.add(lightTaskDto);
+                        break;
+                    case 2:
+                        list2.add(lightTaskDto);
+                        break;
+                    case 4:
+                        list3.add(lightTaskDto);
+                        break;
+                    case 8:
+                        list4.add(lightTaskDto);
+                        break;
+                    case 16:
+                        list5.add(lightTaskDto);
+                        break;
+                    case 32:
+                        list6.add(lightTaskDto);
+                        break;
+                    case 64:
+                        list7.add(lightTaskDto);
+                        break;
+                    default:
+                }
+            }
+        }
+        if (week == 1) {
+            return jisuan(list1);
+        } else if (week == 2) {
+            return jisuan(list2);
+        } else if (week == 3) {
+            return jisuan(list3);
+        } else if (week == 4) {
+            return jisuan(list4);
+        } else if (week == 5) {
+            return jisuan(list5);
+        } else if (week == 6) {
+            return jisuan(list6);
+        } else if (week == 7) {
+            return jisuan(list7);
+        }
+
+        return null;
+    }
+
+
+    public BigDecimal getlistEnergy(Integer week) {
+
+        Long clientId = SecurityUtils.getClientId();
+//        List<LightBo> listLight = baseMapper.listLight(clientId, null);
+//        List<String> deviceCodeList = listLight.stream().map(LightBo::getDeviceCode).collect(Collectors.toList());
+
+        //鑾峰彇鍒版鍦ㄦ墽琛岀殑浠诲姟鍒楄〃
+        List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask();
+
+        LocalDateTime now = LocalDateTime.now();
+
+        List<LightTaskDto> list1 = new ArrayList<>();
+        List<LightTaskDto> list2 = new ArrayList<>();
+        List<LightTaskDto> list3 = new ArrayList<>();
+        List<LightTaskDto> list4 = new ArrayList<>();
+        List<LightTaskDto> list5 = new ArrayList<>();
+        List<LightTaskDto> list6 = new ArrayList<>();
+        List<LightTaskDto> list7 = new ArrayList<>();
+
+        for (LightTaskDto lightTaskDto : lightTaskDtos) {
+            List<Integer> weekList = lightTaskDto.getWeekList();
+            System.out.println(weekList + "===========================");
+            for (Integer one : weekList) {
+                switch (one) {
+                    case 1:
+                        list1.add(lightTaskDto);
+                        break;
+                    case 2:
+                        list2.add(lightTaskDto);
+                        break;
+                    case 4:
+                        list3.add(lightTaskDto);
+                        break;
+                    case 8:
+                        list4.add(lightTaskDto);
+                        break;
+                    case 16:
+                        list5.add(lightTaskDto);
+                        break;
+                    case 32:
+                        list6.add(lightTaskDto);
+                        break;
+                    case 64:
+                        list7.add(lightTaskDto);
+                        break;
+                    default:
+                }
+            }
+        }
+        if (week == 1) {
+            return jisuanEnergy(list1);
+        } else if (week == 2) {
+            return jisuanEnergy(list2);
+        } else if (week == 3) {
+            return jisuanEnergy(list3);
+        } else if (week == 4) {
+            return jisuanEnergy(list4);
+        } else if (week == 5) {
+            return jisuanEnergy(list5);
+        } else if (week == 6) {
+            return jisuanEnergy(list6);
+        } else if (week == 7) {
+            return jisuanEnergy(list7);
+        }
+
+        return null;
+    }
+
+    /**
+     * 涓�澶╃殑鑺傝兘鐜�
+     *
+     * @param list
+     * @return
+     */
+    public BigDecimal jisuan(List<LightTaskDto> list) {
+        final BigDecimal[] bigDecimalResult = {new BigDecimal(0.00)};
+        /**
+         * 鑺傝兘鐜囪绠楀紑濮�
+         */
+        list.forEach(Task -> {
+//            List<BigDecimal> saving = new ArrayList<>();
+            ControlLightCommandVO startTime = parseSwitchLightCommand(Task.getOpenOrder());
+            ControlLightCommandVO endTime = parseSwitchLightCommand(Task.getCloseOrder());
+            //瀛樻斁鑺傝兘鐜�
+            //鑾峰彇鍒板崟鐏换鍔$殑鑺傝兘鐜�
+            if (Task.getControlOrder() != null && !Task.getControlOrder().isEmpty()) {//鏈夋帶绛夋寚浠� 鎷嗗垎璁$畻
+                List<ControlLightCommandVO> controlLightCommandVOS = parseControlLightCommand(Task.getControlOrder());
+
+                for (int i = 0; i < controlLightCommandVOS.size(); i++) {
+                    BigDecimal bigDecimal = calculateEnergySaving(startTime, controlLightCommandVOS.get(i));
+                    bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
+                    if (controlLightCommandVOS.size() == (i + 1)) {
+                        BigDecimal bigDecimal1 = calculateEnergySaving(controlLightCommandVOS.get(i), endTime);
+//                        System.out.println(bigDecimal1 + "===========================");
+//                        saving.add(bigDecimal1);
+                        bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal1);
+                        break;
+                    }
+                    startTime = controlLightCommandVOS.get(i);
+//                    saving.add(bigDecimal);
+                }
+            } else {//鏃犳帶鐏寚浠� 鐩存帴璁$畻
+                BigDecimal bigDecimal = calculateEnergySaving(startTime, endTime);
+//                saving.add(bigDecimal);
+                bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
+            }
+        });
+        return bigDecimalResult[0];
+
+        /**
+         * 鑺傝兘鐜囪绠楃粨鏉�
+         */
+    }
+
+    /**
+     * 鍗曚釜浠诲姟涓�澶╃殑鑺傝兘鐜�
+     *
+     * @return
+     */
+    public BigDecimal jisuan(LightTaskDto Task) {
+        final BigDecimal[] bigDecimalResult = {new BigDecimal(0.00)};
+        /**
+         * 鑺傝兘鐜囪绠楀紑濮�
+         */
+        //TODO
+        //鑾峰彇鏄ㄥぉ鐨勬槦鏈熸暟
+        LocalDateTime now = LocalDateTime.now();
+        LocalDateTime yesterday = now.minusDays(1);
+        int week = yesterday.getDayOfWeek().getValue();
+
+        //鍒ゆ柇Task.getWeekList()鏄惁鍖呭惈鏄ㄥぉ鐨勬槦鏈熸暟   涓嶅寘鍚洿鎺ヨ繑鍥�0  涓嶈繘琛岃绠�
+        if (Task.getWeekList() != null && !Task.getWeekList().isEmpty()) {
+            if (!Task.getWeekList().contains(week)) {
+                return BigDecimal.ZERO;
+            }
+        } else {
+            return BigDecimal.ZERO;
+        }
+
+
+        ControlLightCommandVO startTime = parseSwitchLightCommand(Task.getOpenOrder());
+        ControlLightCommandVO endTime = parseSwitchLightCommand(Task.getCloseOrder());
+        //瀛樻斁鑺傝兘鐜�
+        //鑾峰彇鍒板崟鐏换鍔$殑鑺傝兘鐜�
+        if (Task.getControlOrder() != null && !Task.getControlOrder().isEmpty()) {//鏈夋帶绛夋寚浠� 鎷嗗垎璁$畻
+            List<ControlLightCommandVO> controlLightCommandVOS = parseControlLightCommand(Task.getControlOrder());
+
+            for (int i = 0; i < controlLightCommandVOS.size(); i++) {
+                BigDecimal bigDecimal = calculateEnergySaving(startTime, controlLightCommandVOS.get(i));
+                bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
+                if (controlLightCommandVOS.size() == (i + 1)) {
+                    BigDecimal bigDecimal1 = calculateEnergySaving(controlLightCommandVOS.get(i), endTime);
+                    bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal1);
+                    break;
+                }
+                startTime = controlLightCommandVOS.get(i);
+//                    saving.add(bigDecimal);
+            }
+        } else {//鏃犳帶鐏寚浠� 鐩存帴璁$畻
+            BigDecimal bigDecimal = calculateEnergySaving(startTime, endTime);
+//                saving.add(bigDecimal);
+            bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
+        }
+        return bigDecimalResult[0];
+
+        /**
+         * 鑺傝兘鐜囪绠楃粨鏉�
+         */
+    }
+
+
+    /**
+     * 涓�澶╃殑鑳借��
+     *
+     * @param list
+     * @return
+     */
+    public BigDecimal jisuanEnergy(List<LightTaskDto> list) {
+        //瀛樻斁鑳借��
+        final BigDecimal[] bigEnergy = {new BigDecimal(0.00)};
+        /**
+         * 鑺傝兘鐜囪绠楀紑濮�
+         */
+        list.forEach(Task -> {
+            List<PoleTaskLightPowerBo> poleTaskLightPowerBos = SpringContextHolder.getBean(LightTaskMapper.class).listLightTaskByTaskId(Task.getTaskId());
+            ControlLightCommandVO startTime = parseSwitchLightCommand(Task.getOpenOrder());
+            ControlLightCommandVO endTime = parseSwitchLightCommand(Task.getCloseOrder());
+            //鑾峰彇鍒板崟鐏换鍔$殑鑺傝兘鐜�
+            if (!Task.getControlOrder().isEmpty()) {//鏈夋帶绛夋寚浠� 鎷嗗垎璁$畻
+                List<ControlLightCommandVO> controlLightCommandVOS = parseControlLightCommand(Task.getControlOrder());
+
+
+                for (int i = 0; i < controlLightCommandVOS.size(); i++) {
+                    //寰楀埌鏃堕暱*浜害
+                    BigDecimal bigEnergy1 = calculateEnergyConsumption(startTime, controlLightCommandVOS.get(i));
+                    //鍒ゆ柇 Energy==0 鍒欎笉璁$畻
+                    if (bigEnergy1.compareTo(new BigDecimal(0)) != 0) {
+                        //璁$畻鑳借��(鎬�   鏃堕暱*浜害*鍔熺巼)
+                        BigDecimal totalEnergy = totalEnergy(bigEnergy1, Task.getLightAdress(), poleTaskLightPowerBos);
+                        bigEnergy[0] = bigEnergy[0].add(totalEnergy);
+                    }
+                    if (controlLightCommandVOS.size() == (i + 1)) {
+                        BigDecimal bigEnergy2 = calculateEnergyConsumption(controlLightCommandVOS.get(i), endTime);
+
+                        System.out.println(bigEnergy2 + "鏃堕暱*浜害===========================");
+
+                        //璁$畻鑳借��(鎬�   鏃堕暱*浜害*鍔熺巼)
+                        if (bigEnergy2.compareTo(new BigDecimal(0)) != 0) {
+                            BigDecimal totalEnergy3 = totalEnergy(bigEnergy2, Task.getLightAdress(), poleTaskLightPowerBos);
+                            bigEnergy[0] = bigEnergy[0].add(totalEnergy3);
+                        }
+                        break;
+                    }
+                    startTime = controlLightCommandVOS.get(i);
+                    //璁$畻鑳借��(鎬�   鏃堕暱*浜害*鍔熺巼)
+                }
+            } else {//鏃犳帶鐏寚浠� 鐩存帴璁$畻
+                //璁$畻鑳借��(閮ㄥ垎   鏃堕暱*浜害)
+                BigDecimal Energy = calculateEnergyConsumption(startTime, endTime);
+                //璁$畻鑳借��(鎬�   鏃堕暱*浜害*鍔熺巼)
+                //鍒ゆ柇 Energy==0 鍒欎笉璁$畻
+                if (Energy.compareTo(new BigDecimal(0)) != 0) {
+                    BigDecimal totalEnergy = totalEnergy(Energy, Task.getLightAdress(), poleTaskLightPowerBos);
+                    bigEnergy[0] = bigEnergy[0].add(totalEnergy);
+                }
+
+//                saving.add(bigDecimal);
+
+            }
+        });
+        return bigEnergy[0];
+
+        /**
+         * 璁$畻鑳借�楃粨鏉�
+         */
+    }
+
+
+    /**
+     * 涓�澶╃殑鑳借��
+     *
+     * @param list
+     * @return
+     */
+    public BigDecimal jisuanEnergy(LightTaskDto Task) {
+        //瀛樻斁鑳借��
+        final BigDecimal[] bigEnergy = {new BigDecimal(0.00)};
+
+        //鑾峰彇鏄ㄥぉ鐨勬槦鏈熸暟
+        LocalDateTime now = LocalDateTime.now();
+        LocalDateTime yesterday = now.minusDays(1);
+        int week = yesterday.getDayOfWeek().getValue();
+
+        //鍒ゆ柇Task.getWeekList()鏄惁鍖呭惈鏄ㄥぉ鐨勬槦鏈熸暟   涓嶅寘鍚洿鎺ヨ繑鍥�0  涓嶈繘琛岃绠�
+        if (Task.getWeekList() != null && !Task.getWeekList().isEmpty()) {
+            if (!Task.getWeekList().contains(week)) {
+                return BigDecimal.ZERO;
+            }
+        } else {
+            return BigDecimal.ZERO;
+        }
+
+        /**
+         * 鑺傝兘鐜囪绠楀紑濮�
+         */
+        List<PoleTaskLightPowerBo> poleTaskLightPowerBos = SpringContextHolder.getBean(LightTaskMapper.class).listLightTaskByTaskId(Task.getTaskId());
+        ControlLightCommandVO startTime = parseSwitchLightCommand(Task.getOpenOrder());
+        ControlLightCommandVO endTime = parseSwitchLightCommand(Task.getCloseOrder());
+        //鑾峰彇鍒板崟鐏换鍔$殑鑺傝兘鐜�
+        if (!Task.getControlOrder().isEmpty()) {//鏈夋帶绛夋寚浠� 鎷嗗垎璁$畻
+            List<ControlLightCommandVO> controlLightCommandVOS = parseControlLightCommand(Task.getControlOrder());
+
+
+            for (int i = 0; i < controlLightCommandVOS.size(); i++) {
+                //寰楀埌鏃堕暱*浜害
+                BigDecimal bigEnergy1 = calculateEnergyConsumption(startTime, controlLightCommandVOS.get(i));
+                //鍒ゆ柇 Energy==0 鍒欎笉璁$畻
+                if (bigEnergy1.compareTo(new BigDecimal(0)) != 0) {
+                    //璁$畻鑳借��(鎬�   鏃堕暱*浜害*鍔熺巼)
+                    BigDecimal totalEnergy = totalEnergy(bigEnergy1, Task.getLightAdress(), poleTaskLightPowerBos);
+                    bigEnergy[0] = bigEnergy[0].add(totalEnergy);
+                }
+                if (controlLightCommandVOS.size() == (i + 1)) {
+                    BigDecimal bigEnergy2 = calculateEnergyConsumption(controlLightCommandVOS.get(i), endTime);
+
+                    System.out.println(bigEnergy2 + "鏃堕暱*浜害===========================");
+
+                    //璁$畻鑳借��(鎬�   鏃堕暱*浜害*鍔熺巼)
+                    if (bigEnergy2.compareTo(new BigDecimal(0)) != 0) {
+                        BigDecimal totalEnergy3 = totalEnergy(bigEnergy2, Task.getLightAdress(), poleTaskLightPowerBos);
+                        bigEnergy[0] = bigEnergy[0].add(totalEnergy3);
+                    }
+                    break;
+                }
+                startTime = controlLightCommandVOS.get(i);
+                //璁$畻鑳借��(鎬�   鏃堕暱*浜害*鍔熺巼)
+            }
+        } else {//鏃犳帶鐏寚浠� 鐩存帴璁$畻
+            //璁$畻鑳借��(閮ㄥ垎   鏃堕暱*浜害)
+            BigDecimal Energy = calculateEnergyConsumption(startTime, endTime);
+            //璁$畻鑳借��(鎬�   鏃堕暱*浜害*鍔熺巼)
+            //鍒ゆ柇 Energy==0 鍒欎笉璁$畻
+            if (Energy.compareTo(new BigDecimal(0)) != 0) {
+                BigDecimal totalEnergy = totalEnergy(Energy, Task.getLightAdress(), poleTaskLightPowerBos);
+                bigEnergy[0] = bigEnergy[0].add(totalEnergy);
+            }
+
+//                saving.add(bigDecimal);
+
+        }
+        return bigEnergy[0];
+
+        /**
+         * 璁$畻鑳借�楃粨鏉�
+         */
+    }
+
+
+    private BigDecimal totalEnergy(BigDecimal bigEnergy2, String lightAdress, List<PoleTaskLightPowerBo> poleTaskLightPowerBos) {
+
+        //poleTaskLightPowerBos   鍖呭惈璇ヤ换鍔℃墍鏈夌殑鐏殑鍔熺巼
+        //璁$畻鍔熺巼涔嬪拰
+        BigDecimal totalPower = new BigDecimal(0.00);
+        if ("FFFF".equals(lightAdress)) {
+            int totalPower1 = poleTaskLightPowerBos.stream().mapToInt(PoleTaskLightPowerBo::getPower1).sum();
+            int totalPower2 = poleTaskLightPowerBos.stream().mapToInt(PoleTaskLightPowerBo::getPower2).sum();
+            totalPower = new BigDecimal(totalPower1 + totalPower2);
+        } else if ("0001".equals(lightAdress)) {
+            int totalPower1 = poleTaskLightPowerBos.stream().mapToInt(PoleTaskLightPowerBo::getPower1).sum();
+            totalPower = new BigDecimal(totalPower1);
+        } else if ("0002".equals(lightAdress)) {
+            int totalPower2 = poleTaskLightPowerBos.stream().mapToInt(PoleTaskLightPowerBo::getPower2).sum();
+            totalPower = new BigDecimal(totalPower2);
+        } else {
+            return null;
+        }
+        return totalPower.multiply(bigEnergy2);
+    }
+
+    /**
+     * 瑙f瀽寮�鍏崇伅鎸囦护
+     */
+    public ControlLightCommandVO parseSwitchLightCommand(String command) {
+        char[] chars = command.toCharArray();
+        ControlLightCommandVO vo = new ControlLightCommandVO();
+        if (chars.length == 7) {
+            int hour = Integer.valueOf(command.substring(0, 2));
+            int min = Integer.valueOf(command.substring(2, 4));
+            int brightness = Integer.valueOf(command.substring(4, 7));
+            vo.setHour(hour);
+            vo.setMin(min);
+            vo.setBrightness(brightness);
+        }
+        return vo;
+    }
+
+    /**
+     * 瑙f瀽鎺х伅鎸囦护
+     */
+    public List<ControlLightCommandVO> parseControlLightCommand(String command) {
+        char[] chars = command.toCharArray();
+        int i = chars.length / 7;
+
+        List<ControlLightCommandVO> list = new ArrayList<>();
+
+        for (int j = 0; j < i; j++) {
+            int hour = Integer.valueOf(command.substring(0 + (j * 7), 2 + (j * 7)));
+            int min = Integer.valueOf(command.substring(2 + (j * 7), 4 + (j * 7)));
+            int brightness = Integer.valueOf(command.substring(4 + (j * 7), 7 + (j * 7)));
+            ControlLightCommandVO vo = new ControlLightCommandVO();
+            vo.setHour(hour);
+            vo.setMin(min);
+            vo.setBrightness(brightness);
+            list.add(vo);
+        }
+        return list;
+    }
+
+    //璁$畻鑺傝兘鐜�
+    public BigDecimal calculateEnergySaving(ControlLightCommandVO v1, ControlLightCommandVO v2) {
+
+//        BigDecimal hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
+//        BigDecimal hour;
+        if (v1.getBrightness() == 0) {
+            return BigDecimal.ZERO;
+        }
+        double hour;
+        double min;
+        //璁$畻鏃堕暱
+        if (v2.getHour() > v1.getHour()) {
+            hour = v2.getHour() - v1.getHour();
+        } else if (v2.getHour() < v1.getHour()) {
+            hour = 24 + (v2.getHour() - v1.getHour());
+        } else {
+            hour = 0;
+        }
+        //璁$畻鍒嗛挓
+
+        if (v2.getMin() > v1.getMin()) {
+            min = v2.getMin() - v1.getMin();
+        } else if (v2.getMin() < v1.getMin()) {
+            min = 60 + (v1.getMin() - v2.getMin());
+            hour = hour - 1;
+        } else {
+            min = 0;
+        }
+        //璁$畻鏃堕暱
+        BigDecimal totalTime = BigDecimal.valueOf(hour * 60 + min);
+
+
+        //璁$畻鑺傝兘鐜� ( (1-v1.getBrightness()/100)*totalTime/1440 )   淇濈暀涓や綅灏忔暟
+        // 璁$畻 1-v1.getBrightness()/100鐨勫��
+        BigDecimal Brightness = BigDecimal.valueOf(100 - v1.getBrightness());
+        //Brightness/100*totalTime/1440  淇濈暀涓や綅灏忔暟       Brightness娌℃湁闄や互100  鍦ㄦ�绘椂闂�*100
+        BigDecimal energySaving = Brightness.multiply(totalTime).divide(new BigDecimal(144000), 2, BigDecimal.ROUND_HALF_UP);
+
+        System.out.println(energySaving + "鑺傝兘鐜�");
+        return energySaving;
+    }
+
+
+    //璁$畻鑳借��(閮ㄥ垎    鏃堕暱*浜害)
+    public BigDecimal calculateEnergyConsumption(ControlLightCommandVO v1, ControlLightCommandVO v2) {
+
+        if (v1.getBrightness() == 0) {
+            return BigDecimal.ZERO;
+        }
+        double hour;
+        double min;
+        //璁$畻鏃堕暱
+        if (v2.getHour() > v1.getHour()) {
+            hour = v2.getHour() - v1.getHour();
+        } else if (v2.getHour() < v1.getHour()) {
+            hour = 24 + (v2.getHour() - v1.getHour());
+        } else {
+            hour = 0;
+        }
+        //璁$畻鍒嗛挓
+
+        if (v2.getMin() > v1.getMin()) {
+            min = v2.getMin() - v1.getMin();
+        } else if (v2.getMin() < v1.getMin()) {
+            min = 60 + (v1.getMin() - v2.getMin());
+            hour = hour - 1;
+        } else {
+            min = 0;
+        }
+        //璁$畻鏃堕暱
+        BigDecimal totalTime = BigDecimal.valueOf(hour + min / 60);
+        //璁$畻浜伅鏃堕暱*浜害
+        BigDecimal energyConsumption = totalTime.multiply(BigDecimal.valueOf(v1.getBrightness())).divide(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
+        return energyConsumption;
+    }
+
 
     /**
      * 鐢ㄦ埛鎷ユ湁鐨勮矾鐏�
@@ -224,8 +947,12 @@
      * @return
      */
     public List<Light> listLight() {
-        Long clientId = SecurityUtils.getClientId();
-        List<LightBo> listLight = baseMapper.listLight(clientId, null);
+        List<LightBo> listLight;
+        if (SecurityUtils.getClientId() != null) {
+            listLight = baseMapper.listLight(SecurityUtils.getUserId(), null);
+        } else {
+            listLight = baseMapper.listLight(null, null);
+        }
         List<Light> lightList = new ArrayList<>();
         for (LightBo bean : listLight) {
             Light light = getLight(bean.getDeviceCode());
@@ -234,4 +961,33 @@
         return lightList;
     }
 
+    /**
+     * 鑾峰彇鐢ㄦ埛鎵�鏈夌殑璁惧鐮�
+     */
+    public CommonPage<String> listDeviceCode(int pageNo, int pageSize, String keyword, String deviceCode) {
+        List<String> list;
+        if (SecurityUtils.getClientId() != null) {
+            PageHelper.startPage(pageNo, pageSize);
+            list = baseMapper.listCode(SecurityUtils.getUserId(), keyword, deviceCode);
+        } else {
+
+            PageHelper.startPage(pageNo, pageSize);
+            list = baseMapper.listCode(null, keyword, deviceCode);
+        }
+
+        return CommonPage.restPage(list);
+    }
+
+    public boolean setPower(LightPowerSettingParam lightPowerSettingParam) {
+        if (lightPowerSettingParam.getLightIds().size() == 0) {
+            throw new BusinessException("鐏潌ID涓嶈兘涓虹┖");
+        }
+        List<Light> lights = listByIds(lightPowerSettingParam.getLightIds());
+        lights.forEach(light -> {
+            light.setPower1(lightPowerSettingParam.getPower1());
+            light.setPower2(lightPowerSettingParam.getPower2());
+            light.setLightCount(lightPowerSettingParam.getLightCount());
+        });
+        return updateBatchById(lights);
+    }
 }

--
Gitblit v1.9.3