自动化立体仓库 - WMS系统
#
lty
7 小时以前 9b1437c9bcec2130ee67e43575fcde517f71f9ce
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
package com.zy.asrs.task;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.core.common.Cools;
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.core.ReturnT;
import com.zy.common.entity.Parameter;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * Created by vincent on 2020/7/7
 */
@Slf4j
@Component
public class OrderSyncScheduler {
 
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private ApiLogService apiLogService;
 
    @Scheduled(cron = "0 0 1 * * ? ")
    public void clearApiLog(){
        try {
            apiLogService.clearWeekBefore();
        } catch (Exception e) {
            log.error("第三方接口日志自动清除失败(范围:一周之前", e);
        }
    }
 
//    @Scheduled(cron = "0/5 * * * * ? ")
    @Async("orderThreadPool")
    public void completeAndReport(){
        String erpReport = Parameter.get().getErpReport();
        if (!Cools.isEmpty(erpReport) && erpReport.equals("true")) {
            List<Order> orders = orderService.selectComplete();
            for (Order order : orders) {
                ReturnT<String> result = reportToMesOrderReport(order);
                if (!result.isSuccess()) {
                    log.error("单据[orderNo={}]上报失败:{}", order.getOrderNo(), result.getMsg());
                }
            }
        }
    }
 
    private ReturnT<String> reportToMesOrderReport(Order order) {
        if (order == null) {
            return new ReturnT<>(ReturnT.SUCCESS_CODE, "SUCCESS");
        }
 
        DocType docType = docTypeService.selectById(order.getDocType());
        if (docType == null) {
            return new ReturnT<>(ReturnT.SUCCESS_CODE, "SUCCESS");
        }
 
        String wkType = docType.getDocName();
        List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId());
 
        String uri = "localhost:8889";
        String path = "/lfdwms/open/asrs/MES/orderReport";
        String url = "http://" + uri + path;
 
        Map<String, Object> payload = new HashMap<>();
        payload.put("orderNo", order.getOrderNo());
        payload.put("warehouseId", null);
        payload.put("wkType", wkType);
        payload.put("orderDetls", orderDetls);
        String requestJson = JSON.toJSONString(payload);
 
        String response = "";
        boolean success = false;
        try {
            response = new HttpHandler.Builder()
                    .setUri(uri)
                    .setPath(path)
                    .setJson(requestJson)
                    .build()
                    .doPost();
            JSONObject json = JSON.parseObject(response);
            Integer code = json == null ? null : json.getInteger("code");
            success = code != null && code == 200;
            if (!success) {
                String msg = json == null ? null : json.getString("msg");
                throw new CoolException(!Cools.isEmpty(msg) ? msg : "接口返回失败:" + response);
            }
            if (!orderService.updateSettle(order.getId(), 6L, null)) {
                throw new CoolException("更新单据状态失败");
            }
            return new ReturnT<>(ReturnT.SUCCESS_CODE, "SUCCESS");
        } catch (IOException e) {
            return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
        } catch (Exception e) {
            return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
        } finally {
            try {
                apiLogService.save(
                        "MES订单上报",
                        url,
                        null,
                        "127.0.0.1",
                        requestJson,
                        response,
                        success
                );
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }
 
}