|  |  | 
 |  |  | import jakarta.validation.Valid; | 
 |  |  | import lombok.RequiredArgsConstructor; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.beans.factory.annotation.Value; | 
 |  |  | import org.springframework.http.MediaType; | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  | import org.springframework.validation.BindingResult; | 
 |  |  | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping(path = "createPlan", consumes = MediaType.APPLICATION_JSON_VALUE) | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     //@Transactional(rollbackFor = Exception.class) | 
 |  |  |     public BaseResponse<Boolean> createPlan(@RequestBody @Valid IrrigatePlan planAndSchedule, BindingResult bindingResult){ | 
 |  |  |         if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping(path = "publishPlan", consumes = MediaType.APPLICATION_JSON_VALUE) | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     //@Transactional(rollbackFor = Exception.class) | 
 |  |  |     public BaseResponse<Boolean> publishPlan(@RequestBody @Valid PlanSimple planSimple, BindingResult bindingResult){ | 
 |  |  |         if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         Long planId = planSimple.getPlanId(); | 
 |  |  |         Long operatorId = planSimple.getOperatorId(); | 
 |  |  |         Byte operateType = 1; | 
 |  |  |         Map map_result = irrigatePlanSv.publishPlan(planSimple); | 
 |  |  |         if(map_result.get("success").equals(false)) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(map_result.get("msg").toString()); | 
 |  |  |         } | 
 |  |  |         return BaseResponseUtils.buildSuccess() ; | 
 |  |  |  | 
 |  |  |         /** | 
 |  |  |          * 获取灌溉计划信息 | 
 |  |  |          * 更新灌溉计划起止时间及计划状态 | 
 |  |  |          */ | 
 |  |  |         VoPlanSimple plan = irrigatePlanSv.getPlanSimple(planId); | 
 |  |  |         if(plan == null){ | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("您要发布的计划不存在,或该计划已发布"); | 
 |  |  |         //Long planId = planSimple.getPlanId(); | 
 |  |  |         //Long operatorId = planSimple.getOperatorId(); | 
 |  |  |         //Byte operateType = 1; | 
 |  |  |         // | 
 |  |  |         ///** | 
 |  |  |         // * 获取灌溉计划信息 | 
 |  |  |         // * 更新灌溉计划起止时间及计划状态 | 
 |  |  |         // */ | 
 |  |  |         //VoPlanSimple plan = irrigatePlanSv.getPlanSimple(planId); | 
 |  |  |         //if(plan == null){ | 
 |  |  |         //    return BaseResponseUtils.buildErrorMsg("您要发布的计划不存在,或该计划已发布"); | 
 |  |  |         //} | 
 |  |  |         // | 
 |  |  |         //Byte startupMode = plan.getStartupMode(); | 
 |  |  |         //Integer duration = plan.getDuration(); | 
 |  |  |         //Date planStartTime = plan.getPlanStartTime(); | 
 |  |  |         //Date planStopTime = null; | 
 |  |  |         // | 
 |  |  |         //if(startupMode == 1){ | 
 |  |  |         //    planStartTime = new Date(); | 
 |  |  |         //} | 
 |  |  |         //LocalDateTime startTime = planStartTime.toInstant().atZone(ZoneId.systemDefault()) .toLocalDateTime(); | 
 |  |  |         //if(startupMode == 1){ | 
 |  |  |         //    // 测试阶段延后2分钟,正式发布为5分钟 | 
 |  |  |         //    startTime = startTime.plusMinutes(2); | 
 |  |  |         //    //startTime = startTime.plusMinutes(5); | 
 |  |  |         //} | 
 |  |  |         //planStartTime = Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant()); | 
 |  |  |         //LocalDateTime stopTime = startTime.plusMinutes(duration); | 
 |  |  |         //planStopTime = Date.from(stopTime.atZone(ZoneId.systemDefault()).toInstant()); | 
 |  |  |         // | 
 |  |  |         // | 
 |  |  |         //if(irrigatePlanSv.updatePlanTimes(planStartTime, planStopTime, planId) == 0) { | 
 |  |  |         //    return BaseResponseUtils.buildErrorMsg("计划信息更新失败"); | 
 |  |  |         //} | 
 |  |  |         // | 
 |  |  |         //// 更新每个灌溉次序的开始时间 | 
 |  |  |         //List<VoIrrigateSchedule> schedules = irrigatePlanSv.getSchedulesByPlanId(planId); | 
 |  |  |         //Date scheduleStartTime = null; | 
 |  |  |         //Integer sort = 0; | 
 |  |  |         //for(VoIrrigateSchedule schedule : schedules){ | 
 |  |  |         //    if(scheduleStartTime == null) { | 
 |  |  |         //        scheduleStartTime = planStartTime; | 
 |  |  |         //    } | 
 |  |  |         //    irrigatePlanSv.updateScheduleStartTime(schedule.getScheduleId(), scheduleStartTime); | 
 |  |  |         // | 
 |  |  |         //    // 计算下一组的开始时间 | 
 |  |  |         //    LocalDateTime LocalscheduleStartTime = scheduleStartTime.toInstant().atZone(ZoneId.systemDefault()) .toLocalDateTime(); | 
 |  |  |         //    LocalscheduleStartTime = LocalscheduleStartTime.plusMinutes(schedule.getDuration()); | 
 |  |  |         //    scheduleStartTime = Date.from(LocalscheduleStartTime.atZone(ZoneId.systemDefault()).toInstant()); | 
 |  |  |         //} | 
 |  |  |         // | 
 |  |  |         //// 添加灌溉计划操作记录 | 
 |  |  |         //IrPlanOperate planOperate  = new IrPlanOperate(); | 
 |  |  |         //planOperate.setPlanId(planId); | 
 |  |  |         //planOperate.setOperator(operatorId); | 
 |  |  |         //planOperate.setOperateType(OperateTypeENUM.PUBLISH.getCode()); | 
 |  |  |         //planOperate.setOperateTime(new Date()); | 
 |  |  |         //if(irrigatePlanSv.addPlanOperate(planOperate) == 0){ | 
 |  |  |         //    return BaseResponseUtils.buildErrorMsg("添加灌溉计划操作记录失败"); | 
 |  |  |         //} | 
 |  |  |         // | 
 |  |  |         //schedules = irrigatePlanSv.getSchedulesByPlanId(planId); | 
 |  |  |         //for(VoIrrigateSchedule schedule : schedules) { | 
 |  |  |         //    Long groupId = schedule.getGroupId(); | 
 |  |  |         //    List<Long> intakeIds = irrigatePlanSv.getIntakeIdsByGroupId(groupId); | 
 |  |  |         //    for(Long intakeId : intakeIds) { | 
 |  |  |         //        if(schedule.getDuration() > 0) { | 
 |  |  |         //            // 今发布灌溉时长大于0的 | 
 |  |  |         //            AutomaticClose automaticClose = new AutomaticClose(); | 
 |  |  |         //            automaticClose.setIntakeId(intakeId); | 
 |  |  |         //            automaticClose.setPlannedOpenTime(schedule.getStartTime()); | 
 |  |  |         //            automaticClose.setMinutes(schedule.getDuration()); | 
 |  |  |         //            automaticClose.setOperator(operatorId); | 
 |  |  |         //            automaticClose.setOpenType(Byte.valueOf("1")); | 
 |  |  |         // | 
 |  |  |         //            commandSv.planedOpenTimedClose(automaticClose, planId, operateType, schedule.getStartTime(), schedule.getDuration()); | 
 |  |  |         //        } | 
 |  |  |         //    } | 
 |  |  |         //} | 
 |  |  |         // | 
 |  |  |         //return BaseResponseUtils.buildSuccess(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据计划ID获取计划最新状态 | 
 |  |  |      * @param planId | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @GetMapping(path = "/getPlanLatestState") | 
 |  |  |     public BaseResponse<Integer> getPlanLatestState(@RequestParam Long planId) { | 
 |  |  |         if(planId == null) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("计划ID不能为空"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         Byte startupMode = plan.getStartupMode(); | 
 |  |  |         Integer duration = plan.getDuration(); | 
 |  |  |         Date planStartTime = plan.getPlanStartTime(); | 
 |  |  |         Date planStopTime = null; | 
 |  |  |  | 
 |  |  |         if(startupMode == 1){ | 
 |  |  |             planStartTime = new Date(); | 
 |  |  |         try { | 
 |  |  |             return BaseResponseUtils.buildSuccess(irrigatePlanSv.getPlanLatestState(planId)); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("获取未完的计划异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()); | 
 |  |  |         } | 
 |  |  |         LocalDateTime startTime = planStartTime.toInstant().atZone(ZoneId.systemDefault()) .toLocalDateTime(); | 
 |  |  |         if(startupMode == 1){ | 
 |  |  |             // 测试阶段延后2分钟,正式发布为5分钟 | 
 |  |  |             startTime = startTime.plusMinutes(2); | 
 |  |  |             //startTime = startTime.plusMinutes(5); | 
 |  |  |         } | 
 |  |  |         planStartTime = Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant()); | 
 |  |  |         LocalDateTime stopTime = startTime.plusMinutes(duration); | 
 |  |  |         planStopTime = Date.from(stopTime.atZone(ZoneId.systemDefault()).toInstant()); | 
 |  |  |  | 
 |  |  |         if(irrigatePlanSv.updatePlanTimes(planStartTime, planStopTime, planId) == 0) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("计划信息更新失败"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 更新每个灌溉次序的开始时间 | 
 |  |  |         List<VoIrrigateSchedule> schedules = irrigatePlanSv.getSchedulesByPlanId(planId); | 
 |  |  |         Date scheduleStartTime = null; | 
 |  |  |         Integer sort = 0; | 
 |  |  |         for(VoIrrigateSchedule schedule : schedules){ | 
 |  |  |             if(scheduleStartTime == null) { | 
 |  |  |                 scheduleStartTime = planStartTime; | 
 |  |  |             } | 
 |  |  |             irrigatePlanSv.updateScheduleStartTime(schedule.getScheduleId(), scheduleStartTime); | 
 |  |  |  | 
 |  |  |             // 计算下一组的开始时间 | 
 |  |  |             LocalDateTime LocalscheduleStartTime = scheduleStartTime.toInstant().atZone(ZoneId.systemDefault()) .toLocalDateTime(); | 
 |  |  |             LocalscheduleStartTime = LocalscheduleStartTime.plusMinutes(schedule.getDuration()); | 
 |  |  |             scheduleStartTime = Date.from(LocalscheduleStartTime.atZone(ZoneId.systemDefault()).toInstant()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 添加灌溉计划操作记录 | 
 |  |  |         IrPlanOperate planOperate  = new IrPlanOperate(); | 
 |  |  |         planOperate.setPlanId(planId); | 
 |  |  |         planOperate.setOperator(operatorId); | 
 |  |  |         planOperate.setOperateType(OperateTypeENUM.PUBLISH.getCode()); | 
 |  |  |         planOperate.setOperateTime(new Date()); | 
 |  |  |         if(irrigatePlanSv.addPlanOperate(planOperate) == 0){ | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("添加灌溉计划操作记录失败"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         schedules = irrigatePlanSv.getSchedulesByPlanId(planId); | 
 |  |  |         for(VoIrrigateSchedule schedule : schedules) { | 
 |  |  |             Long groupId = schedule.getGroupId(); | 
 |  |  |             List<Long> intakeIds = irrigatePlanSv.getIntakeIdsByGroupId(groupId); | 
 |  |  |             for(Long intakeId : intakeIds) { | 
 |  |  |                 if(schedule.getDuration() > 0) { | 
 |  |  |                     // 今发布灌溉时长大于0的 | 
 |  |  |                     AutomaticClose automaticClose = new AutomaticClose(); | 
 |  |  |                     automaticClose.setIntakeId(intakeId); | 
 |  |  |                     automaticClose.setPlannedOpenTime(schedule.getStartTime()); | 
 |  |  |                     automaticClose.setMinutes(schedule.getDuration()); | 
 |  |  |                     automaticClose.setOperator(operatorId); | 
 |  |  |                     automaticClose.setOpenType(Byte.valueOf("1")); | 
 |  |  |  | 
 |  |  |                     commandSv.planedOpenTimedClose(automaticClose, planId, operateType, schedule.getStartTime(), schedule.getDuration()); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return BaseResponseUtils.buildSuccess(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping(path = "terminatePlan", consumes = MediaType.APPLICATION_JSON_VALUE) | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     //@Transactional(rollbackFor = Exception.class) | 
 |  |  |     public BaseResponse<Boolean> terminatePlan(@RequestBody @Valid PlanSimple planSimple, BindingResult bindingResult){ | 
 |  |  |         if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |             return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  | 
 |  |  |         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | 
 |  |  |         String terminateStartTimeStr = LocalDate.now().getYear() + "-01-01 00:00:00"; | 
 |  |  |         Date terminateStartTime = Date.from(LocalDateTime.parse(terminateStartTimeStr, formatter).atZone(ZoneId.systemDefault()).toInstant()); | 
 |  |  |         Integer terminateDuration = 9999; | 
 |  |  |         Integer terminateDuration = 0; | 
 |  |  |  | 
 |  |  |         // 判断该计划是否可执行终止操作:未删除、未终止、已发布、当前时间小于计划结束时间 | 
 |  |  |         Date planStartTime = irrigatePlanSv.getToTerminatePlan(planId); | 
 |  |  | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据计划ID获取计划终止操作结果 | 
 |  |  |      * @param planId | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @GetMapping(path = "/getTerminateResults") | 
 |  |  |     public BaseResponse<VoPlanDetails> getTerminateResults(@RequestParam Long planId) { | 
 |  |  |         if(planId == null) { | 
 |  |  |             return BaseResponseUtils.buildErrorMsg("计划ID不能为空"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         try { | 
 |  |  |             VoPlanDetails res = irrigatePlanSv.getTerminateResults(planId); | 
 |  |  |             return BaseResponseUtils.buildSuccess(res); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("获取计划发布结果异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  | } |