自动化立体仓库 - WMS系统
#
zjj
2025-03-24 be14e23722db5053f2ac9048629d72abd05dea08
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
package com.zy.asrs.task.kingdee.handler;
 
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.common.DateUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
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.utils.HttpHandler;
import com.zy.erp.kingdee.enums.KingDeeUtilType;
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.util.*;
 
/**
 * Created by Monkey D. Luffy on 2023.10.21
 */
@Slf4j
@Service
public class ExecuteBillQueryHandler extends AbstractHandler<String> {
    @Value("${erp.address.URL}")
    //端口
    private String URL;
 
    @Value("${erp.address.executeBillQuery}")
    //保存地址
    private String executeBillQuery;
 
    @Autowired
    private OrderService orderService;
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private LoginAuthenticationHandler loginAuthenticationHandler;
 
    /**
     * 查询接口
     * 获取订单物料的ID
     *
     * @param order
     * @return
     */
    @Transactional
    public ReturnT<String> start(Order order) {
        List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId());
        if (Cools.isEmpty(orderDetls) || orderDetls.size() == 0) {
            return SUCCESS;
        }
        //登录金蝶r
        ReturnT<String> start = loginAuthenticationHandler.start();
        DocType docType = docTypeService.selectById(order.getDocType());
        if (null == docType) {
            return SUCCESS;
        }
        KingDeeUtilType kingDeeUtilType = KingDeeUtilType.get(docType.getDocName());
//        if (kingDeeUtilType.entryId!=2){
//            return SUCCESS;
//        }
        Date now = new Date();
        //条件拼接
        JSONObject data = new JSONObject();
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("Ids","100083");
        jsonObject.put("FormId", kingDeeUtilType.correspondingFormId);
        data.put("FormId", kingDeeUtilType.correspondingFormId);
        //查询字段 erp下发的订单ID、行ID、物料编号、数量
        jsonObject.put("FieldKeys", "fid,FEntity_FEntryID,FMaterialID.FNumber,"+kingDeeUtilType.anfme);
 
        List<JSONObject> filterStrings = new ArrayList<>();
        JSONObject filterString = new JSONObject();
        filterString.put("Left", "");
        filterString.put("FieldName", "FBILLNO");
        filterString.put("Compare", "=");
        filterString.put("Value", order.getShipCode());
        filterString.put("Right", "");
        filterString.put("Logic", "AND");
        filterStrings.add(filterString);
        jsonObject.put("FilterString", filterStrings);
 
        jsonObject.put("OrderString", "");
        jsonObject.put("TopRowCount", 0);
        jsonObject.put("StartRow", 0);
        jsonObject.put("Limit", 2000);
        jsonObject.put("SubSystemId", "");
 
        data.put("data", jsonObject);
        String add = data.toJSONString();
        //上报
        String response = "";
        boolean success = false;
        try {
            //获取Cookie值
            HashMap<String, Object> headers = new HashMap<>();
            headers.put("Cookie", start.getContent());
            response = new HttpHandler.Builder()
                    .setHeaders(headers)
                    .setUri(URL)
                    .setPath(executeBillQuery)
                    .setJson(add)
                    .setHttps(true)
                    .build()
                    .doPost();
 
            JSONArray jsonArray = JSON.parseArray(response);
            //保存完成原订单1转2 2.查询完成
            if (jsonArray.size() > 0) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    for (OrderDetl orderDetl : orderDetls) {
                        JSONArray jsonArray1 = JSON.parseArray(jsonArray.get(i).toString());
                        if (jsonArray1.get(2).equals(orderDetl.getMatnr())) {
                            //跟新订单明细行号
                            orderDetl.setBeBatch(Integer.valueOf(jsonArray1.get(1).toString()));
                            if (Cools.isEmpty(orderDetl.getBatch())) {
                                orderDetlService.update(orderDetl, new EntityWrapper<OrderDetl>()
                                        .eq("order_no", order.getOrderNo())
                                        .eq("matnr", orderDetl.getMatnr()));
                            } else {
                                orderDetlService.update(orderDetl, new EntityWrapper<OrderDetl>()
                                        .eq("order_no", order.getOrderNo())
                                        .eq("matnr", orderDetl.getMatnr())
                                        .eq("batch", orderDetl.getBatch()));
                            }
                        }
 
                    }
                }
                //提交完成原订单1转2  2.查询完成
                success = true;
                order.setMemo("2");
                order.setUpdateTime(new Date());
                orderService.update(order, new EntityWrapper<Order>().eq("order_no", order.getOrderNo()));
            }
 
//            if (!bool1.equals("true") || !bool){
//                order.setSettle(7L);
//                order.setUpdateTime(new Date());
//                orderService.update(order,new EntityWrapper<Order>().eq("order_no",order.getOrderNo()));
//            }
 
        } catch (Exception e) {
            log.error("fail", e);
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return FAIL.setMsg(e.getMessage());
        } finally {
            try {
                // 保存接口日志
                apiLogService.save(
                        "2.查询",
                        URL + executeBillQuery,
                        null,
                        "127.0.0.1",
                        add,
                        response,
                        success
                );
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return SUCCESS;
    }
 
    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;
    }
}