wujianwei
2026-03-30 34669fa71bd2cc4a4ff57d7d2440a95da998b6b5
tms/src/main/java/com/ruoyi/tms/service/impl/TmsReceivableFeeServiceImpl.java
@@ -18,11 +18,21 @@
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.core.task.AsyncTaskExecutor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
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;
@@ -57,6 +67,12 @@
    private TmsArBillMapper tmsArBillMapper;
    @Resource
    private TmsArBillItemMapper tmsArBillItemMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${custom.cwxtApi.url}")
    private String url;
    /**
     * 查询应收费用
@@ -74,6 +90,24 @@
        }});
        tmsReceivableFee.setItems(tmsReceivableFeeItems);
        return tmsReceivableFee;
    }
    @Override
    public TmsReceivableFee selectTmsReceivableFeeByDispatchNo(String dispatchNo) {
        TmsReceivableFee tmsReceivableFee = tmsReceivableFeeMapper.selectOne(new LambdaUpdateWrapper<TmsReceivableFee>()
                .eq(TmsReceivableFee::getDispatchNo, dispatchNo)
                .ne(TmsReceivableFee::getStatus, 2)
                .last("limit 1")
        );
        if(tmsReceivableFee != null){
            List<TmsReceivableFeeItem> tmsReceivableFeeItems = tmsReceivableFeeItemMapper.selectTmsReceivableFeeItemList(new TmsReceivableFeeItem() {{
                setHeadId(tmsReceivableFee.getId());
            }});
            tmsReceivableFee.setItems(tmsReceivableFeeItems);
            return tmsReceivableFee;
        }
        return null;
    }
    /**
@@ -239,7 +273,10 @@
        tmsArBill.setSettleAmount(BigDecimal.ZERO);
        tmsReceivableFees.forEach(item ->{
            List<TmsReceivableFeeItem> tmsReceivableFeeItems = tmsReceivableFeeItemMapper.selectTmsReceivableFeeItemList(new TmsReceivableFeeItem() {{
                setHeadId(item.getId());
            }});
            item.setItems(tmsReceivableFeeItems);
            BigDecimal rmbAmount = item.getReceivableHKBAmount()
                    .multiply(exchangeRate)
                    .setScale(2, RoundingMode.HALF_UP);
@@ -248,6 +285,7 @@
        });
        tmsArBill.setTmsReceivableFees(tmsReceivableFees);
        tmsArBill.setActualSettlementAmount(tmsArBill.getSettleAmount());
        return tmsArBill;
@@ -268,6 +306,8 @@
        tmsReceivableFeeMapper.update(new LambdaUpdateWrapper<TmsReceivableFee>()
                .set(TmsReceivableFee::getStatus,1)
                        .set( TmsReceivableFee::getBillRelationId,tmsArBill.getId())
                 .set(TmsReceivableFee::getBillRelationNo ,tmsArBill.getSystemNo())
                .in(TmsReceivableFee::getId, ids)
        );
@@ -287,8 +327,138 @@
            return billItem;
        }).collect(Collectors.toList());
        tmsArBillItemMapper.insertTmsArBillItemBatch(rmb);
        // 向外部系统推送数据
        AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();
        executor.execute(() -> pushToExternalSystem(tmsArBill, tmsReceivableFees));
        return AjaxResult.success();
    }
    /**
     * 向外部系统推送数据
     * @param tmsArBill 应收账单
     * @param tmsReceivableFees 应收费用列表
     */
    @Async
    protected void pushToExternalSystem(TmsArBill tmsArBill, List<TmsReceivableFee> tmsReceivableFees) {
        java.util.Map<String, Object> requestBody = new java.util.HashMap<>();
        try {
            // 构建请求体
            String apiUrl = url+"/addBill";
            // 构建bill部分
            java.util.Map<String, Object> billMap = new java.util.HashMap<>();
            billMap.put("billName", tmsArBill.getBillName());
            billMap.put("customerName", tmsArBill.getCustomerName());
            billMap.put("payee", "");
            billMap.put("responsiblePerson", "");
            billMap.put("responsibleLeader", "");
            billMap.put("settlementMethod", "");
            billMap.put("businessType", "");
            billMap.put("promotionRequirement", "");
            billMap.put("isInternalSettlement", "0");
            billMap.put("internalSettlementUnit", "");
            billMap.put("documentCount", tmsReceivableFees.size());
            billMap.put("totalAmount", tmsArBill.getSettleAmount());
            billMap.put("currency", "RMB");
            billMap.put("discountAmount", 0.00);
            billMap.put("receivedAmount", 0.00);
            billMap.put("pendingAmount", tmsArBill.getSettleAmount());
            billMap.put("exchangeRate", tmsArBill.getSettleRate());
            billMap.put("cnyAmount", tmsArBill.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("settlementCategory", "");
            billMap.put("settlementPeriod", "");
            billMap.put("status", "0");
            billMap.put("remark", "");
            // 构建fees部分
            List<java.util.Map<String, Object>> feesList = new java.util.ArrayList<>();
            for (int i = 0; i < tmsReceivableFees.size(); i++) {
                TmsReceivableFee fee = tmsReceivableFees.get(i);
                java.util.Map<String, Object> feeMap = new java.util.HashMap<>();
                feeMap.put("serialNumber", String.format("%03d", i + 1));
                feeMap.put("relatedBillNo", "");
                feeMap.put("sourceSystem", "TMS");
                feeMap.put("businessSector", "0");
                feeMap.put("documentType", "0");
                feeMap.put("documentNo", fee.getDispatchNo() != null ? fee.getDispatchNo() : "");
                feeMap.put("isInternalSettlement", "0");
                feeMap.put("internalSettlementUnit", "");
                feeMap.put("customerName", tmsArBill.getCustomerName());
                feeMap.put("projectName", fee.getProjectName() != null ? fee.getProjectName() : "");
                feeMap.put("businessTime", fee.getDispatchConfirmTime());
                feeMap.put("receivableConfirmTime", fee.getDispatchConfirmTime());
                feeMap.put("receivableAmount", fee.getReceivableRMBAmount().add(fee.getReceivableHKBAmount()));
                // 构建receivableAmountStr
                BigDecimal rmbAmount = fee.getReceivableRMBAmount();
                BigDecimal hkbAmount = fee.getReceivableHKBAmount();
                StringBuilder amountStr = new StringBuilder();
                if (rmbAmount.compareTo(BigDecimal.ZERO) > 0) {
                    amountStr.append(rmbAmount).append("人民币");
                }
                if (hkbAmount.compareTo(BigDecimal.ZERO) > 0) {
                    if (amountStr.length() > 0) {
                        amountStr.append(" ");
                    }
                    amountStr.append(hkbAmount).append("港币");
                }
                feeMap.put("receivableAmountStr", amountStr.toString());
                feeMap.put("status", "1");
                feeMap.put("remark", "");
                // 构建feeDetails部分
                List<java.util.Map<String, Object>> feeDetailsList = new java.util.ArrayList<>();
                List<TmsReceivableFeeItem> items = fee.getItems();
                for (int j = 0; j < items.size(); j++) {
                    TmsReceivableFeeItem item = items.get(j);
                    java.util.Map<String, Object> feeDetailMap = new java.util.HashMap<>();
                    feeDetailMap.put("serialNumber", String.format("%03d", j + 1));
                    feeDetailMap.put("feeType", item.getFeeType());
                    feeDetailMap.put("feeName", item.getFeeName());
                    feeDetailMap.put("billingUnit", "次");
                    feeDetailMap.put("unitPrice", item.getRegisterAmount());
                    feeDetailMap.put("billingQuantity", item.getRegisterAmount());
                    feeDetailMap.put("billingAmount", item.getRegisterAmount());
                    feeDetailMap.put("actualAmount", item.getRegisterAmount());
                    feeDetailMap.put("currency", item.getCurrency());
                    feeDetailMap.put("feeRegTime", item.getRegisterTime());
                    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: {}, 客户: {}",
                tmsArBill.getId(), tmsArBill.getCustomerName(), e);
            logger.debug("推送失败的请求数据: {}", JSON.toJSONString(requestBody));
            // 推送失败不影响主流程,记录日志即可
        }
    }
    /**
     * 删除应收费用信息