From cd7fd2ea35de966cfd1c6f3038e593097d7a6dec Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期四, 03 七月 2025 11:26:46 +0800
Subject: [PATCH] 轮灌组PC端接口

---
 pipIrr-platform/pipIrr-web/pipIrr-web-irrigate/src/main/java/com/dy/pipIrrIrrigate/irrigatePlan/IrrigatePlanSv.java |  756 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 756 insertions(+), 0 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-irrigate/src/main/java/com/dy/pipIrrIrrigate/irrigatePlan/IrrigatePlanSv.java b/pipIrr-platform/pipIrr-web/pipIrr-web-irrigate/src/main/java/com/dy/pipIrrIrrigate/irrigatePlan/IrrigatePlanSv.java
new file mode 100644
index 0000000..6e43aca
--- /dev/null
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-irrigate/src/main/java/com/dy/pipIrrIrrigate/irrigatePlan/IrrigatePlanSv.java
@@ -0,0 +1,756 @@
+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);
+    }
+
+    /**
+     * 鏍规嵁缁処D鑾峰彇鍙栨按鍙D闆嗗悎
+     *
+     * @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鑾峰彇寰呯粓姝㈢殑鍙栨按鍙e垪琛紙寮�闃�鎴愬姛鐨勶級
+     *
+     * @param planId
+     * @return
+     */
+    public List<VoToTerminateIntakes> getToTerminateIntakes(Long planId) {
+        return irIntakeOperateMapper.getToTerminateIntakes(planId);
+    }
+
+    /**
+     * 鏍规嵁鍛戒护鏃ュ織ID鑾峰彇鍙栨按鍙e強铏氭嫙鍗′俊鎭紝缁堟鐏屾簤璁″垝鏃朵娇鐢紝鐢ㄦ潵鎵ц杩滅▼鍏抽榾
+     *
+     * @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);
+    }
+}
\ No newline at end of file

--
Gitblit v1.8.0