广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package cn.huge.module.syncgrid.service;
 
import cn.huge.base.common.constant.GzRegionBaseEnum;
import cn.huge.base.common.exception.ServiceException;
import cn.huge.base.common.utils.DateUtils;
import cn.huge.base.common.utils.IdUtils;
import cn.huge.base.common.utils.ObjectUtils;
import cn.huge.module.client.api.impl.UtilsClientImpl;
import cn.huge.module.constant.BaseConsts;
import cn.huge.module.ctuser.domain.po.CtAccount;
import cn.huge.module.ctuser.domain.po.CtUnit;
import cn.huge.module.ctuser.domain.po.CtUser;
import cn.huge.module.ctuser.domain.po.CtUserole;
import cn.huge.module.ctuser.service.CtUnitService;
import cn.huge.module.cust.constant.UserBaseConsts;
import cn.huge.module.syncgrid.dao.mapper.ThGridUnitMapper;
import cn.huge.module.syncgrid.domain.po.ThGridUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
 
import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
/**
 * @title: 第三方网格单位信息业务逻辑处理
 * @Description 第三方网格单位信息业务逻辑处理
 * @company hugeinfo
 * @author liyj
 * @Time 2024-12-18 11:40:51
 * @version 1.0.0
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ThGridUnitService extends ServiceImpl<ThGridUnitMapper, ThGridUnit>{
 
    @Autowired
    private ThGridUnitMapper mapper;
 
    @Autowired
    private UtilsClientImpl utilsClient;
    @Autowired
    private CtUnitService ctUnitService;
 
    /**
    * 更新对象
    * @param entity 对象
    */
    public void updateThGridUnit(ThGridUnit entity){
        try{
            mapper.updateThGridUnit(entity);
        }catch (Exception e){
            log.error("[ThGridUnitService.updateThGridUnit]调用失败,异常信息:"+e, e);
            throw new ServiceException("ThGridUnitService.updateThGridUnit", e);
        }
    }
 
    /**
    * 条件更新对象
    * @param entity 对象
    * @param terms 条件
    */
    public void updateThGridUnitTerms(ThGridUnit entity, Map<String, Object> terms){
        try{
            mapper.updateThGridUnitTerms(entity, terms);
        }catch (Exception e){
            log.error("[ThGridUnitService.updateThGridUnitTerms]调用失败,异常信息:"+e, e);
            throw new ServiceException("ThGridUnitService.updateThGridUnitTerms", e);
        }
    }
 
    /**
    * 根据编号物理删除
    * @param id 查询条件集合
    */
    public void deleteThGridUnit(String id){
        try{
            mapper.deleteThGridUnit(id);
        }catch (Exception e){
            log.error("[ThGridUnitService.deleteThGridUnit]调用失败,异常信息:"+e, e);
            throw new ServiceException("ThGridUnitService.deleteThGridUnit", e);
        }
    }
 
    /**
    * 按条件查询
    * @param terms 条件
    * @return List
    */
    public List<ThGridUnit> listTerms(Map<String, Object> terms){
        return mapper.listTerms(terms);
    }
 
    /**
    * 按条件统计
    * @param terms 条件
    * @return long
    */
    public long countTerms(Map<String, Object> terms){
        return mapper.countTerms(terms);
    }
 
    /**
    * 按条件分页查询
    * @param page 分页对象
    * @param terms 条件
    * @return Page
    */
    public Page<ThGridUnit> pageQuery(PageRequest page, Map<String, Object> terms){
        long total = mapper.countTerms(terms);
        List<ThGridUnit> content = mapper.pageTerms(page, terms);
        return new PageImpl<ThGridUnit>(content, page, total);
    }
 
    /**
     * 获取单元格数据
     * @param sheet
     * @param r
     * @param c
     * @return
     */
    public String transform(Sheet sheet, int r, int c) {
        if (ObjectUtils.isNotEmpty(sheet)) {
            Cell cell = sheet.getRow(r).getCell(c);
            if (ObjectUtils.isNotEmpty(cell)) {
                cell.setCellType(CellType.STRING);
                String val = cell.getStringCellValue();
                return val.replace(" ", "");
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
 
    /**
     * 通过Excel操作-找出对应映射的单位信息
     * @param request
     */
    public void inputByExcel241218(MultipartHttpServletRequest request){
        try{
            Iterator<String> itr = request.getFileNames();
            while (itr.hasNext()) {
                MultipartFile file = request.getFile(itr.next());
                String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                if (".xls".equals(fileSuffix) || ".xlsx".equals(fileSuffix) || "xls".equals(fileSuffix) || "xlsx".equals(fileSuffix)) {
                    InputStream inputStream = file.getInputStream();
                    Workbook wb = null;
                    //当excel是2003时
                    if (".xls".equals(fileSuffix)) {
                        wb = new HSSFWorkbook(inputStream);
                    } else {
                        wb = new XSSFWorkbook(inputStream);
                    }
                    //得到第一个shell
                    Sheet sheet = wb.getSheetAt(0);
                    //得到Excel的行数
                    int totalRows = sheet.getPhysicalNumberOfRows();
                    //人员信息
                    Date nowDate = DateUtils.getNowDate();
                    for (int r = 1; r < totalRows; r++) {
                        try {
                            // 获取数据
                            String thirdUnitId = this.transform(sheet, r, 0);
                            String thirdGridCode = this.transform(sheet, r, 1);
                            String thirdUnitCode = this.transform(sheet, r, 2);
                            String thirdUnitName = this.transform(sheet, r, 3);
                            // 新增第三方网格单位
                            ThGridUnit thGridUnit = new ThGridUnit();
                            thGridUnit.setThirdUnitId(thirdUnitId);
                            thGridUnit.setThirdGridCode(thirdGridCode);
                            thGridUnit.setThirdUnitCode(thirdUnitCode);
                            thGridUnit.setThirdUnitName(thirdUnitName);
                            thGridUnit.setCreateTime(nowDate);
                            thGridUnit.setUpdateTime(nowDate);
                            thGridUnit.setThirdUnitName(thirdUnitName);
                            // 查询对应的平台单位
                            QueryWrapper<CtUnit> ctUnitQueryWrapper = new QueryWrapper<>();
                            ctUnitQueryWrapper.eq("area", GzRegionBaseEnum.AREA_7.getIndex());
                            int count = thirdGridCode.split("-").length;
                            // 判断是区级单位
                            if (count == 1){
                                List<CtUnit> ctUnitList = null;
                                ctUnitQueryWrapper.eq("unit_grade", UserBaseConsts.UNIT_GRADE_2);
                                if (thirdUnitName.indexOf("综治中心") != -1){
                                    ctUnitQueryWrapper.eq("unit_type", UserBaseConsts.UNIT_TYPE_101);
                                    ctUnitList = ctUnitService.list(ctUnitQueryWrapper);
 
                                }else if ("白云区".equals(thirdUnitName)){
                                    ctUnitQueryWrapper.eq("unit_type", UserBaseConsts.UNIT_TYPE_101);
                                    ctUnitList = ctUnitService.list(ctUnitQueryWrapper);
 
                                }else {
                                    String newUnitName = thirdUnitName.replace("白云区", "");
                                    ctUnitQueryWrapper.like("unit_name", newUnitName);
                                    ctUnitList = ctUnitService.list(ctUnitQueryWrapper);
                                }
                                // 判断是否匹配
                                if (CollectionUtils.isNotEmpty(ctUnitList)){
                                    thGridUnit.setDyhUnitId(ctUnitList.get(0).getId());
                                }else {
                                    thGridUnit.setUnitCode("区级-未匹配");
                                }
                            }else if (count == 2){
                                // 判断是镇街单位
                                List<CtUnit> ctUnitList = null;
                                ctUnitQueryWrapper.eq("unit_grade", UserBaseConsts.UNIT_GRADE_3).eq("road", thirdGridCode);
                                if (thirdUnitName.indexOf("综治中心") != -1){
                                    ctUnitQueryWrapper.eq("unit_type", UserBaseConsts.UNIT_TYPE_101);
                                    ctUnitList = ctUnitService.list(ctUnitQueryWrapper);
                                }else if (thirdUnitName.indexOf("同和街综合治理办公室") != -1){
                                    ctUnitQueryWrapper.eq("unit_type", UserBaseConsts.UNIT_TYPE_101);
                                    ctUnitList = ctUnitService.list(ctUnitQueryWrapper);
                                }else if ("三元里街".equals(thirdUnitName) ||
                                        "景泰街".equals(thirdUnitName) ||
                                        "同德街".equals(thirdUnitName) ||
                                        "黄石街".equals(thirdUnitName) ||
                                        "棠景街".equals(thirdUnitName) ||
                                        "新市街".equals(thirdUnitName) ||
                                        "京溪街".equals(thirdUnitName) ||
                                        "嘉禾街".equals(thirdUnitName) ||
                                        "均禾街".equals(thirdUnitName) ||
                                        "石井街".equals(thirdUnitName) ||
                                        "金沙街".equals(thirdUnitName) ||
                                        "云城街".equals(thirdUnitName) ||
                                        "鹤龙街".equals(thirdUnitName) ||
                                        "白云湖街".equals(thirdUnitName) ||
                                        "石门街".equals(thirdUnitName) ||
                                        "龙归街".equals(thirdUnitName) ||
                                        "大源街".equals(thirdUnitName) ||
                                        "人和镇".equals(thirdUnitName) ||
                                        "太和镇".equals(thirdUnitName) ||
                                        "钟落潭镇".equals(thirdUnitName) ||
                                        "永平街".equals(thirdUnitName) ||
                                        "同和街".equals(thirdUnitName) ||
                                        "江高镇".equals(thirdUnitName) ||
                                        "鹤龙街".equals(thirdUnitName)){
                                    ctUnitQueryWrapper.eq("unit_type", UserBaseConsts.UNIT_TYPE_101);
                                    ctUnitList = ctUnitService.list(ctUnitQueryWrapper);
                                }else {
                                    String newUnitName = thirdUnitName.replace("三元里街", "").replace("三元里", "")
                                            .replace("景泰街", "").replace("景泰", "")
                                            .replace("同德街", "").replace("同德", "")
                                            .replace("黄石街", "").replace("黄石", "")
                                            .replace("棠景街", "").replace("棠景", "")
                                            .replace("新市街", "").replace("新市", "")
                                            .replace("京溪街", "").replace("京溪", "")
                                            .replace("嘉禾街", "").replace("嘉禾", "")
                                            .replace("均禾街", "").replace("均禾", "")
                                            .replace("石井街", "").replace("石井", "")
                                            .replace("金沙街", "").replace("金沙", "")
                                            .replace("云城街", "").replace("云城", "")
                                            .replace("鹤龙街", "").replace("鹤龙", "")
                                            .replace("白云湖街", "").replace("白云湖", "")
                                            .replace("石门街", "").replace("石门", "")
                                            .replace("龙归街", "").replace("龙归", "")
                                            .replace("大源街", "").replace("大源", "")
                                            .replace("人和镇", "")
                                            .replace("太和镇", "")
                                            .replace("钟落潭镇", "")
                                            .replace("永平街", "").replace("永平", "")
                                            .replace("同和街", "").replace("同和", "")
                                            .replace("江高镇", "")
                                            .replace("鹤龙街", "").replace("鹤龙", "");
                                    ctUnitQueryWrapper.like("unit_name", newUnitName);
                                    ctUnitList = ctUnitService.list(ctUnitQueryWrapper);
                                }
                                // 判断是否匹配
                                if (CollectionUtils.isNotEmpty(ctUnitList)){
                                    thGridUnit.setDyhUnitId(ctUnitList.get(0).getId());
                                }else {
                                    thGridUnit.setUnitCode("镇街级-未匹配");
                                }
 
                            }else if (count == 3){
                                // 判断是村级单位
                                ctUnitQueryWrapper.eq("unit_grade", UserBaseConsts.UNIT_GRADE_4).eq("village", thirdGridCode);
//                                String newUnitName = thirdUnitName.replace("三元里街", "")
//                                        .replace("景泰街", "")
//                                        .replace("同德街", "")
//                                        .replace("黄石街", "")
//                                        .replace("棠景街", "")
//                                        .replace("新市街", "")
//                                        .replace("京溪街", "")
//                                        .replace("嘉禾街", "")
//                                        .replace("均禾街", "")
//                                        .replace("石井街", "")
//                                        .replace("金沙街", "")
//                                        .replace("云城街", "")
//                                        .replace("鹤龙街", "")
//                                        .replace("白云湖街", "")
//                                        .replace("石门街", "")
//                                        .replace("龙归街", "")
//                                        .replace("大源街", "")
//                                        .replace("人和镇", "")
//                                        .replace("太和镇", "")
//                                        .replace("钟落潭镇", "")
//                                        .replace("永平街", "")
//                                        .replace("同和街", "")
//                                        .replace("江高镇", "")
//                                        .replace("鹤龙街", "");
//                                ctUnitQueryWrapper.like("unit_name", newUnitName);
                                List<CtUnit> ctUnitList = ctUnitService.list(ctUnitQueryWrapper);
                                // 判断是否匹配
                                if (CollectionUtils.isNotEmpty(ctUnitList)){
                                    thGridUnit.setDyhUnitId(ctUnitList.get(0).getId());
                                }else {
                                    thGridUnit.setUnitCode("村居级-未匹配");
                                }
                            }else {
                                thGridUnit.setUnitCode("网格机构");
                            }
                            this.save(thGridUnit);
                        } catch (Exception e) {
                            log.error(e.getMessage(), e);
                            continue;
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("[CtUnitService.inputByExcel241124]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUnitService.inputByExcel241124", e);
        }
    }
 
 
    /**
     * 通过Excel操作-更新映射表映射关系和新增对应单位信息
     * @param request
     */
    public void inputByExcel241219(MultipartHttpServletRequest request){
        try{
            Iterator<String> itr = request.getFileNames();
            while (itr.hasNext()) {
                MultipartFile file = request.getFile(itr.next());
                String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                if (".xls".equals(fileSuffix) || ".xlsx".equals(fileSuffix) || "xls".equals(fileSuffix) || "xlsx".equals(fileSuffix)) {
                    InputStream inputStream = file.getInputStream();
                    Workbook wb = null;
                    //当excel是2003时
                    if (".xls".equals(fileSuffix)) {
                        wb = new HSSFWorkbook(inputStream);
                    } else {
                        wb = new XSSFWorkbook(inputStream);
                    }
                    //得到第一个shell
                    Sheet sheet = wb.getSheetAt(0);
                    //得到Excel的行数
                    int totalRows = sheet.getPhysicalNumberOfRows();
                    //人员信息
                    Date nowDate = DateUtils.getNowDate();
                    for (int r = 1; r < totalRows; r++) {
                        try {
                            // 获取数据
                            String thirdUnitId = this.transform(sheet, r, 0);
                            String thirdGridCode = this.transform(sheet, r, 1);
                            String thirdUnitCode = this.transform(sheet, r, 2);
                            String thirdUnitName = this.transform(sheet, r, 3);
                            String dyhUnitId = this.transform(sheet, r, 4);
                            // 新增第三方网格单位
                            UpdateWrapper<ThGridUnit> updateWrapper = new UpdateWrapper<>();
                            updateWrapper.set("dyh_unit_id", dyhUnitId).set("unit_code", null)
                                    .eq("third_unit_id", thirdUnitId);
                            this.update(updateWrapper);
                        } catch (Exception e) {
                            log.error(e.getMessage(), e);
                            continue;
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("[CtUnitService.inputByExcel241124]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUnitService.inputByExcel241124", e);
        }
    }
 
 
    /**
     * 通过Excel操作-更新映射表映射关系和新增对应单位信息
     * @param request
     */
    public void inputByExcel250121(MultipartHttpServletRequest request){
        try{
            Iterator<String> itr = request.getFileNames();
            while (itr.hasNext()) {
                MultipartFile file = request.getFile(itr.next());
                String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                if (".xls".equals(fileSuffix) || ".xlsx".equals(fileSuffix) || "xls".equals(fileSuffix) || "xlsx".equals(fileSuffix)) {
                    InputStream inputStream = file.getInputStream();
                    Workbook wb = null;
                    //当excel是2003时
                    if (".xls".equals(fileSuffix)) {
                        wb = new HSSFWorkbook(inputStream);
                    } else {
                        wb = new XSSFWorkbook(inputStream);
                    }
                    //得到第一个shell
                    Sheet sheet = wb.getSheetAt(0);
                    //得到Excel的行数
                    int totalRows = sheet.getPhysicalNumberOfRows();
                    //人员信息
                    Date nowDate = DateUtils.getNowDate();
                    for (int r = 1; r < totalRows; r++) {
                        try {
                            // 获取数据
                            String thirdUnitCode = this.transform(sheet, r, 0);
                            String thirdUnitName = this.transform(sheet, r, 1);
                            String dyhUnitId = this.transform(sheet, r, 2);
                            String dyhUnitName = this.transform(sheet, r, 3);
                            // 查询第三方网格单位
                            QueryWrapper<ThGridUnit> queryWrapper = new QueryWrapper<>();
                            queryWrapper.eq("third_unit_code", thirdUnitCode);
                            ThGridUnit thGridUnit = this.getOne(queryWrapper);
                            if (ObjectUtils.isNotEmpty(thGridUnit)){
                                thGridUnit.setDyhUnitId(dyhUnitId);
                                thGridUnit.setUnitCode(null);
                                this.updateById(thGridUnit);
                            }
                        } catch (Exception e) {
                            log.error(e.getMessage(), e);
                            continue;
                        }
                    }
                }
            }
        }catch (Exception e){
            log.error("[CtUnitService.inputByExcel250121]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUnitService.inputByExcel250121", e);
        }
    }
 
 
    /**
     * 手动操作-更新映射表映射关系和新增对应单位信息
     */
    public void updateDyh250121(){
        try{
            // 查询第三方网格单位
            QueryWrapper<ThGridUnit> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("unit_code", "网格机构");
            List<ThGridUnit> thGridUnitList = this.list(queryWrapper);
            for (ThGridUnit thGridUnit : thGridUnitList){
                String thirdGridCode = thGridUnit.getThirdGridCode();
                String parentCode = thirdGridCode.substring(0, thirdGridCode.length() - 4);
 
                QueryWrapper<ThGridUnit> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("third_grid_code", parentCode);
                List<ThGridUnit> parentThGridUnits = this.list(queryWrapper2);
 
                if (CollectionUtils.isNotEmpty(parentThGridUnits)){
                    thGridUnit.setDyhUnitId(parentThGridUnits.get(0).getDyhUnitId());
                    thGridUnit.setUnitCode(null);
                    this.updateById(thGridUnit);
                }
            }
        }catch (Exception e){
            log.error("[CtUnitService.inputByExcel250121]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUnitService.inputByExcel250121", e);
        }
    }
 
}