|  |  |  | 
|---|
|  |  |  | package com.dy.pipIrrWechat.irrigatePlan; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSON; | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSONArray; | 
|---|
|  |  |  | import com.alibaba.fastjson2.JSONObject; | 
|---|
|  |  |  | 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.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.stereotype.Service; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | import java.time.LocalDateTime; | 
|---|
|  |  |  | import java.time.ZoneId; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author ZhuBaoMin | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RmCommandHistoryMapper rmdCommandHistoryMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private CommandSv commandSv; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 添加灌溉计划 | 
|---|
|  |  |  | 
|---|
|  |  |  | 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_CurrentProject(planId) > 0) { | 
|---|
|  |  |  | Map map = new HashMap<>(); | 
|---|
|  |  |  | map.put("success", false); | 
|---|
|  |  |  | map.put("msg", "待发布计划的轮灌组在其他项目的灌溉计划中,且该计划尚未完成"); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | planStartTime = Date.from(startTime.atZone(ZoneId.systemDefault()).toInstant()); | 
|---|
|  |  |  | LocalDateTime stopTime = startTime.plusMinutes(duration); | 
|---|
|  |  |  | planStopTime = Date.from(stopTime.atZone(ZoneId.systemDefault()).toInstant()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 根据计划ID更新计划信息(起止时间、计划状态) | 
|---|
|  |  |  | if(irrigatePlanMapper.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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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(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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * 获取已完成的计划列表,小程序计划列表页使用 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public List<VoPlans> getCompletedPlans() { | 
|---|
|  |  |  | return irrigatePlanMapper.getCompletedPlans(); | 
|---|
|  |  |  | 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 VoPlanSimple getPlanSimple(Long planId) { | 
|---|
|  |  |  | return irrigatePlanMapper.getPlanSimple(planId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据计划ID更新计划信息(起止时间、计划状态) | 
|---|
|  |  |  | * @param planStartTime | 
|---|
|  |  |  | * @param planEndTime | 
|---|
|  |  |  | * @param planId | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public Integer updatePlanTimes(Date planStartTime, Date planEndTime, Long planId) { | 
|---|
|  |  |  | return irrigatePlanMapper.updatePlanTimes(planStartTime, planEndTime, planId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据灌溉次序ID更新次序开始时间 | 
|---|
|  |  |  | * @param scheduleId | 
|---|
|  |  |  | * @param startTime | 
|---|
|  |  |  | 
|---|
|  |  |  | * @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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | VoPlanDetails planDetails = irrigatePlanMapper.getPlanDetails(planId); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (VoGroupResult groupResult : groupResults) { | 
|---|
|  |  |  | List<VoIntakeResult> intakeResults = irIntakeOperateMapper.getIntakeResult(planId, groupResult.getGroupId()); | 
|---|
|  |  |  | 
|---|
|  |  |  | planDetails.setGroups(groupResults); | 
|---|
|  |  |  | return planDetails; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //JSONArray array_groupResult = (JSONArray) JSON.toJSON(groupResults); | 
|---|
|  |  |  | //for(int i = 0; i < array_groupResult.size(); i++){ | 
|---|
|  |  |  | //    JSONObject job_groupResult = array_groupResult.getJSONObject(i); | 
|---|
|  |  |  | //    Long groupId = job_groupResult.getLong("groupId"); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    List<VoIntakeResult> intakeResults = irIntakeOperateMapper.getIntakeResult(planId, groupId); | 
|---|
|  |  |  | //    if(intakeResults != null) { | 
|---|
|  |  |  | //        JSONArray array_intakeResult = (JSONArray) JSON.toJSON(intakeResults); | 
|---|
|  |  |  | //        job_groupResult.put("publishResult", array_intakeResult); | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | //} | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //List<VoGroupResult> publishResults = array_groupResult.toJavaList(VoGroupResult.class); | 
|---|
|  |  |  | //return publishResults; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 根据计划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; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|