From d9a9d8a2dad1d1e57b184bf8f972a03d654d883a Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期五, 22 七月 2022 15:07:01 +0800
Subject: [PATCH] 优化

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java |  376 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 312 insertions(+), 64 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 e497560..5986ccf 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
@@ -3,6 +3,7 @@
 import cn.hutool.core.collection.CollectionUtil;
 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;
@@ -33,6 +34,7 @@
 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.enums.OrderByEnums;
 import com.sandu.ximon.dao.mapper.LightMapper;
 import com.sandu.ximon.dao.mapper.LightTaskMapper;
 import lombok.AllArgsConstructor;
@@ -41,10 +43,9 @@
 
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -72,7 +73,7 @@
         Boolean hasKey = redisService.hasKey(LightKey.REPORT_MAC.key(deviceName));
         if (!hasKey) {
             int count = count(Wrappers.lambdaQuery(Light.class).eq(Light::getDeviceCode, deviceName));
-            log.info("redis鏌ヤ笉鍒拌矾鐏暟鎹畕}={}", count, deviceName);
+            log.info("redis鏌ヤ笉鍒拌矾鐏暟鎹� 鏁伴噺:{}={}", count, deviceName);
             // 褰撳墠璺伅琛ㄦ病鏈夊綍鍏ヨ澶囧悧
             if (count == 0) {
                 Light light = new Light();
@@ -80,15 +81,15 @@
                 light.setLightPercent(heartBeatDataPackage.getLightPercent());
                 light.setLight2Percent(heartBeatDataPackage.getLight2Percent());
                 save(light);
+                log.info("鏂板璺伅");
             }
             redisService.set(LightKey.REPORT_MAC.key(deviceName), 1, LightKey.REPORT_MAC.expireSeconds());
-            log.info("鏂板璺伅");
         } else {
             Light light = new Light();
             light.setLightPercent(heartBeatDataPackage.getLightPercent());
             light.setLight2Percent(heartBeatDataPackage.getLight2Percent());
-            update(light, Wrappers.lambdaUpdate(Light.class).eq(Light::getDeviceCode, deviceName));
-            log.info("鏇存柊璺伅浜害");
+            boolean update = update(light, Wrappers.lambdaUpdate(Light.class).eq(Light::getDeviceCode, deviceName));
+            log.info("鏇存柊璺伅浜害 {}", update);
         }
     }
 
@@ -97,15 +98,73 @@
      *
      * @return 杩斿洖缁勫悎鏁版嵁dto
      */
-    public List<LightBo> listLight(int pageNo, int pageSize, String keyword) {
+    public List<LightBo> listLight(int pageNo, int pageSize, String keyword, Integer order, Integer seq) {
         Long clientId = SecurityUtils.getClientId();
 
-        PageHelper.startPage(pageNo, pageSize);
+        //鎺掑簭瀛楁
+        String orderByResult = "light_id";
+        //姝e簭銆佸�掑彊
+        String orderBySeq = OrderByEnums.ASC.getCode();
+        if (order != null) {
+            switch (order) {
+                case 1:
+                    orderByResult = OrderByEnums.LIGHT_POLE_NAME.getCode();
+                    break;
+                case 2:
+                    orderByResult = OrderByEnums.LIGHT_UPDATE_TIME.getCode();
+                    break;
+                case 3:
+                    orderByResult = OrderByEnums.LIGHT_CREATE_TIME.getCode();
+                    break;
+                default:
+            }
+        }
+        if (seq != null) {
+            switch (seq) {
+                case 1:
+                    orderBySeq = OrderByEnums.ASC.getCode();
+                    break;
+                case 2:
+                    orderBySeq = OrderByEnums.DESC.getCode();
+                    break;
+                default:
+                    break;
+            }
+        }
+        //鎺掑簭鏂瑰紡
+        String orderBy = orderByResult + " " + orderBySeq;
+
+        PageHelper.startPage(pageNo, pageSize, orderBy);
 
         List<LightBo> listLight = baseMapper.listLight(clientId, keyword);
 
         // 鑾峰彇鏈�杩戠殑涓婃姤鏃堕棿
         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) {
@@ -205,7 +264,9 @@
                 map.put("deviceCode", param.getDeviceCode());
                 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);
@@ -228,6 +289,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;
     }
 
@@ -236,7 +311,7 @@
      *
      * @return
      */
-    public Map controlEnergySaving() {
+    public List controlEnergySaving() {
         //鑾峰彇鍒版鍦ㄦ墽琛岀殑浠诲姟鍒楄〃
         List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask();
         LocalDateTime now = LocalDateTime.now();
@@ -251,7 +326,6 @@
 
         for (LightTaskDto lightTaskDto : lightTaskDtos) {
             List<Integer> weekList = lightTaskDto.getWeekList();
-            System.out.println(weekList + "===========================");
             for (Integer one : weekList) {
                 switch (one) {
                     case 1:
@@ -280,21 +354,29 @@
             }
         }
 
-        Integer week = now.getDayOfWeek().getValue();
 
-        Map map = new HashMap();
+        //鑾峰彇褰撳墠鏄熸湡鍑� 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++) {
-            week--;
-            if (week < 1) {
-                week = 7;
+            currentWeekValue--;
+            if (currentWeekValue < 1) {
+                currentWeekValue = 7;
             }
-            map.put(i, getlist(week));
+            map = new LinkedHashMap();
+            map.put("time", localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+            map.put("value", getlist(currentWeekValue));
+            temp.add(map);
         }
 
 
-        return map;
+        return temp;
     }
 
 
@@ -303,7 +385,7 @@
      *
      * @return
      */
-    public Map controlEnergy() {
+    public List controlEnergy() {
         //鑾峰彇鍒版鍦ㄦ墽琛岀殑浠诲姟鍒楄〃
         List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask();
         LocalDateTime now = LocalDateTime.now();
@@ -347,21 +429,26 @@
             }
         }
 
-        Integer week = now.getDayOfWeek().getValue();
-
-        Map map = new HashMap();
+        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++) {
-            week--;
-            if (week < 1) {
-                week = 7;
+            currentWeekValue--;
+            if (currentWeekValue < 1) {
+                currentWeekValue = 7;
             }
-            map.put(i, getlistEnergy(week));
+            map = new LinkedHashMap();
+            map.put("time", localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+            map.put("value", getlistEnergy(currentWeekValue));
+            temp.add(map);
         }
 
 
-        return map;
+        return temp;
     }
 
 
@@ -435,9 +522,6 @@
 
     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();
@@ -526,7 +610,7 @@
                     bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
                     if (controlLightCommandVOS.size() == (i + 1)) {
                         BigDecimal bigDecimal1 = calculateEnergySaving(controlLightCommandVOS.get(i), endTime);
-                        System.out.println(bigDecimal1 + "===========================");
+//                        System.out.println(bigDecimal1 + "===========================");
 //                        saving.add(bigDecimal1);
                         bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal1);
                         break;
@@ -540,6 +624,61 @@
                 bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
             }
         });
+        return bigDecimalResult[0];
+
+        /**
+         * 鑺傝兘鐜囪绠楃粨鏉�
+         */
+    }
+
+    /**
+     * 鍗曚釜浠诲姟涓�澶╃殑鑺傝兘鐜�
+     *
+     * @return
+     */
+    public BigDecimal jisuan(LightTaskDto Task) {
+        final BigDecimal[] bigDecimalResult = {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 new BigDecimal(1);
+            }
+        } else {
+            return new BigDecimal(1);
+        }
+
+
+        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];
 
         /**
@@ -572,20 +711,22 @@
                 for (int i = 0; i < controlLightCommandVOS.size(); i++) {
                     //寰楀埌鏃堕暱*浜害
                     BigDecimal bigEnergy1 = calculateEnergyConsumption(startTime, controlLightCommandVOS.get(i));
-//                    //璁$畻鑳借��(鎬�   鏃堕暱*浜害*鍔熺巼)
-                    BigDecimal totalEnergy = totalEnergy(bigEnergy1, Task.getLightAdress(), poleTaskLightPowerBos);
-                    bigEnergy[0] = bigEnergy[0].add(totalEnergy);
-//                    BigDecimal totalEnergy4 = totalEnergy(bigEnergy1, Task.getLightAdress(), poleTaskLightPowerBos);
-//                    bigEnergy[0] = bigEnergy[0].add(totalEnergy4);
+                    //鍒ゆ柇 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 + "鏃堕暱*浜害===========================");
 
                         //璁$畻鑳借��(鎬�   鏃堕暱*浜害*鍔熺巼)
-                        BigDecimal totalEnergy3 = totalEnergy(bigEnergy2, Task.getLightAdress(), poleTaskLightPowerBos);
-//                        saving.add(bigDecimal1);
-                        bigEnergy[0] = bigEnergy[0].add(totalEnergy3);
+                        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);
@@ -595,9 +736,14 @@
                 //璁$畻鑳借��(閮ㄥ垎   鏃堕暱*浜害)
                 BigDecimal Energy = calculateEnergyConsumption(startTime, endTime);
                 //璁$畻鑳借��(鎬�   鏃堕暱*浜害*鍔熺巼)
-                BigDecimal totalEnergy = totalEnergy(Energy, Task.getLightAdress(), poleTaskLightPowerBos);
+                //鍒ゆ柇 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);
-                bigEnergy[0] = bigEnergy[0].add(totalEnergy);
+
             }
         });
         return bigEnergy[0];
@@ -606,6 +752,87 @@
          * 璁$畻鑳借�楃粨鏉�
          */
     }
+
+
+    /**
+     * 涓�澶╃殑鑳借��
+     *
+     * @param
+     * @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) {
 
@@ -671,54 +898,75 @@
     public BigDecimal calculateEnergySaving(ControlLightCommandVO v1, ControlLightCommandVO v2) {
 
 //        BigDecimal hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
-        BigDecimal hour;
+//        BigDecimal hour;
+        if (v1.getBrightness() == 0) {
+            return new BigDecimal(1);
+        }
+        double hour;
+        double min;
+        //璁$畻鏃堕暱
         if (v2.getHour() > v1.getHour()) {
-            hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
+            hour = v2.getHour() - v1.getHour();
         } else if (v2.getHour() < v1.getHour()) {
-            hour = BigDecimal.valueOf((v2.getHour() + 24 - v1.getHour()));
+            hour = 24 + (v2.getHour() - v1.getHour());
         } else {
-            hour = BigDecimal.valueOf(0);
+            hour = 0;
         }
-//        BigDecimal min = BigDecimal.valueOf((v2.getMin() - v1.getMin()) / 60);
-        BigDecimal min;
+        //璁$畻鍒嗛挓
+
         if (v2.getMin() > v1.getMin()) {
-            min = BigDecimal.valueOf((v2.getMin() - v1.getMin()) / 60);
+            min = v2.getMin() - v1.getMin();
         } else if (v2.getMin() < v1.getMin()) {
-            min = BigDecimal.valueOf((v2.getMin() + 60 - v1.getMin()) / 60);
+            min = 60 + (v1.getMin() - v2.getMin());
+            hour = hour - 1;
         } else {
-            min = BigDecimal.valueOf(0);
+            min = 0;
         }
+        //璁$畻鏃堕暱
+        BigDecimal totalTime = BigDecimal.valueOf(hour * 60 + min);
 
-        BigDecimal totalTime = (hour.add(min));
 
-        BigDecimal energySaving = BigDecimal.valueOf((100 - v1.getBrightness())).divide(BigDecimal.valueOf(100)).multiply(totalTime).divide(BigDecimal.valueOf(24));
-        System.out.println(energySaving);
+        //璁$畻鑺傝兘鐜� ( (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;
         //璁$畻鏃堕暱
-        BigDecimal hour;
         if (v2.getHour() > v1.getHour()) {
-            hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
+            hour = v2.getHour() - v1.getHour();
         } else if (v2.getHour() < v1.getHour()) {
-            hour = BigDecimal.valueOf((v2.getHour() + 24 - v1.getHour()));
+            hour = 24 + (v2.getHour() - v1.getHour());
         } else {
-            hour = BigDecimal.valueOf(0);
+            hour = 0;
         }
-        BigDecimal min;
+        //璁$畻鍒嗛挓
+
         if (v2.getMin() > v1.getMin()) {
-            min = BigDecimal.valueOf((v2.getMin() - v1.getMin()) / 60);
+            min = v2.getMin() - v1.getMin();
         } else if (v2.getMin() < v1.getMin()) {
-            min = BigDecimal.valueOf((v2.getMin() + 60 - v1.getMin()) / 60);
+            min = 60 + (v1.getMin() - v2.getMin());
+            hour = hour - 1;
         } else {
-            min = BigDecimal.valueOf(0);
+            min = 0;
         }
-        BigDecimal totalTime = (hour.add(min));
+        //璁$畻鏃堕暱
+        BigDecimal totalTime = BigDecimal.valueOf(hour + min / 60);
         //璁$畻浜伅鏃堕暱*浜害
-        BigDecimal energyConsumption = totalTime.multiply(BigDecimal.valueOf(v1.getBrightness())).divide(BigDecimal.valueOf(100));
+        BigDecimal energyConsumption = totalTime.multiply(BigDecimal.valueOf(v1.getBrightness())).divide(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
         return energyConsumption;
     }
 

--
Gitblit v1.9.3