自动化立体仓库 - WMS系统
lty
昨天 6d47348cf9dbeabcadb5801db46dfecd48f87d85
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
package com.zy.asrs.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zy.asrs.entity.CheckDetl;
import com.zy.asrs.mapper.CheckDetlMapper;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.CheckDetlService;
import com.zy.asrs.task.kingdee.handler.LoginAuthenticationHandler;
import com.zy.common.utils.HttpHandler;
import javafx.print.Printer;
import lombok.extern.slf4j.Slf4j;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
 
@Service
@Slf4j
public class CheckDetlServiceImpl extends ServiceImpl<CheckDetlMapper, CheckDetl> implements CheckDetlService {
    @Value("${erp.address.URL}")
    //端口
    private String URL;
    @Value("${erp.address.outaddressSubmit}")
    //上报出入库地址
    private String outaddressSubmit;
    @Autowired
    private CheckDetlService checkDetlService;
    @Autowired
    private ApiLogService apiLogService;
 
    @Autowired
    private LoginAuthenticationHandler loginAuthenticationHandler;
 
 
    @Override
    @Transactional
    public void upload(List<CheckDetl> list) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 构建主单 JSON 数据
        JSONObject requestBody = new JSONObject();
        requestBody.put("createTime", sdf.format(now));
        // 构建物料列表
        JSONArray matList = new JSONArray();
 
        for(CheckDetl checkDetl : list) {
            JSONObject mat = new JSONObject();
            mat.put("detailId", checkDetl.getDetailId());
            mat.put("orderNo", checkDetl.getOrderNo());
            mat.put("matnr", checkDetl.getMatnr());
            mat.put("anfme", checkDetl.getAnfme());
            matList.add(mat);
        }
        requestBody.put("data", matList);
        // 发送 POST 请求
        String response = "";
        boolean success = false;
        try {
            HashMap<String, Object> headers = new HashMap<>();
            //  cookie
            headers.put("Cookie", loginAuthenticationHandler.start().getContent());
 
            response = new HttpHandler.Builder()
                    .setHeaders(headers)
                    .setUri(URL)
                    .setPath(outaddressSubmit) // 设置你的接口路径
                    .setJson(requestBody.toJSONString())
                    .build()
                    .doPost();
 
            JSONObject data = JSON.parseObject(response);
            Object isSuccess = findValueByKey(data, "IsSuccess");
            String bool = isSuccess != null ? isSuccess.toString() : "false";
 
            if ("true".equals(bool)) {
                success = true;
            }
        } catch (Exception e) {
            log.error("上报ERP失败", e);
        } finally {
            try {
                apiLogService.save(
                        "盘点上报",
                        URL + outaddressSubmit,
                        null,
                        "127.0.0.1",
                        requestBody.toJSONString(),
                        response,
                        success
                );
            } catch (Exception e) {
                log.error("日志保存失败", e);
            }
        }
        if(success) {
            for (CheckDetl entity : list){
                checkDetlService.delete(new EntityWrapper<>(entity));
            }
        }
    }
 
    public static Object findValueByKey(JSONObject json, String key) {
        Set<String> keySet = json.keySet();
        for (String k : keySet) {
            Object v = json.get(k);
            if (k.equals(key)) {
                return v;
            } else if (v instanceof JSONArray) {
                int size = ((JSONArray) v).size();
                for (int i = 0; i <= size - 1; i++) {
                    Object result = findValueByKey((JSONObject) ((JSONArray) v).get(i), key);
                    if (result != null){
                        return result;
                    }
                }
            } else if (v instanceof JSONObject){
                Object result = findValueByKey((JSONObject) v, key);
                if (result != null){
                    return result;
                }
            }
        }
        return null;
    }
}