From 15fe445c820f28139e4e7e1a4cb286ad6ae37e6e Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期一, 06 六月 2022 11:05:17 +0800
Subject: [PATCH] bug修复
---
ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightTaskService.java | 670 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 661 insertions(+), 9 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 6da0e0d..1a2e630 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,15 +1,55 @@
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;
+import com.sandu.ximon.admin.manager.iot.frame.inner.response.A5LightTimerRespInnerFrame;
+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.LightTaskPoleRelationBo;
+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.Pole;
+import com.sandu.ximon.dao.enums.DeviceRespStatusEnums;
+import com.sandu.ximon.dao.enums.OrderByEnums;
import com.sandu.ximon.dao.mapper.LightTaskMapper;
+import com.sandu.ximon.dao.mapper.LightTaskPoleRelationMapper;
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.time.format.DateTimeFormatter;
+import java.util.*;
+import java.util.stream.Collectors;
/**
* @author chenjiantian
@@ -17,33 +57,645 @@
* 璺伅浠诲姟鎿嶄綔
*/
@Service
+@Slf4j
@AllArgsConstructor
public class LightTaskService extends BaseServiceImpl<LightTaskMapper, LightTask> {
- private final static int ORDER_LENGTH = 6;
-
- private final LightTaskQuartzService lightTaskQuartzService;
+ private final PoleService poleService;
+ private final LightTaskPoleRelationService lightTaskPoleRelationService;
+ private final LightTaskMapper lightTaskMapper;
+ private final ClientService clientService;
+ private final LightEnergyDataService lightEnergyDataService;
/**
* 鏂板璺伅浠诲姟
*/
@Transactional(rollbackFor = Exception.class)
public boolean addLightTask(LightTaskParam param) {
- if(StrUtil.length(param.getControlOrder()) % ORDER_LENGTH != 0){
+ 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());
- if(!save(lightTask)) {
+ lightTask.setFramePayload(buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week));
+ if (!save(lightTask)) {
throw new BusinessException("淇濆瓨璺伅浠诲姟澶辫触");
}
- boolean result = lightTaskQuartzService.addLightTaskQuartz(lightTask);
- return false;
+
+
+ /**
+ * 鏂板璺伅浠诲姟鏃ュ織璁板綍寮�濮�
+ */
+ 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锛�" + 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<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;
+ }
+
+ @Transactional(rollbackFor = Exception.class)
+ public String updateLightTask(Long taskId, LightTaskParam param) {
+ if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) {
+ throw new BusinessException("鐏帶鍛戒护鏍煎紡涓嶆纭�");
+ }
+ LightTask lightTask = getById(taskId);
+ if (lightTask == null) {
+ throw new BusinessException("鎵句笉鍒拌矾鐏换鍔�");
+ }
+
+
+ int week = 0;
+ for (Integer w : param.getWeekList()) {
+ week |= w;
+ }
+
+ LightTask 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));
+
+
+ //缂栬緫鍚庣伅鏉咺D闆嗗悎
+ List<Long> poleIdList = param.getPoleIdList();
+ //璁板綍浠诲姟缂栬緫鍣ㄥ墠鐏潌ID闆嗗悎
+ List<LightTaskStatusAndPole> oldLightTaskStatusAndPoles = lightTaskPoleRelationService.listPoleAndStatusIdByTaskId(taskId);
+ List<Long> oldList = oldLightTaskStatusAndPoles.stream().map(LightTaskStatusAndPole::getId).collect(Collectors.toList());
+
+ //鍒ゆ柇param.getPoleIdList()涓槸鍚︽湁鏃х殑鐏潌ID (鐩存帴涓嬪彂)
+ List<Long> newPoleIdList = param.getPoleIdList().stream().filter(poleId -> !oldList.contains(poleId)).collect(Collectors.toList());
+ //鍒ゆ柇param.getPoleIdList()涓槸鍚︽湁鏂扮殑鐏潌ID (瑕嗙洊鎿嶄綔)
+ List<Long> oldPoleIdList = param.getPoleIdList().stream().filter(poleId -> oldList.contains(poleId)).collect(Collectors.toList());
+ //oldList涓湁鐨勭伅鏉咺D锛屼絾鏄痯aram.getPoleIdList()涓病鏈� (鍏崇伅鎿嶄綔)
+ List<Long> closeLight = oldList.stream().filter(poleId -> !param.getPoleIdList().contains(poleId)).collect(Collectors.toList());
+
+
+ List<LightTaskPoleRelation> newPoleAll = new ArrayList<>();
+ List<LightTaskPoleRelation> newPoleSuccess = new ArrayList<>();
+ List<LightTaskPoleRelation> newPoleFail = new ArrayList<>();
+ if (CollectionUtil.isNotEmpty(newPoleIdList)) {
+ //鏂扮伅鏉嗕笅鍙戞柊浠诲姟
+ Map<String, List<LightTaskPoleRelation>> newPoleMap = sendControllerFrame(newPoleIdList, lightTask.getFramePayload(), param.getLightAddress());
+
+ //newPoleAll闆嗗悎鍚庨潰鐢ㄤ簬瀛樺偍鍏崇郴琛�
+ newPoleAll = newPoleMap.getOrDefault("all", new ArrayList<>());
+ newPoleSuccess = newPoleMap.getOrDefault("success", new ArrayList<>());
+ newPoleFail = newPoleMap.getOrDefault("fail", new ArrayList<>());
+ }
+
+ List<LightTaskPoleRelation> oldPoleFail = new ArrayList<>();
+ List<LightTaskPoleRelation> oldPoleSuccess = new ArrayList<>();
+ if (CollectionUtil.isNotEmpty(oldPoleIdList)) {
+ //瑕嗙洊鎿嶄綔鐏潌
+ Map<String, List<LightTaskPoleRelation>> oldPoleMap = sendControllerFrame(oldPoleIdList, lightTask.getFramePayload(), param.getLightAddress());
+ oldPoleFail = oldPoleMap.getOrDefault("fail", new ArrayList<>());
+ oldPoleSuccess = oldPoleMap.getOrDefault("success", new ArrayList<>());
+ }
+
+ List<LightTaskPoleRelation> closePoleFail = new ArrayList<>();
+ List<LightTaskPoleRelation> closePoleSuccess = new ArrayList<>();
+ if (CollectionUtil.isNotEmpty(closeLight)) {
+ //鎵ц鍏崇伅鍐呭抚
+ String framePayloadClose = "7f0000007f173b00";
+ //瑕嗙洊鎿嶄綔鐏潌
+ Map<String, List<LightTaskPoleRelation>> closePoleMap = sendControllerFrame(closeLight, framePayloadClose, param.getLightAddress());
+ closePoleFail = closePoleMap.getOrDefault("fail", new ArrayList<>());
+ closePoleSuccess = closePoleMap.getOrDefault("success", new ArrayList<>());
+ }
+
+
+ if (closePoleFail.size() == 0 && oldPoleFail.size() == 0) {
+ //鏃т换鍔′腑鍏ㄩ儴鍙戦�佹寚浠ゆ垚鍔燂紝鏇存柊鍘熸湰浠诲姟涓殑鏁版嵁
+ newLightTask.setTaskId(lightTask.getTaskId());
+ updateById(newLightTask);
+ } else {
+ if (!newPoleSuccess.isEmpty() || !oldPoleSuccess.isEmpty()) {
+ //鏈夊け鏁楃殑闇�瑕佷繚瀛樻棫鐨勪换鍕欙紝鏂板鏂扮殑浠诲姟鏁版嵁杩涜淇濆瓨
+ save(newLightTask);
+ }
+ }
+
+ /**
+ * 缂栬緫璺伅浠诲姟鏃ュ織璁板綍寮�濮�
+ */
+ 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);
+ /**
+ * 缂栬緫璺伅浠诲姟鏃ュ織璁板綍缁撴潫
+ */
+
+ for (LightTaskPoleRelation bean : newPoleSuccess) {
+ bean.setTaskId(newLightTask.getTaskId());
+ }
+ //鎴愬姛鐢ㄦ柊鐨勪换鍔D
+ for (LightTaskPoleRelation bean : oldPoleSuccess) {
+ bean.setTaskId(newLightTask.getTaskId());
+ }
+ //澶辫触鐢ㄦ棫鐨勪换鍔D
+ for (LightTaskPoleRelation bean : oldPoleFail) {
+ bean.setTaskId(lightTask.getTaskId());
+ }
+
+ //澶辫触鐢ㄦ棫鐨勪换鍔D
+ for (LightTaskPoleRelation bean : closePoleFail) {
+ bean.setTaskId(lightTask.getTaskId());
+ }
+
+ List<LightTaskPoleRelation> all = new ArrayList<>();
+ all.addAll(newPoleSuccess);
+ all.addAll(oldPoleSuccess);
+ all.addAll(oldPoleFail);
+ all.addAll(closePoleFail);
+
+
+ List<Long> allPoleId = new ArrayList<>();
+ for (LightTaskPoleRelation bean : newPoleSuccess) {
+ allPoleId.add(bean.getPoleId());
+ }
+ for (LightTaskPoleRelation bean : oldPoleSuccess) {
+ allPoleId.add(bean.getPoleId());
+ }
+ for (LightTaskPoleRelation bean : oldPoleFail) {
+ allPoleId.add(bean.getPoleId());
+ }
+ for (LightTaskPoleRelation bean : closePoleSuccess) {
+ allPoleId.add(bean.getPoleId());
+ }
+
+ if (!allPoleId.isEmpty()) {
+ lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getPoleId, allPoleId));
+
+ }
+ if (!all.isEmpty()) {
+ lightTaskPoleRelationService.saveBatch(all);
+ }
+
+
+ /**
+ * 涓嬪彂璺伅浠诲姟鏃ュ織璁板綍寮�濮�
+ */
+ String content1 = "{浠诲姟ID锛�" + newLightTask.getTaskId() + "锛� 浠诲姟鍚嶏細" + newLightTask.getTaskName() + "}," + " 鐏潌ID锛�" + param.getPoleIdList().toString() + " }";
+ StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "涓嬪彂璺伅浠诲姟", content1);
+ /**
+ * 涓嬪彂璺伅浠诲姟鏃ュ織璁板綍缁撴潫
+ */
+
+ if (newPoleFail.isEmpty() && oldPoleFail.isEmpty() && closePoleFail.isEmpty()) {
+ return "缂栬緫鎴愬姛";
+ } else if (newPoleSuccess.isEmpty() && oldPoleSuccess.isEmpty() && closePoleSuccess.isEmpty()) {
+ throw new BusinessException("缂栬緫澶辫触,璇锋鏌ョ紪杈戠殑璁惧鏄惁鍦ㄧ嚎!");
+ } else if (!closePoleFail.isEmpty() || !oldPoleFail.isEmpty()) {
+ return "鍘熶换鍔′腑瀛樺湪涓嬪彂寮傚父锛屽師浠诲姟淇濈暀锛屽垱寤烘柊浠诲姟杩涜淇濆瓨";
+ } else if (!newPoleFail.isEmpty()) {
+ return "鏂颁换鍔′腑瀛樺湪涓嬪彂寮傚父锛屽拷鐣ュ紓甯告搷浣滅殑鐏潌";
+ } else {
+ return "鎿嶄綔寮傚父";
+ }
+ }
+
+
+ /**
+ * 鍙戦�佺伅鎺ц姹�
+ *
+ * @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);
+
+ StoreOperationRecordsUtils.storeInnerFrameData(deviceCode, "鍗曠伅甯�-鎺х伅", requestFrame, responseCommonFrame);
+
+ return Optional.ofNullable(responseCommonFrame).map(WrapResponseCommonFrame::getResponseInnerFrame).orElse(null);
+ }
+
+ public List<LightTaskDto> listLightTask(BaseConditionVO conditionVO, String keyword, Integer order, Integer seq) {
+ 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());
+ });
+ }
+
+ //鎺掑簭瀛楁
+ String orderByResult = "task_id";
+ //姝e簭銆佸�掑彊
+ String orderBySeq = OrderByEnums.ASC.getCode();
+ if (order != null) {
+ switch (order) {
+ case 1:
+ orderByResult = OrderByEnums.LIGHT_TASK_UPDATE_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(conditionVO.getPageNo(), conditionVO.getPageSize(), orderBy);
+ 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;
+ }
+
+ /**
+ * 鐢ㄤ簬浠诲姟鏂板,涓嬪彂
+ *
+ * @param lightTask
+ * @param poleIdList
+ * @param framePayload
+ * @param lightAddress
+ * @return
+ */
+ private List<LightTaskPoleRelation> sendControllerFrame(LightTask lightTask, List<Long> poleIdList, String framePayload, String lightAddress) {
+ List<LightTaskPoleRelation> lightTaskPoleRelationList = new ArrayList<>();
+ //鎴愬姛
+ List<LightTaskPoleRelation> success = new ArrayList<>();
+ //澶辫触
+ List<LightTaskPoleRelation> fail = new ArrayList<>();
+
+ Map map = new HashMap();
+
+ 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;
+ }
+
+ /**
+ * 鐢ㄤ簬浠诲姟缂栬緫
+ *
+ * @param poleIdList
+ * @param framePayload
+ * @param lightAddress
+ * @return
+ */
+ private Map<String, List<LightTaskPoleRelation>> sendControllerFrame(List<Long> poleIdList, String framePayload, String lightAddress) {
+ List<LightTaskPoleRelation> lightTaskPoleRelationList = new ArrayList<>();
+ //鎴愬姛
+ List<LightTaskPoleRelation> success = new ArrayList<>();
+ //澶辫触
+ List<LightTaskPoleRelation> fail = new ArrayList<>();
+
+ Map<String, List<LightTaskPoleRelation>> map = new HashMap();
+
+ 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());
+ fail.add(lightTaskPoleRelation);
+ } else {
+ lightTaskPoleRelation.setIssueStatus(HexUtil.hexToInt(a5LightTimerRespInnerFrame.getResponseStatus()));
+ success.add(lightTaskPoleRelation);
+ }
+ } catch (BusinessException e) {
+ lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode());
+ fail.add(lightTaskPoleRelation);
+ }
+
+ lightTaskPoleRelationList.add(lightTaskPoleRelation);
+ }
+ map.put("success", success);
+ map.put("fail", fail);
+ map.put("all", lightTaskPoleRelationList);
+ return map;
+ }
+
+
+ @Transactional(rollbackFor = Exception.class)
+ public boolean delLightTask(List<Long> taskIdList) {
+ List<LightTask> lightTaskList = listByIds(taskIdList);
+ if (CollectionUtil.isEmpty(lightTaskList)) {
+ 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).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("璇疯缃槦鏈�");
+ }
+
+ 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));
+
+ List<Long> taskIdList = new ArrayList<>();
+ for (LightTask bean : lightTasks) {
+ taskIdList.add(bean.getTaskId());
+ }
+ if (taskIdList.size() != 0) {
+ List<LightTaskPoleRelationBo> list = SpringContextHolder.getBean(LightTaskPoleRelationMapper.class).test(taskIdList);
+ for (LightTaskPoleRelationBo bean : list) {
+
+ LightTaskDto lightTaskDto = new LightTaskDto();
+ //TODO
+ BeanUtils.copyProperties(bean, lightTaskDto);
+ lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(bean.getWeek()));
+ BigDecimal energySaving;
+ BigDecimal energy;
+ //鑾峰彇鏄ㄥぉ鐨勬槦鏈熸暟
+ LocalDateTime now = LocalDateTime.now();
+ LocalDateTime yesterday = now.minusDays(1);
+ int week = yesterday.getDayOfWeek().getValue();
+ if (!list.isEmpty()) {
+ //鏈夌粦瀹氱伅鏉� 杩涜璁$畻
+ if (lightTaskDto.getWeekList().contains(week)) {
+ //浠诲姟鏈夎鏄熸湡鏁� 璁$畻
+ energySaving = SpringContextHolder.getBean(LightService.class).jisuan(lightTaskDto);
+ energy = SpringContextHolder.getBean(LightService.class).jisuanEnergy(lightTaskDto);
+ } else {
+ //浠诲姟鏃犺鏄熸湡鏁� 杩斿洖
+ energySaving = new BigDecimal(1);
+ energy = BigDecimal.ZERO;
+ }
+ for (int i = 0; i < list.size(); i++) {
+ LightTaskPoleRelation relation = list.get(i);
+ if (relation.getIssueStatus() != 0) {
+ //浠诲姟涓嬪彂澶辫触 杩斿洖
+ energySaving = new BigDecimal(1);
+ energy = BigDecimal.ZERO;
+ }
+ }
+ } else {
+ //鏈粦瀹氱伅鏉� 鐩存帴杩斿洖
+ energySaving = new BigDecimal(1);
+ energy = BigDecimal.ZERO;
+ }
+
+ /**
+ * 瀛樺偍鏁版嵁
+ */
+
+ LightEnergyData lightEnergyData = new LightEnergyData();
+ lightEnergyData.setTaskId(bean.getTaskId());
+ lightEnergyData.setYtdTime(yesterday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+ lightEnergyData.setEnergySaving(energySaving);
+ lightEnergyData.setEnergy(energy);
+ lightEnergyData.setUserId(bean.getUserId());
+ lightEnergyData.setClientId(bean.getClientId());
+ lightEnergyData.setPoleId(bean.getPoleId());
+ //淇濆瓨鑳借�楁暟鎹埌鏁版嵁搴�
+ lightEnergyDataService.save(lightEnergyData);
+ }
+
+ }
}
}
--
Gitblit v1.9.3