refactor(mapper):优化查询逻辑并处理空列表情况

- 将 filterExistingRelations 查询中的 if 判断替换为 choose-when 结构
- 在 otherwise 分支中添加空结果集查询,避免空列表时 SQL 异常- 统一 filterValidSourceRelations 查询结构,增强代码一致性
-修正 foreach 标签中 UNION ALL 前后的空格问题,确保 SQL 语法正确- 提升 XML 映射文件的可读性和健壮性
This commit is contained in:
2025-09-24 17:50:53 +08:00
parent 94d6b2f443
commit 30805f3e30

View File

@@ -17,7 +17,8 @@
</foreach>
</insert>
<select id="filterExistingRelations" resultType="com.ycwl.basic.model.pc.source.entity.MemberSourceEntity">
<if test="list != null and list.size() > 0">
<choose>
<when test="list != null and list.size() > 0">
SELECT
r.memberId as memberId,
r.sourceId as sourceId,
@@ -49,13 +50,28 @@
AND ms.type = r.type
AND ms.face_id = r.faceId
)
</if>
</when>
<otherwise>
SELECT
NULL as memberId,
NULL as sourceId,
NULL as type,
NULL as faceId,
NULL as scenicId,
NULL as isBuy,
NULL as orderId,
NULL as isFree,
NULL as id
WHERE 1 = 0
</otherwise>
</choose>
</select>
<select id="sourceExists" resultType="boolean">
SELECT COUNT(1) > 0 FROM source WHERE id = #{sourceId}
</select>
<select id="filterValidSourceRelations" resultType="com.ycwl.basic.model.pc.source.entity.MemberSourceEntity">
<if test="list != null and list.size() > 0">
<choose>
<when test="list != null and list.size() > 0">
SELECT
r.memberId as memberId,
r.sourceId as sourceId,
@@ -83,7 +99,21 @@
WHERE EXISTS (
SELECT 1 FROM source s WHERE s.id = r.sourceId
)
</if>
</when>
<otherwise>
SELECT
NULL as memberId,
NULL as sourceId,
NULL as type,
NULL as faceId,
NULL as scenicId,
NULL as isBuy,
NULL as orderId,
NULL as isFree,
NULL as id
WHERE 1 = 0
</otherwise>
</choose>
</select>
<insert id="addSourceWatermark">
insert source_watermark(source_id, face_id, watermark_type, watermark_url)