zuoxiao
2 天以前 e0c3d16c7ba1700be99b4739b883e4d01789cc62
feat(pipIrr): 新增计划终止后限制发布新计划的功能

- 在 IrIrrigatePlanMapper 中添加了判断当前计划与最近终止计划轮灌组是否有重叠的方法
- 在 IrPlanOperateMapper 中添加了获取最近一次计划终止时间和终止计划ID的方法- 在 IrrigatePlanSv 中实现了检查计划终止后限制发布新计划的逻辑
- 在 application-self.yml 中添加了计划终止后限制发布新计划的时间间隔配置项
6个文件已修改
180 ■■■■ 已修改文件
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoIr/IrIrrigatePlanMapper.java 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoIr/IrPlanOperateMapper.java 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/resources/mapper/IrIrrigatePlanMapper.xml 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/resources/mapper/IrPlanOperateMapper.xml 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/irrigatePlan/IrrigatePlanSv.java 117 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/resources/application-self.yml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoIr/IrIrrigatePlanMapper.java
@@ -131,6 +131,14 @@
    Integer hasPlan_OtherProject(Long planId);
    /**
     * 判断当前计划与最近终止的计划的轮灌组是否有重叠
     * @param planId 当前计划ID
     * @param terminatedPlanId 最近终止的计划ID
     * @return
     */
    Integer hasPlanOverlapWithTerminated(@Param("planId") Long planId, @Param("terminatedPlanId") Long terminatedPlanId);
    /**
     * 根据计划ID获取计划最新状态
     * @param planId
     * @return
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/daoIr/IrPlanOperateMapper.java
@@ -3,6 +3,7 @@
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dy.pipIrrGlobal.pojoIr.IrPlanOperate;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
@@ -33,4 +34,18 @@
     * @return
     */
    Date getTerminateTime(Long planId);
    /**
     * 获取最近一次计划终止的时间
     * @param planTerminateLimitMinutes 计划终止后限制发布新计划的时间间隔(分钟)
     * @return 最近一次计划终止的时间,如果没有则返回null
     */
    Date getLastTerminateTime(@Param("planTerminateLimitMinutes") Integer planTerminateLimitMinutes);
    /**
     * 获取最近一次终止的计划ID
     * @param planTerminateLimitMinutes 计划终止后限制发布新计划的时间间隔(分钟)
     * @return 最近一次终止的计划ID,如果没有则返回null
     */
    Long getLastTerminatedPlanId(@Param("planTerminateLimitMinutes") Integer planTerminateLimitMinutes);
}
pipIrr-platform/pipIrr-global/src/main/resources/mapper/IrIrrigatePlanMapper.xml
@@ -438,6 +438,25 @@
    ) AS has_intersection
  </select>
  <!--判断当前计划与最近终止的计划的轮灌组是否有重叠 -->
  <select id="hasPlanOverlapWithTerminated" resultType="java.lang.Integer">
    SELECT COUNT(1)
    FROM (
        SELECT sche1.group_id
        FROM ir_plan_schedule ps1
        INNER JOIN ir_irrigate_schedule sche1 ON ps1.schedule_id = sche1.id
        WHERE ps1.plan_id = #{planId}
    ) AS current_groups
    INNER JOIN (
        SELECT sche2.group_id
        FROM ir_plan_schedule ps2
        INNER JOIN ir_irrigate_schedule sche2 ON ps2.schedule_id = sche2.id
        WHERE ps2.plan_id = #{terminatedPlanId}
    ) AS terminated_groups
    ON current_groups.group_id = terminated_groups.group_id
    LIMIT 1
  </select>
  <!--根据计划ID获取计划最新状态-->
  <select id="getPlanLatestState" resultType="java.lang.Integer">
    SELECT
pipIrr-platform/pipIrr-global/src/main/resources/mapper/IrPlanOperateMapper.xml
@@ -104,4 +104,24 @@
  <select id="getTerminateTime" resultType="java.util.Date">
    SELECT operate_time AS operateTime FROM ir_plan_operate WHERE operate_type = 5 AND plan_id = #{planId} LIMIT 0,1
  </select>
  <!--获取最近一次计划终止的时间,且终止时间在配置的时间间隔内-->
  <select id="getLastTerminateTime" resultType="java.util.Date">
    SELECT operate_time AS operateTime
    FROM ir_plan_operate
    WHERE operate_type = 5
    AND operate_time >= DATE_SUB(NOW(), INTERVAL #{planTerminateLimitMinutes} MINUTE)
    ORDER BY operate_time DESC
    LIMIT 0,1
  </select>
  <!--获取最近一次终止的计划ID-->
  <select id="getLastTerminatedPlanId" resultType="java.lang.Long">
    SELECT plan_id
    FROM ir_plan_operate
    WHERE operate_type = 5
    AND operate_time >= DATE_SUB(NOW(), INTERVAL #{planTerminateLimitMinutes} MINUTE)
    ORDER BY operate_time DESC
    LIMIT 0,1
  </select>
</mapper>
pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/irrigatePlan/IrrigatePlanSv.java
@@ -70,15 +70,19 @@
    private CommandSv commandSv;
    @Value("${wechat.irr.plan.delay:5}")
    private Integer irrPlanDelay ;//轮灌中计划开阀的延迟时长
    private Integer irrPlanDelay;//轮灌中计划开阀的延迟时长
    private static final Integer irrPlanDelayDefault = 5 ;//轮灌中计划开阀的默认延迟时长
    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
     */
@@ -90,6 +94,7 @@
    /**
     * 删除灌溉计划
     *
     * @param planSimple
     * @return
     */
@@ -99,14 +104,14 @@
        Long operatorId = planSimple.getOperatorId();
        Integer planState = irrigatePlanMapper.getPlanState(planId);
        if(planState == null) {
        if (planState == null) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "灌溉计划不存在");
            map.put("content", null);
            return map;
        }
        if(planState != 1) {
        if (planState != 1) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "灌溉计划非草稿状态,不允许删除");
@@ -117,7 +122,7 @@
        try {
            irrigatePlanMapper.deleteByPrimaryKey(planId);
            // 添加灌溉计划操作记录
            IrPlanOperate planOperate  = new IrPlanOperate();
            IrPlanOperate planOperate = new IrPlanOperate();
            planOperate.setPlanId(planId);
            planOperate.setOperator(operatorId);
            planOperate.setOperateType(OperateTypeENUM.DELETE.getCode());
@@ -140,6 +145,7 @@
    /**
     * 发布灌溉计划
     *
     * @param planSimple
     * @return
     */
@@ -148,7 +154,8 @@
        Long operatorId = planSimple.getOperatorId();
        Byte operateType = 1;
        if(irrigatePlanMapper.hasPlan_CurrentProject(planId) > 0) {
        // 检查当前项目是否存在未完成的灌溉计划
        if (irrigatePlanMapper.hasPlan_CurrentProject(planId) > 0) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "当前项目存在未完成的灌溉计划");
@@ -156,7 +163,8 @@
            return map;
        }
        if(irrigatePlanMapper.hasPlan_OtherProject(planId) > 0) {
        // 检查其他项目中是否存在未完成的灌溉计划
        if (irrigatePlanMapper.hasPlan_OtherProject(planId) > 0) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "待发布计划的轮灌组在其他项目的灌溉计划中,且该计划尚未完成");
@@ -164,12 +172,25 @@
            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){
        if (plan == null) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "您要发布的计划不存在,或该计划已发布");
@@ -182,16 +203,16 @@
        Date planStartTime = plan.getPlanStartTime();
        Date planStopTime = null;
        if(startupMode == 1){
        if (startupMode == 1) {
            planStartTime = new Date();
        }
        LocalDateTime startTime = planStartTime.toInstant().atZone(ZoneId.systemDefault()) .toLocalDateTime();
        if(startupMode == 1){
        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 ;
            if (irrPlanDelay == null || irrPlanDelay <= 0) {
                irrPlanDelay = irrPlanDelayDefault;
            }
            startTime = startTime.plusMinutes(irrPlanDelay);
        }
@@ -200,7 +221,7 @@
        planStopTime = Date.from(stopTime.atZone(ZoneId.systemDefault()).toInstant());
        // 根据计划ID更新计划信息(起止时间、计划状态)
        if(this.updatePlanTimes(planStartTime, planStopTime, planId) == 0) {
        if (this.updatePlanTimes(planStartTime, planStopTime, planId) == 0) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "计划信息更新失败");
@@ -212,31 +233,31 @@
        List<VoIrrigateSchedule> schedules = getSchedulesByPlanId(planId);
        Date scheduleStartTime = null;
        Integer sort = 0;
        for(VoIrrigateSchedule schedule : schedules){
            if(scheduleStartTime == null) {
        for (VoIrrigateSchedule schedule : schedules) {
            if (scheduleStartTime == null) {
                scheduleStartTime = planStartTime;
            }
            this.updateScheduleStartTime(schedule.getScheduleId(), scheduleStartTime);
            // 计算下一组的开始时间
            LocalDateTime LocalscheduleStartTime = scheduleStartTime.toInstant().atZone(ZoneId.systemDefault()) .toLocalDateTime();
            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)) {
            if (scheduleStartTime.before(planStartTime)) {
                scheduleStartTime = planStartTime;
            }
        }
        // 添加灌溉计划操作记录
        IrPlanOperate planOperate  = new IrPlanOperate();
        IrPlanOperate planOperate = new IrPlanOperate();
        planOperate.setPlanId(planId);
        planOperate.setOperator(operatorId);
        planOperate.setOperateType(OperateTypeENUM.PUBLISH.getCode());
        planOperate.setOperateTime(new Date());
        if(addPlanOperate(planOperate) == 0){
        if (addPlanOperate(planOperate) == 0) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "添加灌溉计划操作记录失败");
@@ -245,11 +266,11 @@
        }
        schedules = getSchedulesByPlanId(planId);
        for(VoIrrigateSchedule schedule : schedules) {
        for (VoIrrigateSchedule schedule : schedules) {
            Long groupId = schedule.getGroupId();
            List<Long> intakeIds = getIntakeIdsByGroupId(groupId);
            for(Long intakeId : intakeIds) {
                if(schedule.getDuration() > 0) {
            for (Long intakeId : intakeIds) {
                if (schedule.getDuration() > 0) {
                    // 今发布灌溉时长大于0的
                    AutomaticClose automaticClose = new AutomaticClose();
                    automaticClose.setIntakeId(intakeId);
@@ -272,6 +293,7 @@
    /**
     * 根据计划ID获取计划最新状态
     *
     * @param planId
     * @return
     */
@@ -281,6 +303,7 @@
    /**
     * 根据计划ID获取待终止计划的结束时间:未删除、未终止、已发布、当前时间小于计划结束时间
     *
     * @param planId
     * @return
     */
@@ -290,6 +313,7 @@
    /**
     * 根据计划ID获取待终止的取水口ID(已发布开发命令,无论是否成功)
     *
     * @param planId
     * @return
     */
@@ -299,6 +323,7 @@
    /**
     * 根据计划ID获取待终止的命令ID列表
     *
     * @param planId
     * @return
     */
@@ -308,6 +333,7 @@
    /**
     * 根据计划ID获取待终止的取水口列表(开阀成功的)
     *
     * @param planId
     * @return
     */
@@ -317,6 +343,7 @@
    /**
     * 根据命令日志ID获取取水口及虚拟卡信息,终止灌溉计划时使用,用来执行远程关阀
     *
     * @param commandId
     * @return
     */
@@ -326,6 +353,7 @@
    /**
     * 修改灌溉计划
     *
     * @param po
     * @return
     */
@@ -336,7 +364,8 @@
    /**
     * 获取未完成的计划列表,小程序计划列表页使用
      * @return
     *
     * @return
     */
    public List<VoPlans> getNotCompletePlans() {
        return irrigatePlanMapper.getNotCompletePlans();
@@ -344,24 +373,26 @@
    /**
     * 获取已完成的计划列表,小程序计划列表页使用
     *
     * @return
     */
    public QueryResultVo<List<VoPlans>> getCompletedPlans(QueryConditionVo queryVo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(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 ;
        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 ;
        return rsVo;
    }
    /**
     * 根据指定的计划ID获取启动模式
     *
     * @param planId
     * @return
     */
@@ -390,6 +421,7 @@
    /**
     * 添加灌溉计划操作记录
     *
     * @param po
     * @return
     */
@@ -401,6 +433,7 @@
    /**
     * 添加灌溉次序记录
     *
     * @param po
     * @return
     */
@@ -412,6 +445,7 @@
    /**
     * 根据计划ID终止灌溉次序,将灌溉次序的当前状态改为已终止
     *
     * @param planId
     * @return
     */
@@ -422,6 +456,7 @@
    /**
     * 根据计划ID获取灌溉次序记录
     *
     * @param planId
     * @return
     */
@@ -431,6 +466,7 @@
    /**
     * 根据组ID获取取水口ID集合
     *
     * @param groupId
     * @return
     */
@@ -440,6 +476,7 @@
    /**
     * 添加计划次序关联记录
     *
     * @param po
     * @return
     */
@@ -451,6 +488,7 @@
    /**
     * 根据灌溉次序ID更新次序开始时间
     *
     * @param scheduleId
     * @param startTime
     * @return
@@ -472,6 +510,7 @@
    /**
     * 根据灌溉单元ID获取取水口ID
     *
     * @param unitId
     * @return
     */
@@ -481,12 +520,13 @@
    /**
     * 根据计划ID获取计划发布结果
     *
     * @param planId
     * @return
     */
    public VoPlanDetails getPublishResults(Long planId) {
        VoPlanDetails planDetails = irrigatePlanMapper.getPlanDetails(planId);
        if(planDetails == null){
        if (planDetails == null) {
            return null;
        }
        Integer failureCount = Optional.ofNullable(irIntakeOperateMapper.getFailureCount(planId)).orElse(0);
@@ -495,13 +535,13 @@
        Date terminateTime = Optional.ofNullable(irPlanOperateMapper.getTerminateTime(planId)).orElse(null);
        List<VoGroupResult> groupResults = irIrrigateGroupMapper.getGroupResult(planId);
        if(groupResults == null || groupResults.size() == 0){
        if (groupResults == null || groupResults.size() == 0) {
            return null;
        }
        for (VoGroupResult groupResult : groupResults) {
            List<VoIntakeResult> intakeResults = irIntakeOperateMapper.getIntakeResult(planId, groupResult.getGroupId());
            if(intakeResults != null) {
            if (intakeResults != null) {
                groupResult.setPublishResult(intakeResults);
            }
        }
@@ -513,6 +553,7 @@
    /**
     * 根据计划ID获取计划终止操作结果
     *
     * @param planId
     * @return
     */
@@ -521,20 +562,20 @@
        Date terminateTime = Optional.ofNullable(irPlanOperateMapper.getTerminateTime(planId)).orElse(null);
        VoPlanDetails planDetails = irrigatePlanMapper.getPlanDetails_terminate(planId, terminateTime);
        if(planDetails == null){
        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){
        if (groupResults == null || groupResults.size() == 0) {
            return null;
        }
        for (VoGroupResult groupResult : groupResults) {
            List<VoIntakeResult> intakeResults = irIntakeOperateMapper.getIntakeResult(planId, groupResult.getGroupId());
            if(intakeResults != null) {
            if (intakeResults != null) {
                groupResult.setPublishResult(intakeResults);
            }
        }
@@ -545,7 +586,7 @@
    }
    @Transactional(rollbackFor = Exception.class)
    Integer updatePlanTimes(Date planStartTime, Date planEndTime, Long planId){
    Integer updatePlanTimes(Date planStartTime, Date planEndTime, Long planId) {
        return irrigatePlanMapper.updatePlanTimes(planStartTime, planEndTime, planId);
    }
pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/resources/application-self.yml
@@ -17,6 +17,7 @@
        plan:
            delay: 5 #轮灌中计划开阀的延迟时长(分钟)
            preOpeningTime: 10 #提前时间(分钟)
            planTerminateLimitMinutes: 5 #计划终止后限制发布新计划的时间间隔(分钟)
#阿里短信服务
aliyun:
    sms: