Commit c8b8fe1f by 王璐琦

refactor(rpc-dj-smartcarlife): 优化MerchantGoodsInfoMapper查询条件

- 替换原有listWithPageCondition为listWithPageCountCondition
- 新增复杂的查询过滤条件,支持多维度筛选
- 实现根据删除标志、ID、包编码、商品来源等多种属性的动态查询
- 支持根据商家ID列表、场景类型、基地场景编码列表等集合查询
- 增加针对城市码、商品类别、状态及合作商ID的关联过滤
- 添加基于临时价格有效状态和财务审核标记的查询支持
- 引入库存类型存在性判断,分别处理不同库存类型的查询逻辑
- 改善了SQL的可维护性和扩展性,提升数据查询灵活度
1 parent 7525c740
...@@ -316,10 +316,7 @@ ...@@ -316,10 +316,7 @@
left join dj_car_life_base_goods bg on bg.id=ug.base_goods_id and bg.deleted_flag='N' left join dj_car_life_base_goods bg on bg.id=ug.base_goods_id and bg.deleted_flag='N'
left join dj_car_life_goods_info gi on gi.goods_code=ug.goods_code and gi.deleted_flag='N' left join dj_car_life_goods_info gi on gi.goods_code=ug.goods_code and gi.deleted_flag='N'
left join dj_car_life_merchant_goods_ref mgr on mgr.deleted_flag='N' and mgr.merchant_goods_id=mgi.id left join dj_car_life_merchant_goods_ref mgr on mgr.deleted_flag='N' and mgr.merchant_goods_id=mgi.id
left join dj_car_life_scene_merchant_ref smr on smr.main_merchant_id=mgr.merchant_id and smr.scene=mgi.scene_type and smr.sub_merchant_type in (6,8) and smr.deleted_flag='N' <include refid="listWithPageCountCondition" />
left join dj_car_life_scene_merchant_ref smr2 on smr2.main_merchant_id=mgr.merchant_id and smr2.scene=mgi.scene_type and smr2.sub_merchant_type = 7 and smr2.deleted_flag='N'
left join dj_car_life_merchant_goods_inventory_type_ref mgitr on mgitr.merchant_goods_id = mgr.id and mgitr.merchant_id = mgr.merchant_id and (mgitr.package_code is null or mgitr.package_code = '') and mgitr.deleted_flag='N'
<include refid="listWithPageCondition" />
</select> </select>
<select id="count" parameterType="cn.com.glsx.dj.smartcarlife.dto.goods.MerchantGoodsInfoDTO" resultType="long"> <select id="count" parameterType="cn.com.glsx.dj.smartcarlife.dto.goods.MerchantGoodsInfoDTO" resultType="long">
...@@ -334,6 +331,238 @@ ...@@ -334,6 +331,238 @@
<include refid="listWithPageCondition" /> <include refid="listWithPageCondition" />
</select> </select>
<sql id="listWithPageCountCondition">
<where>
<choose>
<!--查询全部数据-->
<when test="'A'.toString().equals(deletedFlag)">
</when>
<!--查询非删除数据-->
<when test="'B'.toString().equals(deletedFlag)">
and mgi.deleted_flag in ('N','I')
</when>
<!--根据类型查询数据-->
<when test="deletedFlag != null and deletedFlag !=''">
and mgi.deleted_flag = #{deletedFlag}
</when>
<otherwise>
<!--查询有效数据-->
and mgi.deleted_flag = 'N'
</otherwise>
</choose>
<if test="id != null">
and mgi.id = #{id}
</if>
<if test="packageCode != null and packageCode !=''">
and mgi.package_code = #{packageCode}
</if>
<if test="packageCodes != null and packageCodes.size()>0">
and mpgi.package_code in
<foreach collection="packageCodes" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="filsterPackageGoods != null">
<if test="filsterPackageGoods == 1">
and mgi.package_code is null
</if>
<if test="filsterPackageGoods == 2">
and mgi.package_code is not null
</if>
</if>
<if test="notInIds != null and notInIds.size() != 0">
and mgi.id not in
<foreach close=")" collection="notInIds" index="index" item="item" open="(" separator=",">
#{item}
</foreach>
</if>
<if test="goodsSource != null">
and gi.goods_source = #{goodsSource}
</if>
<if test="merchantGoodsCode != null and merchantGoodsCode !=''">
and mgi.merchant_goods_code like concat('%', #{merchantGoodsCode}, '%')
</if>
<if test="filterTestGoods == null or filterTestGoods == true ">
and mgr.merchant_id &gt; 0
</if>
<if test="upgradeGoodsId != null">
and mgi.upgrade_goods_id = #{upgradeGoodsId}
</if>
<if test="brandMerchantName != null and brandMerchantName !=''">
and mgi.brand_merchant_name like concat('%', #{brandMerchantName}, '%')
</if>
<if test="supplierName != null and supplierName !=''">
and mgi.supplier_name like concat('%', #{supplierName}, '%')
</if>
<if test="upgradeGoodsName != null and upgradeGoodsName !=''">
and ug.upgrade_goods_name like concat('%', #{upgradeGoodsName}, '%')
</if>
<if test="type != null and type !=''">
and ug.type like concat('%', #{type}, '%')
</if>
<if test="upgradeGoodsNameOrType != null and upgradeGoodsNameOrType !=''">
and (
ug.upgrade_goods_name like concat('%', #{upgradeGoodsNameOrType}, '%') or
ug.type like concat('%', #{upgradeGoodsNameOrType}, '%')
)
</if>
<if test="merchantIdList !=null and merchantIdList.size() != 0">
and mgr.merchant_id in
<foreach close=")" collection="merchantIdList" index="index" item="item" open="(" separator=",">
#{item}
</foreach>
</if>
<if test="sceneType != null">
and mgi.scene_type = #{sceneType}
</if>
<if test="baseSceneCode != null and baseSceneCode !=''">
and mgi.base_scene_code = #{baseSceneCode}
</if>
<if test="baseSceneCodeList != null and baseSceneCodeList.size() &gt; 0">
and mgi.base_scene_code in
<foreach close=")" collection="baseSceneCodeList" index="index" item="item" open="(" separator=",">
#{item}
</foreach>
</if>
<if test="cityCodeList != null and cityCodeList.size() &gt; 0">
AND EXISTS (
SELECT 1
FROM dj_car_life_merchant_goods_area mga
WHERE mga.merchant_goods_id = mgi.id
AND mga.deleted_flag = 'N'
AND mga.city_code in
<foreach close=")" collection="cityCodeList" index="index" item="item" open="(" separator=",">
#{item}
</foreach>
)
</if>
<if test="goodsCode != null and goodsCode !=''">
and ug.goods_code = #{goodsCode}
</if>
<if test="goodsType != null">
and gi.goods_type = #{goodsType}
</if>
<if test="secondaryGoodsType != null">
and gi.secondary_goods_type = #{secondaryGoodsType}
</if>
<if test="thirdLevelGoodsType != null">
and gi.third_level_goods_type = #{thirdLevelGoodsType}
</if>
<if test="goodsTypeList !=null and goodsTypeList.size() != 0">
and gi.goods_type in
<foreach close=")" collection="goodsTypeList" index="index" item="item" open="(" separator=",">
#{item}
</foreach>
</if>
<if test="goodsAlias != null and goodsAlias !=''">
and mgi.goods_alias like CONCAT('%',#{goodsAlias},'%')
</if>
<if test="status != null">
and mgi.status = #{status}
</if>
<if test="statusList !=null and statusList.size() != 0">
and mgi.status in
<foreach close=")" collection="statusList" index="index" item="item" open="(" separator=",">
#{item}
</foreach>
</if>
<if test="yqMerchantId != null">
and EXISTS (
SELECT 1
FROM dj_car_life_scene_merchant_ref smr
WHERE smr.main_merchant_id = mgr.merchant_id and smr.scene = mgi.scene_type
and smr.sub_merchant_type in (6, 8) and smr.deleted_flag = 'N'
and smr.sub_merchant_id = #{yqMerchantId}
)
</if>
<if test="baseProductFlag != null">
and mgi.base_product_flag = #{baseProductFlag}
</if>
<if test="upgradeProductFlag != null">
and mgi.upgrade_product_flag = #{upgradeProductFlag}
</if>
<if test="upradeFlag != null and upradeFlag !=&quot;&quot;">
<if test="upradeFlag == &quot;N&quot;">
and mgi.base_product_flag='Y'
</if>
<if test="upradeFlag == &quot;Y&quot;">
and (mgi.upgrade_product_flag='Y' or mgi.upgrade_product_flag is null)
</if>
</if>
<if test="partnerMerchantId != null">
and bg.partner_merchant_id = #{partnerMerchantId}
</if>
<if test="partnerMerchantIdList !=null and partnerMerchantIdList.size() != 0">
and bg.partner_merchant_id in
<foreach close=")" collection="partnerMerchantIdList" index="index" item="item" open="(" separator=",">
#{item}
</foreach>
</if>
<if test="shoppingMallFlag != null">
and bg.shopping_mall_flag = #{shoppingMallFlag}
</if>
<if test="upgradeCommodityCode != null and upgradeCommodityCode !=''">
and ug.commodity_code = #{upgradeCommodityCode}
</if>
<if test="baseCommodityCode != null and baseCommodityCode !=''">
and bg.commodity_code = #{baseCommodityCode}
</if>
<if test="thirdPartyCode != null and thirdPartyCode !=''">
and ug.third_party_code = #{thirdPartyCode}
</if>
<if test="position != null">
and mgi.position = #{position}
</if>
<if test="sellPriceTempValidStatus != null">
and mgi.sell_price_valid_type = 2
<if test="sellPriceTempValidStatus == 1">
and mgi.temp_valid_start_date <![CDATA[ >= ]]> DATE_ADD(CURDATE(), INTERVAL 1 DAY)
</if>
<if test="sellPriceTempValidStatus == 2">
and mgi.temp_valid_start_date <![CDATA[ < ]]> DATE_ADD(CURDATE(), INTERVAL 1 DAY)
and mgi.temp_valid_end_date <![CDATA[ >= ]]> CURDATE()
</if>
<if test="sellPriceTempValidStatus == 3">
and mgi.temp_valid_end_date <![CDATA[ < ]]> CURDATE()
</if>
</if>
<if test="financeCheckFlag != null and financeCheckFlag !=''">
and mgi.finance_check_flag = #{financeCheckFlag}
</if>
<if test="inventoryType != null">
<if test="inventoryType == 1">
AND EXISTS (
SELECT 1
FROM dj_car_life_merchant_goods_inventory_type_ref itr
WHERE itr.merchant_goods_id = mgi.id
AND itr.deleted_flag = 'N'
AND itr.inventory_type = 1
)
</if>
<if test="inventoryType == 2">
AND EXISTS (
SELECT 1
FROM dj_car_life_merchant_goods_inventory_type_ref mgitr
WHERE mgitr.merchant_goods_id = mgi.id
AND mgitr.deleted_flag = 'N'
AND mgitr.inventory_type = 2
)
AND NOT EXISTS (
SELECT 1
FROM dj_car_life_merchant_goods_inventory_type_ref mgitr
WHERE mgitr.merchant_goods_id = mgi.id
AND mgitr.deleted_flag = 'N'
AND mgitr.inventory_type = 1
)
</if>
</if>
</where>
</sql>
<sql id="listWithPageCondition"> <sql id="listWithPageCondition">
<where> <where>
<choose> <choose>
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(Top 3)

    1. 严重违规:MyBatis SQL 中使用了被禁止的子查询(EXISTS / NOT EXISTS)

    • 问题描述:在新增的 <sql id="listWithPageCountCondition"> 中,多处使用了 EXISTS 和 NOT EXISTS 包含的子查询。例如 cityCodeList、yqMerchantId 以及 inventoryType 的条件判断中,均嵌套了 SELECT 1 FROM ... 的子查询逻辑。
    • 影响分析:严重违反了“mybatis的sql语句不允许使用子查询”的重要规则。在 MySQL 中,过度使用 EXISTS 子查询尤其是在复杂的分页列表查询中,极易导致执行计划走偏,引发严重的性能瓶颈(慢 SQL)。
    • 优化建议:将子查询逻辑改写为 JOIN(如 LEFT JOIN 或 INNER JOIN)。需要在主查询的 FROM 部分引入这些表,并在外层增加过滤条件。注意改写为 JOIN 后,若存在一对多关系,需配合 GROUP BY 或在业务层(Java 代码)进行数据去重处理。

    2. 功能缺陷:存在未定义的表别名导致 SQL 语法错误

    • 问题描述:在第 358 行处理 packageCodes 集合的条件中,使用了未定义的表别名 mpgi: and mpgi.package_code in
    • 影响分析:主查询中定义的别名为 mgi(代表 MerchantGoodsInfo)、ug、bg、gi、mgr 等。mpgi 是未定义的别名,当传入的 packageCodes 参数非空时,该 SQL 在运行时将直接抛出 Unknown column 'mpgi.package_code' in 'where clause' 的 SQL 语法异常,导致核心查询功能崩溃。
    • 优化建议:修正拼写错误,将 mpgi 改为正确的别名(推测应为 mgi)。 xml <if test="packageCodes != null and packageCodes.size()>0"> and mgi.package_code in <!-- 修正 mpgi 为 mgi --> <foreach collection="packageCodes" item="item" open="(" separator="," close=")"> #{item} </foreach> </if>

    3. 性能隐患:大量使用左前缀模糊查询(LIKE '%xxx%')

    • 问题描述:在动态条件中,频繁使用了全模糊匹配,如: mgi.merchant_goods_code like concat('%', #{merchantGoodsCode}, '%') 此外还包括 brandMerchantName、supplierName、upgradeGoodsName、type 等字段。
    • 影响分析:以 % 开头的 LIKE 查询会导致数据库无法使用索引,从而引发全表扫描。对于核心的商品信息表,随着数据量的增长,这种多维度的全表扫描会迅速拖垮数据库性能。
    • 优化建议:
      1. 评估业务需求,尽量改为右模糊匹配(如 concat(#{keyword}, '%')),以便利用联合索引。
      2. 如果业务强制要求全模糊搜索,建议将此类多维度的文本检索剥离至 ElasticSearch 等专业的搜索引擎中处理,而非依赖关系型数据库。

    二、评分明细

    • 1. 功能实现的正确性与健壮性(20/40分) 理由:代码中存在致命的别名拼写错误(mpgi),会在特定入参下直接导致运行时 SQL 异常。此外,替换原有逻辑后,大量使用了被架构明确禁止的子查询,未能符合项目的健壮性基线要求。

    • 2. 安全性与潜在风险(28/30分) 理由:参数绑定均正确使用了 #{},有效防止了 SQL 注入风险。扣除2分是因为未定义的别名构成了潜在的系统宕机风险(运行时崩溃)。

    • 3. 最佳实践与可维护性(10/20分) 理由:动态 SQL 过于庞大且复杂(长达 200 多行),将大量的业务过滤逻辑堆砌在数据库层,导致 XML 极难维护和测试。违反了不使用子查询的团队最佳实践。

    • 4. 性能与资源利用(2/5分) 理由:大量使用 LIKE '%...%' 全模糊匹配,叠加多重 EXISTS/NOT EXISTS 子查询,在分页场景下大概率会演变为慢 SQL,对数据库 CPU 和 IO 资源消耗巨大。

    • 5. 提交信息质量(5/5分) 理由:Commit message 编写非常规范,清晰地列出了重构的目的、新增的过滤维度以及对系统的改善预期,具有很好的可追溯性。

    总分:65分

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