Commit ecb7f1bd by 崔彦峰

feat(clues): 扩展线索状态统计功能支持多维度筛选

- 在 CluesStatusCountReq 中新增多种搜索条件字段,包括线索状态、客户意向等级、跟进状态等多选搜索
- 添加模糊搜索功能,支持线索编号、车主姓名、手机号、品牌车型等模糊匹配
- 增加时间范围搜索,支持录入时间和最新跟进时间的区间筛选
- 在 Mapper XML 中实现动态 SQL 查询逻辑,支持 IN 条件和模糊查询
- 更新接口参数传递方式,从空参数改为接收完整请求对象
- 优化跟进时间状态查询的 SQL 逻辑,支持临期和超期状态判断
1 parent a7a86e35
......@@ -31,7 +31,7 @@ public interface CluesInfoMapper extends BaseMapper<CluesInfo> {
CluesDetailResp detail(@Param("req") CluesDetailReq req);
CluesStatusCountResp statusCount(@Param("request") CluesStatusCountReq request);
CluesStatusCountResp statusCount(@Param("req") CluesStatusCountReq req);
CluesFollowTimeStatusCountResp followTimeStatusCount(
@Param("request") CluesFollowTimeStatusCountReq request);
......
......@@ -85,14 +85,95 @@
COALESCE(SUM(CASE WHEN c.clues_status = 4 THEN 1 ELSE 0 END), 0) AS successCount
FROM vcp_clues_info c
WHERE c.deleted_flag = 'N'
<if test="request.sellerMerchantId != null and request.sellerMerchantId != ''">
AND c.seller_merchant_id = #{request.sellerMerchantId}
<if test="req.sellerMerchantId != null and req.sellerMerchantId != ''">
AND c.seller_merchant_id = #{req.sellerMerchantId}
</if>
<if test="req.sellerUserId != null and req.sellerUserId != ''">
AND c.seller_user_id = #{req.sellerUserId}
</if>
<if test="request.sellerUserId != null and request.sellerUserId != ''">
AND c.seller_user_id = #{request.sellerUserId}
<if test="req.conversionUserId != null and req.conversionUserId != ''">
AND c.conversion_user_id = #{req.conversionUserId}
</if>
<if test="req.cluesStatusList != null and req.cluesStatusList.size() > 0">
AND c.clues_status IN
<foreach collection="req.cluesStatusList" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="req.customerIntentLevelList != null and req.customerIntentLevelList.size() > 0">
AND c.customer_intent_level IN
<foreach collection="req.customerIntentLevelList" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="req.followTimeStatusList != null and req.followTimeStatusList.size() > 0">
AND (
<foreach collection="req.followTimeStatusList" item="item" separator=" OR ">
<choose>
<when test="item == 1">
-- 优化后的分支 1(MySQL 语义):
(c.next_follow_time &gt; NOW()
AND c.next_follow_time &lt;= DATE_ADD(NOW(), INTERVAL 2 DAY))
</when>
<when test="item == 2">
(c.next_follow_time &lt; NOW())
</when>
<otherwise>
1 = 0
</otherwise>
</choose>
</foreach>
)
</if>
<if test="request.conversionUserId != null and request.conversionUserId != ''">
AND c.conversion_user_id = #{request.conversionUserId}
<if test="req.sellerMerchantIdList != null and req.sellerMerchantIdList.size() > 0">
AND c.seller_merchant_id IN
<foreach collection="req.sellerMerchantIdList" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="req.cluesNo != null and req.cluesNo != ''">
AND c.clues_no LIKE CONCAT('%', #{req.cluesNo}, '%')
</if>
<if test="req.carOwnerName != null and req.carOwnerName != ''">
AND c.car_owner_name LIKE CONCAT('%', #{req.carOwnerName}, '%')
</if>
<if test="req.carOwnerPhone != null and req.carOwnerPhone != ''">
AND c.car_owner_phone LIKE CONCAT('%', #{req.carOwnerPhone}, '%')
</if>
<if test="req.carModel != null and req.carModel != ''">
AND (
c.car_brand LIKE CONCAT('%', #{req.carModel}, '%')
OR c.car_childs_brand_name LIKE CONCAT('%', #{req.carModel}, '%')
OR c.car_series_name LIKE CONCAT('%', #{req.carModel}, '%')
OR c.car_type_name LIKE CONCAT('%', #{req.carModel}, '%')
)
</if>
<if test="req.conversionName != null and req.conversionName != ''">
AND c.conversion_name LIKE CONCAT('%', #{req.conversionName}, '%')
</if>
<if test="req.sellerName != null and req.sellerName != ''">
AND c.seller_name LIKE CONCAT('%', #{req.sellerName}, '%')
</if>
<if test="req.enterTimeStart != null">
AND c.enter_time &gt;= #{req.enterTimeStart}
</if>
<if test="req.enterTimeEnd != null">
AND c.enter_time &lt;= #{req.enterTimeEnd}
</if>
<if test="req.latestFollowTimeStart != null">
AND c.latest_follow_time &gt;= #{req.latestFollowTimeStart}
</if>
<if test="req.latestFollowTimeEnd != null">
AND c.latest_follow_time &lt;= #{req.latestFollowTimeEnd}
</if>
<if test="req.sellerUserId != null and req.sellerUserId != ''">
AND c.seller_user_id = #{req.sellerUserId}
</if>
<if test="req.conversionUserId != null and req.conversionUserId != ''">
AND c.conversion_user_id = #{req.conversionUserId}
</if>
<if test="req.enterType != null">
AND c.enter_type = #{req.enterType}
</if>
</select>
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议,如果必要,给出代码示例(仅限 Top 3)

    1. 存在重复的动态 SQL 查询条件(正确性与可维护性)

    • 问题描述:在 CluesInfoMapper.xml 中,关于 sellerUserId 和 conversionUserId 的 <if> 判断条件被重复编写了两次。第一次出现在紧跟 sellerMerchantId 之后,第二次又出现在了文件末尾 latestFollowTimeEnd 之后。
    • 影响分析:虽然对最终的业务逻辑正确性没有致命影响(数据库优化器通常会处理重复的 AND 条件),但这会导致生成的 SQL 语句冗长,且属于明显的 Copy-Paste 遗漏,增加了后续维护的认知成本。
    • 优化建议:删除 XML 末尾冗余的这两个判断代码块。 xml <!-- 建议删除以下冗余代码 --> <if test="req.sellerUserId != null and req.sellerUserId != ''"> AND c.seller_user_id = #{req.sellerUserId} </if> <if test="req.conversionUserId != null and req.conversionUserId != ''"> AND c.conversion_user_id = #{req.conversionUserId} </if>

    2. 滥用前导模糊查询导致索引失效(性能与资源利用)

    • 问题描述:代码中针对 cluesNo、carOwnerPhone、carOwnerName 等字段,尤其是 carModel 字段(使用了 4 个 OR 拼接),全部使用了全模糊匹配 LIKE CONCAT('%', #{...}, '%')。
    • 影响分析:前导百分号(%)会导致数据库无法使用 B+ 树索引,从而引发全表扫描。当前方法是一个统计接口(包含 SUM 聚合函数),在数据量较大的情况下,这种多字段全模糊查询加 OR 的组合会导致严重的性能瓶颈,甚至拖垮数据库。
    • 优化建议:
      1. 对于线索编号 (cluesNo) 和 手机号 (carOwnerPhone),业务上通常可以要求精确匹配或右前缀匹配(如 LIKE CONCAT(#{...}, '%')),从而利用索引。
      2. 对于 carModel 这种多字段的复杂模糊搜索,如果业务确实需要,建议引入 Elasticsearch 等专业搜索引擎,或在 MySQL 中使用全文索引(Full-Text Index),避免在核心业务表上直接进行多 OR 的全模糊扫描。

    3. MyBatis XML 中的注释规范不当(最佳实践)

    • 问题描述:在处理 followTimeStatusList 的 <choose> 分支中,使用了 SQL 单行注释 -- 优化后的分支 1(MySQL 语义):。
    • 影响分析:在 MyBatis XML 中使用 -- 注释,该注释会被 MyBatis 解析并随着 SQL 语句一起发送给 MySQL 服务器执行。虽然不影响逻辑,但浪费了网络带宽和数据库的解析性能。
    • 优化建议:在 MyBatis XML 中,应始终使用 XML 标准注释 <!-- -->,这样注释只保留在代码中,不会被发送到数据库。 xml <!-- 优化建议 --> <when test="item == 1"> <!-- 优化后的分支 1(MySQL 语义): --> (c.next_follow_time &gt; NOW() AND c.next_follow_time &lt;= DATE_ADD(NOW(), INTERVAL 2 DAY)) </when>

    二、评分明细

    • 功能实现的正确性与健壮性(38/40分): 整体逻辑正确,参数重构一致。<foreach> 循环前都严谨地加上了 size() > 0 的判断,避免了空列表导致的 SQL 语法错误;时间区间的闭包处理得当。扣除 2 分是因为出现了重复的 SQL 拼接逻辑。
    • 安全性与潜在风险(30/30分): 所有的参数拼接均使用了 #{} 预编译语法,包括 CONCAT 函数内部,彻底杜绝了 SQL 注入风险,安全性极佳。
    • 最佳实践与可维护性(18/20分): 代码结构清晰,使用了 <choose> 替代复杂的 Java 层逻辑拼接,提高了可读性。扣除 2 分是因为使用了非标准的 XML 注释方式将注释带入了执行 SQL 中。
    • 性能与资源利用(2/5分): 在统计类 SQL 中大量使用全模糊查询(LIKE '%...%')且包含复杂的 OR 逻辑,这是数据库性能的“反模式”,在生产环境中存在极大的慢查询隐患。
    • 提交信息质量(5/5分): Commit Message 非常优秀,遵循了 Angular 规范(feat(clues): ...),并且通过列表详细阐述了本次变更的业务背景与技术实现细节,可追溯性极强。

    总分:93分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!