自动化立体仓库 - WMS系统
lty
19 小时以前 f08dd93e49e8461f362c8f45f17fe10e0fbdebec
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
package com.zy.asrs.task.kingdee.handler;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.entity.*;
import com.zy.asrs.service.*;
import com.zy.asrs.service.impl.ErpSecretServiceImpl;
import com.zy.asrs.service.impl.OrderDetlPakinServiceImpl;
import com.zy.asrs.service.impl.OrderDetlServiceImpl;
import com.zy.asrs.service.impl.OrderPakinServiceImpl;
import com.zy.asrs.task.AbstractHandler;
import com.zy.asrs.task.core.ReturnT;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
@Slf4j
@Service
public class AutoTransferHandler extends AbstractHandler<String> {
 
    @Autowired
    private OrderService orderService;
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private LoginAuthenticationHandler loginAuthenticationHandler;
    @Autowired
    private ErpSecretServiceImpl erpSecretService;
    @Autowired
    private LocDetlService locDetlService;
    @Autowired
    private MatService matService;
    @Autowired
    private OrderDetlPakinServiceImpl orderDetlPakinService;
    @Autowired
    private OrderDetlServiceImpl orderDetlService;
    @Autowired
    private OrderPakinServiceImpl orderPakinServiceImpl;
 
    @Transactional(rollbackFor = Exception.class)
    public ReturnT<String> start(OrderPakin order) {
        JSONObject logData = new JSONObject();
        String responseMsg = "";
        boolean success = false;
 
        try {
            logData.put("orderNo", order.getOrderNo());
            logData.put("docType", order.getDocType());
            logData.put("settle", order.getSettle());
            logData.put("startTime", System.currentTimeMillis());
 
            // 获取该订单下的所有明细
            List<OrderDetlPakin> orderDetlPakins = orderDetlPakinService.selectList(
                    new EntityWrapper<OrderDetlPakin>().eq("order_no", order.getOrderNo()));
 
            if (orderDetlPakins.isEmpty()) {
                throw new IllegalArgumentException("订单 " + order.getOrderNo() + " 无任何明细,无法转换");
            }
 
            // 遍历每个订单明细进行库存转换(原逻辑保持不变)
            for (OrderDetlPakin orderDetlPakin : orderDetlPakins) {
                double orderQuantity = orderDetlPakin.getAnfme();
                double remainingQuantity = orderQuantity;
                double convertedQuantity = 0.0;
 
                log.info("开始处理订单明细: 订单号={}, 物料号={}, 需要转换数量={}",
                        order.getOrderNo(), orderDetlPakin.getMatnr(), orderQuantity);
 
                List<LocDetl> locDetls = locDetlService.selectList(
                        new EntityWrapper<LocDetl>()
                                .eq("matnr", orderDetlPakin.getMatnr())
                                .like("box_type3", "HDU%")
                                .orderBy("anfme")
                );
 
                if (locDetls.isEmpty()) {
                    throw new IllegalArgumentException("无符合条件的库存: 订单 " + order.getOrderNo() +
                            " 物料 " + orderDetlPakin.getMatnr());
                }
 
                for (LocDetl locDetl : locDetls) {
                    if (remainingQuantity <= 0) break;
                    double stockQty = locDetl.getAnfme();
                    if (stockQty <= 0) continue;
 
                    double consumeQty = Math.min(stockQty, remainingQuantity);
 
                    // 创建分配记录...
                    LocDetl allocated = new LocDetl();
                    BeanUtils.copyProperties(locDetl, allocated);
                    allocated.setAnfme(consumeQty);
                    allocated.setStandby1(orderDetlPakin.getStandby1());
                    allocated.setStandby2(orderDetlPakin.getStandby2());
                    allocated.setStandby3(orderDetlPakin.getStandby3());
                    allocated.setBoxType1(orderDetlPakin.getBoxType1());
                    allocated.setBoxType2(orderDetlPakin.getBoxType2());
                    allocated.setBoxType3(orderDetlPakin.getBoxType3());
                    allocated.setOrderNo(orderDetlPakin.getOrderNo());
 
                    locDetlService.insert(allocated);
 
                    // 扣减原库存...
                    double remainingStock = stockQty - consumeQty;
                    if (remainingStock <= 0) {
                        locDetlService.delete(new EntityWrapper<LocDetl>()
                                .eq("loc_no", locDetl.getLocNo())
                                .eq("matnr", locDetl.getMatnr())
                                .eq("box_type3", locDetl.getBoxType3()));
                    } else {
                        locDetl.setAnfme(remainingStock);
                        locDetlService.update(locDetl, new EntityWrapper<LocDetl>()
                                .eq("loc_no", locDetl.getLocNo())
                                .eq("matnr", locDetl.getMatnr())
                                .eq("box_type3", locDetl.getBoxType3()));
                    }
 
                    convertedQuantity += consumeQty;
                    remainingQuantity -= consumeQty;
                }
 
                if (remainingQuantity > 0) {
                    throw new IllegalArgumentException("库存不足: 订单 " + order.getOrderNo() +
                            " 物料 " + orderDetlPakin.getMatnr() + " 缺少 " + remainingQuantity);
                }
 
                orderDetlPakin.setQty(convertedQuantity);
                orderDetlPakinService.updateById(orderDetlPakin);
            }
 
            // 全部成功
            order.setSettle(67L);
            orderPakinServiceImpl.updateById(order);
 
            success = true;
            responseMsg = "库存转换成功,订单号: " + order.getOrderNo() + ",已设置 settle=67";
            log.info(responseMsg);
 
        } catch (Exception e) {
            success = false;
            responseMsg = "库存转换失败: " + e.getMessage();
            log.error("自动转换失败,订单号: {}, 原因: {}", order.getOrderNo(), e.getMessage(), e);
 
            throw e;  // 让 @Transactional 回滚
        } finally {
            // 无论成功失败,都记录日志
            logData.put("endTime", System.currentTimeMillis());
            logData.put("durationMs", logData.getLongValue("endTime") - logData.getLongValue("startTime"));
            logData.put("success", success);
            logData.put("message", responseMsg);
 
            saveApiLog(logData, responseMsg, success);
        }
 
        return success ? SUCCESS : FAIL;
    }
 
    private void saveApiLog(JSONObject add, String response, boolean success) {
        try {
            apiLogService.save(
                    "货主转换",
                    "AutoTransferHandler",
                    null,
                    "127.0.0.1",
                    add.toJSONString(),
                    response,
                    success
            );
        } catch (Exception e) {
            log.error("接口日志保存失败", e);
        }
    }
}