From e0c3d16c7ba1700be99b4739b883e4d01789cc62 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期三, 30 四月 2025 16:20:50 +0800 Subject: [PATCH] feat(pipIrr): 新增计划终止后限制发布新计划的功能 --- pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/irrigatePlan/IrrigatePlanSv.java | 516 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 494 insertions(+), 22 deletions(-) diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/irrigatePlan/IrrigatePlanSv.java b/pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/irrigatePlan/IrrigatePlanSv.java index e205fb6..5db3988 100644 --- a/pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/irrigatePlan/IrrigatePlanSv.java +++ b/pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/irrigatePlan/IrrigatePlanSv.java @@ -1,16 +1,30 @@ package com.dy.pipIrrWechat.irrigatePlan; +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.IrOpeningSchedule; import com.dy.pipIrrGlobal.pojoIr.IrPlanOperate; -import com.dy.pipIrrGlobal.voIr.VoIrrigateSchedule; +import com.dy.pipIrrGlobal.pojoIr.IrPlanSchedule; +import com.dy.pipIrrGlobal.voIr.*; +import com.dy.pipIrrGlobal.voRm.VoIntakeVc; +import com.dy.pipIrrWechat.command.CommandSv; +import com.dy.pipIrrWechat.command.dto.AutomaticClose; +import com.dy.pipIrrWechat.irrigatePlan.dto.PlanSimple; +import com.dy.pipIrrWechat.irrigatePlan.enums.OperateTypeENUM; import lombok.extern.slf4j.Slf4j; +import org.apache.dubbo.common.utils.PojoUtils; +import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; -import java.util.List; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.*; /** * @author ZhuBaoMin @@ -40,34 +54,378 @@ @Autowired private IrIrrigateUnitMapper irIrrigateUnitMapper; + @Autowired + private IrPlanScheduleMapper irPlanScheduleMapper; + + @Autowired + private IrGroupIntakeMapper irGroupIntakeMapper; + + @Autowired + private IrIntakeOperateMapper irIntakeOperateMapper; + + @Autowired + private RmCommandHistoryMapper rmdCommandHistoryMapper; + + @Autowired + private CommandSv commandSv; + + @Value("${wechat.irr.plan.delay:5}") + private Integer irrPlanDelay;//杞亴涓鍒掑紑闃�鐨勫欢杩熸椂闀� + + private static final Integer irrPlanDelayDefault = 5;//杞亴涓鍒掑紑闃�鐨勯粯璁ゅ欢杩熸椂闀� + + @Value("${wechat.irr.plan.preOpeningTime:10}") + private Integer preOpeningTime; + + @Value("${wechat.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(); } - //public List<VoIrrigatePlan> getIrrigatePlans(Map<?, ?> params { - // return irrigatePlanMapper.getIrrigatePlans(params); - //} - /** - * 鍙戝竷鎸囧畾鐨勭亴婧夎鍒� - * @param planId + * 鍒犻櫎鐏屾簤璁″垝 + * + * @param planSimple * @return */ - public Integer publishIrrigatePlan(Long planId) { - return irrigatePlanMapper.publishIrrigatePlan(planId); + @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; + } } /** - * 娣诲姞鐏屾簤璁″垝鎿嶄綔璁板綍 + * 鍙戝竷鐏屾簤璁″垝 + * + * @param planSimple + * @return + */ + 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) { + // 娴嬭瘯闃舵寤跺悗2鍒嗛挓锛屾寮忓彂甯冧负5鍒嗛挓 + //startTime = startTime.plusMinutes(2); + //startTime = startTime.plusMinutes(5); + if (irrPlanDelay == null || irrPlanDelay <= 0) { + irrPlanDelay = irrPlanDelayDefault; + } + startTime = startTime.plusMinutes(irrPlanDelay); + } + planStartTime = Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant()); + 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; + } + + // 鏇存柊姣忎釜鐏屾簤娆″簭鐨勫紑濮嬫椂闂� + List<VoIrrigateSchedule> schedules = getSchedulesByPlanId(planId); + Date scheduleStartTime = null; + Integer sort = 0; + for (VoIrrigateSchedule schedule : schedules) { + if (scheduleStartTime == null) { + scheduleStartTime = planStartTime; + } + this.updateScheduleStartTime(schedule.getScheduleId(), scheduleStartTime); + + // 璁$畻涓嬩竴缁勭殑寮�濮嬫椂闂� + 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; + } + } + + // 娣诲姞鐏屾簤璁″垝鎿嶄綔璁板綍 + 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); + 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 Integer getPlanLatestState(Long planId) { + return irrigatePlanMapper.getPlanLatestState(planId); + } + + /** + * 鏍规嵁璁″垝ID鑾峰彇寰呯粓姝㈣鍒掔殑缁撴潫鏃堕棿锛氭湭鍒犻櫎銆佹湭缁堟銆佸凡鍙戝竷銆佸綋鍓嶆椂闂村皬浜庤鍒掔粨鏉熸椂闂� + * + * @param planId + * @return + */ + public Date getToTerminatePlan(Long planId) { + return irrigatePlanMapper.getToTerminatePlan(planId); + } + + /** + * 鏍规嵁璁″垝ID鑾峰彇寰呯粓姝㈢殑鍙栨按鍙D锛堝凡鍙戝竷寮�鍙戝懡浠わ紝鏃犺鏄惁鎴愬姛锛� + * + * @param planId + * @return + */ + public List<Long> getToTerminateIntakeIds(Long planId) { + return irIntakeOperateMapper.getToTerminateIntakeIds(planId); + } + + /** + * 鏍规嵁璁″垝ID鑾峰彇寰呯粓姝㈢殑鍛戒护ID鍒楄〃 + * + * @param planId + * @return + */ + public List<Long> getTerminateCommandIds(Long planId) { + return irIntakeOperateMapper.getTerminateCommandIds(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); + } + + /** + * 鑾峰彇鏈畬鎴愮殑璁″垝鍒楄〃锛屽皬绋嬪簭璁″垝鍒楄〃椤典娇鐢� + * + * @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; + } + + /** + * 鏍规嵁鎸囧畾鐨勮鍒扞D鑾峰彇鍚姩妯″紡 + * + * @param planId + * @return + */ + public Byte getStartupMode(Long planId) { + return irrigatePlanMapper.getStartupMode(planId); + } + + /** + * 鏍规嵁鎸囧畾鐨勬潯浠惰幏鍙栬鍒掑垪琛� + * @param queryVo + * @return + */ + //public QueryResultVo<List<VoIrrigatePlan>> getIrrigatePlans(QoIrrigatePlan queryVo) { + // Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo); + // + // Long itemTotal = irrigatePlanMapper.getIrrigatePlanCount(params); + // + // QueryResultVo<List<VoIrrigatePlan>> rsVo = new QueryResultVo<>(); + // rsVo.pageSize = queryVo.pageSize; + // rsVo.pageCurr = queryVo.pageCurr; + // + // rsVo.calculateAndSet(itemTotal, params); + // rsVo.obj = irrigatePlanMapper.getIrrigatePlans(params); + // return rsVo; + //} + + /** + * 娣诲姞鐏屾簤璁″垝鎿嶄綔璁板綍 + * + * @param po + * @return + */ + @Transactional(rollbackFor = Exception.class) public Long addPlanOperate(IrPlanOperate po) { irPlanOperateMapper.insert(po); return po.getId(); @@ -75,48 +433,162 @@ /** * 娣诲姞鐏屾簤娆″簭璁板綍 + * * @param po * @return */ + @Transactional(rollbackFor = Exception.class) public Long addIrrigateSchedule(IrIrrigateSchedule po) { irIrrigateScheduleMapper.insert(po); return po.getId(); } /** - * 鏍规嵁璁″垝ID鑾峰彇鐏屾簤娆″簭 + * 鏍规嵁璁″垝ID缁堟鐏屾簤娆″簭锛屽皢鐏屾簤娆″簭鐨勫綋鍓嶇姸鎬佹敼涓哄凡缁堟 + * * @param planId * @return */ - public List<VoIrrigateSchedule> getIrrigateSchedules(Long planId) { - return irIrrigateScheduleMapper.getIrrigateSchedules(planId); + @Transactional(rollbackFor = Exception.class) + public Integer terminateSchedule(Long planId) { + return irIrrigateScheduleMapper.terminateSchedule(planId); } /** - * 娣诲姞寮�闃�璁″垝 + * 鏍规嵁璁″垝ID鑾峰彇鐏屾簤娆″簭璁板綍 + * + * @param planId + * @return + */ + public List<VoIrrigateSchedule> getSchedulesByPlanId(Long planId) { + return irIrrigateScheduleMapper.getSchedulesByPlanId(planId); + } + + /** + * 鏍规嵁缁処D鑾峰彇鍙栨按鍙D闆嗗悎 + * + * @param groupId + * @return + */ + public List<Long> getIntakeIdsByGroupId(Long groupId) { + return irGroupIntakeMapper.getIntakeIdsByGroupId(groupId); + } + + /** + * 娣诲姞璁″垝娆″簭鍏宠仈璁板綍 + * * @param po * @return */ - public Long addOpeningSchedule(IrOpeningSchedule po) { - irOpeningScheduleMapper.insert(po); + @Transactional(rollbackFor = Exception.class) + public Long addPlanSchedule(IrPlanSchedule po) { + irPlanScheduleMapper.insert(po); return po.getId(); } /** - * 鏍规嵁杞亴缁処D鑾峰彇鍙栨按鍙D鍒楄〃 - * @param groupId + * 鏍规嵁鐏屾簤娆″簭ID鏇存柊娆″簭寮�濮嬫椂闂� + * + * @param scheduleId + * @param startTime * @return */ - public List<Long> getIntakesByGroupId(Long groupId) { - return irIrrigateGroupMapper.getIntakesByGroupId(groupId); + @Transactional(rollbackFor = Exception.class) + public Integer updateScheduleStartTime(Long scheduleId, Date startTime) { + return irIrrigateScheduleMapper.updateScheduleStartTime(scheduleId, startTime); } + + ///** + // * 娣诲姞寮�闃�璁″垝 + // * @param po + // * @return + // */ + //public Long addOpeningSchedule(IrOpeningSchedule po) { + // irOpeningScheduleMapper.insert(po); + // return po.getId(); + //} /** * 鏍规嵁鐏屾簤鍗曞厓ID鑾峰彇鍙栨按鍙D + * * @param unitId * @return */ public Long getIntakeIdByUnitId(Long unitId) { return irIrrigateUnitMapper.getIntakeIdByUnitId(unitId); } + + /** + * 鏍规嵁璁″垝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; + + } + + @Transactional(rollbackFor = Exception.class) + Integer updatePlanTimes(Date planStartTime, Date planEndTime, Long planId) { + return irrigatePlanMapper.updatePlanTimes(planStartTime, planEndTime, planId); + } + + } -- Gitblit v1.8.0