manailin
2021-06-12 eefbb8708d62ce3ca9b0f00402d586c78716af77
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
package com.panzhihua.common.listen;
 
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.ListUtils;
import com.panzhihua.common.utlis.Snowflake;
import lombok.extern.slf4j.Slf4j;
 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @description: 社区楼栋导入监听
 * @author: xyh
 */
@Slf4j
public class ComMngBuildingExcelListen extends AnalysisEventListener<Map<Integer, String>> {
 
    private CommunityService communityService;
 
    private GridService gridService;
 
    private Long communityId;
 
    private static int headSize = 0;
 
    private Map<Integer, String> headData;
 
 
    public ComMngBuildingExcelListen(CommunityService communityService, Long communityId) {
        this.communityService = communityService;
        this.communityId = communityId;
    }
 
    private static final int BATCH_COUNT = 5000;
    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() {
        try {
            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));
                vo.setAlley(oneData.get(3));
                vo.setVillageName(oneData.get(4));
                vo.setDoorNo(oneData.get(5));
                vo.setName(oneData.get(6));
                vo.setGridName(oneData.get(15));
                vo.setGridId(oneData.get(15));
                try {
                    vo.setUnitTotal(oneData.get(7) == null ? 0 : Integer.valueOf(oneData.get(7)));
                } catch (Exception e) {
                    vo.setUnitTotal(0);
                }
                try {
                    vo.setBuildFloorSum(oneData.get(8) == null ? 0 : Integer.valueOf(oneData.get(8)));
                } catch (Exception e) {
                    vo.setBuildFloorSum(0);
                }
 
 
                try {
                    vo.setEveryFloorSum(oneData.get(9) == null ? 0 : Integer.valueOf(oneData.get(9)));
                } catch (Exception e) {
                    vo.setEveryFloorSum(0);
                }
 
                try {
                    vo.setBuildUserSum(oneData.get(10) == null ? 0 : Integer.valueOf(oneData.get(10)));
                } catch (Exception e) {
                    vo.setBuildUserSum(0);
                }
                try {
                    vo.setElevatorTotal(oneData.get(11) == null ? 0 : Integer.valueOf(oneData.get(11)));
                } catch (Exception e) {
                    vo.setElevatorTotal(0);
                }
 
                vo.setPropertyCompanyName(oneData.get(12));
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                    vo.setBuildDate(sdf.parse(oneData.get(13)));
                } catch (Exception e) {
 
                }
                vo.setRemark(oneData.get(14));
                vo.setGridId(oneData.get(15));
                voList.add(vo);
            }
 
            //根据list中的IdCard城市来去重
            List<ComMngBuildingExcelVO> newVoList = voList.stream().filter(ListUtils.distinctByKey(ComMngBuildingExcelVO::getName)).collect(Collectors.toList());
            R r = communityService.listSaveBuildingExcelVO(newVoList, communityId, 123141L);
            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) {
 
        }
    }
 
}