huanghongfa
2021-04-16 65bafcbf1353ddb992144245770adfe9419de36c
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
package com.panzhihua.common.listen;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.panzhihua.common.enums.PopulIsOkEnum;
import com.panzhihua.common.enums.PopulPoliticalOutlookEnum;
import com.panzhihua.common.enums.PopulSexEnum;
import com.panzhihua.common.exceptions.ServiceException;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.ComMngPopulationServeExcelVO;
import com.panzhihua.common.model.vos.community.ComMngVillageServeExcelVO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.utlis.AgeUtils;
import com.panzhihua.common.utlis.IdCardUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * @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 = 100;
    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() {
        int index = 2;
        try {
            ArrayList<ComMngPopulationServeExcelVO> voList = Lists.newArrayList();
            for (Map<Integer, String> oneData : list) {
                ComMngPopulationServeExcelVO vo = new ComMngPopulationServeExcelVO();
                if(oneData.get(0) == null){
                    throw new ServiceException("500", "名字不可为空:第" + index + "行,第1列");
                }
                vo.setName(oneData.get(0));
                if(oneData.get(1) == null){
                    throw new ServiceException("500", "身份证号不可为空:第" + index + "行,第2列");
                }
                vo.setCardNo(oneData.get(1));
                //根据身份证号码解析年龄以及性别
                //获取用户生日
                String birthday = vo.getCardNo().substring(6, 14);
                //设置用户年龄
                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(oneData.get(2) != null){
                    vo.setNation(oneData.get(2));
                }
                if(oneData.get(3) != null){
                    vo.setPoliticalOutlook(PopulPoliticalOutlookEnum.getCodeByName(oneData.get(3)));
                }
                if(oneData.get(4) != null){
                    vo.setIsRent(PopulIsOkEnum.getCodeByName(oneData.get(4)));
                }
                if(oneData.get(5) != null){
                    vo.setRelation(oneData.get(5));
                }
                if(oneData.get(6) != null){
                    vo.setRoad(oneData.get(6));
                }
                if(oneData.get(7) != null){
                    vo.setDoorNo(Integer.valueOf(oneData.get(7)));
                }
                if(oneData.get(8) != null){
                    vo.setFloor(oneData.get(8));
                }
                if(oneData.get(9) != null){
                    vo.setUnitNo(Integer.valueOf(oneData.get(9)));
                }
                if(oneData.get(10) != null){
                    vo.setHouseNo(Integer.valueOf(oneData.get(10)));
                }
                if(oneData.get(11) != null){
                    vo.setPhone(oneData.get(11));
                }
                if(oneData.get(12) != null){
                    vo.setNativePlace(oneData.get(12));
                }
                if(oneData.get(13) != null){
                    vo.setCultureLevel(oneData.get(13));
                }
                if(oneData.get(14) != null){
                    vo.setMarriage(oneData.get(14));
                }
                if(oneData.get(15) != null){
                    vo.setHealthy(oneData.get(15));
                }
                if(oneData.get(16) != null){
                    vo.setWorkCompany(oneData.get(16));
                }
                if(oneData.get(17) != null){
                    vo.setRemark(oneData.get(17));
                }
                for (int i = 18; i < headSize; i++) {
                    if (oneData.get(i) != null && oneData.get(i).equals("是")) {
                        vo.getUserTagStr().add(headData.get(i));
                    }
                }
                voList.add(vo);
                index++;
            }
            R r = communityService.listSavePopulationServeExcelVO(voList, communityId);
            if (!R.isOk(r)) {
                throw new ServiceException(r.getMsg());
            }
        } catch (NumberFormatException e) {
            throw new ServiceException("500", "填写数据格式错误:第" + index + "行" + e.getMessage());
        }
    }
}