cp
2024-10-24 50acd5996ba9f88bbfd4341855c38b625598a190
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package com.zy.asrs.common.utils;
 
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.common.domain.dto.DetlDto;
import com.zy.asrs.common.domain.entity.OutLocOrderExcel;
import com.zy.asrs.common.domain.param.OrderDomainParam;
import com.zy.asrs.common.wms.entity.DocType;
import com.zy.asrs.common.wms.entity.Mat;
import com.zy.asrs.common.wms.entity.Order;
import com.zy.asrs.common.wms.entity.OrderDetl;
import com.zy.asrs.common.wms.service.DocTypeService;
import com.zy.asrs.common.wms.service.MatService;
import com.zy.asrs.common.wms.service.OrderDetlService;
import com.zy.asrs.common.wms.service.OrderService;
import com.zy.asrs.framework.common.*;
import com.zy.asrs.framework.exception.CoolException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestBody;
 
import java.util.*;
 
/**
 * Created by vincent on 2019-11-25
 */
@Slf4j
public class OutLocOrderExcelListener extends AnalysisEventListener<OutLocOrderExcel> {
 
    private int total = 0;
    private String orderOneNo = null;
    private String orderSign = null;
    private Long orderSignId = null;
 
    private int totalAnfme = 0;
    private Long userId;
    private Long hostId;
    List<String[]> OutLocOrderCs = new ArrayList<>();
    List<List<String[]>> OutLocOrderList = new ArrayList<>();
 
    public OutLocOrderExcelListener() {
    }
 
    public OutLocOrderExcelListener(Long userId, Long hostId) {
        this.userId = userId;
        this.hostId = hostId;
    }
 
    /**
     * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 50;
 
    private final List<OutLocOrderExcel> list = new ArrayList<>();
 
    /**
     * 这里会一行行的返回头
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    }
 
    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(OutLocOrderExcel excel, AnalysisContext ctx) {
        if (Cools.isEmpty(orderOneNo)){
            if (!Cools.isEmpty(excel.getOrder())){
                orderOneNo = excel.getOrder();
            } else {
                throw new CoolException("订单号参数为空");
            }
        }
        if (Cools.isEmpty(orderSign)){
            if (!Cools.isEmpty(excel.getSign())){
                orderSign = excel.getSign();
                DocTypeService docTypeService = SpringUtils.getBean(DocTypeService.class);
                DocType docType = docTypeService.getOne(new LambdaQueryWrapper<DocType>().eq(DocType::getDocName, orderSign));
                if (Cools.isEmpty(docType)){
                    throw new CoolException("不存在的单据类型:"+orderSign);
                }
                orderSignId = docType.getDocId();
            } else {
                throw new CoolException("入出库标记参数为空");
            }
        }
        String matnr = "";
        try{
            matnr =  excel.getMatnr();
            if (Cools.isEmpty(matnr)){
                throw new CoolException("品号参数为空");
            }
        } catch (Exception e){
            throw new CoolException("品号参数为空");
        }
        MatService matService = SpringUtils.getBean(MatService.class);
        Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, matnr));
        if (Cools.isEmpty(mat)){
            throw new CoolException("品号:"+matnr+"不存在,请先新增商品档案!!!");
        }
        String anfme = "0";
        try{
            String anfme1 = excel.getAnfme();
            try{
                Integer anfmeInt = Integer.parseInt(anfme1);
                if (!Cools.isEmpty(anfmeInt) && anfmeInt>0){
                    anfme = anfmeInt.toString();
                } else {
                    throw new CoolException("数量参数为空");
                }
            } catch (Exception e){
                throw new CoolException("数量参数为空");
            }
        } catch (Exception e){
            throw new CoolException("数量参数为空");
 
        }
 
        String[] s=new String[2];
        s[0] = matnr;
        s[1] = anfme;
        OutLocOrderCs.add(s);
        total++;
//        totalAnfme = totalAnfme+Integer.parseInt(anfme);
    }
 
    /**
     * 所有数据解析完成了调用
     * 适合事务
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext ctx) {
        if (!Cools.isEmpty(OutLocOrderCs) && OutLocOrderCs.size() != 0) {
            ArrayList<String> matnrSign = new ArrayList<>();
            for (String[] outLocOrderCs : OutLocOrderCs){
                if (!matnrSign.contains(outLocOrderCs[0])){
                    matnrSign.add(outLocOrderCs[0]);
                } else {
                    throw new CoolException("品号参数存在重复行:"+outLocOrderCs[0]);
                }
            }
            MatService matService = SpringUtils.getBean(MatService.class);
 
            OrderDomainParam orderDomainParam = new OrderDomainParam();
            orderDomainParam.setDocType(orderSignId);
            orderDomainParam.setOrderNo(orderOneNo);
            List<OrderDetl> orderDetlList = new ArrayList<>();
            for (String[] outLocOrderCs : OutLocOrderCs){
                OrderDetl orderDetl = new OrderDetl();
                orderDetl.setOrderNo(orderOneNo);
                orderDetl.setAnfme(Double.parseDouble(outLocOrderCs[1]));
 
                Mat mat = matService.getOne(new LambdaQueryWrapper<Mat>().eq(Mat::getMatnr, outLocOrderCs[0]));
 
                orderDetl.setMatnr(mat.getMatnr());
                orderDetl.setMaktx(mat.getMaktx());
                orderDetl.setBatch(mat.getBatch());
                orderDetl.setBarcode(mat.getBarcode());
                orderDetl.setSpecs(mat.getSpecs());
                orderDetl.setModel(mat.getModel());
                orderDetl.setStatus(1);
                orderDetl.setMemo(mat.getMemo());
                orderDetl.setHostId(hostId);
                orderDetlList.add(orderDetl);
            }
            orderDomainParam.setOrderDetlList(orderDetlList);
            boolean formAddOrder = formAddOrder(orderDomainParam);
            if (!formAddOrder){
                throw new CoolException("生成订单失败");
            }
 
        }
 
        log.info("成功导入{}条订单明细!", total);
    }
 
    public int[] getTotal() {
        return new int[]{total};
    }
 
    public boolean formAddOrder(OrderDomainParam param){
        OrderService orderService = SpringUtils.getBean(OrderService.class);
        JdbcTemplate jdbcTemplate = SpringUtils.getBean(JdbcTemplate.class);
        SnowflakeIdWorker snowflakeIdWorker = SpringUtils.getBean(SnowflakeIdWorker.class);
        OrderDetlService orderDetlService = SpringUtils.getBean(OrderDetlService.class);
 
        Order order = orderService.selectByNo(param.getOrderNo(), hostId);
        if (order != null) {
            throw new CoolException("单据编号已存在");
        }
        // 判断重复订单号
        Integer orderCount = Optional.ofNullable(jdbcTemplate.queryForObject("select count(1) from man_order where order_no = '" + param.getOrderNo() + "'", Integer.class)).orElse(0);
        Integer orderLogCount = Optional.ofNullable(jdbcTemplate.queryForObject("select count(1) from man_order_log where order_no = '" + param.getOrderNo() + "'", Integer.class)).orElse(0);
        if (orderCount > 0 || orderLogCount > 0) {
            throw new CoolException("单据编号已存在");
        }
        Date now = new Date();
        order = new Order(
                String.valueOf(snowflakeIdWorker.nextId()),    // 编号[非空]
                param.getOrderNo(),    // 订单编号
                DateUtils.convert(now),    // 单据日期
                param.getDocType(),    // 单据类型
                null,    // 项目编号
                null,    //
                null,    // 调拨项目编号
                null,    // 初始票据号
                null,    // 票据号
                null,    // 客户编号
                null,    // 客户
                null,    // 联系方式
                null,    // 操作人员
                null,    // 合计金额
                null,    // 优惠率
                null,    // 优惠金额
                null,    // 销售或采购费用合计
                null,    // 实付金额
                null,    // 付款类型
                null,    // 业务员
                null,    // 结算天数
                null,    // 邮费支付类型
                null,    // 邮费
                null,    // 付款时间
                null,    // 发货时间
                null,    // 物流名称
                null,    // 物流单号
                1L,    // 订单状态
                1,    // 状态
                userId,    // 添加人员
                now,    // 添加时间
                userId,    // 修改人员
                now,    // 修改时间
                null,    // 备注
                hostId
        );
        if (!orderService.save(order)) {
            throw new CoolException("保存订单主档失败");
        }
        List<DetlDto> list = new ArrayList<>();
        for (OrderDetl orderDetl : param.getOrderDetlList()) {
            DetlDto dto = new DetlDto(orderDetl.getMatnr(), orderDetl.getBatch());
            if (DetlDto.has(list, dto)) {
                OrderDetl item = orderDetlService.selectItem(order.getId(), orderDetl.getMatnr(), orderDetl.getBatch(), hostId);
                item.setAnfme(item.getAnfme() + orderDetl.getAnfme());
                if (!orderDetlService.updateById(item)) {
                    throw new CoolException("保存订单明细档失败");
                }
            } else {
                list.add(dto);
                orderDetl.setOrderId(order.getId());
                orderDetl.setOrderNo(order.getOrderNo());
                orderDetl.setCreateBy(userId);
                orderDetl.setCreateTime(now);
                orderDetl.setUpdateBy(userId);
                orderDetl.setUpdateTime(now);
                orderDetl.setStatus(1);
                orderDetl.setQty(0.0D);
                orderDetl.setWorkQty(0.0D);
                if (!orderDetlService.save(orderDetl)) {
                    throw new CoolException("保存订单明细档失败");
                }
            }
        }
        return true;
    }
}