liurunyu
4 天以前 e8937f27fce194d015ce6c27d286e7fc89f5e580
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package com.dy.pipIrrIrrigate.irrigateGroup;
 
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pipIrrGlobal.daoIr.IrGroupClientMapper;
import com.dy.pipIrrGlobal.daoIr.IrGroupIntakeMapper;
import com.dy.pipIrrGlobal.daoIr.IrGroupUnitMapper;
import com.dy.pipIrrGlobal.daoIr.IrIrrigateGroupMapper;
import com.dy.pipIrrGlobal.pojoIr.IrGroupClient;
import com.dy.pipIrrGlobal.pojoIr.IrGroupIntake;
import com.dy.pipIrrGlobal.pojoIr.IrGroupUnit;
import com.dy.pipIrrGlobal.pojoIr.IrIrrigateGroup;
import com.dy.pipIrrGlobal.voIr.VoGroup;
import com.dy.pipIrrGlobal.voIr.VoGroupDetail;
import com.dy.pipIrrGlobal.voIr.VoGroupOne;
import com.dy.pipIrrGlobal.voIr.VoGroupSimple;
import com.dy.pipIrrIrrigate.irrigateGroup.dto.IrrigateGroup;
import com.dy.pipIrrIrrigate.irrigateGroup.qo.QoGroup;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
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;
 
/**
 * @author :WuZeYu
 * @Date :2024/5/21  15:57
 * @LastEditTime :2024/5/21  15:57
 * @Description
 */
@Slf4j
@Service
public class IrrigateGroupSv {
    @Autowired
    private IrIrrigateGroupMapper irIrrigateGroupMapper;
 
    @Autowired
    private IrGroupUnitMapper irGroupUnitMapper;
 
    @Autowired
    private IrGroupClientMapper irGroupClientMapper;
 
    @Autowired
    private IrGroupIntakeMapper irGroupIntakeMapper;
 
    /**
     * 创建轮灌组
     *
     * @param po
     * @return
     */
    public Integer addIrrigateGroup(IrIrrigateGroup po) {
        po.setOperateTime(new Date());
        po.setDeleted((byte) 0);
        int rows = irIrrigateGroupMapper.insertSelective(po);
        if (rows == 0) {
            return 0;
        }
        return 1;
    }
 
    /**
     * 删除轮灌组
     *
     * @param id
     */
    public Integer deleteIrrigateGroup(Long id) {
        int rows = irIrrigateGroupMapper.deleteLogicById(id);
        int rows1 = irGroupUnitMapper.deleteByGroupId(id);
        if (rows == 0) {
            return 0;
        }
        return 1;
    }
 
    /**
     * 修改轮灌组
     * @param po
     * @return
     */
    public Integer updateIrrigateGroup(IrIrrigateGroup po){
        po.setOperateTime(new Date());
        int rows = irIrrigateGroupMapper.updateByPrimaryKeySelective(po);
        if (rows == 0){
            return 0;
        }
        return 1;
    }
 
    /**
     * 获取一个轮灌组
     * @param id
     * @return
     */
    public VoGroupOne selectById(Long id){
        VoGroupOne unit = irIrrigateGroupMapper.selectById(id);
        return unit;
    }
 
    /**
     * 分页查询轮灌组
     * @param queryVo
     * @return
     */
    public QueryResultVo<List<VoGroup>> getIrrigateGroups(QueryVo queryVo){
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        Long itemTotal = irIrrigateGroupMapper.getRecordCount(params);
 
        QueryResultVo<List<VoGroup>> rsVo = new QueryResultVo<>();
        rsVo.pageSize = queryVo.pageSize;
        rsVo.pageCurr = queryVo.pageCurr;
        rsVo.calculateAndSet(itemTotal, params);
        rsVo.obj = irIrrigateGroupMapper.getIrrigateGroups(params);
        return rsVo;
    }
 
    /**
     * 轮灌组添加灌溉单元
     *
     * @param po
     * @return
     */
    public Integer addGroupUnit(IrGroupUnit po) {
        po.setOperateDt(new Date());
        int rows = irGroupUnitMapper.insertSelective(po);
        if (rows == 0) {
            return 0;
        }
        return 1;
    }
 
    /**
     * 轮灌组移除灌溉单元
     *
     * @param po
     * @return
     */
    public Integer deleteGroupUnit(IrGroupUnit po) {
        int rows = irGroupUnitMapper.deleteByUnitIdGroupId(po);
        if (rows == 0) {
            return 0;
        }
        return 1;
    }
 
    /**
     * 查一个轮灌组绑定的灌溉单元id
     * @param groupId
     * @return
     */
    public List<Long> getGroupBindUnits(Long groupId) {
        List<Long> groupBindUnits = irGroupUnitMapper.getGroupBindUnits(groupId);
        return groupBindUnits;
    }
 
    /**
     *     //查未绑定轮灌组的灌溉单元id
     * @return
     */
    public List<Long> getNotBindUnits() {
        List<Long> notBindUnits = irGroupUnitMapper.getNotBindUnits();
        return notBindUnits;
    }
 
    //轮灌组关联农户
    public Long addGroupClient(IrGroupClient po) {
        irGroupClientMapper.insert(po);
        return po.getId();
    }
 
    /**
     * 根据轮灌组名称查询是否存在(添加轮灌组时使用)
     * @param groupCode 轮灌组名称
     * @return 是否存在
     */
    public boolean existsByGroupCode(String groupCode) {
        if (groupCode == null || "".equals(groupCode.trim())) {
            return false;
        }
        return irIrrigateGroupMapper.countByGroupCode(groupCode) > 0;
    }
 
    /**
     * 根据轮灌组名称查询是否存在(修改轮灌组时使用,排除当前ID)
     * @param groupCode 轮灌组名称
     * @param excludeId 需要排除的ID
     * @return 是否存在
     */
    public boolean existsByGroupCodeExcludeId(String groupCode, Long excludeId) {
        if (groupCode == null || "".equals(groupCode.trim())) {
            return false;
        }
        return irIrrigateGroupMapper.countByGroupCodeExcludeId(groupCode, excludeId) > 0;
    }
 
    /**
     * 删除轮灌组与农户管理
     * @param id
     * @return
     */
    public Integer deleteGroupClient (Long id) {
        return irGroupClientMapper.deleteByPrimaryKey(id);
    }
 
    /**
     * 创建轮灌组
     * @param po
     * @return
     */
    public Map addIrrigateGroup(IrrigateGroup po) {
        IrIrrigateGroup irrigateGroup = new IrIrrigateGroup();
        irrigateGroup.setGroupCode(po.getGroupCode());
        irrigateGroup.setDefaultDuration(po.getDefaultDuration());
        irrigateGroup.setOperator(po.getOperator());
        irrigateGroup.setOperateTime(new Date());
        irrigateGroup.setDeleted((byte) 0);
        irIrrigateGroupMapper.insertSelective(irrigateGroup);
        Long groupId = irrigateGroup.getId();
        if(groupId == null) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "创建轮灌组失败");
            map.put("content", null);
            return map;
        }
 
        String[] intakeArr = po.getIntakes().split(",");
        Integer sort = 1;
        for(String intekeId : intakeArr) {
            String trimmed = intekeId.trim();
            if(trimmed.isEmpty()) {
                continue;
            }
            try {
                Long intakeIdLong = Long.parseLong(trimmed);
                IrGroupIntake irGroupIntake = new IrGroupIntake();
                irGroupIntake.setGroupId(groupId);
                irGroupIntake.setIntakeId(intakeIdLong);
                irGroupIntakeMapper.insert(irGroupIntake);
            } catch (NumberFormatException  e) {
                Map map = new HashMap<>();
                map.put("success", false);
                map.put("msg", "取水口ID格式错误");
                map.put("content", null);
                return map;
            }
        }
        Map map = new HashMap<>();
        map.put("success", true);
        map.put("msg", "添加轮灌组成功");
        map.put("content", null);
        return map;
    }
 
    /**
     * 根据组ID删除取水口关联
     * @param groupId
     * @return
     */
    public Map deleteGroup(Long groupId) {
        try {
            irGroupIntakeMapper.deleteByGroupId(groupId);
            irIrrigateGroupMapper.deleteByPrimaryKey(groupId);
 
            Map map = new HashMap<>();
            map.put("success", true);
            map.put("msg", "灌溉项目删除成功");
            map.put("content", null);
            return map;
        } catch (Exception e) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "轮灌组删除失败");
            map.put("content", null);
            return map;
        }
    }
 
    /**
     * 修改轮灌组
     * @param po
     * @return
     */
    public Map updateIrrigateGroup(IrrigateGroup po) {
        // 修改轮灌组
        IrIrrigateGroup irrigateGroup = new IrIrrigateGroup();
        irrigateGroup.setId(po.getGroupId());
        irrigateGroup.setGroupCode(po.getGroupCode());
        irrigateGroup.setDefaultDuration(po.getDefaultDuration());
        irrigateGroup.setOperator(po.getOperator());
        irrigateGroup.setOperateTime(new Date());
        irrigateGroup.setDeleted((byte) 0);
        irIrrigateGroupMapper.updateByPrimaryKeySelective(irrigateGroup);
 
        // 删除轮灌组与取水口关联
        Long groupId = po.getGroupId();
        irGroupIntakeMapper.deleteByGroupId(groupId);
 
        // 添加轮灌组与取水口关联
        String[] intakeArr = po.getIntakes().split(",");
        Integer sort = 1;
        for(String intekeId : intakeArr) {
            String trimmed = intekeId.trim();
            if(trimmed.isEmpty()) {
                continue;
            }
            try {
                Long intakeIdLong = Long.parseLong(trimmed);
                IrGroupIntake irGroupIntake = new IrGroupIntake();
                irGroupIntake.setGroupId(groupId);
                irGroupIntake.setIntakeId(intakeIdLong);
                irGroupIntakeMapper.insert(irGroupIntake);
            } catch (NumberFormatException  e) {
                Map map = new HashMap<>();
                map.put("success", false);
                map.put("msg", "取水口ID格式错误");
                map.put("content", null);
                return map;
            }
 
        }
        Map map = new HashMap<>();
        map.put("success", true);
        map.put("msg", "修改轮灌组成功");
        map.put("content", null);
        return map;
    }
 
    /**
     * 查询轮灌组列表
     * @param queryVo
     * @return
     */
    public QueryResultVo<List<VoGroupSimple>> getSimpleGroups(QoGroup queryVo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        Long itemTotal = irIrrigateGroupMapper.getSimpleGroupCount(params);
 
        QueryResultVo<List<VoGroupSimple>> rsVo = new QueryResultVo<>();
        rsVo.pageSize = queryVo.pageSize;
        rsVo.pageCurr = queryVo.pageCurr;
        rsVo.calculateAndSet(itemTotal, params);
        rsVo.obj = irIrrigateGroupMapper.getSimpleGroups(params);
        return rsVo;
    }
 
    /**
     * 查询全部轮灌组
     * @return
     */
    public List<VoGroupSimple> getAllGroups() {
        //Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        //Long itemTotal = irIrrigateGroupMapper.getSimpleGroupCount(params);
 
        //QueryResultVo<List<VoGroupSimple>> rsVo = new QueryResultVo<>();
        //rsVo.pageSize = queryVo.pageSize;
        //rsVo.pageCurr = queryVo.pageCurr;
        //rsVo.calculateAndSet(itemTotal, params);
        //rsVo.obj = irIrrigateGroupMapper.getSimpleGroups(null);
        //return rsVo;
 
        return irIrrigateGroupMapper.getSimpleGroups(null);
    }
 
    /**
     * 获取轮灌组详情
     * @param groupId
     * @return
     */
    public Map getGroupDetail(Long groupId) {
        try {
            VoGroupDetail voGroupDetail = irIrrigateGroupMapper.getGroupDetail(groupId);
            Map map = new HashMap<>();
            map.put("success", true);
            map.put("msg", "获取轮灌组详情成功");
            map.put("content", voGroupDetail);
            return map;
        } catch (Exception e) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "获取轮灌组详情失败");
            map.put("content", null);
            return map;
        }
    }
 
}