You've already forked FrameTour-BE
refactor(mapper):优化查询逻辑并处理空列表情况
- 将 filterExistingRelations 查询中的 if 判断替换为 choose-when 结构 - 在 otherwise 分支中添加空结果集查询,避免空列表时 SQL 异常- 统一 filterValidSourceRelations 查询结构,增强代码一致性 -修正 foreach 标签中 UNION ALL 前后的空格问题,确保 SQL 语法正确- 提升 XML 映射文件的可读性和健壮性
This commit is contained in:
@@ -17,73 +17,103 @@
|
|||||||
</foreach>
|
</foreach>
|
||||||
</insert>
|
</insert>
|
||||||
<select id="filterExistingRelations" resultType="com.ycwl.basic.model.pc.source.entity.MemberSourceEntity">
|
<select id="filterExistingRelations" resultType="com.ycwl.basic.model.pc.source.entity.MemberSourceEntity">
|
||||||
<if test="list != null and list.size() > 0">
|
<choose>
|
||||||
SELECT
|
<when test="list != null and list.size() > 0">
|
||||||
r.memberId as memberId,
|
SELECT
|
||||||
r.sourceId as sourceId,
|
r.memberId as memberId,
|
||||||
r.type as type,
|
r.sourceId as sourceId,
|
||||||
r.faceId as faceId,
|
r.type as type,
|
||||||
r.scenicId as scenicId,
|
r.faceId as faceId,
|
||||||
r.isBuy as isBuy,
|
r.scenicId as scenicId,
|
||||||
r.orderId as orderId,
|
r.isBuy as isBuy,
|
||||||
r.isFree as isFree,
|
r.orderId as orderId,
|
||||||
r.id as id
|
r.isFree as isFree,
|
||||||
FROM (
|
r.id as id
|
||||||
<foreach collection="list" item="item" separator=" UNION ALL ">
|
FROM (
|
||||||
SELECT
|
<foreach collection="list" item="item" separator=" UNION ALL ">
|
||||||
#{item.memberId} as memberId,
|
SELECT
|
||||||
#{item.sourceId} as sourceId,
|
#{item.memberId} as memberId,
|
||||||
#{item.type} as type,
|
#{item.sourceId} as sourceId,
|
||||||
#{item.faceId} as faceId,
|
#{item.type} as type,
|
||||||
#{item.scenicId} as scenicId,
|
#{item.faceId} as faceId,
|
||||||
#{item.isBuy} as isBuy,
|
#{item.scenicId} as scenicId,
|
||||||
#{item.orderId} as orderId,
|
#{item.isBuy} as isBuy,
|
||||||
#{item.isFree} as isFree,
|
#{item.orderId} as orderId,
|
||||||
#{item.id} as id
|
#{item.isFree} as isFree,
|
||||||
</foreach>
|
#{item.id} as id
|
||||||
) r
|
</foreach>
|
||||||
WHERE NOT EXISTS (
|
) r
|
||||||
SELECT 1 FROM member_source ms
|
WHERE NOT EXISTS (
|
||||||
WHERE ms.member_id = r.memberId
|
SELECT 1 FROM member_source ms
|
||||||
AND ms.source_id = r.sourceId
|
WHERE ms.member_id = r.memberId
|
||||||
AND ms.type = r.type
|
AND ms.source_id = r.sourceId
|
||||||
AND ms.face_id = r.faceId
|
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>
|
||||||
<select id="sourceExists" resultType="boolean">
|
<select id="sourceExists" resultType="boolean">
|
||||||
SELECT COUNT(1) > 0 FROM source WHERE id = #{sourceId}
|
SELECT COUNT(1) > 0 FROM source WHERE id = #{sourceId}
|
||||||
</select>
|
</select>
|
||||||
<select id="filterValidSourceRelations" resultType="com.ycwl.basic.model.pc.source.entity.MemberSourceEntity">
|
<select id="filterValidSourceRelations" resultType="com.ycwl.basic.model.pc.source.entity.MemberSourceEntity">
|
||||||
<if test="list != null and list.size() > 0">
|
<choose>
|
||||||
SELECT
|
<when test="list != null and list.size() > 0">
|
||||||
r.memberId as memberId,
|
SELECT
|
||||||
r.sourceId as sourceId,
|
r.memberId as memberId,
|
||||||
r.type as type,
|
r.sourceId as sourceId,
|
||||||
r.faceId as faceId,
|
r.type as type,
|
||||||
r.scenicId as scenicId,
|
r.faceId as faceId,
|
||||||
r.isBuy as isBuy,
|
r.scenicId as scenicId,
|
||||||
r.orderId as orderId,
|
r.isBuy as isBuy,
|
||||||
r.isFree as isFree,
|
r.orderId as orderId,
|
||||||
r.id as id
|
r.isFree as isFree,
|
||||||
FROM (
|
r.id as id
|
||||||
<foreach collection="list" item="item" separator="UNION ALL ">
|
FROM (
|
||||||
SELECT
|
<foreach collection="list" item="item" separator=" UNION ALL ">
|
||||||
#{item.memberId} as memberId,
|
SELECT
|
||||||
#{item.sourceId} as sourceId,
|
#{item.memberId} as memberId,
|
||||||
#{item.type} as type,
|
#{item.sourceId} as sourceId,
|
||||||
#{item.faceId} as faceId,
|
#{item.type} as type,
|
||||||
#{item.scenicId} as scenicId,
|
#{item.faceId} as faceId,
|
||||||
#{item.isBuy} as isBuy,
|
#{item.scenicId} as scenicId,
|
||||||
#{item.orderId} as orderId,
|
#{item.isBuy} as isBuy,
|
||||||
#{item.isFree} as isFree,
|
#{item.orderId} as orderId,
|
||||||
#{item.id} as id
|
#{item.isFree} as isFree,
|
||||||
</foreach>
|
#{item.id} as id
|
||||||
) r
|
</foreach>
|
||||||
WHERE EXISTS (
|
) r
|
||||||
SELECT 1 FROM source s WHERE s.id = r.sourceId
|
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>
|
</select>
|
||||||
<insert id="addSourceWatermark">
|
<insert id="addSourceWatermark">
|
||||||
insert source_watermark(source_id, face_id, watermark_type, watermark_url)
|
insert source_watermark(source_id, face_id, watermark_type, watermark_url)
|
||||||
|
Reference in New Issue
Block a user