refactor(SQL分层): 剩余 85 条注解 SQL 全量迁进 mapper XML,等价性改由 MyBatis 自己判

96 条语句现在全在 src/main/resources/mapper/<模块>/ 下,注解侧 SQL 归零;判据从"基线清单"
改成完成态(逐接口必须为 0 + XML 语句总数 96 + 反向钉:XML 有则注解必须无)。

搬法上踩实了一个坑:注解参数是 Java 表达式("…" + CLIENT_MODULE_TYPES + "…"),
用只认字面量的抽取器搬家会把常量整段吞掉——上一轮生成的 26 份 XML 里因此出现
IN ()、AS status 前面没有表达式(MySQL 语法错),<script> 形态还被多转义一次变成 &amp;lt;。
更糟的是当时"搬迁前快照"与 XML 由同一个抽取器产出,两边一起错所以逐字比对全绿,
抓到现场的是既有的语义用例(TaskCenter 15 条 + 店铺查重那条断 <> 的)。

所以 MigratedSqlEquivalenceTest 改成:账本存注解原文,Java 侧现场用
getDefaultScriptingLanguageInstance().createSqlSource 编译原文,与 XML 解析出的
MappedStatement 各渲染一遍 BoundSql 对撞(空参数 + foreach 喂满 2 元素各一次),
转义对不对由 MyBatis 判定而不是由生成器判定;账本自己再钉 <script>/<if> 成对、不许出现 IN ()。
resultType 那一支不变:期望值由接口签名反射反推,66 条分母钉住。

取数入口跟着换:6 支原本读 @Select 注解的守卫(TaskCenter/Dedupe/店铺查重/通知保留/
鉴权形状/后台扩权)改走 MapperXmlSupport,断言内容一字未动;后台扩权那条"不许有 wrapper
投影"从全文 doesNotContain(".select(") 改成只扫可执行行——接口里现在只剩签名,
而 javadoc 正写着"别用 selectList(.select(...))",全文判会被自己的注释喂饱。

隔离树全量 4059 例 0 红(基线 4054,+5 为新增等价性与 XML 用例)。这批要生效得随下一版 jar 发版。
This commit is contained in:
2026-10-08 12:00:43 +08:00
parent a76f5ed382
commit 274ad0173d
62 changed files with 1490 additions and 890 deletions
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nanri.aiimage.modules.taskcenter.mapper.TaskCenterQueryMapper">
<!-- 由注解 SQL 平移而来,SQL 文本未改写;语句 id 与接口方法名逐字一致。
等价性由 MigratedSqlEquivalenceTest 按搬迁前的 fixture 逐条比对。 -->
<select id="countFinishedTasksSince" resultType="long">
SELECT COUNT(*) FROM ( SELECT t.id FROM biz_file_task t WHERE t.module_type IN ('PUBLISH','DELETE_BRAND','PRODUCT_RISK_RESOLVE','PRICE_TRACK','SHOP_MATCH','PATROL_DELETE','QUERY_ASIN','WITHDRAW','APPEARANCE_PATENT','SIMILAR_ASIN','COLLECT_DATA','SHOP_DATA_CRAWL') AND t.status IN ('CANCELED','CANCELLED','FAILED','SUCCESS') AND COALESCE(t.finished_at, t.updated_at) >= #{since}<if test='userIds != null'> AND t.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if> UNION ALL SELECT b.id FROM brand_crawl_tasks b WHERE b.status IN ('cancelled','failed','success') AND b.updated_at >= #{since}<if test='userIds != null'> AND b.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if>) u
</select>
<select id="countRunningTasks" resultType="long">
SELECT COUNT(*) FROM ( SELECT t.id FROM biz_file_task t WHERE t.module_type IN ('PUBLISH','DELETE_BRAND','PRODUCT_RISK_RESOLVE','PRICE_TRACK','SHOP_MATCH','PATROL_DELETE','QUERY_ASIN','WITHDRAW','APPEARANCE_PATENT','SIMILAR_ASIN','COLLECT_DATA','SHOP_DATA_CRAWL') AND t.status IN ('PENDING','RUNNING','SCHEDULED')<if test='userIds != null'> AND t.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if> UNION ALL SELECT b.id FROM brand_crawl_tasks b WHERE b.status IN ('pending','running')<if test='userIds != null'> AND b.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if>) u
</select>
<select id="countShops" resultType="long">
SELECT COUNT(*) FROM biz_shop_manage s WHERE 1 = 1<if test='userIds != null'> AND s.created_by_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if>
</select>
<select id="countStuckTasks" resultType="long">
SELECT COUNT(*) FROM ( SELECT t.id FROM biz_file_task t WHERE t.module_type IN ('PUBLISH','DELETE_BRAND','PRODUCT_RISK_RESOLVE','PRICE_TRACK','SHOP_MATCH','PATROL_DELETE','QUERY_ASIN','WITHDRAW','APPEARANCE_PATENT','SIMILAR_ASIN','COLLECT_DATA','SHOP_DATA_CRAWL') AND t.status IN ('PENDING','RUNNING','SCHEDULED') AND t.updated_at &lt; #{idleBefore}<if test='userIds != null'> AND t.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if> UNION ALL SELECT b.id FROM brand_crawl_tasks b WHERE b.status IN ('pending','running') AND b.updated_at &lt; #{idleBefore}<if test='userIds != null'> AND b.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if>) u
</select>
<select id="countTaskPage" resultType="long">
SELECT COUNT(*) FROM (SELECT t.id AS id, t.task_no AS taskNo, UPPER(t.module_type) AS moduleType, CASE WHEN UPPER(t.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(t.status) END AS status, t.user_id AS userId, t.device_id AS deviceId, t.created_at AS createdAt, t.finished_at AS finishedAt, t.updated_at AS updatedAt, t.source_file_count AS totalCount, t.success_file_count AS successCount, t.failed_file_count AS failedCount, 0 AS brand FROM biz_file_task t WHERE t.module_type IN ('PUBLISH','DELETE_BRAND','PRODUCT_RISK_RESOLVE','PRICE_TRACK','SHOP_MATCH','PATROL_DELETE','QUERY_ASIN','WITHDRAW','APPEARANCE_PATENT','SIMILAR_ASIN','COLLECT_DATA','SHOP_DATA_CRAWL')<if test='userIds != null'> AND t.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if><if test='moduleType != null'> AND UPPER(t.module_type) = #{moduleType}</if><if test='statuses != null'> AND CASE WHEN UPPER(t.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(t.status) END IN <foreach collection='statuses' item='st' open='(' separator=',' close=')'>#{st}</foreach></if><if test='startTime != null'> AND t.created_at &gt;= #{startTime}</if><if test='endTime != null'> AND t.created_at &lt;= #{endTime}</if><if test='shop != null'> AND (EXISTS (SELECT 1 FROM biz_publish_file p WHERE p.task_id = t.id AND LOWER(TRIM(p.shop_name)) = LOWER(TRIM(#{shop}))) OR (t.module_type IN ('PATROL_DELETE','PRICE_TRACK','PRODUCT_RISK_RESOLVE','QUERY_ASIN','SHOP_DATA_CRAWL','SHOP_MATCH') AND EXISTS (SELECT 1 FROM biz_file_result f WHERE f.task_id = t.id AND LOWER(TRIM(f.source_filename)) = LOWER(TRIM(#{shop})))))</if> UNION ALL SELECT b.id, CONCAT('brand-', b.id), 'BRAND', CASE WHEN UPPER(b.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(b.status) END, b.user_id, b.device_id, b.created_at, CASE WHEN b.status IN ('cancelled','failed','success') THEN b.updated_at END, b.updated_at, b.progress_total, b.progress_current, 0, 1 FROM brand_crawl_tasks b WHERE 1 = 1<if test='userIds != null'> AND b.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if><if test='moduleType != null'> AND 'BRAND' = #{moduleType}</if><if test='statuses != null'> AND CASE WHEN UPPER(b.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(b.status) END IN <foreach collection='statuses' item='st' open='(' separator=',' close=')'>#{st}</foreach></if><if test='startTime != null'> AND b.created_at &gt;= #{startTime}</if><if test='endTime != null'> AND b.created_at &lt;= #{endTime}</if><if test='shop != null'> AND 1 = 0</if>) m
</select>
<select id="selectActiveTasks" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskRow">
SELECT m.* FROM (SELECT t.id AS id, t.task_no AS taskNo, UPPER(t.module_type) AS moduleType, CASE WHEN UPPER(t.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(t.status) END AS status, t.user_id AS userId, t.device_id AS deviceId, t.created_at AS createdAt, t.finished_at AS finishedAt, t.updated_at AS updatedAt, t.source_file_count AS totalCount, t.success_file_count AS successCount, t.failed_file_count AS failedCount, 0 AS brand FROM biz_file_task t WHERE t.module_type IN ('PUBLISH','DELETE_BRAND','PRODUCT_RISK_RESOLVE','PRICE_TRACK','SHOP_MATCH','PATROL_DELETE','QUERY_ASIN','WITHDRAW','APPEARANCE_PATENT','SIMILAR_ASIN','COLLECT_DATA','SHOP_DATA_CRAWL') AND t.status IN ('PENDING','RUNNING','SCHEDULED')<if test='userIds != null'> AND t.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if> UNION ALL SELECT b.id, CONCAT('brand-', b.id), 'BRAND', CASE WHEN UPPER(b.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(b.status) END, b.user_id, b.device_id, b.created_at, CASE WHEN b.status IN ('cancelled','failed','success') THEN b.updated_at END, b.updated_at, b.progress_total, b.progress_current, 0, 1 FROM brand_crawl_tasks b WHERE b.status IN ('pending','running')<if test='userIds != null'> AND b.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if>) m ORDER BY m.createdAt DESC, m.id DESC LIMIT #{limit}
</select>
<select id="selectBrandTaskById" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskRow">
SELECT b.id AS id, CONCAT('brand-', b.id) AS taskNo, 'BRAND' AS moduleType, CASE WHEN UPPER(b.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(b.status) END AS status, b.user_id AS userId, b.device_id AS deviceId, b.created_at AS createdAt, CASE WHEN b.status IN ('cancelled','failed','success') THEN b.updated_at END AS finishedAt, b.updated_at AS updatedAt, b.progress_total AS totalCount, b.progress_current AS successCount, 0 AS failedCount, 1 AS brand, b.error_message AS errorMessage FROM brand_crawl_tasks b WHERE b.id = #{id}
</select>
<select id="selectCollectDataKeywordStats" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterCollectStatsRow">
SELECT t.id AS taskId, JSON_UNQUOTE(JSON_EXTRACT(t.result_json, '$.totalRows')) AS totalRows, JSON_UNQUOTE(JSON_EXTRACT(t.result_json, '$.processedRows')) AS processedRows FROM biz_file_task t WHERE t.id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach> AND UPPER(t.module_type) = 'COLLECT_DATA' AND t.result_json IS NOT NULL
</select>
<select id="selectDeviceNames" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterDeviceRow">
SELECT d.device_id AS deviceId, MAX(d.device_name) AS deviceName FROM device_log_file d WHERE d.device_id IN <foreach collection='deviceIds' item='did' open='(' separator=',' close=')'>#{did}</foreach> GROUP BY d.device_id
</select>
<select id="selectFileResultShops" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterShopRow">
SELECT f.task_id AS taskId, f.source_filename AS shop FROM biz_file_result f JOIN biz_file_task ft ON ft.id = f.task_id AND ft.module_type IN ('PATROL_DELETE','PRICE_TRACK','PRODUCT_RISK_RESOLVE','QUERY_ASIN','SHOP_DATA_CRAWL','SHOP_MATCH') WHERE f.task_id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach> AND f.source_filename IS NOT NULL AND f.source_filename != '' GROUP BY f.task_id, f.source_filename
</select>
<select id="selectPublishShopStats" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterShopStatRow">
SELECT p.shop_name AS scopeKey, COUNT(*) AS total, SUM(UPPER(p.status) = 'SUCCESS') AS success, SUM(UPPER(p.status) = 'FAILED') AS failed FROM biz_publish_file p WHERE p.task_id = #{taskId} AND p.shop_name IS NOT NULL AND p.shop_name != '' GROUP BY p.shop_name ORDER BY COUNT(*) DESC, p.shop_name LIMIT 200
</select>
<select id="selectPublishShops" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterShopRow">
SELECT p.task_id AS taskId, p.shop_name AS shop FROM biz_publish_file p WHERE p.task_id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach> AND p.shop_name IS NOT NULL AND p.shop_name != '' GROUP BY p.task_id, p.shop_name
</select>
<select id="selectResultItemShopStats" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterShopStatRow">
SELECT i.scope_key AS scopeKey, COUNT(*) AS total, SUM(UPPER(i.status) = 'SUCCESS') AS success, SUM(UPPER(i.status) = 'FAILED') AS failed FROM biz_task_result_item i WHERE i.task_id = #{taskId} AND i.scope_key IS NOT NULL AND i.scope_key != '' GROUP BY i.scope_key ORDER BY COUNT(*) DESC, i.scope_key LIMIT 200
</select>
<select id="selectShopNames" resultType="string">
SELECT DISTINCT s.shop_name FROM biz_shop_manage s WHERE 1 = 1<if test='userIds != null'> AND s.created_by_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if> ORDER BY s.shop_name LIMIT 2000
</select>
<select id="selectSnapshots" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterSnapshotRow">
SELECT s.task_id AS taskId, s.module_type AS moduleType, s.total_count AS totalCount, s.success_count AS successCount, s.failed_count AS failedCount, s.pending_count AS pendingCount, s.current_scope_key AS currentScopeKey, s.message AS message FROM biz_task_progress_snapshot s WHERE s.task_id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>
</select>
<select id="selectTaskByTaskNo" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskRow">
SELECT t.id AS id, t.task_no AS taskNo, UPPER(t.module_type) AS moduleType, CASE WHEN UPPER(t.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(t.status) END AS status, t.user_id AS userId, t.device_id AS deviceId, t.created_at AS createdAt, t.finished_at AS finishedAt, t.updated_at AS updatedAt, t.source_file_count AS totalCount, t.success_file_count AS successCount, t.failed_file_count AS failedCount, 0 AS brand, t.error_message AS errorMessage FROM biz_file_task t WHERE t.task_no = #{taskNo} AND t.module_type IN ('PUBLISH','DELETE_BRAND','PRODUCT_RISK_RESOLVE','PRICE_TRACK','SHOP_MATCH','PATROL_DELETE','QUERY_ASIN','WITHDRAW','APPEARANCE_PATENT','SIMILAR_ASIN','COLLECT_DATA','SHOP_DATA_CRAWL') LIMIT 1
</select>
<select id="selectTaskCountries" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterCountryRow">
SELECT t.id AS taskId, COALESCE(NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.filters.countryCode')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.countryCode')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.country')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.publish_country')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.countryCodes')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.country_codes')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.filters.countryCodes')), ''), 'null')) AS country FROM biz_file_task t WHERE t.id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>
</select>
<select id="selectTaskPage" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskRow">
SELECT m.* FROM (SELECT t.id AS id, t.task_no AS taskNo, UPPER(t.module_type) AS moduleType, CASE WHEN UPPER(t.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(t.status) END AS status, t.user_id AS userId, t.device_id AS deviceId, t.created_at AS createdAt, t.finished_at AS finishedAt, t.updated_at AS updatedAt, t.source_file_count AS totalCount, t.success_file_count AS successCount, t.failed_file_count AS failedCount, 0 AS brand FROM biz_file_task t WHERE t.module_type IN ('PUBLISH','DELETE_BRAND','PRODUCT_RISK_RESOLVE','PRICE_TRACK','SHOP_MATCH','PATROL_DELETE','QUERY_ASIN','WITHDRAW','APPEARANCE_PATENT','SIMILAR_ASIN','COLLECT_DATA','SHOP_DATA_CRAWL')<if test='userIds != null'> AND t.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if><if test='moduleType != null'> AND UPPER(t.module_type) = #{moduleType}</if><if test='statuses != null'> AND CASE WHEN UPPER(t.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(t.status) END IN <foreach collection='statuses' item='st' open='(' separator=',' close=')'>#{st}</foreach></if><if test='startTime != null'> AND t.created_at &gt;= #{startTime}</if><if test='endTime != null'> AND t.created_at &lt;= #{endTime}</if><if test='shop != null'> AND (EXISTS (SELECT 1 FROM biz_publish_file p WHERE p.task_id = t.id AND LOWER(TRIM(p.shop_name)) = LOWER(TRIM(#{shop}))) OR (t.module_type IN ('PATROL_DELETE','PRICE_TRACK','PRODUCT_RISK_RESOLVE','QUERY_ASIN','SHOP_DATA_CRAWL','SHOP_MATCH') AND EXISTS (SELECT 1 FROM biz_file_result f WHERE f.task_id = t.id AND LOWER(TRIM(f.source_filename)) = LOWER(TRIM(#{shop})))))</if> UNION ALL SELECT b.id, CONCAT('brand-', b.id), 'BRAND', CASE WHEN UPPER(b.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(b.status) END, b.user_id, b.device_id, b.created_at, CASE WHEN b.status IN ('cancelled','failed','success') THEN b.updated_at END, b.updated_at, b.progress_total, b.progress_current, 0, 1 FROM brand_crawl_tasks b WHERE 1 = 1<if test='userIds != null'> AND b.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if><if test='moduleType != null'> AND 'BRAND' = #{moduleType}</if><if test='statuses != null'> AND CASE WHEN UPPER(b.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(b.status) END IN <foreach collection='statuses' item='st' open='(' separator=',' close=')'>#{st}</foreach></if><if test='startTime != null'> AND b.created_at &gt;= #{startTime}</if><if test='endTime != null'> AND b.created_at &lt;= #{endTime}</if><if test='shop != null'> AND 1 = 0</if>) m ORDER BY m.createdAt DESC, m.id DESC LIMIT #{limit} OFFSET #{offset}
</select>
<select id="selectUserIdsByUsername" resultType="long">
SELECT id FROM users WHERE username LIKE CONCAT('%', #{keyword}, '%') LIMIT 500
</select>
<select id="selectUsernames" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterUserRow">
SELECT id AS userId, username FROM users WHERE id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach>
</select>
</mapper>