manailin
2021-06-12 7a2e1fd9e34ab58caf9fa8565e94dd9fcd174a1a
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
package com.panzhihua.common.listen;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.panzhihua.common.constants.BcDictionaryConstants;
import com.panzhihua.common.enums.*;
import com.panzhihua.common.exceptions.ServiceException;
import com.panzhihua.common.model.vos.BcDictionaryVO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.ComMngPopulationImportErrorVO;
import com.panzhihua.common.model.vos.community.ComMngPopulationServeExcelVO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.utlis.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @description: 实有人口导入监听
 * @author: llming
 */
@Slf4j
public class ComMngPopulationServeExcelListen extends AnalysisEventListener<Map<Integer, String>> {
    private CommunityService communityService;
 
    private Long communityId;
 
    private static int headSize = 0;
 
    private Map<Integer, String> headData;
 
 
    public ComMngPopulationServeExcelListen(CommunityService communityService, Long communityId) {
        this.communityService = communityService;
        this.communityId = communityId;
    }
 
    /**
     * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 5000;
    List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();
 
    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        list.add(data);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }
 
    /**
     * 这里会一行行的返回头
     *
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        headSize = headMap.size();
        headData = headMap;
    }
 
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        log.info("所有数据解析完成!");
    }
 
    /**
     * 不是固定的列只能手动处理
     */
    private void saveData() {
        R<List<BcDictionaryVO>> dictionaryR = communityService.listDictionaryByKey(BcDictionaryConstants.FAMILY);
 
        Map<String,String> dictMap = dictionaryR.getData().stream().collect(Collectors.toMap(BcDictionaryVO::getDictName,BcDictionaryVO::getDictValue));
//        Map<String,Integer> dictMap = new HashMap<>();
//        for(BcDictionaryVO vo : dictionaryR.getData()){
//            dictMap.put(vo.getDictName(),Integer.valueOf(vo.getDictValue()));
//        }
 
 
        int index = 2;
        try {
            ArrayList<ComMngPopulationServeExcelVO> voList = Lists.newArrayList();
            List<ComMngPopulationImportErrorVO> populationImportErrorVOList = new ArrayList<>();
            for (Map<Integer, String> oneData : list) {
                ComMngPopulationServeExcelVO vo = new ComMngPopulationServeExcelVO();
                //姓名和身份证都为空,为空户,无需操作,否则就解析年龄,性别,出生年月日
                if(StringUtils.isNotEmpty(oneData.get(0)) || StringUtils.isNotEmpty(oneData.get(1))){
                    if(StringUtils.isEmpty(oneData.get(0))){
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorPosition("第" + index + "行,第1列");
                        importErrorVO.setErrorMsg("名字不可为空,请填写姓名");
                        populationImportErrorVOList.add(importErrorVO);
                        index++;
                        continue;
                    }
                    vo.setName(oneData.get(0));
                    if(StringUtils.isEmpty(oneData.get(1))){
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorPosition("第" + index + "行,第2列"+oneData.get(1));
                        importErrorVO.setErrorMsg("身份证号不可为空,请填写身份证号");
                        populationImportErrorVOList.add(importErrorVO);
                        index++;
                        continue;
                    }
 
                    //判断身份证号码位数
                    if(oneData.get(1).length() != 18){
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorPosition("第" + index + "行,第2列"+oneData.get(1));
                        importErrorVO.setErrorMsg("身份证号位数有误,请检查身份证号码是否正确");
                        populationImportErrorVOList.add(importErrorVO);
                        index++;
                        continue;
                    }
                    vo.setCardNo(oneData.get(1));
                    //根据身份证号码解析年龄以及性别
                    //获取用户生日
                    String birthday = vo.getCardNo().substring(6, 14);
                    if(StringUtils.isNotEmpty(birthday)){
                        String year = birthday.substring(0,4);
                        String month = birthday.substring(4,6);
                        String day = birthday.substring(6,8);
                        vo.setBirthday(year + "-" + month + "-" + day);
                    }
                    //设置用户年龄
//                    vo.setAge(AgeUtils.getAgeFromBirthTime(birthday));
                    //获取用户性别
                    int sex = Integer.parseInt(vo.getCardNo().substring(16, 17));
                    if(sex%2 == 1){
                        vo.setSex(PopulSexEnum.nan.getCode());
                    }else{
                        vo.setSex(PopulSexEnum.nv.getCode());
                    }
                }
                if(StringUtils.isNotEmpty(oneData.get(2))){
//                    String nation = oneData.get(2);
//                    if(!nation.contains("族")){
//                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
//                        importErrorVO.setErrorPosition("第" + index + "行,第3列");
//                        importErrorVO.setErrorMsg("您填写的民族格式有误");
//                        populationImportErrorVOList.add(importErrorVO);
//                        index++;
//                        continue;
//                    }
                    vo.setNation(oneData.get(2));
                }
                if(StringUtils.isNotEmpty(oneData.get(3))){
                    vo.setPoliticalOutlook(PopulPoliticalOutlookEnum.getCodeByName(oneData.get(3)));
                }
                if(StringUtils.isNotEmpty(oneData.get(4))){
                    Integer isOk = PopulHouseUseEnum.getCodeByName(oneData.get(4));
                    if(isOk.equals(-1)){
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorPosition("第" + index + "行,第5列");
                        importErrorVO.setErrorMsg("您填写的是否租住有误");
                        populationImportErrorVOList.add(importErrorVO);
                        index++;
                        continue;
                    }
                    vo.setIsRent(isOk);
                }
                if(StringUtils.isNotEmpty(oneData.get(5))){
                    String ra = oneData.get(5);
                    if(Objects.equals(ra,"女儿")){
                        ra = "女";
                    }else if(Objects.equals(ra,"儿子")){
                        ra = "子";
                    }else if(Objects.equals(ra,"大女")){
                        ra = "长女";
                    }else if(Objects.equals(ra,"二女")){
                        ra = "次女";
                    }else if(Objects.equals(ra,"非亲属")){
                        ra = "其他";
                    }else if(Objects.equals(ra,"姐")){
                        ra = "姐姐";
                    }else if(Objects.equals(ra,"户主")){
                        ra = "本人";
                    }else if(Objects.equals(ra,"妻子")){
                        ra = "妻";
                    }else if(Objects.equals(ra,"哥") || Objects.equals(ra,"哥哥")){
                        ra = "兄";
                    }
 
 
                    String relation = dictMap.get(ra);
                    if(StringUtils.isEmpty(relation)){
                        vo.setRelationStr(ra);
//                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
//                        importErrorVO.setErrorPosition("第" + index + "行,第6列");
//                        importErrorVO.setErrorMsg("您填写的与户主关系有误");
//                        populationImportErrorVOList.add(importErrorVO);
//                        index++;
//                        continue;
                    }else{
                        vo.setRelation(Integer.valueOf(relation));
                    }
 
                }
                if(StringUtils.isEmpty(oneData.get(6))){
                    ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                    importErrorVO.setErrorPosition("第" + index + "行,第7列");
                    importErrorVO.setErrorMsg("街路巷不可为空");
                    populationImportErrorVOList.add(importErrorVO);
                    index++;
                    continue;
                }
                vo.setRoad(oneData.get(6));
                if(StringUtils.isEmpty(oneData.get(7))){
                    ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                    importErrorVO.setErrorPosition("第" + index + "行,第8列");
                    importErrorVO.setErrorMsg("小区号不可为空");
                    populationImportErrorVOList.add(importErrorVO);
                    index++;
                    continue;
                }
                vo.setDoorNo(oneData.get(7).trim());
                if(StringUtils.isEmpty(oneData.get(8))){
                    ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                    importErrorVO.setErrorPosition("第" + index + "行,第9列");
                    importErrorVO.setErrorMsg("楼排号不可为空");
                    populationImportErrorVOList.add(importErrorVO);
                    index++;
                    continue;
                }
                vo.setFloor(oneData.get(8).trim());
                if(StringUtils.isEmpty(oneData.get(9))){
                    ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                    importErrorVO.setErrorPosition("第" + index + "行,第10列");
                    importErrorVO.setErrorMsg("单元号不可为空");
                    populationImportErrorVOList.add(importErrorVO);
                    index++;
                    continue;
                }
                vo.setUnitNo(oneData.get(9).trim());
                if(StringUtils.isEmpty(oneData.get(10))){
                    ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                    importErrorVO.setErrorPosition("第" + index + "行,第11列");
                    importErrorVO.setErrorMsg("户室不可为空");
                    populationImportErrorVOList.add(importErrorVO);
                    index++;
                    continue;
                }
                vo.setHouseNo(oneData.get(10).trim());
                if(StringUtils.isNotEmpty(oneData.get(11))){
                    vo.setBuildPurpose(oneData.get(11).trim());
                }
                if(StringUtils.isNotEmpty(oneData.get(12))){
                    vo.setBuildArea(oneData.get(12).trim());
                }
                if(StringUtils.isNotEmpty(oneData.get(13))){
                    Integer isOk = PopulHouseStatusEnum.getCodeByName(oneData.get(13).trim());
                    if(isOk.equals(-1)){
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorPosition("第" + index + "行,第14列");
                        importErrorVO.setErrorMsg("您填写的房屋状态有误");
                        populationImportErrorVOList.add(importErrorVO);
                        index++;
                        continue;
                    }
                    vo.setHouseStatus(isOk);
                }
                if(StringUtils.isNotEmpty(oneData.get(14))){
                    Integer isOk = PopulHousePurposeEnum.getCodeByName(oneData.get(14).trim());
                    if(isOk.equals(-1)){
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorPosition("第" + index + "行,第15列");
                        importErrorVO.setErrorMsg("您填写的房屋用途有误");
                        populationImportErrorVOList.add(importErrorVO);
                        index++;
                        continue;
                    }
                    vo.setHousePurpose(PopulHousePurposeEnum.getCodeByName(oneData.get(14).trim()));
                }
                if(StringUtils.isNotEmpty(oneData.get(15))){
                    Integer isOk = PopulHouseControlStatusEnum.getCodeByName(oneData.get(15));
                    if(isOk.equals(-1)){
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorPosition("第" + index + "行,第16列");
                        importErrorVO.setErrorMsg("您填写的管控状态有误");
                        populationImportErrorVOList.add(importErrorVO);
                        index++;
                        continue;
                    }
                    vo.setHouseStatus(isOk);
                }
 
                if(StringUtils.isNotEmpty(oneData.get(16))){
                    vo.setPhone(oneData.get(16).trim());
                }
                if(StringUtils.isNotEmpty(oneData.get(17))){
                    vo.setNativePlace(oneData.get(17).trim());
                }
                if(StringUtils.isNotEmpty(oneData.get(18))){
                    vo.setCultureLevel(PopulCultureLevelEnum.getCodeByName(oneData.get(18)));
                }
                if(StringUtils.isNotEmpty(oneData.get(19))){
                    String ma = oneData.get(19);
 
                    if(Objects.equals("已",ma)){
                        ma = "已婚";
                    }else if(Objects.equals("未",ma)){
                        ma = "未婚";
                    }else if(Objects.equals("初",ma)){
                        ma = "初婚";
                    }else if(Objects.equals("再",ma)){
                        ma = "再婚";
                    }else if(Objects.equals("复",ma)){
                        ma = "复婚";
                    }else if(Objects.equals("丧",ma)){
                        ma = "丧偶";
                    }else if(Objects.equals("离",ma)){
                        ma = "离婚";
                    }else if(Objects.equals("分",ma)){
                        ma = "分居";
                    }
                    Integer isOk = PopulMarriageEnum.getCodeByName(ma);
                    if(isOk.equals(-1)){
                        vo.setMarriageStr(ma);
//                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
//                        importErrorVO.setErrorPosition("第" + index + "行,第20列");
//                        importErrorVO.setErrorMsg("您填写的婚姻状况有误");
//                        populationImportErrorVOList.add(importErrorVO);
//                        index++;
//                        continue;
                    }else{
                        vo.setMarriage(isOk);
                    }
 
                }
                if(StringUtils.isNotEmpty(oneData.get(20))){
                    vo.setHealthy(oneData.get(20).trim());
                }
                if(StringUtils.isNotEmpty(oneData.get(21))){
                    vo.setWorkCompany(oneData.get(21).trim());
                }
                if(StringUtils.isNotEmpty(oneData.get(22))){
                    Integer isOk = PopulOutOrLocalEnum.getCodeByName(oneData.get(22));
                    if(isOk.equals(-1)){
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorPosition("第" + index + "行,第23列");
                        importErrorVO.setErrorMsg("您填写的外地or本地有误");
                        populationImportErrorVOList.add(importErrorVO);
                        index++;
                        continue;
                    }
                    vo.setOutOrLocal(isOk);
                }
                if(StringUtils.isNotEmpty(oneData.get(23))){
                    vo.setCensusRegister(oneData.get(23).trim());
                }
                if(StringUtils.isNotEmpty(oneData.get(24))){
                    vo.setRemark(oneData.get(24).trim());
                }
 
                if(StringUtils.isNotEmpty(oneData.get(25))){
                    vo.setIdCardPositive(oneData.get(25).trim());
                }
                if(StringUtils.isNotEmpty(oneData.get(26))){
                    vo.setIdCardBack(oneData.get(26).trim());
                }
                if(StringUtils.isNotEmpty(oneData.get(27))){
                    vo.setHouseHold(oneData.get(27).trim());
                }
                for (int i = 28; i < headSize; i++) {
                    if (oneData.get(i) != null && oneData.get(i).equals("是")) {
                        vo.getUserTagStr().add(headData.get(i).substring(0,headData.get(i).indexOf("(")));
                    }
                }
 
                //将重复的数据进行MD5加密实现去重
                String distinct = vo.getName() + vo.getCardNo() + vo.getRoad() + vo.getDoorNo() + vo.getFloor() + vo.getUnitNo() + vo.getHouseNo();
                try {
                    String distinctPass = PayUtil.MD5(distinct);
                    if(StringUtils.isNotEmpty(distinctPass)){
                        vo.setDistinctPass(distinctPass);
                    }
                }catch (Exception e){
                    log.error("组装MD5加密字段失败,数据表格行数:" + index);
                    continue;
                }
                voList.add(vo);
                index++;
            }
            //客户需要暂时注释,等客户处理完成需要恢复
//            if(populationImportErrorVOList.isEmpty()){
//                R r = communityService.listSavePopulationServeExcelVO(voList, communityId);
//                if (!R.isOk(r)) {
//                    throw new ServiceException(r.getMsg());
//                }
//            }else{
//                throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
//            }
            //根据list中的IdCard城市来去重
            List<ComMngPopulationServeExcelVO> newVoList = voList.stream().filter(ListUtils.distinctByKey(ComMngPopulationServeExcelVO::getDistinctPass)).collect(Collectors.toList());
            R r = communityService.listSavePopulationServeExcelVO(newVoList, communityId);
            if (!R.isOk(r)) {
                String errMsg = r.getMsg();
                List<ComMngPopulationImportErrorVO> errorList = JSON.parseArray(errMsg,ComMngPopulationImportErrorVO.class);
                if(!errorList.isEmpty()){
                    populationImportErrorVOList.addAll(errorList);
                }
                throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
            }else{
                if(!populationImportErrorVOList.isEmpty()){
                    throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
                }
            }
        } catch (NumberFormatException e) {
            List<ComMngPopulationImportErrorVO> populationImportErrorVOList = new ArrayList<>();
            ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
            importErrorVO.setErrorPosition("第" + index + "行");
            importErrorVO.setErrorMsg("数据格式有误,请检查文档内数据");
            populationImportErrorVOList.add(importErrorVO);
            throw new ServiceException("500", JSON.toJSONString(populationImportErrorVOList));
        }
    }
}