package com.sandu.ximon.admin.service; 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; 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; import com.sandu.ximon.admin.manager.iot.frame.inner.response.A5LightBrightnessRespInnerFrame; 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.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; /** * @author chenjiantian * @date 2021/12/13 16:00 * 灯设备service */ @Service @Slf4j @AllArgsConstructor public class LightService extends BaseServiceImpl { private final RedisService redisService; private final LightReportDataService lightReportDataService; private final PoleBindingService bindingService; private final PoleService poleService; /** * 录入当前设备码的路灯数据 * * @param deviceName mac * @param heartBeatDataPackage 心跳数据 */ public void saveLight(String deviceName, A5LightHeartbeatReportInnerFrame.HeartBeatDataPackage heartBeatDataPackage) { 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); // 当前路灯表没有录入设备吗 if (count == 0) { Light light = new Light(); light.setDeviceCode(deviceName); light.setLightPercent(heartBeatDataPackage.getLightPercent()); light.setLight2Percent(heartBeatDataPackage.getLight2Percent()); save(light); } 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("更新路灯亮度"); } } /** * 获取路灯列表 * * @return 返回组合数据dto */ public List listLight(int pageNo, int pageSize, String keyword) { Long clientId = SecurityUtils.getClientId(); PageHelper.startPage(pageNo, pageSize); List listLight = baseMapper.listLight(clientId, keyword); // 获取最近的上报时间 List deviceCodeList = listLight.stream().map(Light::getDeviceCode).collect(Collectors.toList()); //拆分list List> split = CollectionUtil.split(deviceCodeList, 100); List deviceStatuses = null; for (List 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 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()); } }); for (LightReportData lightReportData : reportDataList) { if (StrUtil.equals(lightBo.getDeviceCode(), lightReportData.getDeviceCode())) { lightBo.setReportTime(lightReportData.getCreateTime1()); break; } } } } return listLight; } /** * 获取单个路灯信息 * * @param deviceCode 设备码 * @return */ public Light getLight(String deviceCode) { Light one = getOne(Wrappers.lambdaQuery().eq(Light::getDeviceCode, deviceCode)); Object o = redisService.get(LightKey.REPORT_MAC.key(deviceCode)); if (o != null) { one.setOnlineStatus(1); } else { one.setOnlineStatus(0); } 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.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) { throw new BusinessException("找不到路灯"); } Light update = new Light(); update.setLightId(param.getLightId()); update.setRemark(param.getRemark()); return updateById(update); } /** * 单灯亮度控制 * * @return com.sandu.ximon.dao.enums.DeviceRespStatusEnums */ public List> controlBrightness(List paramList) { if (CollectionUtil.isEmpty(paramList)) { throw new BusinessException("参数不能为空"); } List> resultList = new ArrayList<>(); for (LightControlParam param : paramList) { A5LightBrightnessReqInnerFrame lightControlFrame = new A5LightBrightnessReqInnerFrame(param.getBrightness(), param.getLightAddress()); A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_LIGHT_DATA.getCode(), lightControlFrame); Map map = new HashMap<>(); try { map.put("deviceCode", param.getDeviceCode()); WrapResponseCommonFrame 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); continue; } String responseStatus = frame.getResponseInnerFrame().getResponseStatus(); int status = HexUtil.hexToInt(responseStatus); map.put("status", status); resultList.add(map); // 更新亮度成功,修改到数据库 if (DeviceRespStatusEnums.SUCCESS.getCode().equals(status)) { Light light = new Light(); light.setLightPercent(param.getBrightness()); update(light, Wrappers.lambdaUpdate(Light.class).eq(Light::getDeviceCode, param.getDeviceCode())); } } catch (BusinessException e) { map.put("status", DeviceRespStatusEnums.OTHER_ERROR.getCode()); resultList.add(map); } } /** * 服务端批量控灯日志记录开始 */ String content = "{控灯请求:" + paramList.toString() + ", 控灯结果:" + resultList.toString() + "}"; List codeList = new ArrayList<>(); for (LightControlParam bean : paramList) { codeList.add(bean.getDeviceCode()); } StoreOperationRecordsUtils.storeOperationData(codeList, null, "服务端批量控灯", content); /** * 服务端批量控灯日志记录结束 */ return resultList; } /** * 单灯节能率 * * @return */ public List controlEnergySaving() { //获取到正在执行的任务列表 List lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask(); LocalDateTime now = LocalDateTime.now(); List list1 = new ArrayList<>(); List list2 = new ArrayList<>(); List list3 = new ArrayList<>(); List list4 = new ArrayList<>(); List list5 = new ArrayList<>(); List list6 = new ArrayList<>(); List list7 = new ArrayList<>(); for (LightTaskDto lightTaskDto : lightTaskDtos) { List 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: } } } //获取当前星期几 1-7 int currentWeekValue = now.getDayOfWeek().getValue(); Map map; List 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 lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask(); LocalDateTime now = LocalDateTime.now(); List list1 = new ArrayList<>(); List list2 = new ArrayList<>(); List list3 = new ArrayList<>(); List list4 = new ArrayList<>(); List list5 = new ArrayList<>(); List list6 = new ArrayList<>(); List list7 = new ArrayList<>(); for (LightTaskDto lightTaskDto : lightTaskDtos) { List 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 temp = new ArrayList<>(); //获取当前日期 格式为yyyy-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 listLight = baseMapper.listLight(clientId, null); // List deviceCodeList = listLight.stream().map(LightBo::getDeviceCode).collect(Collectors.toList()); //获取到正在执行的任务列表 List lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask(); LocalDateTime now = LocalDateTime.now(); List list1 = new ArrayList<>(); List list2 = new ArrayList<>(); List list3 = new ArrayList<>(); List list4 = new ArrayList<>(); List list5 = new ArrayList<>(); List list6 = new ArrayList<>(); List list7 = new ArrayList<>(); for (LightTaskDto lightTaskDto : lightTaskDtos) { List 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 listLight = baseMapper.listLight(clientId, null); // List deviceCodeList = listLight.stream().map(LightBo::getDeviceCode).collect(Collectors.toList()); //获取到正在执行的任务列表 List lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask(); LocalDateTime now = LocalDateTime.now(); List list1 = new ArrayList<>(); List list2 = new ArrayList<>(); List list3 = new ArrayList<>(); List list4 = new ArrayList<>(); List list5 = new ArrayList<>(); List list6 = new ArrayList<>(); List list7 = new ArrayList<>(); for (LightTaskDto lightTaskDto : lightTaskDtos) { List 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 list) { final BigDecimal[] bigDecimalResult = {new BigDecimal(0.00)}; /** * 节能率计算开始 */ list.forEach(Task -> { // List saving = new ArrayList<>(); ControlLightCommandVO startTime = parseSwitchLightCommand(Task.getOpenOrder()); ControlLightCommandVO endTime = parseSwitchLightCommand(Task.getCloseOrder()); //存放节能率 //获取到单灯任务的节能率 if (Task.getControlOrder() != null && !Task.getControlOrder().isEmpty()) {//有控等指令 拆分计算 List 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]; /** * 节能率计算结束 */ } /** * 一天的能耗 * * @param list * @return */ public BigDecimal jisuanEnergy(List list) { //存放能耗 final BigDecimal[] bigEnergy = {new BigDecimal(0.00)}; /** * 节能率计算开始 */ list.forEach(Task -> { List poleTaskLightPowerBos = SpringContextHolder.getBean(LightTaskMapper.class).listLightTaskByTaskId(Task.getTaskId()); ControlLightCommandVO startTime = parseSwitchLightCommand(Task.getOpenOrder()); ControlLightCommandVO endTime = parseSwitchLightCommand(Task.getCloseOrder()); //获取到单灯任务的节能率 if (!Task.getControlOrder().isEmpty()) {//有控等指令 拆分计算 List 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 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); } /** * 解析开关灯指令 */ 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; } /** * 解析控灯指令 */ public List parseControlLightCommand(String command) { char[] chars = command.toCharArray(); int i = chars.length / 7; List 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; 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/3600 ) 保留两位小数 BigDecimal energySaving = BigDecimal.valueOf((1 - v1.getBrightness() / 100) * totalTime.doubleValue() / 3600).setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println(energySaving + "节能率"); return energySaving; } //计算能耗(部分 时长*亮度) public BigDecimal calculateEnergyConsumption(ControlLightCommandVO v1, ControlLightCommandVO v2) { //计算时长 BigDecimal hour; if (v2.getHour() > v1.getHour()) { hour = BigDecimal.valueOf((v2.getHour() - v1.getHour())); } else if (v2.getHour() < v1.getHour()) { hour = BigDecimal.valueOf((v2.getHour() + 24 - v1.getHour())); } else { hour = BigDecimal.valueOf(0); } BigDecimal min; if (v2.getMin() > v1.getMin()) { min = BigDecimal.valueOf((v2.getMin() - v1.getMin()) / 60); } else if (v2.getMin() < v1.getMin()) { min = BigDecimal.valueOf((v2.getMin() + 60 - v1.getMin()) / 60); hour = hour.subtract(BigDecimal.valueOf(1)); } else { min = BigDecimal.valueOf(0); } BigDecimal totalTime = (hour.add(min)); //计算亮灯时长*亮度 BigDecimal energyConsumption = totalTime.multiply(BigDecimal.valueOf(v1.getBrightness())).divide(BigDecimal.valueOf(100)); return energyConsumption; } /** * 用户拥有的路灯 * * @return */ public List listLight() { List listLight; if (SecurityUtils.getClientId() != null) { listLight = baseMapper.listLight(SecurityUtils.getUserId(), null); } else { listLight = baseMapper.listLight(null, null); } List lightList = new ArrayList<>(); for (LightBo bean : listLight) { Light light = getLight(bean.getDeviceCode()); lightList.add(light); } return lightList; } /** * 获取用户所有的设备码 */ public CommonPage listDeviceCode(int pageNo, int pageSize, String keyword, String deviceCode) { List 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 lights = listByIds(lightPowerSettingParam.getLightIds()); lights.forEach(light -> { light.setPower1(lightPowerSettingParam.getPower1()); light.setPower2(lightPowerSettingParam.getPower2()); light.setLightCount(lightPowerSettingParam.getLightCount()); }); return updateBatchById(lights); } }