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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package com.dy.pipIrrWechat.irrigatePlan;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
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.IrPlanOperate;
import com.dy.pipIrrGlobal.pojoIr.IrPlanSchedule;
import com.dy.pipIrrGlobal.voIr.*;
import com.dy.pipIrrGlobal.voRm.VoIntakeVc;
import lombok.extern.slf4j.Slf4j;
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.List;
 
/**
 * @author ZhuBaoMin
 * @date 2025-02-20 15:27
 * @LastEditTime 2025-02-20 15:27
 * @Description
 */
 
@Slf4j
@Service
public class IrrigatePlanSv {
    @Autowired
    private IrIrrigatePlanMapper irrigatePlanMapper;
 
    @Autowired
    private IrPlanOperateMapper irPlanOperateMapper;
 
    @Autowired
    private IrIrrigateScheduleMapper irIrrigateScheduleMapper;
 
    @Autowired
    private IrOpeningScheduleMapper irOpeningScheduleMapper;
 
    @Autowired
    private IrIrrigateGroupMapper irIrrigateGroupMapper;
 
    @Autowired
    private IrIrrigateUnitMapper irIrrigateUnitMapper;
 
    @Autowired
    private IrPlanScheduleMapper irPlanScheduleMapper;
 
    @Autowired
    private IrGroupIntakeMapper irGroupIntakeMapper;
 
    @Autowired
    private IrIntakeOperateMapper irIntakeOperateMapper;
 
    @Autowired
    private RmCommandHistoryMapper rmdCommandHistoryMapper;
 
    /**
     * 添加灌溉计划
     * @param po
     * @return
     */
    public Long addIrrigatePlan(IrIrrigatePlan po) {
        irrigatePlanMapper.insert(po);
        return po.getId();
    }
 
    /**
     * 根据计划ID获取待终止计划的结束时间:未删除、未终止、已发布、当前时间小于计划结束时间
     * @param planId
     * @return
     */
    public Date getToTerminatePlan(Long planId) {
        return irrigatePlanMapper.getToTerminatePlan(planId);
    }
 
    /**
     * 根据计划ID获取待终止的取水口ID(已发布开发命令,无论是否成功)
     * @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获取待终止的取水口列表(开阀成功的)
     * @param planId
     * @return
     */
    public List<VoToTerminateIntakes> getToTerminateIntakes(Long planId) {
        return irIntakeOperateMapper.getToTerminateIntakes(planId);
    }
 
    /**
     * 根据命令日志ID获取取水口及虚拟卡信息,终止灌溉计划时使用,用来执行远程关阀
     * @param commandId
     * @return
     */
    public VoIntakeVc getValveOpen(Long commandId) {
        return rmdCommandHistoryMapper.getValveOpen(commandId);
    }
 
    /**
     * 修改灌溉计划
     * @param po
     * @return
     */
    public Integer updatePlan(IrIrrigatePlan po) {
        return irrigatePlanMapper.updateByPrimaryKeySelective(po);
    }
 
    /**
     * 获取未完成的计划列表,小程序计划列表页使用
      * @return
     */
    public List<VoPlans> getNotCompletePlans() {
        return irrigatePlanMapper.getNotCompletePlans();
    }
 
    /**
     * 获取已完成的计划列表,小程序计划列表页使用
     * @return
     */
    public List<VoPlans> getCompletedPlans() {
        return irrigatePlanMapper.getCompletedPlans();
    }
 
    /**
     * 根据指定的计划ID获取启动模式
     * @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
     */
    public Long addPlanOperate(IrPlanOperate po) {
        irPlanOperateMapper.insert(po);
        return po.getId();
    }
 
    /**
     * 添加灌溉次序记录
     * @param po
     * @return
     */
    public Long addIrrigateSchedule(IrIrrigateSchedule po) {
        irIrrigateScheduleMapper.insert(po);
        return po.getId();
    }
 
    /**
     * 根据计划ID终止灌溉次序,将灌溉次序的当前状态改为已终止
     * @param planId
     * @return
     */
    public Integer terminateSchedule(Long planId) {
        return irIrrigateScheduleMapper.terminateSchedule(planId);
    }
 
    /**
     * 根据计划ID获取灌溉次序记录
     * @param planId
     * @return
     */
    public List<VoIrrigateSchedule> getSchedulesByPlanId(Long planId) {
        return irIrrigateScheduleMapper.getSchedulesByPlanId(planId);
    }
 
    /**
     * 根据组ID获取取水口ID集合
     * @param groupId
     * @return
     */
    public List<Long> getIntakeIdsByGroupId(Long groupId) {
        return irGroupIntakeMapper.getIntakeIdsByGroupId(groupId);
    }
 
    /**
     * 添加计划次序关联记录
     * @param po
     * @return
     */
    public Long addPlanSchedule(IrPlanSchedule po) {
        irPlanScheduleMapper.insert(po);
        return po.getId();
    }
 
    /**
     * 根据计划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 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获取取水口ID
     * @param unitId
     * @return
     */
    public Long getIntakeIdByUnitId(Long unitId) {
        return irIrrigateUnitMapper.getIntakeIdByUnitId(unitId);
    }
 
    /**
     * 根据计划ID获取计划发布结果
     * @param planId
     * @return
     */
    public VoPlanDetails getPublishResults(Long planId) {
        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());
            if(intakeResults != null) {
                groupResult.setPublishResult(intakeResults);
            }
        }
 
        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;
 
    }
}