自动化立体仓库 - WMS系统
#
luxiaotao1123
2020-11-03 9f32f328bff4112086adceb82c96f9d44fc5ac2a
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
package com.zy.common.utils.excel.matcode;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.MatCode;
import com.zy.asrs.service.MatCodeService;
import com.zy.asrs.utils.VersionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * Created by vincent on 2019-11-25
 */
public class MatCodeExcelListener extends AnalysisEventListener<MatCodeExcel> {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(MatCodeExcelListener.class);
 
    private int total = 0;
    private Long userId;
 
    public MatCodeExcelListener() {
    }
 
    public MatCodeExcelListener(Long userId) {
        this.userId = userId;
    }
 
    /**
     * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 50;
 
    private final List<MatCode> list = new ArrayList<>();
 
    /**
     * 这里会一行行的返回头
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    }
 
    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(MatCodeExcel data, AnalysisContext ctx) {
        MatCodeService matCodeService = SpringUtils.getBean(MatCodeService.class);
        if (matCodeService.selectById(data.getMatNo()) == null) {
            MatCode matCode = new MatCode();
            VersionUtils.setMatCode(matCode, data);
            matCode.setModiTime(new Date());
            matCode.setModiUser(this.userId);
            matCode.setAppeTime(new Date());
            matCode.setAppeUser(this.userId);
            if (!matCodeService.insert(matCode)) {
                throw new CoolException("导入数据异常");
            } else {
                System.out.println("===>> "+total);
            }
            total ++;
        }
        // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
//        if (list.size() >= BATCH_COUNT) {
//
//            // 存储完成清理 list
//            list.clear();
//        }
    }
 
    /**
     * 所有数据解析完成了调用
     * 适合事务
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext ctx) {
        LOGGER.info("新增{}条物料信息!", total);
    }
 
    public int getTotal() {
        return total;
    }
}