refactor(report): 优化销售报表查询SQL

- 修复销售员统计数据中子查询的条件引用问题
- 统一SQL查询格式化,提升代码可读性
- 优化销售员占比计算的子查询结构
- 添加完整的查询条件到销售员统计子查询中
- 保持所有现有功能逻辑不变
This commit is contained in:
2025-12-29 13:04:07 +08:00
parent 76616f0f65
commit 911dcb9684

View File

@@ -153,9 +153,84 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
COALESCE(SUM(o.order_amount), 0) as salesAmount,
ROUND(
COALESCE(SUM(o.order_amount), 0) * 100.0 /
NULLIF((SELECT SUM(order_amount) FROM crm_order o2
NULLIF((
SELECT SUM(o2.order_amount)
FROM crm_order o2
LEFT JOIN crm_customer c2 ON o2.customer_id = c2.customer_id
<include refid="selectCondition"/>), 0),
<!-- 子查询使用独立的条件片段避免引用外层o/c -->
<where>
o2.del_flag = 0 AND c2.del_flag = 0
<if test="bo.startTime != null">
AND DATE(o2.create_time) &gt;= #{bo.startTime}
</if>
<if test="bo.endTime != null">
AND DATE(o2.create_time) &lt;= #{bo.endTime}
</if>
<if test="bo.salesmanList != null and bo.salesmanList.size() > 0">
AND o2.salesman IN
<foreach collection="bo.salesmanList" item="salesman" open="(" separator="," close=")">
#{salesman}
</foreach>
</if>
<if test="bo.customerIdList != null and bo.customerIdList.size() > 0">
AND o2.customer_id IN
<foreach collection="bo.customerIdList" item="customerId" open="(" separator="," close=")">
#{customerId}
</foreach>
</if>
<if test="bo.customerLevelList != null and bo.customerLevelList.size() > 0">
AND c2.customer_level IN
<foreach collection="bo.customerLevelList" item="level" open="(" separator="," close=")">
#{level}
</foreach>
</if>
<if test="bo.industryList != null and bo.industryList.size() > 0">
AND c2.industry IN
<foreach collection="bo.industryList" item="industry" open="(" separator="," close=")">
#{industry}
</foreach>
</if>
<if test="bo.orderStatusList != null and bo.orderStatusList.size() > 0">
AND o2.order_status IN
<foreach collection="bo.orderStatusList" item="status" open="(" separator="," close=")">
#{status}
</foreach>
</if>
<if test="bo.financeStatusList != null and bo.financeStatusList.size() > 0">
AND o2.finance_status IN
<foreach collection="bo.financeStatusList" item="status" open="(" separator="," close=")">
#{status}
</foreach>
</if>
<if test="bo.orderTypeList != null and bo.orderTypeList.size() > 0">
AND o2.order_type IN
<foreach collection="bo.orderTypeList" item="type" open="(" separator="," close=")">
#{type}
</foreach>
</if>
<if test="bo.minOrderAmount != null">
AND o2.order_amount &gt;= #{bo.minOrderAmount}
</if>
<if test="bo.maxOrderAmount != null">
AND o2.order_amount &lt;= #{bo.maxOrderAmount}
</if>
<if test="bo.onlyUnpaidOrders != null and bo.onlyUnpaidOrders == true">
AND o2.unpaid_amount > 0
</if>
<if test="bo.companyNameKeyword != null and bo.companyNameKeyword != ''">
AND c2.company_name LIKE CONCAT('%', #{bo.companyNameKeyword}, '%')
</if>
<if test="bo.orderCodeKeyword != null and bo.orderCodeKeyword != ''">
AND o2.order_code LIKE CONCAT('%', #{bo.orderCodeKeyword}, '%')
</if>
<if test="bo.includeObjectionOrders != null and bo.includeObjectionOrders == false">
AND NOT EXISTS (
SELECT 1 FROM crm_sales_objection obj
WHERE obj.order_id = o2.order_id AND obj.del_flag = 0
)
</if>
</where>
), 0),
2
) as percentage
FROM crm_order o