huanghongfa
2021-03-31 376e1f1c688c71a31ee321805e443af8e92c8d5e
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
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 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();
                vo.setName(oneData.get(0));
                vo.setSex(PopulSexEnum.getCodeByName(oneData.get(1)));
                if(oneData.get(2) == null){
                    throw new ServiceException("500", "年龄不可为空:第" + index + "行,第3列");
                }
                vo.setAge(Integer.valueOf(oneData.get(2)));
                if(oneData.get(3) == null){
                    throw new ServiceException("500", "是否租住不可为空:第" + index + "行,第4列");
                }
                vo.setIsRent(PopulIsOkEnum.getCodeByName(oneData.get(3)));
                vo.setRoad(oneData.get(4));
                if(oneData.get(5) == null){
                    throw new ServiceException("500", "门牌号不可为空:第" + index + "行,第6列");
                }
                vo.setDoorNo(Integer.valueOf(oneData.get(5)));
                vo.setFloor(oneData.get(6));
                if(oneData.get(7) == null){
                    throw new ServiceException("500", "单元号不可为空:第" + index + "行,第8列");
                }
                vo.setUnitNo(Integer.valueOf(oneData.get(7)));
                if(oneData.get(8) == null){
                    throw new ServiceException("500", "户室不可为空:第" + index + "行,第9列");
                }
                vo.setHouseNo(Integer.valueOf(oneData.get(8)));
                vo.setNation(oneData.get(9));
                if(oneData.get(10) == null){
                    throw new ServiceException("500", "政治面貌不可为空:第" + index + "行,第11列");
                }
                vo.setPoliticalOutlook(PopulPoliticalOutlookEnum.getCodeByName(oneData.get(10)));
                vo.setCardNo(oneData.get(11));
                vo.setPhone(oneData.get(12));
                vo.setNativePlace(oneData.get(13));
                vo.setWorkCompany(oneData.get(14));
                for (int i = 15; 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());
        }
    }
}