自动化立体仓库 - WMS系统
#
whycq
2024-07-02 ff98dce5d697563771adf7ae08cae895d234874b
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
package com.zy.asrs.task.handler;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.core.exception.CoolException;
import com.zy.asrs.entity.DocType;
import com.zy.asrs.entity.Order;
import com.zy.asrs.entity.OrderDetl;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.DocTypeService;
import com.zy.asrs.service.OrderDetlService;
import com.zy.asrs.service.OrderService;
import com.zy.asrs.task.AbstractHandler;
import com.zy.asrs.task.core.ReturnT;
import com.zy.common.constant.MesConstant;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * Created by vincent on 2020/7/7
 */
@Slf4j
@Service
public class OrderSyncHandler extends AbstractHandler<String> {
 
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private DocTypeService docTypeService;
 
 
 
    @Transactional
    public ReturnT<String> start(Order order) {
        DocType docType = docTypeService.selectById(order.getDocType());
        if (null == docType) {
            log.info("上报出错,不存在该单据类型: " + order);
            return SUCCESS;
        }
        List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId());
 
        // 获取请求头
        Map<String, Object> headers = getHeaders();
 
        // 构造请求体
        String body = getBody(orderDetls, order);
 
        // 入库完成上报
        String response = "";
        boolean success = false;
        try {
            response = new HttpHandler.Builder()
                    .setUri(MesConstant.URL)
                    .setPath(MesConstant.PAKIN_URL)
                    .setHeaders(headers)
                    .setJson(body)
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            JSONObject std_data = jsonObject.getJSONObject("std_data");
            JSONObject execution = std_data.getJSONObject("execution");
            String code = execution.getString("code");
            JSONObject std_data1 = std_data.getJSONObject("parameter");
            JSONObject execution1 = std_data1.getJSONObject("response_result");
            String status = execution1.getString("Status");
 
            if ("0".equals(code) && "0".equals(status)) {
                success = true;
                // 修改订单状态 4.完成 ===>> 6.已上报
                if (!orderService.updateSettle(order.getId(), 6L, null)) {
                    throw new CoolException("服务器内部错误,请联系管理员");
                }
            } else {
                orderService.updateSettle(order.getId(), 7L, null);
                log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL + MesConstant.PAKIN_URL, body, response);
                throw new CoolException("上报mes系统失败");
            }
        } catch (Exception e) {
            log.error("fail", e);
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return FAIL.setMsg(e.getMessage());
        } finally {
            try {
                // 保存接口日志
                apiLogService.save(
                        docType.getPakin() == 1 ?"入库上报" : "出库上报",
                        MesConstant.URL + MesConstant.PAKIN_URL,
                        null,
                        "127.0.0.1",
                        JSON.toJSONString(body),
                        response,
                        success
                );
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return SUCCESS;
    }
 
    private String getBody(List<OrderDetl> orderDetls, Order order) {
 
        JSONObject orderJson = new JSONObject();
        JSONObject orderJson1 = new JSONObject();
        JSONObject orderJson2 = new JSONObject();
        JSONObject orderJson3 = new JSONObject();
 
        JSONArray jsonArray = new JSONArray();
        for(OrderDetl orderDetl:orderDetls) {
            Map<String,Object> map = new HashMap<>();
            map.put("matnr",orderDetl.getMatnr());
            map.put("anfme",orderDetl.getAnfme());
            jsonArray.add(map);
        }
 
        orderJson3.put("orderNo",order.getOrderNo());
        orderJson3.put("TypeKey",order.getDocType$());
        orderJson3.put("matList",jsonArray);
        orderJson2.put("importData",orderJson3);
        orderJson1.put("parameter",orderJson2);
        orderJson.put("std_data",orderJson1);
        return orderJson.toJSONString();
    }
 
    Map<String, Object> getHeaders(){
//        digi-type: sync
//        digi-protocol: raw
//        digi-host: {"prod":"XThirdParty","ver":"1.0","ip":"61.153.227.86","id":"XWMS","timezone":"+8","timestamp":"20240613062240538","acct":"dcms"}
//        digi-service: {"prod":"E10","name":"XCommon.ImportData","id":"XFX","ip":"139.196.196.39"}
//        digi-key: 504011D3B5AD32B465FE0C097C7FDAAC
//        digi-datakey: XCommon.ImportData
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
 
        // 定义日期时间格式化器,根据你的需求定义格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
 
        // 格式化日期时间为字符串
        String formattedDateTime = now.format(formatter);
 
        JSONObject digiHost = new JSONObject();
        digiHost.put("prod","XThirdParty");
        digiHost.put("ver","1.0");
        digiHost.put("ip","61.153.227.86");
        digiHost.put("id","XWMS");
        digiHost.put("timezone","+8");
        digiHost.put("timestamp",formattedDateTime);
        digiHost.put("acct","dcms");
 
        JSONObject digiService = new JSONObject();
        digiService.put("prod","E10");
        digiService.put("name","XCommon.ImportData");
        digiService.put("ip","139.196.196.39");
        digiService.put("id","XFX");
 
        String key = digiHost.toJSONString() + digiService.toJSONString();
        String keyMd5 = DigestUtils.md5Hex(key);
 
 
        Map<String,Object> headers = new HashMap<>();
        headers.put("digi-type","sync ");
        headers.put("digi-protocol","raw");
        headers.put("digi-host",digiHost.toJSONString());
        headers.put("digi-service",digiService.toJSONString());
        headers.put("digi-key",keyMd5);
        headers.put("digi-datakey"," XCommon.ImportData");
 
        return headers;
    }
 
    public static void main(String[] args) {
 
        String msg = "{\n" +
                "    \"std_data\": {\n" +
                "        \"execution\": {\n" +
                "            \"code\": \"0\",\n" +
                "            \"sql_code\": \"\",\n" +
                "            \"description\": \"执行成功\",\n" +
                "            \"token_id\": \"e7b4b7d668ec41fa83f643eb1b322959\"\n" +
                "        },\n" +
                "        \"parameter\": {\n" +
                "            \"response_result\": {\n" +
                "                \"Status\": \"0\",\n" +
                "                \"Message\": \"操作成功!\",\n" +
                "                \"DOC_NO\": \"\"\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}";
        JSONObject jsonObject = JSON.parseObject(msg);
        JSONObject std_data = jsonObject.getJSONObject("std_data");
        JSONObject execution = std_data.getJSONObject("execution");
        System.out.println(jsonObject.get("std_data"));
        System.out.println(execution.getString("code"));
 
//        JSONObject orderJson = new JSONObject();
//        JSONObject orderJson1 = new JSONObject();
//        JSONObject orderJson2 = new JSONObject();
//        JSONObject orderJson3 = new JSONObject();
//
//        JSONArray jsonArray = new JSONArray();
//        Map<String,Object> map = new HashMap<>();
//        map.put("matnr","301080001");
//        map.put("anfme","23456");
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("matnr","301080001");
//        jsonObject.put("anfme","23456");
//        jsonArray.add(map);
//
//        orderJson3.put("orderNo","3600-201702010001");
//        orderJson3.put("TypeKey","PURCHASE_ARRIVAL");
//        orderJson3.put("matList",jsonArray);
//        orderJson2.put("importData",orderJson3);
//        orderJson1.put("parameter",orderJson2);
//        orderJson.put("std_data",orderJson1);
//        System.out.println(orderJson.toJSONString());
    }
}