自动化立体仓库 - WMS系统
1
zhangc
2025-04-16 2a0f422815c0379e6d4bf795a2eac435da9b3df2
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
package com.zy.asrs.task.handler;
 
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.exception.CoolException;
import com.zy.asrs.entity.MesLocDetl;
import com.zy.asrs.entity.ReportData;
import com.zy.asrs.entity.ReportDataLog;
import com.zy.asrs.entity.param.MesStock;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.MesLocDetlService;
import com.zy.asrs.service.ReportDataLogService;
import com.zy.asrs.service.ReportDataService;
import com.zy.asrs.task.AbstractHandler;
import com.zy.asrs.task.core.ReturnT;
import com.zy.common.utils.HttpHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * Created by vincent on 2020/7/7
 */
@Service
public class ReportDataHandler extends AbstractHandler<String> {
 
    private static final Logger log = LoggerFactory.getLogger(ReportDataHandler.class);
 
 
    @Autowired
    private ReportDataService reportDataService;
 
    @Autowired
    private ReportDataLogService reportDataLogService;
 
    @Autowired
    private ApiLogService apiLogService;
 
 
    @Autowired
    private MesLocDetlService mesLocDetlService;
 
    @Value("${mes.url}")
    private String url;
 
    @Value("${mes.orderReportPath}")
    private String orderReportPath;
 
    @Value("${mes.pakinReportPath}")
    private String pakinReportPath;
 
    @Value("${mes.buyPakinReportPath}")
    private String buyPakinReportPath;
 
    @Value("${mes.pakoutReportPath}")
    private String pakoutReportPath;
 
    @Value("${mes.stock}")
    private String stock;
 
 
    @Transactional
    public ReturnT<String> start(ReportData data, Map<String, Object> mesTokenInfo) {
        String mesPath = "";
        if ("MES".equals(data.getReportType())) {
            switch (data.getMemo()) {
                case "上报mes采购入库信息":
                    mesPath = buyPakinReportPath;
                    break;
                case "上报mes入库信息":
                    mesPath = pakinReportPath;
                    break;
                case "上报mes出库信息":
                    mesPath = pakoutReportPath;
                    break;
                case "上报mes调拨信息":
                    mesPath = orderReportPath;
                    break;
            }
            ReportDataLog reportDataLog = new ReportDataLog();
            String result = doHttpRequest(data.getReportJson(), mesTokenInfo, data.getMemo(), url, mesPath, null, "127.0.0.1");
            reportDataService.deleteById(data.getId());
            BeanUtil.copyProperties(data, reportDataLog);
            reportDataLog.setReportTime(new Date());
            reportDataLog.setReportDataId(data.getId());
            reportDataLog.setThreeCode(result);
            reportDataLogService.insert(reportDataLog);
        }
        return SUCCESS;
    }
 
 
    private String doHttpRequest(String requestParam, Map<String, Object> headParam, String namespace, String url, String path, String appkey, String ip) {
        String response = "fail";
        boolean success = false;
 
        try {
            response = new HttpHandler.Builder().setUri(url).setPath(path).setTimeout(30, TimeUnit.SECONDS).setHeaders(headParam).setJson(requestParam).setHttps(true).build().doPost();
            JSONObject jsonObject = JSON.parseObject(response);
 
            if (Cools.isEmpty(jsonObject.get("code")) || Integer.parseInt(jsonObject.get("code").toString()) != 200) {
                log.info("mes接口调用失败,返回信息:" + jsonObject);
                //TODO 张超
                throw new CoolException("mes接口调用失败,返回信息:" + jsonObject);
            }
            success = true;
            return response;
 
        } catch (Exception e) {
            log.error("mes接口调用失败,返回信息:{}", e.getMessage());
            //throw new CoolException(e.getMessage());
        } finally {
            apiLogService.save(namespace, url + path, appkey, ip, requestParam, response, success);
        }
        return response;
    }
 
 
    @Transactional
    public ReturnT<String> stock(Map<String, Object> mesTokenInfo) {
        List<MesLocDetl> all = mesLocDetlService.selectList(new EntityWrapper<>());
        if (all != null) {
            List<Long> collect = all.stream().map(MesLocDetl::getId).collect(Collectors.toList());
            boolean del = true;
            int d = 1;
            while (del && collect.size() > 0) {
                if (collect.size() > d * 1000) {
                    mesLocDetlService.deleteBatchIds(collect.subList((d - 1) * 1000, d * 1000));
                } else {
                    mesLocDetlService.deleteBatchIds(collect.subList((d - 1) * 1000, collect.size()));
                    del = false;
                }
                d++;
            }
        }
 
        boolean flag = true;
        Map<String, Object> req = new HashMap<>();
        int i = 1;
        while (flag) {
            req.put("page", i);
            req.put("size", 1000);
            String result = doHttpRequest(JSONObject.toJSONString(req), mesTokenInfo, "同步MES库存", url, stock, null, "127.0.0.1");
            JSONObject jsonObject = JSONObject.parseObject(result);
            Object o = jsonObject.get("data");
            List<MesLocDetl> mesLocDetls = new ArrayList<>();
            MesLocDetl mesLocDetl;
            if (o != null) {
                JSONObject jsonObject1 = JSONObject.parseObject(o.toString());
                Object o1 = jsonObject1.get("list");
                if (o1 != null) {
                    List<MesStock> mesStocks = JSONArray.parseArray(o1.toString(), MesStock.class);
                    if (mesStocks.isEmpty()) {
                        flag = false;
                    }
                    for (MesStock mesStock : mesStocks) {
                        mesLocDetl = new MesLocDetl();
                        mesLocDetl.setMatnr(mesStock.getMaterialInfo().getMaterialCode());
                        mesLocDetl.setMaktx(mesStock.getMaterialInfo().getMaterialName());
                        mesLocDetl.setBatch(mesStock.getBatchNo());
                        mesLocDetl.setAnfme(mesStock.getOpAmount().getAmount().getAmount());
                        mesLocDetls.add(mesLocDetl);
                        mesLocDetlService.insert(mesLocDetl);
                    }
                } else {
                    flag = false;
                }
            }
            if (!mesLocDetls.isEmpty()) {
                //mesLocDetlService.insertBatch(mesLocDetls);
            }
            i++;
        }
        return SUCCESS;
    }
 
 
}