zhubaomin
2025-04-02 bab406b3393a3df6854ee5970a5a62a33f34e597
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
package com.dy.pipIrrIrrigate.project;
 
 
import com.dy.common.webUtil.QueryResultVo;
import com.dy.pipIrrGlobal.daoIr.IrProjectGroupMapper;
import com.dy.pipIrrGlobal.daoIr.IrProjectMapper;
import com.dy.pipIrrGlobal.pojoIr.IrProject;
import com.dy.pipIrrGlobal.pojoIr.IrProjectGroup;
import com.dy.pipIrrGlobal.voIr.VoProject;
import com.dy.pipIrrGlobal.voIr.VoProjectDetail;
import com.dy.pipIrrGlobal.voIr.VoProjectOne;
import com.dy.pipIrrGlobal.voIr.VoProjectSimple;
import com.dy.pipIrrIrrigate.project.dto.IrrigateProject;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.PojoUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @author :WuZeYu
 * @Date :2024/5/14  13:56
 * @LastEditTime :2024/5/14  13:56
 * @Description
 */
@Slf4j
@Service
public class ProjectSv {
    @Autowired
    private IrProjectMapper irProjectMapper;
 
    @Autowired
    private IrProjectGroupMapper irProjectGroupMapper;
 
    /**
     * 添加项目,旧版本,原则上废弃
     * @param po
     * @return
     */
    public Integer addProject(IrProject po) {
        po.setOperateTime(new Date());
        po.setDeleted((byte) 0);
        if (po.getVillageId() != null) {
            po.setTownId(irProjectMapper.getSupperByVillageId(po.getVillageId()));
            po.setCountyId(irProjectMapper.getSupperByVillageId(po.getTownId()));
            po.setCityId(irProjectMapper.getSupperByVillageId(po.getCountyId()));
            po.setProvinceId(irProjectMapper.getSupperByVillageId(po.getCityId()));
        }
        if (po.getTownId() != null) {
            po.setCountyId(irProjectMapper.getSupperByVillageId(po.getTownId()));
            po.setCityId(irProjectMapper.getSupperByVillageId(po.getCountyId()));
            po.setProvinceId(irProjectMapper.getSupperByVillageId(po.getCityId()));
        }
        if (po.getCountyId() != null) {
            po.setCityId(irProjectMapper.getSupperByVillageId(po.getCountyId()));
            po.setProvinceId(irProjectMapper.getSupperByVillageId(po.getCityId()));
        }
        if (po.getCityId() != null) {
            po.setProvinceId(irProjectMapper.getSupperByVillageId(po.getCityId()));
        }
        int rows = irProjectMapper.insertSelective(po);
        if (rows == 0) {
            return 0;
        }
        return 1;
    }
 
    /**
     * 添加项目,目前使用
     * @param po
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Map addIrrigateProject(IrrigateProject po) {
        String projectName = po.getProjectName();
        String irrigateGroups = po.getIrrigateGroups();
        Long operatorId = po.getOperator();
 
        IrProject irProject = new IrProject();
        irProject.setProjectName(projectName);
        irProject.setProjectState((byte)1);
        irProject.setOperator(operatorId);
        irProject.setOperateTime(new Date());
        irProject.setDeleted((byte)0);
        irProjectMapper.insert(irProject);
        Long projectId = irProject.getId();
        if(projectId == null) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "创建灌溉项目失败");
            map.put("content", null);
            return map;
        }
 
        String[] irrigateGroupArr = irrigateGroups.split(",");
        Integer sort = 1;
        for(String irrigateGroupId : irrigateGroupArr) {
            String trimmed = irrigateGroupId.trim();
            if(trimmed.isEmpty()) {
                continue;
            }
            try {
                Long irrigateGroupIdLong = Long.parseLong(trimmed);
                IrProjectGroup irProjectGroup = new IrProjectGroup();
                irProjectGroup.setProjectId(projectId);
                irProjectGroup.setGroupId(irrigateGroupIdLong);
                irProjectGroup.setSort(sort++);
                irProjectGroupMapper.insert(irProjectGroup);
            } 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 projectId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Map deleteIrrigateProject(Long projectId) {
        try {
            irProjectGroupMapper.unbindGroup(projectId);
            irProjectMapper.deleteByPrimaryKey(projectId);
 
            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
     */
    @Transactional(rollbackFor = Exception.class)
    public Map updateIrrigateProject(IrrigateProject po) {
        Long projectId = po.getProjectId();
        Map map_deleteProject = ((ProjectSv) AopContext.currentProxy()).deleteIrrigateProject(projectId);
        if(map_deleteProject.get("success").equals(false)) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", map_deleteProject.get("msg").toString());
            map.put("content", null);
            return map;
        }
 
        Map map_addProject = ((ProjectSv) AopContext.currentProxy()).addIrrigateProject(po);
        if(map_addProject.get("success").equals(false)) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", map_addProject.get("msg").toString());
            map.put("content", null);
            return map;
        }
 
        Map map = new HashMap<>();
        map.put("success", true);
        map.put("msg", "修改灌溉项目成功");
        map.put("content", null);
        return map;
    }
 
    /**
     * 删除项目,旧版本,原则上废弃
     * @param id
     */
    public Integer deleteProject(Long id) {
        int rows = irProjectMapper.deleteLogicById(id);
        if (rows == 0) {
            return 0;
        }
            return 1;
    }
 
    /**
     * 修改项目信息
     * @param po
     * @return
     */
    public Integer updateProject(IrProject po){
        po.setOperateTime(new Date());
        if (po.getVillageId() != null) {
            po.setTownId(irProjectMapper.getSupperByVillageId(po.getVillageId()));
            po.setCountyId(irProjectMapper.getSupperByVillageId(po.getTownId()));
            po.setCityId(irProjectMapper.getSupperByVillageId(po.getCountyId()));
            po.setProvinceId(irProjectMapper.getSupperByVillageId(po.getCityId()));
        }
        if (po.getTownId() != null) {
            po.setCountyId(irProjectMapper.getSupperByVillageId(po.getTownId()));
            po.setCityId(irProjectMapper.getSupperByVillageId(po.getCountyId()));
            po.setProvinceId(irProjectMapper.getSupperByVillageId(po.getCityId()));
        }
        if (po.getCountyId() != null) {
            po.setCityId(irProjectMapper.getSupperByVillageId(po.getCountyId()));
            po.setProvinceId(irProjectMapper.getSupperByVillageId(po.getCityId()));
        }
        if (po.getCityId() != null) {
            po.setProvinceId(irProjectMapper.getSupperByVillageId(po.getCityId()));
        }
        int rows = irProjectMapper.updateByPrimaryKeySelective(po);
        if (rows == 0){
            return 0;
        }
        return 1;
    }
 
    /**
     * 修改项目状态
     * @param po
     * @return
     */
    public Integer updateProjectState(IrProject po){
        po.setOperateTime(new Date());
        int rows = irProjectMapper.updateProjectState(po);
        if (rows == 0){
            return 0;
        }
        return 1;
    }
 
    /**
     * 获取一个项目数据
     * @param id
     * @return
     */
    public VoProjectOne selectById(Long id){
        VoProjectOne irProject = irProjectMapper.selectById(id);
        return irProject;
    }
 
    /**
     * 分页查询项目,旧版本,原则上废弃
     * @param queryVo
     * @return
     */
    public QueryResultVo<List<VoProject>> getProjects(QueryVo queryVo){
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        Long itemTotal = irProjectMapper.getRecordCount(params);
 
        QueryResultVo<List<VoProject>> rsVo = new QueryResultVo<>();
        rsVo.pageSize = queryVo.pageSize;
        rsVo.pageCurr = queryVo.pageCurr;
        rsVo.calculateAndSet(itemTotal, params);
        rsVo.obj = irProjectMapper.getProjects(params);
        return rsVo;
    }
 
    /**
     * 分页查询项目,新版本,目前使用
     * @param queryVo
     * @return
     */
    public QueryResultVo<List<VoProjectSimple>> getSimpleProjects(QueryVo queryVo) {
        Map<String, Object> params = (Map<String, Object>) PojoUtils.generalize(queryVo);
        Long itemTotal = irProjectMapper.getSimpleProjectsCount(params);
 
        QueryResultVo<List<VoProjectSimple>> rsVo = new QueryResultVo<>();
        rsVo.pageSize = queryVo.pageSize;
        rsVo.pageCurr = queryVo.pageCurr;
        rsVo.calculateAndSet(itemTotal, params);
        rsVo.obj = irProjectMapper.getSimpleProjects(params);
        return rsVo;
    }
 
    /**
     * 根据项目Id获取项目详情,编辑项目信息使用
     * @param projectId
     * @return
     */
    public Map getProjectDetail(Long projectId) {
        try {
            VoProjectDetail voProjectDetail = irProjectMapper.getProjectDetail(projectId);
            Map map = new HashMap<>();
            map.put("success", true);
            map.put("msg", "获取项目详情成功");
            map.put("content", voProjectDetail);
            return map;
        } catch (Exception e) {
            Map map = new HashMap<>();
            map.put("success", false);
            map.put("msg", "获取项目详情失败");
            map.put("content", null);
            return map;
        }
    }
}