refactor(report): 优化销售报表查询SQL
- 修复销售员统计数据中子查询的条件引用问题 - 统一SQL查询格式化,提升代码可读性 - 优化销售员占比计算的子查询结构 - 添加完整的查询条件到销售员统计子查询中 - 保持所有现有功能逻辑不变
This commit is contained in:
@@ -148,16 +148,91 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
<!-- 查询销售员统计数据 -->
|
||||
<select id="selectSalesmanStats" resultType="com.klp.crm.domain.vo.CrmSalesReportVo$SalesmanStat">
|
||||
SELECT
|
||||
o.salesman,
|
||||
COUNT(o.order_id) as orderCount,
|
||||
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
|
||||
LEFT JOIN crm_customer c2 ON o2.customer_id = c2.customer_id
|
||||
<include refid="selectCondition"/>), 0),
|
||||
2
|
||||
) as percentage
|
||||
o.salesman,
|
||||
COUNT(o.order_id) as orderCount,
|
||||
COALESCE(SUM(o.order_amount), 0) as salesAmount,
|
||||
ROUND(
|
||||
COALESCE(SUM(o.order_amount), 0) * 100.0 /
|
||||
NULLIF((
|
||||
SELECT SUM(o2.order_amount)
|
||||
FROM crm_order o2
|
||||
LEFT JOIN crm_customer c2 ON o2.customer_id = c2.customer_id
|
||||
<!-- 子查询使用独立的条件片段,避免引用外层o/c -->
|
||||
<where>
|
||||
o2.del_flag = 0 AND c2.del_flag = 0
|
||||
<if test="bo.startTime != null">
|
||||
AND DATE(o2.create_time) >= #{bo.startTime}
|
||||
</if>
|
||||
<if test="bo.endTime != null">
|
||||
AND DATE(o2.create_time) <= #{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 >= #{bo.minOrderAmount}
|
||||
</if>
|
||||
<if test="bo.maxOrderAmount != null">
|
||||
AND o2.order_amount <= #{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
|
||||
LEFT JOIN crm_customer c ON o.customer_id = c.customer_id
|
||||
<include refid="selectCondition"/>
|
||||
|
||||
Reference in New Issue
Block a user