自动化立体仓库 - WMS系统
zyx
2024-02-22 8a9b1bc2a82166cfa563bc32057503a0274c561d
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
package com.zy.asrs.utils;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.core.common.Cools;
import com.core.common.SpringUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.Mat;
import com.zy.asrs.entity.Node;
import com.zy.asrs.entity.Pla;
import com.zy.asrs.entity.param.GlobleParameter;
import com.zy.asrs.service.MatService;
import com.zy.asrs.service.NodeService;
import com.zy.asrs.service.PlaService;
import com.zy.common.entity.MatExcel;
import com.zy.common.entity.PlaExcel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
/**
 * Created by vincent on 2019-11-25
 */
@Slf4j
@Data
public class PlaExcelListener extends AnalysisEventListener<PlaExcel> {
 
    private static final String OWNER_HZ = "海正生物";
    private static final String OWNER_HNE = "海诺尔";
 
    private int total = 0;
    private int index = 1;
    private Long userId;
 
    public PlaExcelListener() {
    }
 
    public PlaExcelListener(Long userId) {
        this.userId = userId;
    }
 
    /**
     * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 50;
 
    private final List<MatExcel> list = new ArrayList<>();
 
    /**
     * 这里会一行行的返回头
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    }
 
    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(PlaExcel excel, AnalysisContext ctx) {
        index ++;
        PlaService plaService = SpringUtils.getBean(PlaService.class);
        NodeService nodeService = SpringUtils.getBean(NodeService.class);
        MatService matService = SpringUtils.getBean(MatService.class);
        Date now = new Date();
 
        // 商品
        if(Cools.isEmpty(excel.getBatch()) || Cools.isEmpty(excel.getPackageNo())){
            return;
        }
        Pla pla = plaService.selectByBatchAndPackageNo(excel.getBatch(),excel.getPackageNo());
        if (pla == null) {
            pla = excel;
            pla.setStep(2);
            pla.setCreateTime(new Date());
            pla.setModifyTime(new Date());
            pla.setStatus(GlobleParameter.PLA_STATUS_00);
            pla.setStockFreeze(1);
            pla.setWeightAnfme(pla.getWeight());
            Mat mat = matService.selectByMaktx(pla.getBrand());
            if(Cools.isEmpty(mat)){
                throw new CoolException("无法找到对应的牌号信息,牌号=" + pla.getBrand());
            }
            pla.setMatnr(mat.getMatnr());
            try {
                pla.setType(getType(mat.getMatnr()));
                //车间、产线、主体
                setPla(pla);
            }catch (Exception e){
                log.info(e.getMessage() + "Pla: " + pla.getBatch());
                throw new CoolException(e.getMessage() + ",批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo());
            }
 
            if(!Cools.isEmpty(pla.getLocNo())){
                Node node = nodeService.selectByUuid(pla.getLocNo());
                if(Cools.isEmpty(node)){
                    throw new CoolException("未能找到相应的库位信息,库位编号=" +pla.getLocNo());
                    //log.info("未能找到相应的库位信息,库位编号={}",pla.getLocNo());
                }
                pla.setStash(node.getParentName());
                pla.setLocNo(node.getUuid());
                pla.setStatus(GlobleParameter.PLA_STATUS_1);
                pla.setPakinTime(new Date());
            }
            try {
                plaService.insert(pla);
            }catch (Exception e){
                log.error("保存商品信息失败,批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo() + "原因为:" + e.getMessage());
                throw new CoolException("保存商品信息失败,批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo() + "原因为:" + e.getMessage());
            }
//            if (!plaService.insert(pla)) {
//                throw new CoolException("保存商品信息失败,商品编码:" + excel.getMatnr());
//            }
            total++;
        }else if (pla.getStatus().equals(GlobleParameter.PLA_STATUS_00)){
            excel.setId(pla.getId());
            excel.setCreateTime(pla.getCreateTime());
            excel.setModifyTime(new Date());
            excel.setStep(2);
            excel.setStatus(GlobleParameter.PLA_STATUS_00);
            BeanUtils.copyProperties(excel,pla);
            try {
                plaService.updateById(pla);
            }catch (Exception e){
                log.error("保存商品信息失败,批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo() + "原因为:" + e.getMessage());
                throw new CoolException("保存商品信息失败,批号:" + excel.getBatch() + ", 包号:" + excel.getPackageNo() + "原因为:" + e.getMessage());
            }
            total++;
        }
    }
 
    private String getType(String matnr){
        String type = matnr.substring(0,3);
        if(Cools.eq(type,"301")){
            return "纯树脂";
        }
        if (Cools.eq(type,"302")){
            return "改性树脂";
        }
        if (Cools.eq(type,"303")){
            return "副产物";
        }
        if (Cools.eq(type,"304")){
            return "委托加工产品";
        }
        if (Cools.eq(type,"305")){
            return "丙交酯";
        }
 
        return "";
    }
 
    private void setPla(Pla pla){
        String batch = pla.getBatch();
        String line = batch.substring(8,10);
 
        pla.setLine(line);
        if("纯树脂".equals(pla.getType())){
            if("01".equals(line) || "02".equals(line)){
                pla.setOwner(OWNER_HZ);
                pla.setWorkshop("一车间");
            }else {
                pla.setOwner(OWNER_HNE);
                pla.setWorkshop("三车间");
            }
        }else if ("改性树脂".equals(pla.getType())){
            if ("01".equals(line) || "02".equals(line) || "03".equals(line) || "04".equals(line)){
                pla.setOwner(OWNER_HNE);
                pla.setWorkshop("二车间");
            }
        }
    }
 
    /**
     * 所有数据解析完成了调用
     * 适合事务
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext ctx) {
        log.info("新增{}条物料信息!", total);
    }
 
    public int getTotal() {
        return total;
    }
}