mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
package com.panzhihua.common.listen;
 
import java.util.*;
import java.util.stream.Collectors;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.panzhihua.common.enums.EldersAuthLevelEnum;
import com.panzhihua.common.exceptions.ServiceException;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.EldersAuthElderlyExcelVO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.common.utlis.ListUtils;
import com.panzhihua.common.utlis.StringUtils;
 
import cn.hutool.core.util.IdcardUtil;
import lombok.extern.slf4j.Slf4j;
 
/**
 * @description: 高龄老人导入监听
 * @author: xyh
 * @date: 2021/3/11 13:26
 */
@Slf4j
public class ComEldersAuthElderlyExcelListen extends AnalysisEventListener<EldersAuthElderlyExcelVO> {
 
    private static final int BATCH_COUNT = 3000;
    private CommunityService communityService;
    private Long communityId;
    private Long createBy;
    private List<EldersAuthElderlyExcelVO> list = new ArrayList<>();
 
    public ComEldersAuthElderlyExcelListen(CommunityService communityService, Long communityId, Long createBy) {
        this.communityService = communityService;
        this.communityId = communityId;
        this.createBy = createBy;
    }
 
    /**
     * @author cedoo
     * @Description:找出list中的重复数据
     * @param datas
     * @return
     */
    public static <T> List<T> findRepeat(Collection<T> datas) {
        if (datas instanceof Set) {
            return new ArrayList<>();
        }
        HashSet<T> set = new HashSet<T>();
        List<T> repeatEles = new ArrayList<T>();
        for (T t : datas) {
            if (set.contains(t)) {
                if (!repeatEles.contains(t)) {
                    repeatEles.add(t);
                }
            } else {
                set.add(t);
            }
        }
        return repeatEles;
    }
 
    @Override
    public void invoke(EldersAuthElderlyExcelVO eldersAuthElderlyExcelVO, AnalysisContext analysisContext) {
        if (StringUtils.isEmpty(eldersAuthElderlyExcelVO.getName())) {
            throw new ServiceException("姓名不可为空");
        }
        if (StringUtils.isEmpty(eldersAuthElderlyExcelVO.getIsExist())) {
            eldersAuthElderlyExcelVO.setIsExist("是");
        }
        if (StringUtils.isEmpty(eldersAuthElderlyExcelVO.getIsBigAge())) {
            eldersAuthElderlyExcelVO.setIsBigAge("是");
        }
        if (StringUtils.isEmpty(eldersAuthElderlyExcelVO.getIdCard())) {
            throw new ServiceException("身份证号错误");
        }
 
        String idCard = eldersAuthElderlyExcelVO.getIdCard();
        if (idCard != null && idCard.contains("x")) {
            idCard = idCard.replaceAll("x", "X");
        }
        eldersAuthElderlyExcelVO.setIdCard(idCard);
 
        eldersAuthElderlyExcelVO.setBirthday(DateUtils.getDateStringYMD(IdcardUtil.getBirthDate(idCard)));
        eldersAuthElderlyExcelVO.setLevel(EldersAuthLevelEnum.getCode(IdcardUtil.getAgeByIdCard(idCard)));
 
        list.add(eldersAuthElderlyExcelVO);
        // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
        if (list.size() >= BATCH_COUNT) {
            doAfterAllAnalysed(analysisContext);
            list.clear();
        }
 
    }
 
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 去重
        List<String> idcardList = new ArrayList<>();
        list.forEach(voInList -> {
            idcardList.add(voInList.getIdCard());
        });
        List<String> repeatList = findRepeat(idcardList);
        if (repeatList != null && repeatList.size() > 0) {
            String repeatedId = StringUtils.join(repeatList, ",");
            throw new ServiceException(repeatedId + " 身份证号码存在多条");
        }
        List<EldersAuthElderlyExcelVO> newVoList = list.stream()
            .filter(ListUtils.distinctByKey(EldersAuthElderlyExcelVO::getIdCard)).collect(Collectors.toList());
        R r = this.communityService.listSaveEldersAuthElderlyExcelVO(newVoList, this.communityId, this.createBy);
        if (!R.isOk(r)) {
            throw new ServiceException(r.getMsg());
        }
    }
}