tjx
10 天以前 d0443c2b5d277377f22748be405d9a06dafe04e6
src/main/java/com/gs/xky/service/PurchaseService.java
@@ -10,6 +10,7 @@
import com.gs.xky.entity.PurchaseOrderCompare;
import com.gs.xky.entity.PurchaseOrderDetail;
import com.gs.xky.mapper.PurchaseOrderCompareMapper;
import com.gs.xky.mapper.PurchaseOrderDetailMapper;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -30,6 +31,7 @@
    private final MesRohInDataService mesRohInDataService;
    private final PurchaseOrderDetailService purchaseOrderDetailService;
    private final PurchaseOrderCompareMapper purchaseOrderCompareMapper;
    private final PurchaseOrderDetailMapper purchaseOrderDetailMapper;
    /**
     * 同步采购订单明细数据
@@ -44,6 +46,10 @@
        long currentTimeMillis = System.currentTimeMillis();
        // 限制请求时间范围为24小时
        long startDate = currentTimeMillis - (24 * 60 * 60 * 1000L);
        // 先删除已有数据,避免重复
        purchaseOrderDetailMapper.deleteByPrimaryKey();
        purchaseOrderCompareMapper.deleteByPrimaryKey();
        XkyCommonParam<PurchaseParam> param = XkyCommonParam.GetInit();
        PurchaseParam bodyParam = new PurchaseParam();
@@ -73,8 +79,35 @@
        log.info("【syncPurchaseOrderDetails】获取到{}条采购订单数据", orderDetails.size());
        // 处理采购订单明细数据
        orderDetails.forEach(detail -> {
        // 分批处理数据,减少内存占用
        int batchSize = 100; // 每批处理100条数据
        int totalSize = orderDetails.size();
        int batchCount = (totalSize + batchSize - 1) / batchSize; // 向上取整计算批次数
        for (int i = 0; i < batchCount; i++) {
            int fromIndex = i * batchSize;
            int toIndex = Math.min((i + 1) * batchSize, totalSize);
            log.info("【syncPurchaseOrderDetails】处理第{}批数据,范围:{}-{}", i + 1, fromIndex, toIndex);
            // 获取当前批次的数据
            List<PurchaseOrderDetail> batchDetails = orderDetails.subList(fromIndex, toIndex);
            // 处理当前批次的数据
            processBatch(batchDetails);
            // 手动触发GC,释放内存(谨慎使用,仅在内存紧张时考虑)
            // System.gc();
        }
    }
    /**
     * 批量处理采购订单明细数据
     *
     * @param batchDetails 当前批次的采购订单明细数据
     */
    private void processBatch(List<PurchaseOrderDetail> batchDetails) {
        batchDetails.forEach(detail -> {
            try {
                // 根据有效标志和订单状态处理不同的业务逻辑
                if (detail.getValidFlag() != null && detail.getValidFlag() == 0) {
@@ -107,6 +140,10 @@
                // 保存SRM采购订单明细
                savePurchaseOrderDetail(detail);
                // 帮助GC回收不再使用的对象
                wrapper = null;
                erpData = null;
            } catch (Exception e) {
                log.error("【syncPurchaseOrderDetails 处理异常】订单号: {}, 项次: {}, 异常: {}",
                        detail.getPoErpNo(), detail.getLineNo(), e.getMessage(), e);
@@ -166,13 +203,14 @@
        // 计算SRM待收数量
        Integer srmPurchaseQty = detail.getTotalAnswerQty();
        Integer srmReceivedQty = detail.getTotalReceiveQty();
        Integer srmReceivedQty = detail.getPoWaitDeliveryQty() + detail.getReturnWaitDeliveryQty();
        //poWaitDeliveryQty + returnWaitDeliveryQty
        Integer srmWaitReceiveQty = srmPurchaseQty - srmReceivedQty;
        // 设置SRM数据
        compareData.setSrmPurchaseQty(srmPurchaseQty);
        compareData.setSrmReceivedQty(srmReceivedQty);
        compareData.setSrmWaitReceiveQty(srmWaitReceiveQty);
        compareData.setSrmReceivedQty(srmWaitReceiveQty);
        compareData.setSrmWaitReceiveQty(srmReceivedQty);
        // 设置ERP数据和差异
        if (erpData == null) {