no
DESKTOP-71BH0QO\L、ming
2021-04-20 8e60521ae27dd9bb69206ce62f0f41eb19e41cd6
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
package com.panzhihua.common.listen;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.common.enums.RealAssetsCategoryType;
import com.panzhihua.common.exceptions.ServiceException;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.ComMngRealAssetsExcelVO;
import com.panzhihua.common.model.vos.community.ComMngRealCompanyExcelVO;
import com.panzhihua.common.service.community.CommunityService;
import com.panzhihua.common.utlis.StringUtils;
import lombok.extern.slf4j.Slf4j;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @description: 实有单位导入监听
 * @author: Null
 * @date: 2021/3/11 13:26
 */
@Slf4j
public class ComMngRealAssetsExcelListen extends AnalysisEventListener<ComMngRealAssetsExcelVO> {
 
    private CommunityService communityService;
 
    private Long communityId;
 
    public ComMngRealAssetsExcelListen(CommunityService communityService, Long communityId){
        this.communityService = communityService;
        this.communityId = communityId;
    }
 
 
    private static final int BATCH_COUNT = 100;
    private List<ComMngRealAssetsExcelVO> list = new ArrayList<>();
 
    @Override
    public void invoke(ComMngRealAssetsExcelVO comMngRealCompanyExcelVO, AnalysisContext analysisContext) {
        list.add(comMngRealCompanyExcelVO);
        // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
        if(list.size() >= BATCH_COUNT){
            log.info("excel导入数据【{}】", JSONObject.toJSONString(list));
            R r = this.communityService.listSaveMngRealAssetsExcelVO(list,this.communityId);
            if (!R.isOk(r)) {
                throw new ServiceException(r.getMsg());
            }
            list.clear();           //清空list
        }
 
    }
 
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("excel导入数据【{}】", JSONObject.toJSONString(list));
        R r = this.communityService.listSaveMngRealAssetsExcelVO(list,this.communityId);//确保最后遗留的数据保存在数据库中
        if (!R.isOk(r)) {
            throw new ServiceException(r.getMsg());
        }
    }
}