自动化立体仓库 - WMS系统
skyouc
3 天以前 c26f14542b18a9442df8bc75c0d6bb9fbaad2474
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
package com.zy.asrs.utils;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.DocType;
import com.zy.asrs.entity.Mat;
import com.zy.asrs.entity.Order;
import com.zy.asrs.entity.OrderDetl;
import com.zy.asrs.service.DocTypeService;
import com.zy.asrs.service.MatService;
import com.zy.asrs.service.OrderDetlService;
import com.zy.asrs.service.OrderService;
import com.zy.common.entity.OrderExcel;
import lombok.extern.slf4j.Slf4j;
 
import java.util.*;
 
import org.springframework.transaction.annotation.Transactional;
 
@Slf4j
public class OrderExcelListener  extends AnalysisEventListener<OrderExcel> {
 
    private int total = 0;
    private Long userId;
 
    public OrderExcelListener(Long userId) {
        this.userId = userId;
    }
 
    /**
     * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 50;
 
    private final List<OrderExcel> list = new ArrayList<>();
 
    /**
     * 这里会一行行的返回头
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
 
    }
 
    /**
     * 单据导入实现
     * @author Ryan
     * @date 2026/1/8 17:25
     * @param data
     * @param context 
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void invoke(OrderExcel data, AnalysisContext context) {
        OrderService orderService = (OrderService) SpringUtils.getBean(OrderService.class);
        OrderDetlService detlService = (OrderDetlService) SpringUtils.getBean(OrderDetlService.class);
        MatService matService = SpringUtils.getBean(MatService.class);
        DocTypeService docTypeService = SpringUtils.getBean(DocTypeService.class);
        Date now = new Date();
        if (Objects.isNull(data)) {
            throw new CoolException("参数不能为空!!");
        }
        if (Objects.isNull(data.getOrderNo())) {
            throw new CoolException("订单已创建,不可以重复创建!!");
        }
        if (Objects.isNull(data.getDocType())) {
            throw new CoolException("单据类型不能为空!");
        }
        DocType docType = docTypeService.selectOne(new EntityWrapper<DocType>().eq("doc_name", data.getDocType()));
        if (Objects.isNull(docType)) {
            throw new CoolException("单据类型[" + data.getDocType() + "]不存在!!");
        }
 
        Order order = new Order();
        order.setOrderNo(data.getOrderNo());
        order.setDocType(docType.getDocId());
        order.setUuid(UUID.randomUUID().toString());
        order.setCreateTime(now);
        order.setUpdateTime(now);
        order.setSettle(1L);
        order.setStatus(1);
        order.setCreateBy(userId);
        order.setUpdateBy(userId);
        order.setPakinPakoutStatus(docType.getPakin() == 1 ? 1 : 2);    
        if (!orderService.insert(order)) {
            throw new CoolException("订单创建失败!!");
        }
        if (Objects.isNull(data.getMatnr())) {
            throw new CoolException("物料编码不能为空!");
        }
        Mat mat = matService.selectOne(new EntityWrapper<Mat>().eq("matnr", data.getMatnr()));
        if (Objects.isNull(mat)) {
            throw new CoolException("物料[" + data.getMatnr() + "]不存在!!");
        }
        OrderDetl detl = new OrderDetl();
        detl.setOrderNo(data.getOrderNo());
        detl.setOrderId(order.getId());
        detl.setMatnr(data.getMatnr());
        detl.setMaktx(mat.getMaktx());
        detl.setQty(data.getQty());
        detl.setBatch(data.getBatch());
        detl.setVolume(data.getVolume());
        detl.setSafeQty(mat.getSafeQty());
        detl.setAnfme(data.getAnfme());
        detl.setBarcode(data.getBarcode());
        detl.setUpdateTime(now);
        detl.setCreateTime(now);
        detl.setUpdateBy(userId);
        detl.setCreateBy(userId);
        if (!detlService.insert(detl)) {
            throw new CoolException("订单详情创建失败!!");
        }
        total ++ ;
    }
 
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("新增{}条物料信息!", total);
    }
 
    public int getTotal() {
        return total;
    }
 
}