package com.sandu.ximon.admin.service; import cn.hutool.core.bean.BeanUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.sandu.common.execption.BusinessException; import com.sandu.common.service.impl.BaseServiceImpl; import com.sandu.ximon.admin.config.RealtimeServerBean; import com.sandu.ximon.admin.param.LEDScheduleParam_xixun; import com.sandu.ximon.admin.security.SecurityUtils; import com.sandu.ximon.admin.utils.LightemitUtils; import com.sandu.ximon.admin.utils.request.Schedules; import com.sandu.ximon.admin.utils.request.Task; import com.sandu.ximon.admin.utils.request.TaskSchedules; import com.sandu.ximon.dao.domain.LedScheduleEntity; import com.sandu.ximon.dao.domain.PoleLightemitEntity; import com.sandu.ximon.dao.mapper.LedScheduleEntityMapper; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.util.*; /** * @Author liuhaonan * @Date 2022/2/28 13:45 * @Version 1.0 */ @Service @AllArgsConstructor public class LedScheduleService extends BaseServiceImpl { private final LedScheduleEntityMapper ledScheduleEntityMapper; private final ClientService clientService; private final PoleLightemitService poleLightemitService; private final LightemitUtils lightemitUtils; private final RealtimeServerBean realtimeServerBean; public boolean insert(LEDScheduleParam_xixun ledEntity) { LedScheduleEntity ledScheduleEntity = new LedScheduleEntity(); BeanUtil.copyProperties(ledEntity,ledScheduleEntity); ledScheduleEntity.setUserId(SecurityUtils.getClientId()); Schedules schedules = new Schedules(); Task task = new Task(); TaskSchedules taskSchedules = new TaskSchedules(); if(ledEntity.getStartTime() =="" || ledEntity.getEndTime() == "" || ledEntity.getStartTime() == null || ledEntity.getEndTime() ==null ){ schedules.setTimeType("All"); }else { schedules.setTimeType("Range"); schedules.setStartTime(ledEntity.getStartTime()); schedules.setEndTime(ledEntity.getEndTime()); } if(ledEntity.getStartDate() =="" || ledEntity.getEndDate() == "" || ledEntity.getStartDate() == null || ledEntity.getEndDate() ==null ){ schedules.setDateType("All"); }else { schedules.setDateType("Range"); schedules.setStartDate(ledEntity.getStartDate()); schedules.setEndDate(ledEntity.getEndDate()); } if(ledEntity.getWeek().length != 0){ schedules.setFilterType("Week"); schedules.setWeekFilter(ledEntity.getWeek()); }else { schedules.setFilterType("None"); } List list =new ArrayList(); list.add(schedules); task.setSchedules(list); task.setCreateDate(ledEntity.getCreateDate()); task.setDateCreated(ledEntity.getCreateDate()); String id = UUID.randomUUID().toString(); task.set_id(id); task.setId(id); task.set_departmentd(UUID.randomUUID().toString()); task.set_role(UUID.randomUUID().toString()); task.setName(ledEntity.getName()); taskSchedules.setTask(task); String json = JSON.toJSONString(taskSchedules, SerializerFeature.WriteMapNullValue); // String json = new Gson().toJson(taskSchedules); ledScheduleEntity.setSchedule(json); return this.save(ledScheduleEntity); } /** * 定时推送 * @param scheduleId * @param lightemitIds */ public void ledschedulepush(Integer scheduleId, Long[] lightemitIds) { LedScheduleEntity ledScheduleEntity = baseMapper.selectById(scheduleId); // SendCommand sendCommand = new SendCommand(); // sendCommand.setTask(ledScheduleEntity.getSchedule()); // String json = new Gson().toJson(sendCommand); Collection poleLightemitEntities = poleLightemitService.listByIds(Arrays.asList(lightemitIds)); if(poleLightemitEntities != null){ for (PoleLightemitEntity entity: poleLightemitEntities) { lightemitUtils.post(realtimeServerBean.getCommand() + entity.getLightemitControlCode(), ledScheduleEntity.getSchedule()); } } } public boolean updateSchedule(LEDScheduleParam_xixun paramXixun){ if(paramXixun.getId()==null){ throw new BusinessException("参数错误"); } LedScheduleEntity byId = getById(paramXixun.getId()); if(byId==null){ throw new BusinessException("未找到该定时数据"); } boolean result=false; if( removeById(paramXixun.getId())){ result = insert(paramXixun); } return result; } public LedScheduleEntity getSchedule(Integer id){ if(id==null){ throw new BusinessException("参数错误"); } LedScheduleEntity byId = getById(id); if(byId==null){ throw new BusinessException("未找到该定时数据"); } //List schedule = JSON.parseArray(byId.getSchedule(), List.class); return byId; } }