自动化立体仓库 - WMS系统
#
whycq
2025-01-08 d3cb0e841e6585aa84a45f18bb30965db8d1a6aa
src/main/java/com/zy/asrs/task/handler/OrderSyncHandler.java
@@ -1,9 +1,8 @@
package com.zy.asrs.task.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.exception.CoolException;
import com.zy.asrs.entity.DocType;
import com.zy.asrs.entity.Order;
@@ -15,16 +14,20 @@
import com.zy.asrs.task.AbstractHandler;
import com.zy.asrs.task.core.ReturnT;
import com.zy.common.constant.MesConstant;
import com.zy.common.model.MesPakinParam;
import com.zy.common.model.MesPakoutParam;
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
@@ -44,116 +47,191 @@
    @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);
        // 入库完成上报
        if (docType.getPakin() == 1) {
            MesPakinParam pakinParam = new MesPakinParam();
            pakinParam.setPakinTime(DateUtils.convert(order.getUpdateTime()));
            pakinParam.setLgortFrom("5008");
            pakinParam.setLgortTo("5006");
            for (OrderDetl orderDetl : orderDetls) {
                String serial = Cools.isEmpty(orderDetl.getBatch()) ? "" : orderDetl.getBatch();
                pakinParam.getList().add(new MesPakinParam.Detl(orderDetl.getMatnr() + (Cools.isEmpty(serial) ? "" : "-" + serial), orderDetl.getAnfme()));
            }
            String response = "";
            boolean success = false;
            try {
                response = new HttpHandler.Builder()
                        .setUri(MesConstant.URL)
                        .setPath(MesConstant.PAKIN_URL)
                        .setJson(JSON.toJSONString(pakinParam))
                        .build()
                        .doPost();
                JSONObject jsonObject = JSON.parseObject(response);
                if (jsonObject.getInteger("code").equals(200)) {
                    success = true;
                    // 修改订单状态 4.完成 ===>> 6.已上报
                    if (!orderService.updateSettle(order.getId(), 6L, null)) {
                        throw new CoolException("服务器内部错误,请联系管理员");
                    }
                } else {
                    log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL+MesConstant.PAKIN_URL, JSON.toJSONString(pakinParam), response);
                    throw new CoolException("上报mes系统失败");
        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("服务器内部错误,请联系管理员");
                }
            } catch (Exception e) {
                log.error("fail", e);
            } else {
                orderService.updateSettle(order.getId(), 6L, 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(
                            "成品库入库上报",
                            MesConstant.URL + MesConstant.PAKIN_URL,
                            null,
                            "127.0.0.1",
                            JSON.toJSONString(pakinParam),
                            response,
                            success
                    );
                } catch (Exception e) { log.error("", e); }
            }
        }
        // 出库完成上报
        if (docType.getPakout() == 1) {
            if (true) {
                return SUCCESS;
            }
            MesPakoutParam pakoutParam = new MesPakoutParam();
            pakoutParam.setPakoutTime(DateUtils.convert(order.getUpdateTime()));    // todo:luxiaotao
            pakoutParam.setLgortFrom("5008");   // todo:luxiaotao
            pakoutParam.setLgortTo("5006");   // todo:luxiaotao
            pakoutParam.setOrderNo(order.getOrderNo());
            for (OrderDetl orderDetl : orderDetls) {
                String serial = Cools.isEmpty(orderDetl.getBatch()) ? "" : orderDetl.getBatch();
                pakoutParam.getList().add(new MesPakoutParam.Detl(orderDetl.getMatnr() + (Cools.isEmpty(serial) ? "" : "-" + serial), orderDetl.getAnfme()));
            }
            String response = "";
            boolean success = false;
            return FAIL.setMsg(e.getMessage());
        } finally {
            try {
                response = new HttpHandler.Builder()
                        .setUri(MesConstant.URL)
                        .setPath(MesConstant.PAKOUT_URL)    // todo:luxiaotao
                        .setJson(JSON.toJSONString(pakoutParam))
                        .build()
                        .doPost();
                JSONObject jsonObject = JSON.parseObject(response);
                if (jsonObject.getInteger("code").equals(200)) {
                    success = true;
                    // 修改订单状态 4.完成 ===>> 6.已上报
                    if (!orderService.updateSettle(order.getId(), 6L, null)) {
                        throw new CoolException("服务器内部错误,请联系管理员");
                    }
                } else {
                    log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL+MesConstant.PAKOUT_URL, JSON.toJSONString(pakoutParam), response);
                    throw new CoolException("上报mes系统失败");
                }
                // 保存接口日志
                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("fail", e);
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return FAIL.setMsg(e.getMessage());
            } finally {
                try {
                    // 保存接口日志
                    apiLogService.save(
                            "成品库出库上报",
                            MesConstant.URL + MesConstant.PAKOUT_URL,
                            null,
                            "127.0.0.1",
                            JSON.toJSONString(pakoutParam),
                            response,
                            success
                    );
                } catch (Exception e) { log.error("", 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());
    }
}