refactor(report): 优化销售报表查询SQL
- 修复销售员统计数据中子查询的条件引用问题 - 统一SQL查询格式化,提升代码可读性 - 优化销售员占比计算的子查询结构 - 添加完整的查询条件到销售员统计子查询中 - 保持所有现有功能逻辑不变
This commit is contained in:
@@ -73,7 +73,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
</if>
|
</if>
|
||||||
<if test="bo.includeObjectionOrders != null and bo.includeObjectionOrders == false">
|
<if test="bo.includeObjectionOrders != null and bo.includeObjectionOrders == false">
|
||||||
AND NOT EXISTS (
|
AND NOT EXISTS (
|
||||||
SELECT 1 FROM crm_sales_objection obj
|
SELECT 1 FROM crm_sales_objection obj
|
||||||
WHERE obj.order_id = o.order_id AND obj.del_flag = 0
|
WHERE obj.order_id = o.order_id AND obj.del_flag = 0
|
||||||
)
|
)
|
||||||
</if>
|
</if>
|
||||||
@@ -82,7 +82,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
|
|
||||||
<!-- 查询销售汇总统计数据 -->
|
<!-- 查询销售汇总统计数据 -->
|
||||||
<select id="selectSalesSummary" resultType="com.klp.crm.domain.vo.CrmSalesReportVo$SalesSummary">
|
<select id="selectSalesSummary" resultType="com.klp.crm.domain.vo.CrmSalesReportVo$SalesSummary">
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(o.order_id) as totalOrderCount,
|
COUNT(o.order_id) as totalOrderCount,
|
||||||
COALESCE(SUM(o.order_amount), 0) as totalSalesAmount,
|
COALESCE(SUM(o.order_amount), 0) as totalSalesAmount,
|
||||||
COUNT(CASE WHEN o.order_status = 3 THEN 1 END) as completedOrderCount,
|
COUNT(CASE WHEN o.order_status = 3 THEN 1 END) as completedOrderCount,
|
||||||
@@ -96,7 +96,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
|
|
||||||
<!-- 查询订单明细列表 -->
|
<!-- 查询订单明细列表 -->
|
||||||
<select id="selectOrderDetailList" resultType="com.klp.crm.domain.vo.CrmSalesReportVo$OrderDetail">
|
<select id="selectOrderDetailList" resultType="com.klp.crm.domain.vo.CrmSalesReportVo$OrderDetail">
|
||||||
SELECT
|
SELECT
|
||||||
o.order_id,
|
o.order_id,
|
||||||
o.order_code,
|
o.order_code,
|
||||||
c.customer_code,
|
c.customer_code,
|
||||||
@@ -116,19 +116,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
FROM crm_order o
|
FROM crm_order o
|
||||||
LEFT JOIN crm_customer c ON o.customer_id = c.customer_id
|
LEFT JOIN crm_customer c ON o.customer_id = c.customer_id
|
||||||
LEFT JOIN (
|
LEFT JOIN (
|
||||||
SELECT
|
SELECT
|
||||||
order_id,
|
order_id,
|
||||||
COUNT(*) as item_count
|
COUNT(*) as item_count
|
||||||
FROM crm_order_item
|
FROM crm_order_item
|
||||||
WHERE del_flag = 0
|
WHERE del_flag = 0
|
||||||
GROUP BY order_id
|
GROUP BY order_id
|
||||||
) item_stats ON o.order_id = item_stats.order_id
|
) item_stats ON o.order_id = item_stats.order_id
|
||||||
LEFT JOIN (
|
LEFT JOIN (
|
||||||
SELECT
|
SELECT
|
||||||
order_id,
|
order_id,
|
||||||
COUNT(*) as objection_count
|
COUNT(*) as objection_count
|
||||||
FROM crm_sales_objection
|
FROM crm_sales_objection
|
||||||
WHERE del_flag = 0
|
WHERE del_flag = 0
|
||||||
GROUP BY order_id
|
GROUP BY order_id
|
||||||
) obj_stats ON o.order_id = obj_stats.order_id
|
) obj_stats ON o.order_id = obj_stats.order_id
|
||||||
<include refid="selectCondition"/>
|
<include refid="selectCondition"/>
|
||||||
@@ -147,17 +147,92 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
|
|
||||||
<!-- 查询销售员统计数据 -->
|
<!-- 查询销售员统计数据 -->
|
||||||
<select id="selectSalesmanStats" resultType="com.klp.crm.domain.vo.CrmSalesReportVo$SalesmanStat">
|
<select id="selectSalesmanStats" resultType="com.klp.crm.domain.vo.CrmSalesReportVo$SalesmanStat">
|
||||||
SELECT
|
SELECT
|
||||||
o.salesman,
|
o.salesman,
|
||||||
COUNT(o.order_id) as orderCount,
|
COUNT(o.order_id) as orderCount,
|
||||||
COALESCE(SUM(o.order_amount), 0) as salesAmount,
|
COALESCE(SUM(o.order_amount), 0) as salesAmount,
|
||||||
ROUND(
|
ROUND(
|
||||||
COALESCE(SUM(o.order_amount), 0) * 100.0 /
|
COALESCE(SUM(o.order_amount), 0) * 100.0 /
|
||||||
NULLIF((SELECT SUM(order_amount) FROM crm_order o2
|
NULLIF((
|
||||||
LEFT JOIN crm_customer c2 ON o2.customer_id = c2.customer_id
|
SELECT SUM(o2.order_amount)
|
||||||
<include refid="selectCondition"/>), 0),
|
FROM crm_order o2
|
||||||
2
|
LEFT JOIN crm_customer c2 ON o2.customer_id = c2.customer_id
|
||||||
) as percentage
|
<!-- 子查询使用独立的条件片段,避免引用外层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
|
FROM crm_order o
|
||||||
LEFT JOIN crm_customer c ON o.customer_id = c.customer_id
|
LEFT JOIN crm_customer c ON o.customer_id = c.customer_id
|
||||||
<include refid="selectCondition"/>
|
<include refid="selectCondition"/>
|
||||||
@@ -167,7 +242,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
|
|
||||||
<!-- 查询客户等级统计数据 -->
|
<!-- 查询客户等级统计数据 -->
|
||||||
<select id="selectCustomerLevelStats" resultType="com.klp.crm.domain.vo.CrmSalesReportVo$CustomerLevelStat">
|
<select id="selectCustomerLevelStats" resultType="com.klp.crm.domain.vo.CrmSalesReportVo$CustomerLevelStat">
|
||||||
SELECT
|
SELECT
|
||||||
c.customer_level,
|
c.customer_level,
|
||||||
COUNT(DISTINCT c.customer_id) as customerCount,
|
COUNT(DISTINCT c.customer_id) as customerCount,
|
||||||
COUNT(o.order_id) as orderCount,
|
COUNT(o.order_id) as orderCount,
|
||||||
@@ -181,7 +256,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
|
|
||||||
<!-- 查询行业统计数据 -->
|
<!-- 查询行业统计数据 -->
|
||||||
<select id="selectIndustryStats" resultType="com.klp.crm.domain.vo.CrmSalesReportVo$IndustryStat">
|
<select id="selectIndustryStats" resultType="com.klp.crm.domain.vo.CrmSalesReportVo$IndustryStat">
|
||||||
SELECT
|
SELECT
|
||||||
COALESCE(c.industry, '未分类') as industry,
|
COALESCE(c.industry, '未分类') as industry,
|
||||||
COUNT(DISTINCT c.customer_id) as customerCount,
|
COUNT(DISTINCT c.customer_id) as customerCount,
|
||||||
COUNT(o.order_id) as orderCount,
|
COUNT(o.order_id) as orderCount,
|
||||||
@@ -193,4 +268,4 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
ORDER BY salesAmount DESC
|
ORDER BY salesAmount DESC
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
Reference in New Issue
Block a user