| package com.dy.pipIrrIrrigate.irrigatePlan; | 
|   | 
| import com.alibaba.fastjson2.JSON; | 
| import com.alibaba.fastjson2.JSONObject; | 
| import com.dy.common.multiDataSource.DataSourceContext; | 
| import com.dy.common.mw.protocol.Command; | 
| import com.dy.common.mw.protocol.CommandType; | 
| import com.dy.common.webUtil.BaseResponse; | 
| import com.dy.common.webUtil.BaseResponseUtils; | 
| import com.dy.common.webUtil.QueryConditionVo; | 
| import com.dy.common.webUtil.QueryResultVo; | 
| import com.dy.pipIrrGlobal.daoIr.*; | 
| import com.dy.pipIrrGlobal.daoRm.RmCommandHistoryMapper; | 
| import com.dy.pipIrrGlobal.pojoIr.IrIrrigatePlan; | 
| import com.dy.pipIrrGlobal.pojoIr.IrIrrigateSchedule; | 
| import com.dy.pipIrrGlobal.pojoIr.IrPlanOperate; | 
| import com.dy.pipIrrGlobal.pojoIr.IrPlanSchedule; | 
| import com.dy.pipIrrGlobal.rtuMw.CodeLocal; | 
| import com.dy.pipIrrGlobal.voIr.*; | 
| import com.dy.pipIrrGlobal.voRm.VoIntakeVc; | 
| import com.dy.pipIrrIrrigate.command.CommandSv; | 
| import com.dy.pipIrrIrrigate.command.dto.AutomaticClose; | 
| import com.dy.pipIrrIrrigate.command.dto.ValveClose; | 
| import com.dy.pipIrrIrrigate.irrigatePlan.dto.IrrigatePlan; | 
| import com.dy.pipIrrIrrigate.irrigatePlan.dto.IrrigateSchedule; | 
| import com.dy.pipIrrIrrigate.irrigatePlan.dto.PlanSimple; | 
| import com.dy.pipIrrIrrigate.irrigatePlan.enums.OperateTypeENUM; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.apache.dubbo.common.utils.PojoUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.core.env.Environment; | 
| import org.springframework.http.HttpEntity; | 
| import org.springframework.http.HttpHeaders; | 
| import org.springframework.http.HttpMethod; | 
| import org.springframework.http.ResponseEntity; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
| import org.springframework.web.client.RestTemplate; | 
| import org.springframework.web.util.UriComponentsBuilder; | 
|   | 
| import java.time.LocalDate; | 
| import java.time.LocalDateTime; | 
| import java.time.ZoneId; | 
| import java.time.format.DateTimeFormatter; | 
| import java.util.*; | 
|   | 
| /** | 
|  * @author ZhuBaoMin | 
|  * @date 2025-06-30 14:58 | 
|  * @LastEditTime 2025-06-30 14:58 | 
|  * @Description | 
|  */ | 
|   | 
| @Slf4j | 
| @Service | 
| public class IrrigatePlanSv { | 
|     @Autowired | 
|     private IrIrrigatePlanMapper irrigatePlanMapper; | 
|   | 
|     @Autowired | 
|     private IrIrrigateScheduleMapper irIrrigateScheduleMapper; | 
|   | 
|     @Autowired | 
|     private IrPlanScheduleMapper irPlanScheduleMapper; | 
|   | 
|     @Autowired | 
|     private IrPlanOperateMapper irPlanOperateMapper; | 
|   | 
|     @Autowired | 
|     private IrGroupIntakeMapper irGroupIntakeMapper; | 
|   | 
|     @Autowired | 
|     private CommandSv commandSv; | 
|   | 
|     @Autowired | 
|     private IrIntakeOperateMapper irIntakeOperateMapper; | 
|   | 
|     @Autowired | 
|     private RmCommandHistoryMapper rmdCommandHistoryMapper; | 
|   | 
|     @Autowired | 
|     private IrIrrigateGroupMapper irIrrigateGroupMapper; | 
|   | 
|     protected String comSendUrl; | 
|     private String key_mw = "comSendUrl"; | 
|   | 
|     @Value("${irr.plan.delay:5}") | 
|     private Integer irrPlanDelay;//轮灌中计划开阀的延迟时长 | 
|   | 
|     private static final Integer irrPlanDelayDefault = 5;//轮灌中计划开阀的默认延迟时长 | 
|   | 
|     @Value("${irr.plan.preOpeningTime:10}") | 
|     private Integer preOpeningTime; | 
|   | 
|     @Value("${irr.plan.planTerminateLimitMinutes:5}") | 
|     private Integer planTerminateLimitMinutes; // 计划终止后限制发布新计划的时间间隔(分钟) | 
|   | 
|     /** | 
|      * 添加灌溉计划 | 
|      * | 
|      * @param po | 
|      * @return | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public Long addIrrigatePlan(IrIrrigatePlan po) { | 
|         irrigatePlanMapper.insert(po); | 
|         return po.getId(); | 
|     } | 
|   | 
|     /** | 
|      * 添加灌溉次序记录 | 
|      * | 
|      * @param po | 
|      * @return | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public Long addIrrigateSchedule(IrIrrigateSchedule po) { | 
|         irIrrigateScheduleMapper.insert(po); | 
|         return po.getId(); | 
|     } | 
|   | 
|     /** | 
|      * 添加计划次序关联记录 | 
|      * | 
|      * @param po | 
|      * @return | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public Long addPlanSchedule(IrPlanSchedule po) { | 
|         irPlanScheduleMapper.insert(po); | 
|         return po.getId(); | 
|     } | 
|   | 
|     /** | 
|      * 添加灌溉计划操作记录 | 
|      * | 
|      * @param po | 
|      * @return | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public Long addPlanOperate(IrPlanOperate po) { | 
|         irPlanOperateMapper.insert(po); | 
|         return po.getId(); | 
|     } | 
|   | 
|     /** | 
|      * 创建灌溉计划 | 
|      * 1. 添加灌溉计划 | 
|      * 2. 添加灌溉次序 | 
|      * 3. 添加灌溉计划操作记录 | 
|      * @param planAndSchedule | 
|      * @return | 
|      */ | 
|     public Map createPlan(IrrigatePlan planAndSchedule) { | 
|         Map map = new HashMap<>(); | 
|         map.put("success", false); | 
|   | 
|         Long projectId = planAndSchedule.getProjectId(); | 
|         Long operatorId = planAndSchedule.getOperatorId(); | 
|         Byte startupMode = planAndSchedule.getStartupMode(); | 
|         Date planStartTime = planAndSchedule.getPlanStartTime();; | 
|         Date planStopTime = null; | 
|   | 
|         Integer duration = 0; | 
|         for(IrrigateSchedule schedule : planAndSchedule.getSchedules()){ | 
|             duration = duration + schedule.getDuration(); | 
|         } | 
|   | 
|         if(startupMode == 2){ | 
|             if(planStartTime == null) { | 
|                 map.put("msg", "自动启动模式必须指定计划启动时间"); | 
|                 map.put("content", null); | 
|                 return map; | 
|             } | 
|   | 
|             LocalDateTime startTime = planStartTime.toInstant().atZone(ZoneId.systemDefault()) .toLocalDateTime(); | 
|             LocalDateTime currentTime = LocalDateTime.now(); | 
|             currentTime = currentTime.plusHours(8); | 
|   | 
|             if(!startTime.isAfter(currentTime)) { | 
|                 map.put("msg", "启动时间不能在8小时之内"); | 
|                 map.put("content", null); | 
|                 return map; | 
|             } | 
|         } | 
|   | 
|         IrIrrigatePlan plan = new IrIrrigatePlan(); | 
|         plan.setProjectId(projectId); | 
|         plan.setPlanName(planAndSchedule.getPlanName()); | 
|         plan.setStartupMode(startupMode); | 
|         plan.setPlanStartTime(planStartTime); | 
|         plan.setDuration(duration); | 
|         plan.setPlanState((byte)1); | 
|         plan.setExecutingState((byte)1); | 
|         plan.setDeleted(0L); | 
|   | 
|         Long planId = addIrrigatePlan(plan); | 
|         if(planId == null) { | 
|             map.put("msg", "创建灌溉计划失败"); | 
|             map.put("content", null); | 
|             return map; | 
|         } | 
|   | 
|         // 添加灌溉次序及计划次序关联表 | 
|         Integer sort = 1; | 
|         for(IrrigateSchedule schedule : planAndSchedule.getSchedules()){ | 
|             IrIrrigateSchedule po = new IrIrrigateSchedule(); | 
|             po.setGroupId(schedule.getGroupId()); | 
|             po.setDuration(schedule.getDuration()); | 
|             po.setCurrentState((byte)1); | 
|             Long scheduleId = addIrrigateSchedule(po); | 
|             if(scheduleId == null) { | 
|                 map.put("msg", "创建灌溉次序失败"); | 
|                 map.put("content", null); | 
|                 return map; | 
|             } | 
|   | 
|             IrPlanSchedule planSchedule = new IrPlanSchedule(); | 
|             planSchedule.setPlanId(planId); | 
|             planSchedule.setScheduleId(scheduleId); | 
|             planSchedule.setSort(sort++); | 
|             Long planScheduleId = addPlanSchedule(planSchedule); | 
|             if(planScheduleId == null) { | 
|                 map.put("msg", "创建灌溉次序失败"); | 
|                 map.put("content", null); | 
|                 return map; | 
|             } | 
|   | 
|         } | 
|   | 
|         // 添加灌溉计划操作记录 | 
|         IrPlanOperate planOperate  = new IrPlanOperate(); | 
|         planOperate.setPlanId(planId); | 
|         planOperate.setOperator(operatorId); | 
|         planOperate.setOperateType(OperateTypeENUM.CREATE.getCode()); | 
|         planOperate.setOperateTime(new Date()); | 
|         if(addPlanOperate(planOperate) == 0){ | 
|             map.put("msg", "添加灌溉计划操作记录失败"); | 
|             map.put("content", null); | 
|             return map; | 
|         } | 
|   | 
|         map.put("success", true); | 
|         map.put("msg", "灌溉计划创建成功"); | 
|         map.put("content", null); | 
|         return map; | 
|     } | 
|   | 
|     /** | 
|      * 删除灌溉计划 | 
|      * | 
|      * @param planSimple | 
|      * @return | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public Map deletePlan(PlanSimple planSimple) { | 
|         Long planId = planSimple.getPlanId(); | 
|         Long operatorId = planSimple.getOperatorId(); | 
|   | 
|         Integer planState = irrigatePlanMapper.getPlanState(planId); | 
|         if (planState == null) { | 
|             Map map = new HashMap<>(); | 
|             map.put("success", false); | 
|             map.put("msg", "灌溉计划不存在"); | 
|             map.put("content", null); | 
|             return map; | 
|         } | 
|         if (planState != 1) { | 
|             Map map = new HashMap<>(); | 
|             map.put("success", false); | 
|             map.put("msg", "灌溉计划非草稿状态,不允许删除"); | 
|             map.put("content", null); | 
|             return map; | 
|         } | 
|   | 
|         try { | 
|             irrigatePlanMapper.deleteByPrimaryKey(planId); | 
|             // 添加灌溉计划操作记录 | 
|             IrPlanOperate planOperate = new IrPlanOperate(); | 
|             planOperate.setPlanId(planId); | 
|             planOperate.setOperator(operatorId); | 
|             planOperate.setOperateType(OperateTypeENUM.DELETE.getCode()); | 
|             planOperate.setOperateTime(new Date()); | 
|             addPlanOperate(planOperate); | 
|   | 
|             Map map = new HashMap<>(); | 
|             map.put("success", true); | 
|             map.put("msg", "灌溉项目删除成功"); | 
|             map.put("content", null); | 
|             return map; | 
|         } catch (Exception e) { | 
|             Map map = new HashMap<>(); | 
|             map.put("success", false); | 
|             map.put("msg", "灌溉项目删除失败"); | 
|             map.put("content", null); | 
|             return map; | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 根据计划ID获取灌溉次序记录 | 
|      * | 
|      * @param planId | 
|      * @return | 
|      */ | 
|     public List<VoIrrigateSchedule> getSchedulesByPlanId(Long planId) { | 
|         return irIrrigateScheduleMapper.getSchedulesByPlanId(planId); | 
|     } | 
|   | 
|     /** | 
|      * 根据灌溉次序ID更新次序开始时间 | 
|      * | 
|      * @param scheduleId | 
|      * @param startTime | 
|      * @return | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public Integer updateScheduleStartTime(Long scheduleId, Date startTime) { | 
|         return irIrrigateScheduleMapper.updateScheduleStartTime(scheduleId, startTime); | 
|     } | 
|   | 
|     @Transactional(rollbackFor = Exception.class) | 
|     Integer updatePlanTimes(Date planStartTime, Date planEndTime, Long planId) { | 
|         return irrigatePlanMapper.updatePlanTimes(planStartTime, planEndTime, planId); | 
|     } | 
|   | 
|     /** | 
|      * 根据组ID获取取水口ID集合 | 
|      * | 
|      * @param groupId | 
|      * @return | 
|      */ | 
|     public List<Long> getIntakeIdsByGroupId(Long groupId) { | 
|         return irGroupIntakeMapper.getIntakeIdsByGroupId(groupId); | 
|     } | 
|   | 
|     /** | 
|      * 发布灌溉计划 | 
|      * | 
|      * @param planSimple | 
|      * @return | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public Map publishPlan(PlanSimple planSimple) { | 
|         Long planId = planSimple.getPlanId(); | 
|         Long operatorId = planSimple.getOperatorId(); | 
|         Byte operateType = 1; | 
|   | 
|         // 检查当前项目是否存在未完成的灌溉计划 | 
|         if (irrigatePlanMapper.hasPlan_CurrentProject(planId) > 0) { | 
|             Map map = new HashMap<>(); | 
|             map.put("success", false); | 
|             map.put("msg", "当前项目存在未完成的灌溉计划"); | 
|             map.put("content", null); | 
|             return map; | 
|         } | 
|   | 
|         // 检查其他项目中是否存在未完成的灌溉计划 | 
|         if (irrigatePlanMapper.hasPlan_OtherProject(planId) > 0) { | 
|             Map map = new HashMap<>(); | 
|             map.put("success", false); | 
|             map.put("msg", "待发布计划的轮灌组在其他项目的灌溉计划中,且该计划尚未完成"); | 
|             map.put("content", null); | 
|             return map; | 
|         } | 
|   | 
|         // 检查5分钟内是否有终止的计划,且轮灌组有重叠 | 
|         Long lastTerminatedPlanId = irPlanOperateMapper.getLastTerminatedPlanId(planTerminateLimitMinutes); | 
|         if (lastTerminatedPlanId != null) { | 
|             // 检查当前计划与最近终止计划的轮灌组是否有重叠 | 
|             if (irrigatePlanMapper.hasPlanOverlapWithTerminated(planId, lastTerminatedPlanId) > 0) { | 
|                 Map map = new HashMap<>(); | 
|                 map.put("success", false); | 
|                 map.put("msg", "上一个计划终止后" + planTerminateLimitMinutes + "分钟内不能发布新计划"); | 
|                 map.put("content", null); | 
|                 return map; | 
|             } | 
|         } | 
|   | 
|         /** | 
|          * 获取灌溉计划信息 | 
|          * 更新灌溉计划起止时间及计划状态 | 
|          */ | 
|         VoPlanSimple plan = irrigatePlanMapper.getPlanSimple(planId); | 
|         if (plan == null) { | 
|             Map map = new HashMap<>(); | 
|             map.put("success", false); | 
|             map.put("msg", "您要发布的计划不存在,或该计划已发布"); | 
|             map.put("content", null); | 
|             return map; | 
|         } | 
|   | 
|         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) { | 
|             if (irrPlanDelay == null || irrPlanDelay <= 0) { | 
|                 irrPlanDelay = irrPlanDelayDefault; | 
|             } | 
|             startTime = startTime.plusMinutes(irrPlanDelay); | 
|         } | 
|         planStartTime = Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant()); | 
|   | 
|         // 更新每个灌溉次序的开始时间并计算最后一组关阀时间 | 
|         List<VoIrrigateSchedule> schedules = getSchedulesByPlanId(planId); | 
|         Date scheduleStartTime = null; | 
|         LocalDateTime lastScheduleEndTime = null; | 
|         Integer sort = 0; | 
|   | 
|         // 第一次遍历:更新每个灌溉次序的开始时间 | 
|         for (VoIrrigateSchedule schedule : schedules) { | 
|             if (scheduleStartTime == null) { | 
|                 scheduleStartTime = planStartTime; | 
|             } | 
|             this.updateScheduleStartTime(schedule.getScheduleId(), scheduleStartTime); | 
|   | 
|             // 计算当前次序的结束时间 | 
|             LocalDateTime currentScheduleStartTime = scheduleStartTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); | 
|             LocalDateTime currentScheduleEndTime = currentScheduleStartTime.plusMinutes(schedule.getDuration()); | 
|   | 
|             // 更新最后一组的结束时间 | 
|             lastScheduleEndTime = currentScheduleEndTime; | 
|   | 
|             // 计算下一组的开始时间 | 
|             LocalDateTime LocalscheduleStartTime = scheduleStartTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); | 
|             LocalscheduleStartTime = LocalscheduleStartTime.plusMinutes(schedule.getDuration()); | 
|             LocalscheduleStartTime = LocalscheduleStartTime.minusMinutes(preOpeningTime); | 
|             scheduleStartTime = Date.from(LocalscheduleStartTime.atZone(ZoneId.systemDefault()).toInstant()); | 
|   | 
|             // 确保下一组的开始时间不早于计划开始时间 | 
|             if (scheduleStartTime.before(planStartTime)) { | 
|                 scheduleStartTime = planStartTime; | 
|             } | 
|         } | 
|   | 
|         // 根据最后一组关阀时间设置计划结束时间 | 
|         if (lastScheduleEndTime != null) { | 
|             planStopTime = Date.from(lastScheduleEndTime.atZone(ZoneId.systemDefault()).toInstant()); | 
|         } else { | 
|             // 如果没有灌溉次序,则使用原来的计算方式 | 
|             LocalDateTime stopTime = startTime.plusMinutes(duration); | 
|             planStopTime = Date.from(stopTime.atZone(ZoneId.systemDefault()).toInstant()); | 
|         } | 
|   | 
|         // 根据计划ID更新计划信息(起止时间、计划状态) | 
|         if (this.updatePlanTimes(planStartTime, planStopTime, planId) == 0) { | 
|             Map map = new HashMap<>(); | 
|             map.put("success", false); | 
|             map.put("msg", "计划信息更新失败"); | 
|             map.put("content", null); | 
|             return map; | 
|         } | 
|   | 
|         // 添加灌溉计划操作记录 | 
|         IrPlanOperate planOperate = new IrPlanOperate(); | 
|         planOperate.setPlanId(planId); | 
|         planOperate.setOperator(operatorId); | 
|         planOperate.setOperateType(OperateTypeENUM.PUBLISH.getCode()); | 
|         planOperate.setOperateTime(new Date()); | 
|         if (addPlanOperate(planOperate) == 0) { | 
|             Map map = new HashMap<>(); | 
|             map.put("success", false); | 
|             map.put("msg", "添加灌溉计划操作记录失败"); | 
|             map.put("content", null); | 
|             return map; | 
|         } | 
|   | 
|         schedules = getSchedulesByPlanId(planId); | 
|         for (VoIrrigateSchedule schedule : schedules) { | 
|             Long groupId = schedule.getGroupId(); | 
|             List<Long> intakeIds = getIntakeIdsByGroupId(groupId); | 
|             if(intakeIds == null || intakeIds.size() <= 0) { | 
|                 Map map = new HashMap<>(); | 
|                 map.put("success", false); | 
|                 map.put("msg", "当前轮灌组内没有有效的取水口"); | 
|                 map.put("content", null); | 
|                 return map; | 
|             } | 
|             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()); | 
|                 } | 
|             } | 
|         } | 
|   | 
|         Map map = new HashMap<>(); | 
|         map.put("success", true); | 
|         map.put("msg", "灌溉计划发布成功"); | 
|         map.put("content", null); | 
|         return map; | 
|     } | 
|   | 
|     /** | 
|      * 根据计划ID获取待终止计划的结束时间:未删除、未终止、已发布、当前时间小于计划结束时间 | 
|      * | 
|      * @param planId | 
|      * @return | 
|      */ | 
|     public Date getToTerminatePlan(Long planId) { | 
|         return irrigatePlanMapper.getToTerminatePlan(planId); | 
|     } | 
|   | 
|     /** | 
|      * 根据计划ID获取待终止的取水口列表(开阀成功的) | 
|      * | 
|      * @param planId | 
|      * @return | 
|      */ | 
|     public List<VoToTerminateIntakes> getToTerminateIntakes(Long planId) { | 
|         return irIntakeOperateMapper.getToTerminateIntakes(planId); | 
|     } | 
|   | 
|     /** | 
|      * 根据命令日志ID获取取水口及虚拟卡信息,终止灌溉计划时使用,用来执行远程关阀 | 
|      * | 
|      * @param commandId | 
|      * @return | 
|      */ | 
|     public VoIntakeVc getValveOpen(Long commandId) { | 
|         return rmdCommandHistoryMapper.getValveOpen(commandId); | 
|     } | 
|   | 
|     /** | 
|      * 修改灌溉计划 | 
|      * | 
|      * @param po | 
|      * @return | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public Integer updatePlan(IrIrrigatePlan po) { | 
|         return irrigatePlanMapper.updateByPrimaryKeySelective(po); | 
|     } | 
|   | 
|     /** | 
|      * 根据计划ID终止灌溉次序,将灌溉次序的当前状态改为已终止 | 
|      * | 
|      * @param planId | 
|      * @return | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public Integer terminateSchedule(Long planId) { | 
|         return irIrrigateScheduleMapper.terminateSchedule(planId); | 
|     } | 
|   | 
|     /** | 
|      * 终止灌溉计划 | 
|      * @param planSimple | 
|      * @return | 
|      */ | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public Map terminatePlan(PlanSimple planSimple) { | 
|         Map map = new HashMap<>(); | 
|         map.put("success", false); | 
|         map.put("content", null); | 
|   | 
|         Long planId = planSimple.getPlanId(); | 
|         Long operatorId = planSimple.getOperatorId(); | 
|   | 
|         // 生成终止开始时间、终止灌溉时长 | 
|         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 = 0; | 
|   | 
|         // 判断该计划是否可执行终止操作:未删除、未终止、已发布、当前时间小于计划结束时间 | 
|         Date planStartTime = getToTerminatePlan(planId); | 
|         if(planStartTime == null){ | 
|             map.put("msg", "该计划不存在或不支持终止操作"); | 
|             return map; | 
|         } | 
|   | 
|         List<VoToTerminateIntakes> toTerminateIntakes = getToTerminateIntakes(planId); | 
|         if(toTerminateIntakes != null && toTerminateIntakes.size() > 0){ | 
|             for(VoToTerminateIntakes toTerminateIntake : toTerminateIntakes){ | 
|                 Long intakeId = toTerminateIntake.getIntakeId(); | 
|                 Long commandId = toTerminateIntake.getCommandId(); | 
|                 Date startTime = toTerminateIntake.getStartTime(); | 
|                 Date currentTime = new Date(); | 
|                 if(currentTime.before(startTime)){ | 
|                     // 取消(覆盖开阀计划) | 
|                     AutomaticClose automaticClose = new AutomaticClose(); | 
|                     automaticClose.setIntakeId(intakeId); | 
|                     automaticClose.setPlannedOpenTime(terminateStartTime); | 
|                     automaticClose.setMinutes(terminateDuration); | 
|                     automaticClose.setOperator(operatorId); | 
|                     automaticClose.setOpenType(Byte.valueOf("1")); | 
|   | 
|                     commandSv.planedOpenTimedClose(automaticClose, planId, (byte)2, terminateStartTime, terminateDuration); | 
|                 }else { | 
|                     // 终止(远程关阀) | 
|                     VoIntakeVc intakeVc = getValveOpen(commandId); | 
|                     String rtuAddr = intakeVc.getRtuAddr(); | 
|                     String vcNum = intakeVc.getVcNum(); | 
|                     String orderNo = intakeVc.getOrderNo(); | 
|   | 
|                     ValveClose valveClose = new ValveClose(); | 
|                     valveClose.setRtuAddr(rtuAddr); | 
|                     valveClose.setVcNum(vcNum); | 
|                     valveClose.setOrderNo(orderNo); | 
|                     valveClose.setOperator(operatorId); | 
|                     valveClose.setOpenType(Byte.valueOf("1")); | 
|                     commandSv.closeWx(valveClose, planId, (byte)3); | 
|                 } | 
|             } | 
|         } | 
|   | 
|         // 终止计划 | 
|         IrIrrigatePlan iIrrigatePlan = new IrIrrigatePlan(); | 
|         iIrrigatePlan.setId(planId); | 
|         iIrrigatePlan.setExecutingState((byte)3); | 
|         if(updatePlan(iIrrigatePlan) == 0){ | 
|             map.put("msg", "终止计划失败"); | 
|             return map; | 
|         } | 
|   | 
|         // 终止灌溉次序 | 
|         if(terminateSchedule(planId) == 0){ | 
|             map.put("msg", "终止灌溉次序失败"); | 
|             return map; | 
|         } | 
|   | 
|         // 添加终止操作记录 | 
|         IrPlanOperate planOperate  = new IrPlanOperate(); | 
|         planOperate.setPlanId(planId); | 
|         planOperate.setOperator(operatorId); | 
|         planOperate.setOperateType(OperateTypeENUM.TERMINATE.getCode()); | 
|         planOperate.setOperateTime(new Date()); | 
|         if(addPlanOperate(planOperate) == 0){ | 
|             map.put("msg", "添加终止操作记录失败"); | 
|             return map; | 
|         } | 
|         map.put("success", true); | 
|         map.put("msg", "终止灌溉计划成功"); | 
|         return map; | 
|     } | 
|   | 
|     /** | 
|      * 获取未完成的计划列表,灌溉模块计划列表页使用 | 
|      * | 
|      * @return | 
|      */ | 
|     public List<VoPlans> getNotCompletePlans() { | 
|         return irrigatePlanMapper.getNotCompletePlans(); | 
|     } | 
|   | 
|     /** | 
|      * 获取已完成的计划列表,灌溉模块计划列表页使用 | 
|      * | 
|      * @return | 
|      */ | 
|     public QueryResultVo<List<VoPlans>> getCompletedPlans(QueryConditionVo queryVo) { | 
|         Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); | 
|   | 
|         Long itemTotal = (long) irrigatePlanMapper.getCompletedPlansCount(params); | 
|         QueryResultVo<List<VoPlans>> rsVo = new QueryResultVo<>(); | 
|         rsVo.pageSize = queryVo.pageSize; | 
|         rsVo.pageCurr = queryVo.pageCurr; | 
|   | 
|         rsVo.calculateAndSet(itemTotal, params); | 
|         rsVo.obj = irrigatePlanMapper.getCompletedPlans(params); | 
|   | 
|         return rsVo; | 
|     } | 
|   | 
|     /** | 
|      * 根据计划ID获取计划发布结果 | 
|      * | 
|      * @param planId | 
|      * @return | 
|      */ | 
|     public VoPlanDetails getPublishResults(Long planId) { | 
|         VoPlanDetails planDetails = irrigatePlanMapper.getPlanDetails(planId); | 
|         if (planDetails == null) { | 
|             return null; | 
|         } | 
|         Integer failureCount = Optional.ofNullable(irIntakeOperateMapper.getFailureCount(planId)).orElse(0); | 
|         planDetails.setFailureCount(failureCount); | 
|   | 
|         Date terminateTime = Optional.ofNullable(irPlanOperateMapper.getTerminateTime(planId)).orElse(null); | 
|   | 
|         List<VoGroupResult> groupResults = irIrrigateGroupMapper.getGroupResult(planId); | 
|         if (groupResults == null || groupResults.size() == 0) { | 
|             return null; | 
|         } | 
|   | 
|         for (VoGroupResult groupResult : groupResults) { | 
|             List<VoIntakeResult> intakeResults = irIntakeOperateMapper.getIntakeResult(planId, groupResult.getGroupId()); | 
|             if (intakeResults != null) { | 
|                 groupResult.setPublishResult(intakeResults); | 
|             } | 
|         } | 
|   | 
|         planDetails.setGroups(groupResults); | 
|         return planDetails; | 
|   | 
|     } | 
|   | 
|     /** | 
|      * 根据计划ID获取计划终止操作结果 | 
|      * | 
|      * @param planId | 
|      * @return | 
|      */ | 
|     public VoPlanDetails getTerminateResults(Long planId) { | 
|         // 获取计划终止时间 | 
|         Date terminateTime = Optional.ofNullable(irPlanOperateMapper.getTerminateTime(planId)).orElse(null); | 
|   | 
|         VoPlanDetails planDetails = irrigatePlanMapper.getPlanDetails_terminate(planId, terminateTime); | 
|         if (planDetails == null) { | 
|             return null; | 
|         } | 
|         Integer failureCount = Optional.ofNullable(irIntakeOperateMapper.getFailureCount(planId)).orElse(0); | 
|         planDetails.setFailureCount(failureCount); | 
|   | 
|         List<VoGroupResult> groupResults = irIrrigateGroupMapper.getGroupResult_terminate(planId, terminateTime); | 
|         if (groupResults == null || groupResults.size() == 0) { | 
|             return null; | 
|         } | 
|   | 
|         for (VoGroupResult groupResult : groupResults) { | 
|             List<VoIntakeResult> intakeResults = irIntakeOperateMapper.getIntakeResult(planId, groupResult.getGroupId()); | 
|             if (intakeResults != null) { | 
|                 groupResult.setPublishResult(intakeResults); | 
|             } | 
|         } | 
|   | 
|         planDetails.setGroups(groupResults); | 
|         return planDetails; | 
|   | 
|     } | 
|   | 
|     /** | 
|      * 根据计划ID获取计划最新状态 | 
|      * | 
|      * @param planId | 
|      * @return | 
|      */ | 
|     public Integer getPlanLatestState(Long planId) { | 
|         return irrigatePlanMapper.getPlanLatestState(planId); | 
|     } | 
| } |