啊鑫
5 天以前 9f7368ceb60b8f5c635cf455914f435d8d782a90
src/main/java/com/gs/xky/service/XkyService.java
@@ -2,6 +2,7 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gs.xky.config.ApiResponse;
@@ -13,6 +14,8 @@
import com.gs.xky.dto.XkyEntity;
import com.gs.xky.entity.*;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -24,9 +27,8 @@
@RequiredArgsConstructor
public class XkyService {
    private static final Logger log = LoggerFactory.getLogger(XkyService.class);
    private final ApiService apiService;
    private final DeliveryNoticeService deliveryNoticeService;
    private final TblBarcodeInformationService barcodeInformationService;
@@ -40,52 +42,154 @@
    public void GetSaveDetail() throws IOException {
        long currentTimeMillis = System.currentTimeMillis();
        long startDate = currentTimeMillis - (30 * 60 * 1000); // 计算 120 分钟前的时间戳
        XkyCommonParam param = XkyCommonParam.GetInit();
        // 计算五分钟前的时间戳
        long startDate = currentTimeMillis - (20 * 60 * 1000); // 5 分钟 = 5 * 60 * 1000 毫秒
        // 创建 BodyParam 对象并赋值
        XkyCommonParam<BodyParam> param = XkyCommonParam.GetInit();
        BodyParam bodyParam = new BodyParam();
        bodyParam.setStartDate(startDate);
        bodyParam.setEndDate(currentTimeMillis);
        bodyParam.setErpCode(DataAcquisitionConfiguration.TEST_ERP_CODE);
        bodyParam.setStatus(new int[]{1, 6});
//        bodyParam.setLogisticsStatus(2);
        param.setBody(bodyParam);
        // 记录请求参数日志
        log.info("【GetSaveDetail 请求参数】时间: {}, 参数: {}", currentTimeMillis, JSONUtil.toJsonStr(param));
        ApiResponse<XkyEntity> noList = apiService.sendListRequest(param, XkyEntity.class, "https://openapi.xiekeyun.com/delivery/getNoList.json");
//        List<String> deliveryNoList = noList.getDataList().stream().map(XkyEntity::getDeliveryNo).collect(Collectors.toList());
        // 记录返回数据日志
        log.info("【GetSaveDetail 响应数据】时间: {}, 返回: {}", currentTimeMillis, JSONUtil.toJsonStr(noList));
        List<XkyEntity> deliveryNoList = noList.getDataList();
        if (CollUtil.isEmpty(deliveryNoList)) {
            log.info("【GetSaveDetail】返回列表为空,跳过处理");
            return;
        }
        deliveryNoList.forEach(deliveryNo -> {
        log.info("【GetSaveDetail】获取到{}条送货单数据", deliveryNoList.size());
        // 分批处理数据,减少内存占用
        int batchSize = 10; // 每批处理10条数据
        int totalSize = deliveryNoList.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("【GetSaveDetail】处理第{}批送货单数据,范围:{}-{}", i + 1, fromIndex, toIndex);
            // 获取当前批次的数据
            List<XkyEntity> batchDeliveries = deliveryNoList.subList(fromIndex, toIndex);
            // 处理当前批次的数据
            processBatchDeliveries(batchDeliveries);
        }
        log.info("【GetSaveDetail】所有送货单处理完成");
    }
    /**
     * 批量处理送货单数据
     *
     * @param batchDeliveries 当前批次的送货单数据
     */
    private void processBatchDeliveries(List<XkyEntity> batchDeliveries) {
        batchDeliveries.forEach(deliveryNo -> {
            try {
                if ("6".equals(deliveryNo.getStatus()) || "0".equals(deliveryNo.getLogisticsStatus())) {
                    remove(deliveryNo);
                } else if ("1".equals(deliveryNo.getStatus()) && "2".equals(deliveryNo.getLogisticsStatus())) {
                    log.info("【processBatchDeliveries】移除送货单: {}", deliveryNo.getDeliveryNo());
                    remove1(deliveryNo);
                } else if ("1".equals(deliveryNo.getStatus()) && ("2".equals(deliveryNo.getLogisticsStatus()) || "1".equals(deliveryNo.getLogisticsStatus()))) {
                    log.info("【processBatchDeliveries】处理送货单: {}, 物流状态: {}", deliveryNo.getDeliveryNo(), deliveryNo.getLogisticsStatus());
                    XkyDetail detail = getDetail(deliveryNo.getDeliveryNo());
                    deliveryNoticeService.saveDeliveryNotice(detail);
                    List<BarcodeDeliveryNo> barcodeDeliveryNos = GetBarcodeInformation(deliveryNo.getDeliveryNo());
                    barcodeInformationService.SaveBarcodeInformation(barcodeDeliveryNos, deliveryNo.getDeliveryNo());
                    deliveryNoticeService.callPdaReceiptBtn("送货单签收[BTNOK[PL017[" + deliveryNo.getDeliveryNo(), "");
                    //已送达的才自动转换为MES到货单
                    if ("2".equals(deliveryNo.getLogisticsStatus())) {
                        log.info("【processBatchDeliveries】送货单已送达,执行签收: {}", deliveryNo.getDeliveryNo());
                        deliveryNoticeService.callPdaReceiptBtn("送货单签收[BTNOK[PL017[" + deliveryNo.getDeliveryNo(), "");
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
                log.error("【processBatchDeliveries】处理送货单异常: {}, 异常: {}", deliveryNo.getDeliveryNo(), e.getMessage(), e);
                // 不抛出异常,避免一个送货单的异常导致整个批次失败
            } catch (Exception e) {
                log.error("【processBatchDeliveries】处理送货单未预期异常: {}, 异常: {}", deliveryNo.getDeliveryNo(), e.getMessage(), e);
                // 不抛出异常,避免一个送货单的异常导致整个批次失败
            }
        });
    }
    public void remove1(XkyEntity deliveryNo) {
        //MesQaItemsDetect01与MesInvItemArnDetail的关联关系是
        //MesQaItemsDetect01.lotNo=MesInvItemArnDetail.billNo
        //and MesQaItemsDetect01.item_id=MesInvItemArnDetail.item_id
        //我现在希望不清空DeliveryNotice,DeliveryNoticeDetail,TblBarcodeInformation表的数据了
        //还有MesInvItemArn,MesInvItemArnDetail,MesInvItemBarcodes表的数据
        //只删除MesQaItemsDetect01表的数据
        LambdaQueryWrapper<MesInvItemArn> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MesInvItemArn::getPaperBillNo, deliveryNo.getDeliveryNo());
        MesInvItemArn mesInvItemArn = mesInvItemArnService.getOne(wrapper, false);
        // 只有当找到对应的MesInvItemArn记录时才删除MesQaItemsDetect01表的数据
        if (mesInvItemArn != null) {
            // 更新MesInvItemArn表,设置syncFlag=1,syncMsg="SRM手动删除送货单"
            LambdaUpdateWrapper<MesInvItemArn> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(MesInvItemArn::getId, mesInvItemArn.getId())
                    .set(MesInvItemArn::getSyncFlag, 1)
                    .set(MesInvItemArn::getStatus, 1)
                    .set(MesInvItemArn::getSyncMsg, "SRM手动删除送货单");
            mesInvItemArnService.update(updateWrapper);
            //MesInvItemArnDetail
            LambdaUpdateWrapper<MesInvItemArnDetail> deleteWrapper = new LambdaUpdateWrapper<>();
            deleteWrapper.eq(MesInvItemArnDetail::getMid, mesInvItemArn.getId())
                    .set(MesInvItemArnDetail::getReturnFlag, 1);
            mesInvItemArnDetailService.update(deleteWrapper);
            // 删除MesQaItemsDetect01表的数据
            mesQaItemsDetect01Service.removeQa(mesInvItemArn.getBillNo());
        }
    }
    private XkyDetail getDetail(String deliveryNo) throws IOException {
        XkyCommonParam<BodyParam> param = XkyCommonParam.GetInit();
        // 创建 BodyParam 对象并赋值
        BodyParam bodyParam = new BodyParam();
        bodyParam.setErpCode(DataAcquisitionConfiguration.TEST_ERP_CODE);
        bodyParam.setDeliveryNo(deliveryNo);
        param.setBody(bodyParam);
        ApiResponse<XkyDetail> noList = apiService.sendListRequest(param, XkyDetail.class, "https://openapi.xiekeyun.com/delivery/getDetail.json");
        return noList.getData();
    }
    private List<BarcodeDeliveryNo> GetBarcodeInformation(String deliveryNo) throws IOException {
        XkyCommonParam<BodyParam> param = XkyCommonParam.GetInit();
        // 创建 BodyParam 对象并赋值
        BodyParam bodyParam = new BodyParam();
        bodyParam.setErpCode(DataAcquisitionConfiguration.TEST_ERP_CODE);
        bodyParam.setDeliveryNo(deliveryNo);
        param.setBody(bodyParam);
        ApiResponse<BarcodeDeliveryNo> noList = apiService.sendListRequest(param, BarcodeDeliveryNo.class, "https://openapi.xiekeyun.com/barcode/byDeliveryNo.json");
        return noList.getDataList();
    }
    private void remove(XkyEntity deliveryNo) {
@@ -130,39 +234,5 @@
                mesQaItemsDetect01Service.removeQa(mesInvItemArn.getBillNo());
            }
        }
    }
    private XkyDetail getDetail(String deliveryNo) throws IOException {
        XkyCommonParam param = XkyCommonParam.GetInit();
        // 创建 BodyParam 对象并赋值
        BodyParam bodyParam = new BodyParam();
        bodyParam.setErpCode(DataAcquisitionConfiguration.TEST_ERP_CODE);
        bodyParam.setDeliveryNo(deliveryNo);
        param.setBody(bodyParam);
        ApiResponse<XkyDetail> noList = apiService.sendListRequest(param, XkyDetail.class, "https://openapi.xiekeyun.com/delivery/getDetail.json");
        return noList.getData();
    }
    private List<BarcodeDeliveryNo> GetBarcodeInformation(String deliveryNo) throws IOException {
        XkyCommonParam param = XkyCommonParam.GetInit();
        // 创建 BodyParam 对象并赋值
        BodyParam bodyParam = new BodyParam();
        bodyParam.setErpCode(DataAcquisitionConfiguration.TEST_ERP_CODE);
        bodyParam.setDeliveryNo(deliveryNo);
        param.setBody(bodyParam);
        ApiResponse<BarcodeDeliveryNo> noList = apiService.sendListRequest(param, BarcodeDeliveryNo.class, "https://openapi.xiekeyun.com/barcode/byDeliveryNo.json");
        return noList.getDataList();
    }
}