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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package com.panzhihua.common.listen;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.panzhihua.common.constants.Constants;
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.DateUtils;
import com.panzhihua.common.utlis.Snowflake;
import com.panzhihua.common.utlis.StringUtils;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
 
/**
 * @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 StringRedisTemplate stringRedisTemplate;
    private GridService gridService;
    private Long communityId;
    private Map<Integer, String> headData;
    public ComMngBuildingExcelListen(CommunityService communityService, Long communityId, StringRedisTemplate stringRedisTemplate) {
        this.communityService = communityService;
        this.communityId = communityId;
        this.stringRedisTemplate = stringRedisTemplate;
    }
 
    @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<>();
            ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
            String key = Constants.BUILDING_ERROR_LIST + communityId;
            for (int i = 0; i < list.size(); i++) {
                Map<Integer, String> oneData = list.get(i);
                ComMngBuildingExcelVO vo = new ComMngBuildingExcelVO();
                vo.setId(Snowflake.getId());
//                vo.setUseType(oneData.get(0));
//                vo.setBuildType(oneData.get(1));
                if (StringUtils.isEmpty(oneData.get(0))) {
                    ComMngPopulationImportErrorVO importErrorVO = setError(oneData);
                    importErrorVO.setErrorMsg("街路巷不能为空");
                    populationImportErrorVOList.add(importErrorVO);
                    continue;
                }
                vo.setAlley(oneData.get(0));
                vo.setVillageName(oneData.get(1));
                if (StringUtils.isEmpty(oneData.get(2))) {
                    ComMngPopulationImportErrorVO importErrorVO = setError(oneData);
                    importErrorVO.setErrorMsg("街路巷号不能为空");
                    populationImportErrorVOList.add(importErrorVO);
                    continue;
                }
                vo.setDoorNo(oneData.get(2));
 
                if (StringUtils.isEmpty(oneData.get(3))) {
                    ComMngPopulationImportErrorVO importErrorVO = setError(oneData);
                    importErrorVO.setErrorMsg("楼栋号不能为空");
                    populationImportErrorVOList.add(importErrorVO);
                    continue;
                }
                vo.setName(oneData.get(3));
 
//                try {
//                    vo.setUnitTotal(oneData.get(6) == null ? 0 : Integer.valueOf(oneData.get(6)));
//                } catch (Exception e1) {
//                    vo.setUnitTotal(0);
//                }
//                try {
//                    vo.setBuildFloorSum(oneData.get(7) == null ? 0 : Integer.valueOf(oneData.get(7)));
//                } catch (Exception e1) {
//                    vo.setBuildFloorSum(0);
//                }
//
//                try {
//                    vo.setEveryFloorSum(oneData.get(8) == null ? 0 : Integer.valueOf(oneData.get(8)));
//                } catch (Exception e1) {
//                    vo.setEveryFloorSum(0);
//                }
//
//                try {
//                    vo.setBuildUserSum(oneData.get(9) == null ? 0 : Integer.valueOf(oneData.get(9)));
//                } catch (Exception e1) {
//                    vo.setBuildUserSum(0);
//                }
//                try {
//                    vo.setElevatorTotal(oneData.get(10) == null ? 0 : Integer.valueOf(oneData.get(10)));
//                } catch (Exception e1) {
//                    vo.setElevatorTotal(0);
//                }
//
//                vo.setPropertyCompanyName(oneData.get(11));
//                try {
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
//                    vo.setBuildDate(sdf.parse(oneData.get(12)));
//                } catch (Exception e1) {
//                    vo.setBuildDate(null);
//                }
//                vo.setRemark(oneData.get(13));
//                vo.setGridId(oneData.get(14));
//                vo.setGridName(oneData.get(14));
                voList.add(vo);
                index++;
            }
 
            R r = communityService.listSaveBuildingExcelVO(voList, communityId);
            if (!R.isOk(r)) {
                List<ComMngPopulationImportErrorVO> errorList =
                        JSONArray.parseArray(JSONArray.toJSONString(r.getData()), ComMngPopulationImportErrorVO.class);
                if (!errorList.isEmpty()) {
                    populationImportErrorVOList.addAll(errorList);
                }
                log.info("将错误数据存入redis中");
                valueOperations.set(key, JSONArray.toJSONString(populationImportErrorVOList), 1, TimeUnit.HOURS);
                log.info("将错误数据存入redis中成功");
                throw new ServiceException("500", key);
            } else {
                if (!populationImportErrorVOList.isEmpty()) {
                    log.info("将错误数据存入redis中");
                    valueOperations.set(key, JSONArray.toJSONString(populationImportErrorVOList), 1, TimeUnit.HOURS);
                    log.info("将错误数据存入redis中成功");
                    throw new ServiceException("500", key);
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
 
    private ComMngPopulationImportErrorVO setError(Map<Integer, String> oneData){
        ComMngPopulationImportErrorVO result = new ComMngPopulationImportErrorVO();
        if(oneData != null){
//            result.setUseType(oneData.get(0));
//            result.setBuildType(oneData.get(1));
            result.setAlley(oneData.get(0));
            result.setVillageName(oneData.get(1));
            result.setDoorNo(oneData.get(2));
            result.setName(oneData.get(3));
//            result.setUnitTotal(Integer.parseInt(oneData.get(6)));
//            result.setBuildFloorSum(Integer.parseInt(oneData.get(7)));
//            result.setEveryFloorSum(Integer.parseInt(oneData.get(8)));
//            result.setBuildUserSum(Integer.parseInt(oneData.get(9)));
//            result.setElevatorTotal(Integer.parseInt(oneData.get(10)));
//            result.setPropertyCompanyName(oneData.get(11));
//            result.setBuildDate(DateUtils.stringToDate(oneData.get(12),DateUtils.format_yyyy));
//            result.setRemark(oneData.get(13));
//            result.setGridId(oneData.get(14));
        }
        return result;
    }
}