自动化立体仓库 - WMS系统
pang.jiabao
1 天以前 6cd08fe4de88df0a21851f96bdc4e617c5d92e36
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
package com.zy.asrs.task.handler;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.entity.DocType;
import com.zy.asrs.entity.OrderDetlPakin;
import com.zy.asrs.entity.OrderPakin;
import com.zy.asrs.mapper.OrderDetlPakinMapper;
import com.zy.asrs.mapper.OrderPakinMapper;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.DocTypeService;
import com.zy.asrs.service.OrderDetlPakinService;
import com.zy.asrs.service.OrderPakinService;
import com.zy.asrs.task.AbstractHandler;
import com.zy.asrs.task.core.ReturnT;
import com.zy.common.utils.HttpHandler;
import com.zy.system.timer.LoadingConfigTimer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * Created by vincent on 2020/7/7
 */
@Slf4j
@Service
public class OrderPakinSyncHandler extends AbstractHandler<String> {
 
    @Autowired
    private OrderPakinService orderPakinService;
    @Autowired
    private OrderDetlPakinService orderDetlPakinService;
    @Autowired
    private ApiLogService apiLogService;
 
    @Resource
    private LoadingConfigTimer loadingConfigTimer;
 
 
    @Transactional
    public void startOrderReport(OrderPakin order) {
 
        // 构造请求体
        JSONObject param = new JSONObject();
        param.put("orderNo", order.getOrderNo());
        List<OrderDetlPakin> orderDetls = orderDetlPakinService.selectByOrderId(order.getId());
        JSONArray detl = new JSONArray();
        for (OrderDetlPakin orderDetlPakin : orderDetls) {
            JSONObject object = new JSONObject();
            object.put("matnr", orderDetlPakin.getMatnr());
            object.put("batch", orderDetlPakin.getBatch());
            object.put("anfme", orderDetlPakin.getQty());
            detl.add(object);
        }
        param.put("matList", detl);
 
        String response = "";
        boolean success = false;
        String errorMsg = null;
        String requestJson = param.toJSONString();
        String url = loadingConfigTimer.getErpReportURL() + loadingConfigTimer.getErpInReportPath();
        String nameSpaces = "入库单上报";
        try {
 
            response = new HttpHandler.Builder()
                    .setUri(loadingConfigTimer.getErpReportURL())
                    .setPath(loadingConfigTimer.getErpInReportPath())
                    .setJson(requestJson)
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject.getString("code") != null && jsonObject.getString("code").equals("200")) {
                orderPakinService.updateSettle(order.getId(), 6L, null);
                log.info("入库单据上报成功,单据编号:{}", order.getOrderNo());
                success = true;
            } else {
                errorMsg = response;
                log.error(nameSpaces + "调用外部接口失败,url:{},request:{},response:{}", url, requestJson, response);
            }
        } catch (Exception e) {
            errorMsg = e.getMessage();
            log.error(nameSpaces + "调用外部接口异常,url:{},request:{},response:{}", url, requestJson, response, e);
        } finally {
            try {
                apiLogService.save(
                        nameSpaces,
                        url,
                        null,
                        "127.0.0.1",
                        requestJson,
                        response,
                        success,
                        errorMsg
                );
            } catch (Exception e) {
                log.error(nameSpaces + "保存接口日志失败", e);
            }
        }
    }
 
    @Transactional
    public void reportInStockOrders(List<OrderDetlPakin> orderDetlPakinList) {
        // 按订单分组
        Map<String, List<OrderDetlPakin>> listMap = orderDetlPakinList.stream().collect(Collectors.groupingBy(OrderDetlPakin::getOrderNo));
 
        // 构造请求体
        JSONArray param = new JSONArray();
        for (Map.Entry<String, List<OrderDetlPakin>> entry : listMap.entrySet()) {
            JSONObject object = new JSONObject();
            object.put("orderNo", entry.getKey());
            List<OrderDetlPakin> orderDetlPakins = entry.getValue();
            JSONArray detl = new JSONArray();
            for (OrderDetlPakin orderDetlPakin : orderDetlPakins) {
                JSONObject detlObject = new JSONObject();
                detlObject.put("sku", orderDetlPakin.getSku());
                detlObject.put("standby1", orderDetlPakin.getStandby1());
                detlObject.put("matnr", orderDetlPakin.getMatnr());
                detlObject.put("batch", orderDetlPakin.getBatch());
                detlObject.put("anfme", orderDetlPakin.getQty() - orderDetlPakin.getUnits());
                detl.add(detlObject);
            }
            object.put("matList", detl);
            param.add(object);
        }
 
        String response = "";
        boolean success = false;
        String errorMsg = null;
        String requestJson = param.toJSONString();
        String url = loadingConfigTimer.getErpReportURL() + loadingConfigTimer.getErpInReportPath();
        String nameSpaces = "入库单上报";
        try {
 
            response = new HttpHandler.Builder()
                    .setUri(loadingConfigTimer.getErpReportURL())
                    .setPath(loadingConfigTimer.getErpInReportPath())
                    .setJson(requestJson)
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject.getString("code") != null && jsonObject.getString("code").equals("200")) {
                // 批量更新units 上报数量
                List<OrderDetlPakin> collect = orderDetlPakinList.stream().peek(orderDetlPakin -> orderDetlPakin.setUnits(orderDetlPakin.getQty())).collect(Collectors.toList());
                orderDetlPakinService.updateBatchById(collect);
                success = true;
            } else {
                errorMsg = response;
                log.error(nameSpaces + "调用外部接口失败,url:{},request:{},response:{}", url, requestJson, response);
            }
        } catch (Exception e) {
            errorMsg = e.getMessage();
            log.error(nameSpaces + "调用外部接口异常,url:{},request:{},response:{}", url, requestJson, response, e);
        } finally {
            try {
                apiLogService.save(
                        nameSpaces,
                        url,
                        null,
                        "127.0.0.1",
                        requestJson,
                        response,
                        success,
                        errorMsg
                );
            } catch (Exception e) {
                log.error(nameSpaces + "保存接口日志失败", e);
            }
        }
    }
 
    @Resource
    private OrderDetlPakinMapper orderDetlPakinMapper;
 
    public void completeOrderReport() {
        List<OrderPakin> orderPakins = orderPakinService.selectList(new EntityWrapper<OrderPakin>().where("settle = 4"));
 
        // 使用 removeIf 简化代码
        orderPakins.removeIf(orderPakin -> orderDetlPakinMapper.selectReportComplete(orderPakin.getId()) == 0);
 
        // 对剩余数据进行settle设置
        orderPakins.forEach(orderPakin -> orderPakin.setSettle(6L));
 
        // 批量更新
        if (!orderPakins.isEmpty()) {
            orderPakinService.updateBatchById(orderPakins);
        }
    }
}