package com.dy.pipIrrWechat.irrigatePlan; import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.BaseResponseUtils; import com.dy.pipIrrGlobal.pojoIr.IrIrrigatePlan; import com.dy.pipIrrGlobal.pojoIr.IrIrrigateSchedule; import com.dy.pipIrrWechat.irrigatePlan.dto.IrrigatePlan; import com.dy.pipIrrWechat.irrigatePlan.dto.IrrigateSchedule; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Objects; /** * @author ZhuBaoMin * @date 2025-02-20 15:27 * @LastEditTime 2025-02-20 15:27 * @Description */ @Slf4j @RestController @RequestMapping(path = "plan") @RequiredArgsConstructor public class IrrigatePlanCtrl { private final IrrigatePlanSv irrigatePlanSv; @PostMapping(path = "createPlan", consumes = MediaType.APPLICATION_JSON_VALUE) @Transactional(rollbackFor = Exception.class) public BaseResponse createPlan(@RequestBody @Valid IrrigatePlan planAndSchedule, BindingResult bindingResult){ if(bindingResult != null && bindingResult.hasErrors()){ return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); } IrIrrigatePlan plan = new IrIrrigatePlan(); plan.setProjectId(planAndSchedule.getProjectId()); plan.setPlanName(planAndSchedule.getPlanName()); plan.setStartupMode(planAndSchedule.getStartupMode()); plan.setPlanStartTime(planAndSchedule.getPlanStartTime()); plan.setPlanStopTime(planAndSchedule.getPlanStopTime()); plan.setPlanState((byte)1); plan.setExecutingState((byte)1); plan.setDeleted(0L); Long planId = irrigatePlanSv.addIrrigatePlan(plan); if(planId != null){ for(IrrigateSchedule schedule : planAndSchedule.getSchedules()){ IrIrrigateSchedule po = new IrIrrigateSchedule(); po.setPlanId(planId); if(planAndSchedule.getIrrigateType() == 1) { po.setGroupId(schedule.getIrrigateId()); List intakesData = irrigatePlanSv.getIntakesByGroupId(schedule.getIrrigateId()); if(intakesData != null && intakesData.size() > 0){ String intakeIds = ""; for(Long intakeId : intakesData){ intakeIds += intakeId + ","; } po.setIntakeIds(intakeIds.substring(0, intakeIds.length() - 1)); } }else { po.setUnitId(schedule.getIrrigateId()); Long intakeId = irrigatePlanSv.getIntakeIdByUnitId(schedule.getIrrigateId()); if(intakeId != null){ po.setIntakeIds(intakeId.toString()); } } po.setStartTime(schedule.getStartTime()); po.setStopTime(schedule.getStopTime()); //po.setIntakeIds(schedule.getIntakeIds()); po.setCurrentState((byte)1); Long scheduleId = irrigatePlanSv.addIrrigateSchedule(po); } } return BaseResponseUtils.buildSuccess(); } }