huanghongfa
2021-03-31 5ab3e73c4025fe18548fb035f112aa76183dc829
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
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.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(oneData.get(1));
                vo.setAge(Integer.valueOf(oneData.get(2)));
                vo.setIsRent(oneData.get(3));
                vo.setRoad(oneData.get(4));
                vo.setDoorNo(Integer.valueOf(oneData.get(5)));
                vo.setFloor(oneData.get(6));
                vo.setUnitNo(Integer.valueOf(oneData.get(7)));
                vo.setHouseNo(Integer.valueOf(oneData.get(8)));
                vo.setNation(oneData.get(9));
                vo.setPoliticalOutlook(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());
        }
    }
}