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> 形态还被多转义一次变成 &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:
-134
@@ -10,7 +10,6 @@ import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskRow;
|
||||
import com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterUserRow;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
@@ -108,59 +107,15 @@ public interface TaskCenterQueryMapper {
|
||||
String USER_SCOPE_S = "<if test='userIds != null'> AND s.created_by_id IN"
|
||||
+ " <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if>";
|
||||
|
||||
@Select("<script>"
|
||||
+ "SELECT COUNT(*) FROM ("
|
||||
+ " SELECT t.id FROM biz_file_task t"
|
||||
+ " WHERE t.module_type IN (" + CLIENT_MODULE_TYPES + ")"
|
||||
+ " AND t.status IN (" + ACTIVE_STATUSES + ")"
|
||||
+ USER_SCOPE_T
|
||||
+ " UNION ALL"
|
||||
+ " SELECT b.id FROM brand_crawl_tasks b"
|
||||
+ " WHERE b.status IN (" + BRAND_ACTIVE_STATUSES + ")"
|
||||
+ USER_SCOPE_B
|
||||
+ ") u"
|
||||
+ "</script>")
|
||||
long countRunningTasks(@Param("userIds") List<Long> userIds);
|
||||
|
||||
@Select("<script>"
|
||||
+ "SELECT COUNT(*) FROM ("
|
||||
+ " SELECT t.id FROM biz_file_task t"
|
||||
+ " WHERE t.module_type IN (" + CLIENT_MODULE_TYPES + ")"
|
||||
+ " AND t.status IN (" + ACTIVE_STATUSES + ")"
|
||||
+ " AND t.updated_at < #{idleBefore}"
|
||||
+ USER_SCOPE_T
|
||||
+ " UNION ALL"
|
||||
+ " SELECT b.id FROM brand_crawl_tasks b"
|
||||
+ " WHERE b.status IN (" + BRAND_ACTIVE_STATUSES + ")"
|
||||
+ " AND b.updated_at < #{idleBefore}"
|
||||
+ USER_SCOPE_B
|
||||
+ ") u"
|
||||
+ "</script>")
|
||||
long countStuckTasks(@Param("userIds") List<Long> userIds,
|
||||
@Param("idleBefore") LocalDateTime idleBefore);
|
||||
|
||||
@Select("<script>"
|
||||
+ "SELECT COUNT(*) FROM ("
|
||||
+ " SELECT t.id FROM biz_file_task t"
|
||||
+ " WHERE t.module_type IN (" + CLIENT_MODULE_TYPES + ")"
|
||||
+ " AND t.status IN (" + TERMINAL_STATUSES + ")"
|
||||
+ " AND COALESCE(t.finished_at, t.updated_at) >= #{since}"
|
||||
+ USER_SCOPE_T
|
||||
+ " UNION ALL"
|
||||
+ " SELECT b.id FROM brand_crawl_tasks b"
|
||||
+ " WHERE b.status IN (" + BRAND_TERMINAL_STATUSES + ")"
|
||||
+ " AND b.updated_at >= #{since}"
|
||||
+ USER_SCOPE_B
|
||||
+ ") u"
|
||||
+ "</script>")
|
||||
long countFinishedTasksSince(@Param("userIds") List<Long> userIds,
|
||||
@Param("since") LocalDateTime since);
|
||||
|
||||
/** 总店铺:店铺库(biz_shop_manage)在可见账号下的数量。 */
|
||||
@Select("<script>"
|
||||
+ "SELECT COUNT(*) FROM biz_shop_manage s WHERE 1 = 1"
|
||||
+ USER_SCOPE_S
|
||||
+ "</script>")
|
||||
long countShops(@Param("userIds") List<Long> userIds);
|
||||
|
||||
/**
|
||||
@@ -169,11 +124,6 @@ public interface TaskCenterQueryMapper {
|
||||
* <p>biz_shop_manage 里带 account/password/zn_username,这一列之外的都不能出,
|
||||
* 由 {@code TaskCenterQueryMapperSqlTest.shopOptionsAreDistinctNamesWithoutCredentialColumns} 守住。</p>
|
||||
*/
|
||||
@Select("<script>"
|
||||
+ "SELECT DISTINCT s.shop_name FROM biz_shop_manage s WHERE 1 = 1"
|
||||
+ USER_SCOPE_S
|
||||
+ " ORDER BY s.shop_name LIMIT 2000"
|
||||
+ "</script>")
|
||||
List<String> selectShopNames(@Param("userIds") List<Long> userIds);
|
||||
|
||||
/** 状态归一化:CANCELED 并入 CANCELLED(历史脏值),前端只认一套。 */
|
||||
@@ -265,11 +215,6 @@ public interface TaskCenterQueryMapper {
|
||||
+ USER_SCOPE_B
|
||||
+ ")";
|
||||
|
||||
@Select("<script>"
|
||||
+ "SELECT m.* FROM " + TASK_LIST_ROWS + " m"
|
||||
+ " ORDER BY m.createdAt DESC, m.id DESC"
|
||||
+ " LIMIT #{limit} OFFSET #{offset}"
|
||||
+ "</script>")
|
||||
List<TaskCenterTaskRow> selectTaskPage(@Param("userIds") List<Long> userIds,
|
||||
@Param("moduleType") String moduleType,
|
||||
@Param("statuses") List<String> statuses,
|
||||
@@ -279,9 +224,6 @@ public interface TaskCenterQueryMapper {
|
||||
@Param("limit") long limit,
|
||||
@Param("offset") long offset);
|
||||
|
||||
@Select("<script>"
|
||||
+ "SELECT COUNT(*) FROM " + TASK_LIST_ROWS + " m"
|
||||
+ "</script>")
|
||||
long countTaskPage(@Param("userIds") List<Long> userIds,
|
||||
@Param("moduleType") String moduleType,
|
||||
@Param("statuses") List<String> statuses,
|
||||
@@ -290,43 +232,21 @@ public interface TaskCenterQueryMapper {
|
||||
@Param("endTime") LocalDateTime endTime);
|
||||
|
||||
/** 店铺面板的原料:当前全部进行中任务(超管口径下上限 500,截断由服务层显式提示)。 */
|
||||
@Select("<script>"
|
||||
+ "SELECT m.* FROM " + ACTIVE_TASK_ROWS + " m"
|
||||
+ " ORDER BY m.createdAt DESC, m.id DESC"
|
||||
+ " LIMIT #{limit}"
|
||||
+ "</script>")
|
||||
List<TaskCenterTaskRow> selectActiveTasks(@Param("userIds") List<Long> userIds,
|
||||
@Param("limit") long limit);
|
||||
|
||||
/** 账号名筛选 → 用户号(与可见账号集合求交由服务层负责)。 */
|
||||
@Select("SELECT id FROM users WHERE username LIKE CONCAT('%', #{keyword}, '%') LIMIT 500")
|
||||
List<Long> selectUserIdsByUsername(@Param("keyword") String keyword);
|
||||
|
||||
@Select("<script>"
|
||||
+ "SELECT id AS userId, username FROM users WHERE id IN"
|
||||
+ " <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach>"
|
||||
+ "</script>")
|
||||
List<TaskCenterUserRow> selectUsernames(@Param("userIds") List<Long> userIds);
|
||||
|
||||
/** 设备展示名(详情里保留「跑在哪台机」;列表与面板不再按设备装配)。 */
|
||||
@Select("<script>"
|
||||
+ "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"
|
||||
+ "</script>")
|
||||
List<TaskCenterDeviceRow> selectDeviceNames(@Param("deviceIds") List<String> deviceIds);
|
||||
|
||||
/** 品牌任务在列表/详情里的任务号前缀(task_no = brand-<id>)。 */
|
||||
String BRAND_TASK_NO_PREFIX = "brand-";
|
||||
|
||||
/** 店铺来源一:上架结果文件(shop_name)。 */
|
||||
@Select("<script>"
|
||||
+ "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"
|
||||
+ "</script>")
|
||||
List<TaskCenterShopRow> selectPublishShops(@Param("taskIds") List<Long> taskIds);
|
||||
|
||||
/**
|
||||
@@ -336,24 +256,9 @@ public interface TaskCenterQueryMapper {
|
||||
* <p>JOIN 回 biz_file_task 而不是靠调用方保证:店铺面板传进来的是两表混合 id,
|
||||
* 品牌任务 id 与 biz id 会撞号,白名单必须先由 biz 行筛掉。</p>
|
||||
*/
|
||||
@Select("<script>"
|
||||
+ "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 (" + FILE_RESULT_SHOP_MODULES + ")"
|
||||
+ " 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"
|
||||
+ "</script>")
|
||||
List<TaskCenterShopRow> selectFileResultShops(@Param("taskIds") List<Long> taskIds);
|
||||
|
||||
/** 进度快照(只用于「当前处理」与进度列,不作为店铺来源)。按 (taskId, moduleType) 由服务层匹配。 */
|
||||
@Select("<script>"
|
||||
+ "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>"
|
||||
+ "</script>")
|
||||
List<TaskCenterSnapshotRow> selectSnapshots(@Param("taskIds") List<Long> taskIds);
|
||||
|
||||
/**
|
||||
@@ -362,15 +267,6 @@ public interface TaskCenterQueryMapper {
|
||||
*
|
||||
* <p>列是原生 JSON 类型,缺键只会得到 NULL(服务层按「无进度」读),不会让整页查询报错。</p>
|
||||
*/
|
||||
@Select("<script>"
|
||||
+ "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"
|
||||
+ "</script>")
|
||||
List<TaskCenterCollectStatsRow> selectCollectDataKeywordStats(@Param("taskIds") List<Long> taskIds);
|
||||
|
||||
/**
|
||||
@@ -392,47 +288,17 @@ public interface TaskCenterQueryMapper {
|
||||
+ "NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.filters.countryCodes')), ''), 'null')"
|
||||
+ ")";
|
||||
|
||||
@Select("<script>"
|
||||
+ "SELECT t.id AS taskId, " + COUNTRY_PATHS + " AS country"
|
||||
+ " FROM biz_file_task t"
|
||||
+ " WHERE t.id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>"
|
||||
+ "</script>")
|
||||
List<TaskCenterCountryRow> selectTaskCountries(@Param("taskIds") List<Long> taskIds);
|
||||
|
||||
/** 详情:按任务号取单行(只认 12 个客户端模块,其余模块不在看板范围)。 */
|
||||
@Select("SELECT t.id AS id, t.task_no AS taskNo, UPPER(t.module_type) AS moduleType,"
|
||||
+ " " + STATUS_T + " 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 (" + CLIENT_MODULE_TYPES + ")"
|
||||
+ " LIMIT 1")
|
||||
TaskCenterTaskRow selectTaskByTaskNo(@Param("taskNo") String taskNo);
|
||||
|
||||
/** 详情:品牌任务行(task_no = brand-<id>,这里按 id 取)。 */
|
||||
@Select("SELECT b.id AS id, CONCAT('" + BRAND_TASK_NO_PREFIX + "', b.id) AS taskNo, 'BRAND' AS moduleType,"
|
||||
+ " " + STATUS_B + " AS status, b.user_id AS userId, b.device_id AS deviceId,"
|
||||
+ " b.created_at AS createdAt,"
|
||||
+ " CASE WHEN b.status IN (" + BRAND_TERMINAL_STATUSES + ") 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}")
|
||||
TaskCenterTaskRow selectBrandTaskById(@Param("id") Long id);
|
||||
|
||||
/** 详情分组明细(上架):按店铺聚合文件数 + 成功/失败文件数,最多的排前面。 */
|
||||
@Select("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")
|
||||
List<TaskCenterShopStatRow> selectPublishShopStats(@Param("taskId") Long taskId);
|
||||
|
||||
/** 详情分组明细(其余模块):按任务结果分组 scope_key 聚合条目数。 */
|
||||
@Select("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")
|
||||
List<TaskCenterShopStatRow> selectResultItemShopStats(@Param("taskId") Long taskId);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user