自动化立体仓库 - WMS系统
pang.jiabao
2025-04-22 11b91a53371953c684c1f966c8cb5834fb46a4ef
src/main/java/com/zy/asrs/task/handler/OrderSendHandler.java
@@ -2,87 +2,121 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.entity.DocType;
import com.core.common.Cools;
import com.core.exception.CoolException;
import com.zy.asrs.entity.Order;
import com.zy.asrs.entity.OrderDetl;
import com.zy.asrs.entity.param.OpenOrderPakoutParam;
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.ErpAccessTokenScheduler;
import com.zy.asrs.utils.Utils;
import com.zy.common.utils.HttpHandler;
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.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
@Transactional
public class OrderSendHandler extends AbstractHandler<String> {
    private static final String erpIp = "10.100.221.19:81";
    private static final String inPath = "/ierp/kapi/app/im/PurinbillApiPlugin";
    private static final String outPath = "/ierp/kapi/app/im/SaloutbillApiPlugin";
    @Value("${u8.url}")
    private String url;
    @Value("${u8.path}")
    private String orderReportPath;
    @Autowired
    private OrderService orderService;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private OrderDetlService orderDetlService;
    public void start(Order order) {
        String accessToken = ErpAccessTokenScheduler.accessToken;
        if (accessToken.equals("")) {
            return;
        List<OpenOrderPakoutParam> paramList = new ArrayList<>();
        OpenOrderPakoutParam param = new OpenOrderPakoutParam();
//        paramList.add(param);
        List<OrderDetl> orderDetlList = orderDetlService.selectByOrderId(order.getId());
        param.setcCode(order.getOrderNo());
        param.setcMaker(order.getOperMemb());
        param.setcCusName(order.getCstmrName());
        param.setcAccount(order.getNumber());
        param.setcMemo(order.getMemo());
        param.setType(Cools.isEmpty(order.getDocType$()) ? "" : order.getDocType$());
        param.setdDate(Utils.getDateStr(new Date()));
        param.setAllocation(order.getItemName());
        List<OpenOrderPakoutParam.Body> bodyList = new ArrayList<>();
        param.setaDDBody(bodyList);
        for (OrderDetl orderDetl : orderDetlList) {
            OpenOrderPakoutParam.Body body = new OpenOrderPakoutParam.Body();
            body.setiQuantity(orderDetl.getQty());
            body.setcInvCode(orderDetl.getMatnr());
            body.setCbMemo(orderDetl.getMemo());
            bodyList.add(body);
        }
        HashMap<String, Object> header = new HashMap<>();
        header.put("accessToken",accessToken);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderNo",order.getOrderNo());
        HttpHandler.Builder prepBuilder = new HttpHandler.Builder()
                .setUri(erpIp)
                .setHeaders(header)
                .setJson(jsonObject.toJSONString());
        JSONObject respJson;
        doHttpRequest(param, "上报ERP出库单据", url, orderReportPath, null, "127.0.0.1");
        orderService.updateSettle(order.getId(), 6L, 0L);
        //入库单据
        if (docTypeService.selectOne(new EntityWrapper<DocType>()
                .eq("doc_id",order.getDocType())).getPakin() == 1) {
            try {
                respJson = JSON.parseObject(
                        prepBuilder.setPath(inPath)
                        .build()
                        .doPost());
            } catch (IOException e) {
                throw new RuntimeException(e);
    }
    private int doHttpRequest(Object requestParam, String namespace, String url, String path, String appkey, String ip) {
        String response = "";
        boolean success = false;
        log.info(JSONObject.toJSONString(requestParam));
        try {
            response = new HttpHandler.Builder()
                    .setUri(url)
                    .setPath(path)
                    .setJson(JSONObject.toJSONString(requestParam))
                    .build()
                    .doPost();
            log.info("上报ERP出库单据请求返回报文:{}", response);
            JSONObject jsonObject = JSON.parseObject(response);
            if (Cools.isEmpty(jsonObject.get("ErrorCode"))) {
                throw new CoolException(jsonObject.get("ErrorMsg").toString());
            }
        }else {
            //出库单据
            try {
                respJson = JSON.parseObject(
                prepBuilder.setPath(outPath)
                        .build()
                        .doPost());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        if ((boolean)respJson.get("success")) {
            orderService.updateSettle(order.getId(),6L,1L);
            log.info("单据:" + order.getOrderNo() + "回传成功,已更新单据状态");
        }else {
            order.setItemId(order.getItemId() + 1);
            orderService.updateById(order);
            log.info("单据:"+ order.getOrderNo()+" 回传失败,这是第" + order.getItemId() + "次回传");
            log.info(respJson.toJSONString());
            int code = Integer.parseInt(jsonObject.get("ErrorCode").toString());
            if (code == 0) {
                throw new CoolException(jsonObject.get("ErrorMsg").toString());
            }
            success = true;
            return code;
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new CoolException("调用接口响应错误");
        } finally {
            apiLogService.save(
                    namespace,
                    url + path,
                    appkey,
                    ip,
                    JSON.toJSONString(JSONObject.toJSONString(requestParam)),
                    response,
                    success
            );
        }
    }
}