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
package com.panzhihua.common.listen;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
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.volunteer.ComMngVolunteerErrorExcelVO;
import com.panzhihua.common.model.vos.community.volunteer.ComMngVolunteerExcelVO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.utlis.ListUtils;
import com.panzhihua.common.utlis.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * title: 志愿者导入监听
 * @author : lyq
 */
@Slf4j
public class ComMngVolunteerExcelListen extends AnalysisEventListener<Map<Integer, String>> {
    /**
     * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 3000;
    private static int headSize = 0;
    List<Map<Integer, String>> list = new ArrayList<Map<Integer, String>>();
    private CommunityService communityService;
    private Long communityId;
    private Long userId;
    private Map<Integer, String> headData;
    private StringRedisTemplate stringRedisTemplate;
 
    public ComMngVolunteerExcelListen(CommunityService communityService, Long communityId, Long userId,
                                      StringRedisTemplate stringRedisTemplate) {
        this.communityService = communityService;
        this.communityId = communityId;
        this.userId = userId;
        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() {
        log.info("表格总数据:" + list.size());
        if (list.size() == 0) {
            throw new ServiceException("000", "导入数据为空!");
        }
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        String key = Constants.COM_MNG_VOLUNTEER_ERROR_LIST + communityId;
        int index = 2;
        try {
            ArrayList<ComMngVolunteerExcelVO> voList = Lists.newArrayList();
            ArrayList<ComMngVolunteerErrorExcelVO> mistakes = Lists.newArrayList();
            for (Map<Integer, String> oneData : list) {
                ComMngVolunteerExcelVO vo = new ComMngVolunteerExcelVO();
 
                if (StringUtils.isEmpty(oneData.get(0))) {
                    index++;
                    mistakes.add(setErrorObject(oneData,"志愿者名称不可为空,请填写志愿者名称"));
                    continue;
                }else{
                    //判断导入的名字的长度是否超过10
                    if(oneData.get(0).length() > 10){
                        index++;
                        mistakes.add(setErrorObject(oneData,"志愿者名称长度不可超过10,请重新填写志愿者名称"));
                        continue;
                    }
                }
                vo.setName(oneData.get(0));
                if (StringUtils.isEmpty(oneData.get(1))) {
                    index++;
                    mistakes.add(setErrorObject(oneData,"志愿者手机号不可为空,请填写志愿者手机号"));
                    continue;
                }
                vo.setPhone(oneData.get(1));
                if (StringUtils.isEmpty(oneData.get(2))) {
                    index++;
                    mistakes.add(setErrorObject(oneData,"志愿者身份证不可为空,请填写志愿者身份证"));
                    continue;
                }
                // 判断身份证号码位数
                if (oneData.get(2).length() != 18) {
                    if (oneData.get(2).length() != 15) {
                        index++;
                        mistakes.add(setErrorObject(oneData,"志愿者身份证位数有误,请检查身份证号码是否正确"));
                        continue;
                    }
                }
                if (oneData.get(2).length() != 15) {
                    if (oneData.get(2).length() != 18) {
                        index++;
                        mistakes.add(setErrorObject(oneData,"志愿者身份证位数有误,请检查身份证号码是否正确"));
                        continue;
                    }
                }
                vo.setIdCard(oneData.get(2).toUpperCase());
                if (StringUtils.isNotEmpty(oneData.get(3))) {
                    vo.setOrgName(oneData.get(3));
                }
 
                if (StringUtils.isNotEmpty(oneData.get(4))) {
                    vo.setTeamName(oneData.get(4));
                }
                if (StringUtils.isNotEmpty(oneData.get(5))) {
                    vo.setSkillName(oneData.get(5));
                }
                vo.setIsUniversity(2);
                if (StringUtils.isNotEmpty(oneData.get(6))) {
                    vo.setIsUniversity(1);
                    vo.setUniversityName(oneData.get(6));
                }
                voList.add(vo);
                index++;
            }
            List<ComMngVolunteerExcelVO> newVoList =
                voList.stream().filter(ListUtils.distinctByKey(ComMngVolunteerExcelVO::getIdCard))
                    .collect(Collectors.toList());
            R r = communityService.importVolunteerAdmin(newVoList, communityId, userId);
            if (!R.isOk(r)) {
                List<ComMngVolunteerErrorExcelVO> list =
                    JSONArray.parseArray(JSONArray.toJSONString(r.getData()), ComMngVolunteerErrorExcelVO.class);
                mistakes.addAll(list);
                valueOperations.set(key, JSONArray.toJSONString(mistakes), 1, TimeUnit.HOURS);
                throw new ServiceException("500", key);
            } else {
                if (!mistakes.isEmpty()) {
                    valueOperations.set(key, JSONArray.toJSONString(mistakes), 1, TimeUnit.HOURS);
                    throw new ServiceException("500", key);
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            throw new ServiceException("500", "导入失败");
        }
    }
 
    private void setError(Map<Integer, String> map, ComMngVolunteerErrorExcelVO vo) {
        vo.setName(map.get(0));
        vo.setPhone(map.get(1));
        vo.setIdCard(map.get(2));
        vo.setOrgName(map.get(3));
        vo.setTeamName(map.get(4));
        vo.setSkillName(map.get(5));
    }
 
    /**
     * 组装错误信息
     * @param oneData   数据表格对象
     * @param error     错误信息
     * @return  错误对象
     */
    private ComMngVolunteerErrorExcelVO setErrorObject(Map<Integer, String> oneData, String error){
        ComMngVolunteerErrorExcelVO mistake = new ComMngVolunteerErrorExcelVO();
        setError(oneData, mistake);
        mistake.setError(error);
        return mistake;
    }
}