| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.sandu.ximon.admin.service; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | 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.PageHelper; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | 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.param.LightTaskParam; |
| | | import com.sandu.ximon.admin.security.SecurityUtils; |
| | | import com.sandu.ximon.dao.domain.LightTask; |
| | | import com.sandu.ximon.dao.domain.LightTaskPoleRelation; |
| | | import com.sandu.ximon.dao.domain.LightTaskQuartz; |
| | | import com.sandu.ximon.dao.mapper.LightTaskMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @author chenjiantian |
| | | * @date 2021/12/15 16:33 |
| | | * è·¯ç¯ä»»å¡æä½ |
| | | */ |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class LightTaskService extends BaseServiceImpl<LightTaskMapper, LightTask> { |
| | | |
| | | |
| | | private final LightTaskQuartzService lightTaskQuartzService; |
| | | private final PoleService poleService; |
| | | private final LightTaskPoleRelationService lightTaskPoleRelationService; |
| | | |
| | | /** |
| | | * æ°å¢è·¯ç¯ä»»å¡ |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean addLightTask(LightTaskParam param) { |
| | | if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) { |
| | | throw new BusinessException("ç¯æ§å½ä»¤æ ¼å¼ä¸æ£ç¡®"); |
| | | } |
| | | LightTask lightTask = new LightTask(); |
| | | lightTask.setClientId(SecurityUtils.getClientId()); |
| | | lightTask.setTaskName(param.getTaskName()); |
| | | lightTask.setWeek(param.getWeek()); |
| | | lightTask.setCloseOrder(param.getCloseOrder()); |
| | | lightTask.setOpenOrder(param.getOpenOrder()); |
| | | lightTask.setControlOrder(param.getControlOrder()); |
| | | lightTask.setCreateUser(SecurityUtils.getUsername()); |
| | | if (!save(lightTask)) { |
| | | throw new BusinessException("ä¿åè·¯ç¯ä»»å¡å¤±è´¥"); |
| | | } |
| | | List<LightTaskQuartz> list = lightTaskQuartzService.addLightTaskQuartz(lightTask); |
| | | if (CollectionUtil.isEmpty(list)) { |
| | | throw new BusinessException("æ·»å 宿¶ä»»å¡å¤±è´¥"); |
| | | } |
| | | // æ·»å ç»å®ç¯æ |
| | | if (!lightTaskPoleRelationService.updateLightTaskPoleRelation(lightTask.getTaskId(), param.getPoleIdList())) { |
| | | throw new BusinessException("ç»å®ç¯æå¤±è´¥"); |
| | | } |
| | | |
| | | List<Long> poleIdList = param.getPoleIdList(); |
| | | if (CollectionUtil.isNotEmpty(poleIdList)) { |
| | | List<String> deviceCodeList = poleService.listDeviceCodeByIds(poleIdList); |
| | | if (CollectionUtil.isEmpty(deviceCodeList)) { |
| | | throw new BusinessException("æ¾ä¸å°ç¯æmacï¼æ æ³åé宿¶ä»»å¡"); |
| | | } |
| | | // è·å宿¶ç¯æ§å½ä»¤ |
| | | String framePayload = list.get(0).getFramePayload(); |
| | | |
| | | // rrpc åç宿¶å½ä»¤ |
| | | for (String deviceCode : deviceCodeList) { |
| | | sendTimeRRpc(framePayload, deviceCode); |
| | | } |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * åéç¯æ§è¯·æ± |
| | | * |
| | | * @param framePayload ç¯æ§åæ° |
| | | * @param deviceCode 设å¤å |
| | | * @return è¿å帧 |
| | | */ |
| | | public A5LightTimerRespInnerFrame sendTimeRRpc(String framePayload, String deviceCode) { |
| | | IRequestFrame requestFrame = FrameBuilder.builderA5().innerFrame(new A5LightTimerReqInnerFrame(framePayload)) |
| | | .orderType(A5OrderEnum.REQUEST_LIGHT_DATA.getCode()).build(); |
| | | WrapResponseCommonFrame<A5LightTimerRespInnerFrame> responseCommonFrame = MainBoardInvokeSyncService.getInstance() |
| | | .sendRRPC(deviceCode, requestFrame, A5LightTimerRespInnerFrame.class); |
| | | System.out.println("åéç»æ"); |
| | | A5LightTimerRespInnerFrame responseInnerFrame = responseCommonFrame.getResponseInnerFrame(); |
| | | System.out.println(responseInnerFrame.getResponseStatus()); |
| | | return responseInnerFrame; |
| | | } |
| | | |
| | | public List<LightTask> listLightTask(int pageNo, int pageSize, String keyword) { |
| | | LambdaQueryWrapper<LightTask> wrapper = Wrappers.lambdaQuery(LightTask.class); |
| | | if (StrUtil.isNotBlank(keyword)) { |
| | | wrapper.like(LightTask::getTaskName, keyword); |
| | | } |
| | | Long clientId = SecurityUtils.getClientId(); |
| | | if (clientId != null) { |
| | | wrapper.eq(LightTask::getClientId, clientId); |
| | | } |
| | | |
| | | PageHelper.startPage(pageNo, pageSize); |
| | | return list(wrapper); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean 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("æ¾ä¸å°è·¯ç¯ä»»å¡"); |
| | | } |
| | | |
| | | lightTask.setTaskName(param.getTaskName()); |
| | | lightTask.setWeek(param.getWeek()); |
| | | lightTask.setControlOrder(param.getControlOrder()); |
| | | lightTask.setOpenOrder(param.getOpenOrder()); |
| | | lightTask.setCloseOrder(param.getCloseOrder()); |
| | | lightTask.setUpdateTime(LocalDateTime.now()); |
| | | |
| | | if (!updateById(lightTask)) { |
| | | throw new BusinessException("ç¼è¾ä»»å¡å¤±è´¥"); |
| | | } |
| | | |
| | | List<LightTaskQuartz> list = lightTaskQuartzService.updateLightTaskQuartz(lightTask); |
| | | if (CollectionUtil.isEmpty(list)) { |
| | | throw new BusinessException("ç¼è¾å®æ¶ä»»å¡å¤±è´¥"); |
| | | } |
| | | |
| | | // æ·»å ç»å®ç¯æ |
| | | if (!lightTaskPoleRelationService.updateLightTaskPoleRelation(lightTask.getTaskId(), param.getPoleIdList())) { |
| | | throw new BusinessException("ç»å®ç¯æå¤±è´¥"); |
| | | } |
| | | List<Long> poleIdList = param.getPoleIdList(); |
| | | if (CollectionUtil.isNotEmpty(poleIdList)) { |
| | | List<String> deviceCodeList = poleService.listDeviceCodeByIds(poleIdList); |
| | | if (CollectionUtil.isEmpty(deviceCodeList)) { |
| | | throw new BusinessException("æ¾ä¸å°ç¯æmacï¼æ æ³åé宿¶ä»»å¡"); |
| | | } |
| | | // è·å宿¶ç¯æ§å½ä»¤ |
| | | String framePayload = list.get(0).getFramePayload(); |
| | | |
| | | // rrpc åç宿¶å½ä»¤ |
| | | for (String deviceCode : deviceCodeList) { |
| | | sendTimeRRpc(framePayload, deviceCode); |
| | | } |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean delLightTask(List<Long> taskIdList) { |
| | | List<LightTask> lightTaskList = listByIds(taskIdList); |
| | | if (CollectionUtil.isEmpty(lightTaskList)) { |
| | | throw new BusinessException("æ¾ä¸å°ä»»å¡ä¿¡æ¯"); |
| | | } |
| | | |
| | | // å é¤ä»»å¡ |
| | | if (!removeByIds(taskIdList)) { |
| | | throw new BusinessException("å é¤ä»»å¡å¤±è´¥"); |
| | | } |
| | | |
| | | // å é¤ç»å®ç¯æ |
| | | if (!lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getTaskId, taskIdList))) { |
| | | throw new BusinessException("å é¤ç»å®ç¯æå¤±è´¥"); |
| | | } |
| | | |
| | | // å é¤å®æ¶å¨ |
| | | if (!lightTaskQuartzService.removeLightTaskQuartz(taskIdList)) { |
| | | throw new BusinessException("å é¤å®æ¶å¨å¤±è´¥"); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | } |