wujianwei
2026-03-27 3078ed5432eb05d74dbd91bda2c7bfe6d2faa5f4
tms/src/main/java/com/ruoyi/tms/service/impl/TmsPayableFeeServiceImpl.java
@@ -6,6 +6,7 @@
import java.util.List;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.SystemDataNoEnum;
@@ -18,11 +19,19 @@
import com.ruoyi.tms.domain.*;
import com.ruoyi.tms.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.client.RestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.annotation.DataSource;
@@ -60,7 +69,10 @@
    ISysConfigService sysConfigService;
    @Autowired
    ISystemDataNoService systemDataNoService;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${custom.cwxtApi.url}")
    private String url;
    /**
     * 查询应付费用
     *
@@ -79,6 +91,28 @@
        );
        tmsPayableFee.setPayableFeeItems(tmsPayableFeeItems);
        return tmsPayableFee;
    }
    @DataSource(DataSourceType.SLAVE)
    @Override
    public List<TmsPayableFee> selectTmsPayableFeeByDispatchNo(String dispatchNo)
    {
        List<TmsPayableFee> tmsPayableFeeList = tmsPayableFeeMapper.selectList(new LambdaQueryWrapper<TmsPayableFee>()
                .eq(TmsPayableFee::getDispatchNo, dispatchNo)
                .ne(TmsPayableFee::getStatus, 2)
        );
        if (tmsPayableFeeList != null && !tmsPayableFeeList.isEmpty()){
            tmsPayableFeeList.forEach(tmsPayableFee -> {
                List<TmsPayableFeeItem> tmsPayableFeeItems = tmsPayableFeeItemMapper.selectTmsPayableFeeItemList(
                        new TmsPayableFeeItem() {{
                            setHeadId(tmsPayableFee.getId());
                        }}
                );
                tmsPayableFee.setPayableFeeItems(tmsPayableFeeItems);
            });
        }
        return tmsPayableFeeList;
    }
    /**
@@ -289,8 +323,110 @@
        if (!rmb.isEmpty()){
            tmsApBillItemMapper.insertTmsApBillItemBatch(rmb);
        }
        //todo 向外部系统推送数据
        pushPayableToExternalSystem(tmsApBill, tmsPayableFeeList);
        return AjaxResult.success();
    }
    /**
     * 向外部系统推送应付数据
     * @param tmsApBill 应付账单
     * @param tmsPayableFeeList 应付费用列表
     */
    @Async
    protected void pushPayableToExternalSystem(TmsApBill tmsApBill, List<TmsPayableFee> tmsPayableFeeList) {
        java.util.Map<String, Object> requestBody = new java.util.HashMap<>();
        try {
            // 构建请求体
            String apiUrl = url+"/addPayableBill";
            // 构建bill部分
            java.util.Map<String, Object> billMap = new java.util.HashMap<>();
            billMap.put("billName", tmsApBill.getBillName());
            billMap.put("supplierName", tmsApBill.getServiceProviderName());
            billMap.put("totalAmount", tmsApBill.getSettleAmount());
            billMap.put("currency", "RMB");
            billMap.put("status", "draft");
            billMap.put("isInternalSettlement", "0");
            billMap.put("internalSettlementUnit", tmsApBill.getServiceProviderName());
            billMap.put("documentCount", tmsPayableFeeList.size());
            billMap.put("discountAmount", 0.00);
            billMap.put("paidAmount", 0.00);
            billMap.put("pendingAmount", tmsApBill.getSettleAmount());
            billMap.put("exchangeRate", tmsApBill.getSettleRate());
            billMap.put("cnyAmount", tmsApBill.getSettleAmount());
            billMap.put("periodType", "");
            billMap.put("businessStartDate", "");
            billMap.put("businessEndDate", "");
            billMap.put("billingStartDate", "");
            billMap.put("billingEndDate", "");
            billMap.put("billGenerateDate", "");
            billMap.put("billSendDate", "");
            billMap.put("billDueDate", "");
            billMap.put("remark", "");
            // 构建fees部分
            List<java.util.Map<String, Object>> feesList = new java.util.ArrayList<>();
            for (int i = 0; i < tmsPayableFeeList.size(); i++) {
                TmsPayableFee fee = tmsPayableFeeList.get(i);
                java.util.Map<String, Object> feeMap = new java.util.HashMap<>();
                feeMap.put("sourceSystem", "TMS");
                feeMap.put("documentNo", fee.getDispatchNo() != null ? fee.getDispatchNo() : "");
                feeMap.put("supplierName", tmsApBill.getServiceProviderName());
                feeMap.put("payableAmount", fee.getPayableRmbSumAmount());
                feeMap.put("serialNumber", String.format("%03d", i + 1));
                feeMap.put("relatedBillNo", "");
                feeMap.put("businessSector", "");
                feeMap.put("documentType", "");
                feeMap.put("isInternalSettlement", "0");
                feeMap.put("internalSettlementUnit", "");
                feeMap.put("projectName", fee.getProjectName() != null ? fee.getProjectName() : "");
                feeMap.put("businessTime", fee.getDispatchConfirmTime());
                feeMap.put("payableConfirmTime", fee.getDispatchConfirmTime());
                feeMap.put("status", "confirmed");
                feeMap.put("remark", "");
                // 构建feeDetails部分
                List<java.util.Map<String, Object>> feeDetailsList = new java.util.ArrayList<>();
                java.util.Map<String, Object> feeDetailMap = new java.util.HashMap<>();
                feeDetailMap.put("feeType", "运输费");
                feeDetailMap.put("feeName", "干线运输费");
                feeDetailMap.put("unitPrice", 1200.00);
                feeDetailMap.put("billingQuantity", 5.00);
                feeDetailMap.put("billingAmount", fee.getPayableRmbSumAmount());
                feeDetailMap.put("serialNumber", String.format("%03d-01", i + 1));
                feeDetailMap.put("billingUnit", "车次");
                feeDetailMap.put("actualAmount", fee.getPayableRmbSumAmount());
                feeDetailMap.put("currency", "RMB");
                feeDetailMap.put("feeRegTime", "");
                feeDetailMap.put("remark", "");
                feeDetailsList.add(feeDetailMap);
                feeMap.put("feeDetails", feeDetailsList);
                feesList.add(feeMap);
            }
            // 构建完整请求体
            requestBody.put("bill", billMap);
            requestBody.put("fees", feesList);
            // 设置HTTP头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(requestBody), headers);
            // 发送API请求
            ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class);
            logger.info("推送应付数据到外部系统成功,响应: {}", response.getBody());
        } catch (Exception e) {
            logger.error("推送应付数据到外部系统失败,账单ID: {}, 供应商: {}",
                tmsApBill.getId(), tmsApBill.getServiceProviderName(), e);
            logger.debug("推送失败的请求数据: {}", JSON.toJSONString(requestBody));
            // 推送失败不影响主流程,记录日志即可
        }
    }
    /**
     * 删除应付费用信息