liurunyu
2025-02-21 ff2fffad2b049c0be0df5085496429ab4861857f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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<Boolean> 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<Long> 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();
    }
}