自动化立体仓库 - WMS系统
#
lty
20 小时以前 9b1437c9bcec2130ee67e43575fcde517f71f9ce
src/main/java/com/zy/asrs/task/OrderSyncScheduler.java
@@ -1,17 +1,29 @@
package com.zy.asrs.task;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.core.common.Cools;
import com.core.exception.CoolException;
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.core.ReturnT;
import com.zy.asrs.task.handler.OrderSyncHandler;
import com.zy.common.entity.Parameter;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by vincent on 2020/7/7
@@ -21,18 +33,99 @@
public class OrderSyncScheduler {
    @Autowired
    private OrderSyncHandler orderSyncHandler;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private DocTypeService docTypeService;
    @Autowired
    private ApiLogService apiLogService;
    @Scheduled(cron = "0/5 * * * * ? ")
    @Scheduled(cron = "0 0 1 * * ? ")
    public void clearApiLog(){
        try {
            apiLogService.clearWeekBefore();
        } catch (Exception e) {
            log.error("第三方接口日志自动清除失败(范围:一周之前", e);
        }
    }
//    @Scheduled(cron = "0/5 * * * * ? ")
    @Async("orderThreadPool")
    public void completeAndReport(){
        List<Order> orders = orderService.selectList(new EntityWrapper<Order>().eq("settle", 4L).eq("status", 1));
        for (Order order : orders) {
            ReturnT<String> result = orderSyncHandler.start(order);
            if (!result.isSuccess()) {
                log.error("单据[orderNo={}]上报erp失败", order.getOrderNo());
        String erpReport = Parameter.get().getErpReport();
        if (!Cools.isEmpty(erpReport) && erpReport.equals("true")) {
            List<Order> orders = orderService.selectComplete();
            for (Order order : orders) {
                ReturnT<String> result = reportToMesOrderReport(order);
                if (!result.isSuccess()) {
                    log.error("单据[orderNo={}]上报失败:{}", order.getOrderNo(), result.getMsg());
                }
            }
        }
    }
    private ReturnT<String> reportToMesOrderReport(Order order) {
        if (order == null) {
            return new ReturnT<>(ReturnT.SUCCESS_CODE, "SUCCESS");
        }
        DocType docType = docTypeService.selectById(order.getDocType());
        if (docType == null) {
            return new ReturnT<>(ReturnT.SUCCESS_CODE, "SUCCESS");
        }
        String wkType = docType.getDocName();
        List<OrderDetl> orderDetls = orderDetlService.selectByOrderId(order.getId());
        String uri = "localhost:8889";
        String path = "/lfdwms/open/asrs/MES/orderReport";
        String url = "http://" + uri + path;
        Map<String, Object> payload = new HashMap<>();
        payload.put("orderNo", order.getOrderNo());
        payload.put("warehouseId", null);
        payload.put("wkType", wkType);
        payload.put("orderDetls", orderDetls);
        String requestJson = JSON.toJSONString(payload);
        String response = "";
        boolean success = false;
        try {
            response = new HttpHandler.Builder()
                    .setUri(uri)
                    .setPath(path)
                    .setJson(requestJson)
                    .build()
                    .doPost();
            JSONObject json = JSON.parseObject(response);
            Integer code = json == null ? null : json.getInteger("code");
            success = code != null && code == 200;
            if (!success) {
                String msg = json == null ? null : json.getString("msg");
                throw new CoolException(!Cools.isEmpty(msg) ? msg : "接口返回失败:" + response);
            }
            if (!orderService.updateSettle(order.getId(), 6L, null)) {
                throw new CoolException("更新单据状态失败");
            }
            return new ReturnT<>(ReturnT.SUCCESS_CODE, "SUCCESS");
        } catch (IOException e) {
            return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
        } catch (Exception e) {
            return new ReturnT<>(ReturnT.FAIL_CODE, e.getMessage());
        } finally {
            try {
                apiLogService.save(
                        "MES订单上报",
                        url,
                        null,
                        "127.0.0.1",
                        requestJson,
                        response,
                        success
                );
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }