liyong
2026-07-03 bee96a1d36c86068cd5a7eb69f4e3294a8123b04
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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<CrmSaleTraceRespDTO> 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<CrmSaleTraceRespDTO> 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<List<CrmSaleTraceRespDTO>> getTraceListByCustomerId(Long customerId) {
        CrmCustomerDO customer = customerService.getCustomer(customerId);
        if (customer == null) {
            return success(new ArrayList<>());
        }
 
        // 查询客户所有合同
        List<CrmContractDO> contracts = contractMapper.selectListByCustomerId(customerId);
        if (CollUtil.isEmpty(contracts)) {
            return success(new ArrayList<>());
        }
 
        List<CrmSaleTraceRespDTO> 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());
        }
    }
 
}