自动化立体仓库 - WMS系统
skyouc
昨天 3aed7236f2db67dd90115a39bc8f1465e4cc8b73
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package com.zy.api.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.api.entity.OrderParams;
import com.zy.api.entity.PubOrderParams;
import com.zy.api.entity.ReportOrderParam;
import com.zy.api.entity.SyncMatParmas;
import com.zy.api.enums.MatLocType;
import com.zy.api.enums.OrderType;
import com.zy.api.enums.OrderWkType;
import com.zy.api.service.KopenApiService;
import com.zy.asrs.entity.Mat;
import com.zy.asrs.entity.Order;
import com.zy.asrs.entity.OrderDetl;
import com.zy.asrs.enums.CommonEnum;
import com.zy.asrs.enums.OrderSettle;
import com.zy.asrs.service.MatService;
import com.zy.asrs.service.OrderDetlService;
import com.zy.asrs.service.OrderService;
 
import com.zy.asrs.utils.Utils;
import com.zy.common.utils.HttpHandler;
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.Date;
import java.util.Objects;
 
@Service("kopenApiServiceImpl")
public class KopenApiServiceImpl implements KopenApiService {
 
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private MatService matService;
 
    /**
     * 接收下发订单信息
     *
     * @author Ryan
     * @date 2025/11/24 14:49
     * @param params
     * @return com.core.common.R
     */
    @Override
    public R receiveOrders(PubOrderParams params) {
        if (params.getType().equals(OrderWkType.getTypeVal(params.getType()))) {
            return R.error("当前类型不是上架派工单!!");
        }
        addOrUpdateOrders(params, "add");
        return R.ok("单据下发成功!!");
    }
 
    /**
     * 上架派工单反馈
     * 
     * @author Ryan
     * @date 2025/11/24 15:33
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R getInDispatchResult(ReportOrderParam params) {
        if (Objects.isNull(params)) {
            return R.error("参数不能为空!!");
        }
        String response = null;
        try {
            response = new HttpHandler.Builder()
                    .setUri("127.0.0.1:8081")
                    .setPath("/wms/order/getInDispatchResult")
                    .setJson(JSONObject.toJSONString(params))
                    .build()
                    .doPost();
            if (Objects.isNull(response) || response.trim().isEmpty()) {
                return R.error("外网接口无响应!!");
            }
            JSONObject jsonObject = JSONObject.parseObject(response);
            Integer code = jsonObject.getInteger("code");
            if (!Objects.isNull(code) && code.equals(1)) {
                return R.ok("入库单上报完成!!");
            } else {
                String msg = jsonObject.getString("message");
                return R.error(Objects.isNull(msg) ? "上报失败!!" : msg);
            }
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }
 
    /* */
    /**
     * 备货指示派工单下发
     * 
     * @author Ryan
     * @date 2025/11/24 15:21
     * @param params
     * @return com.core.common.R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R sendOutDispatch(PubOrderParams params) {
        if (Objects.isNull(params)) {
            return R.error("参数不能为空!!");
        }
        // 校验参数
        if (Objects.isNull(params.getDispatch_no())) {
            return R.error("派工单编号不能为空!!");
        }
        if (Objects.isNull(params.getKopen_id())) {
            return R.error("流水号不能为空!!");
        }
        if (Objects.isNull(params.getCompany_id())) {
            return R.error("公司ID不能为空!!");
        }
 
        addOrUpdateOrders(params, "add");
 
        return R.ok("备货指示派工单下发成功!!");
    }
 
    /**
     * 备货指示派工单
     * 
     * @author Ryan
     * @date 2025/12/16 9:15
     * @param params
     */
    private void outOrderAddAndUpdate(PubOrderParams params, String type) {
        if (Objects.isNull(params)) {
            throw new CoolException("参数不能为空!!");
        }
        if (Objects.isNull(params.getType())) {
            throw new CoolException("订单类型不能为空!!");
        }
 
        OrderParams orderParams = JSONObject.parseObject(JSONObject.toJSONString(params), OrderParams.class);
        Order order = orderService.selectOne(new EntityWrapper<Order>().eq("order_no", orderParams.getInv_no()));
        if (type.equals("add") && !Objects.isNull(order)) {
            throw new CoolException("单据已存在, 不可重复添加!!");
        }
        // 判断订单是否存在
        if (Objects.isNull(order)) {
            /** 不存在,新增订单 */
            generateOrders(params);
        } else {
            /** 存在,删除老订单,更新插入新订单 */
            // 删除旧订单明细
            if (!orderDetlService.delete(new EntityWrapper<OrderDetl>().eq("order_id", order.getId()))) {
                throw new CoolException("订单明细删除失败!!");
            }
            ;
            if (!orderService.deleteById(order.getId())) {
                throw new CoolException("原单据删除失败!!");
            }
            generateOrders(params);
        }
 
    }
 
    /**
     * 基础零件变更
     *
     * @author Ryan
     * @date 2025/11/24 15:05
     * @param params
     * @return com.core.common.R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized R basMatupdate(SyncMatParmas params) {
        if (Objects.isNull(params.getPro_komcode())) {
            return R.error("零件编码不能为空!!");
        }
        Mat matnr = matService.selectOne(new EntityWrapper<Mat>().eq("matnr", params.getPro_komcode()));
        if (!Objects.isNull(matnr)) {
            // 订单时间
            if (Utils.isValidFormat(params.getUpdate_time(), "yyyy-MM-dd HH:mm:ss")) {
                Date date1 = Utils.getFormateDate(params.getUpdate_time());
                matnr.setUpdateTime(date1);
            }
            matnr.setMaktx(params.getPro_name());
            matnr.setSpecs(params.getPro_size());
            matnr.setWeight(Double.parseDouble(params.getPro_wet()));
            matnr.setSuppCode(params.getPro_id());
            matnr.setTagId(MatLocType.getTag(params.getPro_type()));
            matnr.setManu(params.getCompany_id());
            if (!matService.updateById(matnr)) {
                throw new CoolException("物料更新失败或无需更新!!");
            }
        } else {
            if (Objects.isNull(matnr)) {
                matnr = new Mat();
            }
            // 订单时间
            if (Utils.isValidFormat(params.getUpdate_time(), "yyyy-MM-dd HH:mm:ss")) {
                Date date1 = Utils.getFormateDate(params.getUpdate_time());
                matnr.setUpdateTime(date1);
            }
            matnr.setMaktx(params.getPro_name());
            matnr.setMatnr(params.getPro_komcode());
            matnr.setSpecs(params.getPro_size());
            matnr.setWeight(Double.parseDouble(params.getPro_wet()));
            matnr.setSuppCode(params.getPro_id());
            matnr.setTagId(MatLocType.getTag(params.getPro_type()));
            matnr.setManu(params.getCompany_id());
            if (!matService.insert(matnr)) {
                throw new CoolException("物料更新失败!!");
            }
        }
        return R.ok("保存成功!!");
    }
 
    /**
     * 新增或修改订单信息
     *
     * @author Ryan
     * @date 2025/11/24 15:32
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdateOrders(PubOrderParams params, String type) {
        if (Objects.isNull(params)) {
            throw new CoolException("参数不能为空!!");
        }
        if (Objects.isNull(params.getType())) {
            throw new CoolException("订单类型不能为空!!");
        }
        OrderParams orderParams = JSONObject.parseObject(JSONObject.toJSONString(params), OrderParams.class);
        Order order = new Order();
        if (OrderType.ORDER_IN.type.equals(OrderWkType.getTypeVal(params.getType()))) {
            // 入库
          order = orderService.selectOne(new EntityWrapper<Order>().eq("order_no", orderParams.getInv_no()));
        } else if (OrderType.ORDER_OUT.type.equals(OrderWkType.getTypeVal(params.getType()))) {
            // 出库
           order = orderService.selectOne(new EntityWrapper<Order>().eq("order_no", orderParams.getDispatch_no()));
        }
        if (type.equals("add") && !Objects.isNull(order)) {
            throw new CoolException("单据已存在, 不可重复添加!!");
        }
        // 判断订单是否存在
        if (Objects.isNull(order)) {
            /** 不存在,新增订单 */
            generateOrders(params);
        } else {
            /** 存在,删除老订单,更新插入新订单 */
            // 删除旧订单明细
            if (!orderDetlService.delete(new EntityWrapper<OrderDetl>().eq("order_id", order.getId()))) {
                throw new CoolException("订单明细删除失败!!");
            }
 
            if (!orderService.deleteById(order.getId())) {
                throw new CoolException("原单据删除失败!!");
            }
            generateOrders(params);
        }
    }
 
    public static String generateUUID(OrderParams params) {
        return java.util.UUID.randomUUID().toString();
    }
 
    /**
     * 生成订单信息
     * 
     * @param params
     */
    @Transactional(rollbackFor = Exception.class)
    public void generateOrders(PubOrderParams params) {
        // 将数据当新订单插入
        Order newOrder = new Order();
        if (OrderType.ORDER_IN.type.equals(OrderWkType.getTypeVal(params.getType()))) {
            // 入库
            newOrder.setPakinPakoutStatus(1);
            newOrder.setDocType(Long.parseLong(params.getType()));
            newOrder.setOrderNo(params.getInv_no());
        } else if (OrderType.ORDER_OUT.type.equals(OrderWkType.getTypeVal(params.getType()))) {
            // 出库
            newOrder.setPakinPakoutStatus(2);
            newOrder.setDocType(5L);
            newOrder.setOrderNo(params.getDispatch_no());
        }
        newOrder.setUuid(generateUUID(params));
        // 流水号(唯一)
        newOrder.setDefNumber(params.getKopen_id());
        // 派工单号
        newOrder.setNumber(params.getDispatch_no());
        // 箱号
        newOrder.setItemName(params.getPm_tktid());
        newOrder.setSettle(OrderSettle.ORDER_SETTLE_HOLDING.type);
        newOrder.setStatus(CommonEnum.COMMON_ENUM_Y.type);
        // 订单时间
        if (Utils.isValidFormat(params.getUpdate_time(), "yyyy-MM-dd HH:mm:ss")) {
            newOrder.setOrderTime(params.getUpdate_time());
        }
        // 公司ID
        newOrder.setCstmrName(params.getCompany_id());
        newOrder.setCreateTime(new Date());
        newOrder.setUpdateTime(new Date());
        if (!orderService.insert(newOrder)) {
            throw new RuntimeException("新增订单失败!!");
        }
        if (!Objects.isNull(params.getDetails()) && !params.getDetails().isEmpty()) {
            params.getDetails().forEach(item -> {
                Mat matnr = matService.selectOne(new EntityWrapper<Mat>().eq("matnr", item.getPro_komcode()));
                if (Objects.isNull(matnr)) {
                    throw new CoolException("物料编码不存在!!");
                }
                OrderDetl orderItem = new OrderDetl();
                BeanUtils.copyProperties(matnr, orderItem);
                orderItem.setOrderId(newOrder.getId());
                orderItem.setOrderNo(newOrder.getOrderNo());
 
                if (OrderType.ORDER_IN.type.equals(OrderWkType.getTypeVal(params.getType()))) {
                    // 入库
                    orderItem.setAnfme(Math.round(item.getInv_qty() * 10000) / 10000.0);
                } else if (OrderType.ORDER_OUT.type.equals(OrderWkType.getTypeVal(params.getType()))) {
                    // 出库
                    newOrder.setPakinPakoutStatus(2);
                    orderItem.setAnfme(Math.round(item.getOrder_qty() * 10000) / 10000.0);
                }
                orderItem.setMatnr(matnr.getMatnr());
                orderItem.setMaktx(matnr.getMaktx());
                orderItem.setBrand(matnr.getBrand());
                orderItem.setBatch(1 + "");
                orderItem.setStandby1(item.getPro_id());
                // 关联上加派工单号+零件代码+供应商代码
                orderItem.setThreeCode(item.getTotal_serial());
                // 供应商代码
                orderItem.setSuppCode(item.getPro_id());
                orderItem.setCreateTime(new Date());
                orderItem.setUpdateTime(new Date());
                if (!orderDetlService.insert(orderItem)) {
                    throw new CoolException("订单明细新增失败!!");
                }
            });
        }
    }
 
}