huanghongfa
2021-09-02 177249c76aeea0b4bf8d8816d4994e3b445b45ce
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
package com.panzhihua.common.listen;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
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.exceptions.ServiceException;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.ComMngBuildingExcelVO;
import com.panzhihua.common.model.vos.community.ComMngPopulationImportErrorVO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.service.grid.GridService;
import com.panzhihua.common.utlis.Snowflake;
import com.panzhihua.common.utlis.StringUtils;
 
import lombok.extern.slf4j.Slf4j;
 
/**
 * @description: 社区楼栋导入监听
 * @author: xyh
 */
@Slf4j
public class ComMngBuildingExcelListen extends AnalysisEventListener<Map<Integer, String>> {
    private static final int BATCH_COUNT = 5000;
    private static int headSize = 0;
    List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();
    private CommunityService communityService;
    private GridService gridService;
    private Long communityId;
    private Map<Integer, String> headData;
    public ComMngBuildingExcelListen(CommunityService communityService, Long communityId) {
        this.communityService = communityService;
        this.communityId = communityId;
    }
 
    @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() {
        try {
            int index = 2;
            ArrayList<ComMngBuildingExcelVO> voList = Lists.newArrayList();
            List<ComMngPopulationImportErrorVO> populationImportErrorVOList = new ArrayList<>();
            for (int i = 1; i < list.size(); i++) {
                Map<Integer, String> oneData = list.get(i);
                ComMngBuildingExcelVO vo = new ComMngBuildingExcelVO();
                vo.setId(Snowflake.getId());
                vo.setUseType(oneData.get(1));
                vo.setBuildType(oneData.get(2));
                if (StringUtils.isEmpty(oneData.get(oneData.get(3)))) {
                    ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                    importErrorVO.setErrorMsg("街路巷不能为空");
                    importErrorVO.setErrorPosition("第" + index + "行");
                    populationImportErrorVOList.add(importErrorVO);
                    vo.setAlley(oneData.get(3));
                }
 
                vo.setVillageName(oneData.get(4));
                vo.setDoorNo(oneData.get(5));
                vo.setName(oneData.get(6));
                try {
                    vo.setUnitTotal(oneData.get(7) == null ? 0 : Integer.valueOf(oneData.get(7)));
                } catch (Exception e) {
                    if (StringUtils.isEmpty(oneData.get(oneData.get(5)))) {
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorMsg("门牌号不能为空");
                        importErrorVO.setErrorPosition("第" + index + "行");
                        populationImportErrorVOList.add(importErrorVO);
                        vo.setDoorNo(oneData.get(5));
                    }
 
                    if (StringUtils.isEmpty(oneData.get(oneData.get(5)))) {
                        ComMngPopulationImportErrorVO importErrorVO = new ComMngPopulationImportErrorVO();
                        importErrorVO.setErrorMsg("楼栋号不能为空");
                        importErrorVO.setErrorPosition("第" + index + "行");
                        populationImportErrorVOList.add(importErrorVO);
                        vo.setName(oneData.get(6));
                    }
 
                    try {
                        vo.setUnitTotal(oneData.get(7) == null ? 0 : Integer.valueOf(oneData.get(7)));
                    } catch (Exception e1) {
                        vo.setUnitTotal(0);
                    }
                    try {
                        vo.setBuildFloorSum(oneData.get(8) == null ? 0 : Integer.valueOf(oneData.get(8)));
                    } catch (Exception e1) {
                        vo.setBuildFloorSum(0);
                    }
 
                    try {
                        vo.setEveryFloorSum(oneData.get(9) == null ? 0 : Integer.valueOf(oneData.get(9)));
                    } catch (Exception e1) {
                        vo.setEveryFloorSum(0);
                    }
 
                    try {
                        vo.setBuildUserSum(oneData.get(10) == null ? 0 : Integer.valueOf(oneData.get(10)));
                    } catch (Exception e1) {
                        vo.setBuildUserSum(0);
                    }
                    try {
                        vo.setElevatorTotal(oneData.get(11) == null ? 0 : Integer.valueOf(oneData.get(11)));
                    } catch (Exception e1) {
                        vo.setElevatorTotal(0);
                    }
 
                    vo.setPropertyCompanyName(oneData.get(12));
                    try {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                        vo.setBuildDate(sdf.parse(oneData.get(13)));
                    } catch (Exception e1) {
 
                    }
                    vo.setRemark(oneData.get(14));
                    vo.setGridId(oneData.get(15));
                    vo.setGridName(oneData.get(15));
                    voList.add(vo);
                    index++;
                }
 
                R r = communityService.listSaveBuildingExcelVO(voList, 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) {
            e.printStackTrace();
        }
    }
}