package cn.iocoder.yudao.module.crm.api.trace; import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.module.crm.api.trace.dto.CrmSaleTraceRespDTO; import cn.iocoder.yudao.module.crm.dal.dataobject.business.CrmBusinessDO; import cn.iocoder.yudao.module.crm.dal.dataobject.contract.CrmContractDO; import cn.iocoder.yudao.module.crm.dal.dataobject.customer.CrmCustomerDO; import cn.iocoder.yudao.module.crm.dal.mysql.business.CrmBusinessMapper; import cn.iocoder.yudao.module.crm.dal.mysql.contract.CrmContractMapper; import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService; import cn.iocoder.yudao.module.crm.service.receivable.CrmReceivableService; import cn.iocoder.yudao.module.erp.api.sale.ErpSaleOrderApi; import cn.iocoder.yudao.module.erp.api.sale.dto.ErpSaleOrderRespDTO; import jakarta.annotation.Resource; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; /** * CRM 销售追溯 API 实现类 * * @author 芋道源码 */ @RestController @RequestMapping(CrmSaleTraceApi.PREFIX) @Validated public class CrmSaleTraceApiImpl implements CrmSaleTraceApi { @Resource private CrmBusinessMapper businessMapper; @Resource private CrmContractMapper contractMapper; @Resource private CrmCustomerService customerService; @Resource private CrmReceivableService receivableService; @Resource private ErpSaleOrderApi saleOrderApi; @Override public CommonResult getTraceByBusinessId(Long businessId) { CrmBusinessDO business = businessMapper.selectById(businessId); if (business == null) { return success(null); } CrmSaleTraceRespDTO trace = buildBaseTrace(business.getCustomerId()); // 填充商机信息 trace.setBusinessId(business.getId()); trace.setBusinessName(business.getName()); trace.setBusinessPrice(business.getTotalPrice()); trace.setBusinessCreateTime(business.getCreateTime()); // 查询关联合同 CrmContractDO contract = contractMapper.selectByBusinessId(businessId); if (contract != null) { fillContractInfo(trace, contract); // 查询关联订单 if (contract.getOrderId() != null) { fillOrderInfo(trace, contract.getOrderId()); } } return success(trace); } @Override public CommonResult getTraceByContractId(Long contractId) { CrmContractDO contract = contractMapper.selectById(contractId); if (contract == null) { return success(null); } CrmSaleTraceRespDTO trace = buildBaseTrace(contract.getCustomerId()); // 填充合同信息 fillContractInfo(trace, contract); // 填充商机信息 if (contract.getBusinessId() != null) { CrmBusinessDO business = businessMapper.selectById(contract.getBusinessId()); if (business != null) { trace.setBusinessId(business.getId()); trace.setBusinessName(business.getName()); trace.setBusinessPrice(business.getTotalPrice()); trace.setBusinessCreateTime(business.getCreateTime()); } } // 查询关联订单 if (contract.getOrderId() != null) { fillOrderInfo(trace, contract.getOrderId()); } return success(trace); } @Override public CommonResult> getTraceListByCustomerId(Long customerId) { CrmCustomerDO customer = customerService.getCustomer(customerId); if (customer == null) { return success(new ArrayList<>()); } // 查询客户所有合同 List contracts = contractMapper.selectListByCustomerId(customerId); if (CollUtil.isEmpty(contracts)) { return success(new ArrayList<>()); } List traces = new ArrayList<>(); for (CrmContractDO contract : contracts) { CrmSaleTraceRespDTO trace = new CrmSaleTraceRespDTO(); trace.setCustomerId(customerId); trace.setCustomerName(customer.getName()); fillContractInfo(trace, contract); // 填充商机信息 if (contract.getBusinessId() != null) { CrmBusinessDO business = businessMapper.selectById(contract.getBusinessId()); if (business != null) { trace.setBusinessId(business.getId()); trace.setBusinessName(business.getName()); trace.setBusinessPrice(business.getTotalPrice()); trace.setBusinessCreateTime(business.getCreateTime()); } } // 查询关联订单 if (contract.getOrderId() != null) { fillOrderInfo(trace, contract.getOrderId()); } traces.add(trace); } return success(traces); } /** * 构建基础追溯信息 */ private CrmSaleTraceRespDTO buildBaseTrace(Long customerId) { CrmSaleTraceRespDTO trace = new CrmSaleTraceRespDTO(); if (customerId != null) { CrmCustomerDO customer = customerService.getCustomer(customerId); if (customer != null) { trace.setCustomerId(customerId); trace.setCustomerName(customer.getName()); } } return trace; } /** * 填充合同信息 */ private void fillContractInfo(CrmSaleTraceRespDTO trace, CrmContractDO contract) { trace.setContractId(contract.getId()); trace.setContractName(contract.getName()); trace.setContractPrice(contract.getTotalPrice()); trace.setContractStatus(contract.getAuditStatus()); trace.setContractCreateTime(contract.getCreateTime()); // 计算回款金额 BigDecimal receivablePrice = receivableService.getReceivablePriceByContractId(contract.getId()); trace.setReceivablePrice(receivablePrice != null ? receivablePrice : BigDecimal.ZERO); } /** * 填充订单信息 */ private void fillOrderInfo(CrmSaleTraceRespDTO trace, Long orderId) { ErpSaleOrderRespDTO order = saleOrderApi.getSaleOrder(orderId).getCheckedData(); if (order != null) { trace.setOrderId(orderId); trace.setOrderNo(order.getNo()); trace.setOrderPrice(order.getTotalPrice()); trace.setOrderStatus(order.getStatus()); } } }