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 4d2f3fc754
commit 419ab3be64
62 changed files with 1490 additions and 890 deletions
@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.brand.model.entity.BrandCrawlTaskEntity;
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;
@@ -21,13 +20,6 @@ public interface BrandCrawlTaskMapper extends BaseMapper<BrandCrawlTaskEntity> {
* 时间线用 updated_at,与 BrandTaskStaleRepairSpiImpl 的陈旧判定同款口径,
* 可命中 V120 的 idx_brand_crawl_task_status_updated(status, updated_at) 索引。
*/
@Select("""
SELECT id FROM brand_crawl_tasks
WHERE status IN ('success', 'failed', 'cancelled')
AND updated_at < #{cutoff}
ORDER BY id ASC
LIMIT #{batchSize}
""")
List<Long> selectExpiredTerminalTaskIds(@Param("cutoff") LocalDateTime cutoff,
@Param("batchSize") int batchSize);
}
@@ -2,7 +2,6 @@ package com.nanri.aiimage.modules.collectdata.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.collectdata.model.entity.CollectDataItemEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -11,16 +10,5 @@ import java.util.List;
@Mapper
public interface CollectDataItemMapper extends BaseMapper<CollectDataItemEntity> {
@Insert("""
<script>
INSERT INTO biz_collect_data_item
(task_id, row_index, source_file_key, source_filename, keyword, status_value, extra_json, created_at)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.taskId}, #{row.rowIndex}, #{row.sourceFileKey}, #{row.sourceFilename},
#{row.keyword}, #{row.statusValue}, #{row.extraJson}, #{row.createdAt})
</foreach>
</script>
""")
int insertBatch(@Param("rows") List<CollectDataItemEntity> rows);
}
@@ -2,10 +2,8 @@ package com.nanri.aiimage.modules.dedupe.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.dedupe.model.entity.DedupeTotalDataEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import java.util.List;
@@ -14,7 +12,6 @@ import java.util.StringJoiner;
@Mapper
public interface DedupeTotalDataMapper extends BaseMapper<DedupeTotalDataEntity> {
@Select("SELECT data_value FROM biz_dedupe_total_data")
List<String> selectAllDataValues();
@SelectProvider(type = SqlProvider.class, method = "selectExistingDataValues")
@@ -25,16 +22,6 @@ public interface DedupeTotalDataMapper extends BaseMapper<DedupeTotalDataEntity>
* index remains the final concurrency guard when another request inserts
* the same value between the pre-check and this statement.
*/
@Insert("""
<script>
INSERT IGNORE INTO biz_dedupe_total_data
(data_value, country, group_id, uploader_user_id, uploader_username, company_id)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.dataValue}, #{row.country}, #{row.groupId}, #{row.uploaderUserId}, #{row.uploaderUsername}, #{row.companyId})
</foreach>
</script>
""")
int insertBatchIgnore(@Param("rows") List<DedupeTotalDataEntity> rows);
class SqlProvider {
@@ -2,7 +2,6 @@ package com.nanri.aiimage.modules.devicelog.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.devicelog.model.entity.DeviceLogFileEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -17,10 +16,5 @@ public interface DeviceLogFileMapper extends BaseMapper<DeviceLogFileEntity> {
* <p>只删 {@code log_date} 早于 cutoff 的行:这条线正好是查询侧可见窗口的边界,
* 即"早已看不见、只剩占位"的行,删掉不影响任何读取路径。
*/
@Delete("""
DELETE FROM device_log_file
WHERE log_date < #{cutoff}
LIMIT #{batchSize}
""")
int deleteOlderThanBatch(@Param("cutoff") LocalDate cutoff, @Param("batchSize") int batchSize);
}
@@ -2,7 +2,6 @@ package com.nanri.aiimage.modules.imagevideo.mapper;
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;
@@ -14,39 +13,6 @@ import java.util.Map;
@Mapper
public interface ImageVideoAdminTaskMapper {
@Select("""
<script>
SELECT t.id, t.user_id, t.status, t.request_json, t.submit_response_json, t.result_json,
t.video_urls_json, t.debug_url, t.archived_videos_json, t.archive_status,
t.archive_error, t.archive_attempt_count,
DATE_FORMAT(t.archived_at, '%Y-%m-%d %H:%i:%s') AS archived_at,
t.error_message, t.coze_execute_id, t.coze_status,
DATE_FORMAT(t.submitted_at, '%Y-%m-%d %H:%i:%s') AS submitted_at,
DATE_FORMAT(t.completed_at, '%Y-%m-%d %H:%i:%s') AS completed_at,
u.username,
COALESCE((
SELECT GROUP_CONCAT(DISTINCT g.group_name ORDER BY g.id SEPARATOR '、')
FROM biz_shop_manage_group g
LEFT JOIN biz_shop_manage_group_member gm ON gm.group_id = g.id
WHERE g.created_by_id = t.user_id OR g.user_id = t.user_id OR gm.user_id = t.user_id
), '') AS group_name
FROM biz_image_video_async_task t
LEFT JOIN users u ON u.id = t.user_id
WHERE t.task_type = 'IMAGE_VIDEO_WORKFLOW'
AND t.submitted_at &gt;= DATE_SUB(NOW(), INTERVAL 3 DAY)
<if test="userId != null"> AND t.user_id = #{userId}</if>
<if test="userIds != null"> AND t.user_id IN
<foreach collection="userIds" item="uid" open="(" separator="," close=")">#{uid}</foreach>
</if>
<if test="username != null and username != ''"> AND u.username LIKE CONCAT('%', #{username}, '%')</if>
<if test="status != null and status != ''"> AND t.status = #{status}</if>
<if test="executeId != null and executeId != ''"> AND t.coze_execute_id LIKE CONCAT('%', #{executeId}, '%')</if>
<if test="submittedFrom != null"> AND t.submitted_at &gt;= #{submittedFrom}</if>
<if test="submittedTo != null"> AND t.submitted_at &lt;= #{submittedTo}</if>
ORDER BY t.submitted_at DESC, t.id DESC
LIMIT #{limit} OFFSET #{offset}
</script>
""")
List<Map<String, Object>> selectAdminTaskPage(
@Param("userId") Long userId,
@Param("userIds") List<Long> userIds,
@@ -58,24 +24,6 @@ public interface ImageVideoAdminTaskMapper {
@Param("limit") int limit,
@Param("offset") int offset);
@Select("""
<script>
SELECT COUNT(*) AS total
FROM biz_image_video_async_task t
LEFT JOIN users u ON u.id = t.user_id
WHERE t.task_type = 'IMAGE_VIDEO_WORKFLOW'
AND t.submitted_at &gt;= DATE_SUB(NOW(), INTERVAL 3 DAY)
<if test="userId != null"> AND t.user_id = #{userId}</if>
<if test="userIds != null"> AND t.user_id IN
<foreach collection="userIds" item="uid" open="(" separator="," close=")">#{uid}</foreach>
</if>
<if test="username != null and username != ''"> AND u.username LIKE CONCAT('%', #{username}, '%')</if>
<if test="status != null and status != ''"> AND t.status = #{status}</if>
<if test="executeId != null and executeId != ''"> AND t.coze_execute_id LIKE CONCAT('%', #{executeId}, '%')</if>
<if test="submittedFrom != null"> AND t.submitted_at &gt;= #{submittedFrom}</if>
<if test="submittedTo != null"> AND t.submitted_at &lt;= #{submittedTo}</if>
</script>
""")
Long countAdminTaskPage(
@Param("userId") Long userId,
@Param("userIds") List<Long> userIds,
@@ -85,41 +33,7 @@ public interface ImageVideoAdminTaskMapper {
@Param("submittedFrom") LocalDateTime submittedFrom,
@Param("submittedTo") LocalDateTime submittedTo);
@Select("""
<script>
SELECT t.id, t.user_id, t.video_urls_json, t.archived_videos_json
FROM biz_image_video_async_task t
WHERE t.task_type = 'IMAGE_VIDEO_WORKFLOW'
AND t.submitted_at &gt;= DATE_SUB(NOW(), INTERVAL 3 DAY)
AND t.id IN
<foreach collection="taskIds" item="taskId" open="(" separator="," close=")">
#{taskId}
</foreach>
</script>
""")
List<Map<String, Object>> selectAdminZipRows(@Param("taskIds") List<Long> taskIds);
@Select("""
SELECT t.id, t.user_id, t.status, t.request_json, t.submit_response_json, t.result_json,
t.video_urls_json, t.debug_url, t.archived_videos_json, t.archive_status,
t.archive_error, t.archive_attempt_count,
DATE_FORMAT(t.archived_at, '%Y-%m-%d %H:%i:%s') AS archived_at,
t.error_message, t.coze_execute_id, t.coze_status,
DATE_FORMAT(t.submitted_at, '%Y-%m-%d %H:%i:%s') AS submitted_at,
DATE_FORMAT(t.completed_at, '%Y-%m-%d %H:%i:%s') AS completed_at,
u.username,
COALESCE((
SELECT GROUP_CONCAT(DISTINCT g.group_name ORDER BY g.id SEPARATOR '、')
FROM biz_shop_manage_group g
LEFT JOIN biz_shop_manage_group_member gm ON gm.group_id = g.id
WHERE g.created_by_id = t.user_id OR g.user_id = t.user_id OR gm.user_id = t.user_id
), '') AS group_name
FROM biz_image_video_async_task t
LEFT JOIN users u ON u.id = t.user_id
WHERE t.task_type = 'IMAGE_VIDEO_WORKFLOW'
AND t.submitted_at &gt;= DATE_SUB(NOW(), INTERVAL 3 DAY)
AND t.id = #{taskId}
LIMIT 1
""")
Map<String, Object> selectAdminTaskDetail(@Param("taskId") Long taskId);
}
@@ -4,29 +4,13 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.imagevideo.model.entity.ImageVideoAsyncTaskEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
@Mapper
public interface ImageVideoAsyncTaskMapper extends BaseMapper<ImageVideoAsyncTaskEntity> {
@Update("UPDATE biz_image_video_async_task "
+ "SET status = 'RUNNING', owner_instance_id = CASE "
+ "WHEN owner_instance_id IS NULL OR owner_instance_id = '' THEN #{ownerInstanceId} "
+ "ELSE owner_instance_id END, updated_at = NOW(), attempt_count = attempt_count + 1 "
+ "WHERE id = #{taskId} AND status = 'PENDING' "
+ "AND (owner_instance_id = #{ownerInstanceId} OR owner_instance_id IS NULL OR owner_instance_id = '')")
int claimPending(@Param("taskId") Long taskId, @Param("ownerInstanceId") String ownerInstanceId);
@Update("UPDATE biz_image_video_async_task "
+ "SET status = 'POLLING', owner_instance_id = CASE "
+ "WHEN owner_instance_id IS NULL OR owner_instance_id = '' THEN #{ownerInstanceId} "
+ "ELSE owner_instance_id END, updated_at = NOW(), attempt_count = attempt_count + 1 "
+ "WHERE id = #{taskId} AND status = 'WAITING' "
+ "AND (owner_instance_id = #{ownerInstanceId} OR owner_instance_id IS NULL OR owner_instance_id = '')")
int claimWaiting(@Param("taskId") Long taskId, @Param("ownerInstanceId") String ownerInstanceId);
@Update("UPDATE biz_image_video_async_task SET status = 'WAITING', updated_at = NOW() "
+ "WHERE status = 'POLLING' "
+ "AND (owner_instance_id = #{ownerInstanceId} OR owner_instance_id IS NULL OR owner_instance_id = '')")
int requeuePollingTasks(@Param("ownerInstanceId") String ownerInstanceId);
}
@@ -2,7 +2,6 @@ package com.nanri.aiimage.modules.invalidasin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.invalidasin.model.entity.InvalidAsinDataEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -25,15 +24,5 @@ public interface InvalidAsinDataMapper extends BaseMapper<InvalidAsinDataEntity>
* 不告诉 MyBatis 类型它会抛 "Error setting null parameter",而调用方把异常吞成 log.warn,
* 于是整批黑名单静默丢掉。</p>
*/
@Insert("""
<script>
INSERT IGNORE INTO biz_invalid_asin_data
(data_value, brand, record_source, uploader_user_id)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.dataValue}, #{row.brand}, #{row.recordSource}, #{row.uploaderUserId,jdbcType=BIGINT})
</foreach>
</script>
""")
int insertBatchIgnore(@Param("rows") List<InvalidAsinDataEntity> rows);
}
@@ -2,7 +2,6 @@ package com.nanri.aiimage.modules.notification.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.notification.model.entity.UserNotificationEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -17,18 +16,8 @@ public interface UserNotificationMapper extends BaseMapper<UserNotificationEntit
* <p>两条都按 {@code created_at < cutoff} 走建表即有的 {@code idx_created_at}:保留窗口外的行
* 几乎全部命中删除条件,索引 range 扫前 N 行就能凑满一批,因此不必再为 read_at 补组合索引。
*/
@Delete("""
DELETE FROM biz_user_notification
WHERE read_at IS NOT NULL AND created_at < #{cutoff}
LIMIT #{batchSize}
""")
int deleteReadBeforeBatch(@Param("cutoff") LocalDateTime cutoff, @Param("batchSize") int batchSize);
/** 分批删除保留期外的<b>未读</b>通知(与已读同一条时间线)。 */
@Delete("""
DELETE FROM biz_user_notification
WHERE read_at IS NULL AND created_at < #{cutoff}
LIMIT #{batchSize}
""")
int deleteUnreadBeforeBatch(@Param("cutoff") LocalDateTime cutoff, @Param("batchSize") int batchSize);
}
@@ -1,45 +1,26 @@
package com.nanri.aiimage.modules.permission.mapper;
import com.nanri.aiimage.modules.permission.model.entity.UserColumnPermissionEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserColumnPermissionMapper {
@Insert("INSERT INTO user_column_permission (user_id, column_id) VALUES (#{userId}, #{columnId})")
int insert(UserColumnPermissionEntity entity);
@Select("SELECT user_id, column_id FROM user_column_permission WHERE user_id = #{userId}")
List<UserColumnPermissionEntity> selectByUserId(@Param("userId") Long userId);
@Select("SELECT user_id, column_id FROM user_column_permission WHERE column_id = #{columnId}")
List<UserColumnPermissionEntity> selectByColumnId(@Param("columnId") Long columnId);
@Select("SELECT COUNT(*) FROM user_column_permission WHERE user_id = #{userId} AND column_id = #{columnId}")
Long countByUserIdAndColumnId(@Param("userId") Long userId, @Param("columnId") Long columnId);
@Delete("DELETE FROM user_column_permission WHERE user_id = #{userId}")
int deleteByUserId(@Param("userId") Long userId);
@Delete("DELETE FROM user_column_permission WHERE column_id = #{columnId}")
int deleteByColumnId(@Param("columnId") Long columnId);
@Delete("""
<script>
DELETE FROM user_column_permission
WHERE user_id = #{userId}
AND column_id IN
<foreach collection="columnIds" item="columnId" open="(" separator="," close=")">
#{columnId}
</foreach>
</script>
""")
int deleteByUserIdAndColumnIds(@Param("userId") Long userId,
@Param("columnIds") List<Long> columnIds);
}
@@ -2,7 +2,6 @@ package com.nanri.aiimage.modules.pricetrack.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.pricetrack.model.entity.PriceTrackLoopRunEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -19,11 +18,5 @@ public interface PriceTrackLoopRunMapper extends BaseMapper<PriceTrackLoopRunEnt
* created_at),口径与改造前完全一致):原来这个表达式直接写在 WHERE 里不可走索引,
* 每条 DELETE 都要全表扫描并对扫过的行加锁,而 (status, cleanup_at) 索引已就位。
*/
@Delete("""
DELETE FROM biz_price_track_loop_run
WHERE status IN ('SUCCESS', 'FAILED', 'STOPPED')
AND cleanup_at < #{cutoff}
LIMIT #{batchSize}
""")
int deleteFinishedBefore(@Param("cutoff") LocalDateTime cutoff, @Param("batchSize") int batchSize);
}
@@ -2,7 +2,6 @@ package com.nanri.aiimage.modules.publish.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.publish.model.entity.PublishItemEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -11,18 +10,5 @@ import java.util.List;
@Mapper
public interface PublishItemMapper extends BaseMapper<PublishItemEntity> {
@Insert("""
<script>
INSERT INTO biz_publish_item
(task_id, file_id, row_index, source_id, asin, country, brand, price_value,
status_value, sync_status, sync_countries, created_at, updated_at)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.taskId}, #{row.fileId}, #{row.rowIndex}, #{row.sourceId}, #{row.asin},
#{row.country}, #{row.brand}, #{row.priceValue}, #{row.statusValue},
#{row.syncStatus}, #{row.syncCountries}, #{row.createdAt}, #{row.updatedAt})
</foreach>
</script>
""")
int insertBatch(@Param("rows") List<PublishItemEntity> rows);
}
@@ -6,7 +6,6 @@ import com.nanri.aiimage.modules.shopdatacrawl.model.dto.ShopDataCrawlAdminGroup
import com.nanri.aiimage.modules.shopdatacrawl.model.dto.ShopDataCrawlAdminRow;
import com.nanri.aiimage.modules.shopdatacrawl.model.dto.ShopDataCrawlDownloadRowDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import java.util.List;
@@ -47,20 +46,9 @@ public interface ShopDataCrawlAdminTasksMapper {
List<ShopDataCrawlDownloadRowDto> selectDownloadRows(Map<String, Object> p);
/** 按每日累计档 id 取单行(下载/删除用)。 */
@Select("""
SELECT df.id AS resultId, df.latest_task_id AS taskId, df.user_id AS userId,
df.shop_name AS sourceFilename, df.result_filename AS resultFilename,
df.result_file_url AS resultFileUrl
FROM biz_shop_data_crawl_daily_file df
WHERE df.id = #{dailyFileId}
""")
ShopDataCrawlDownloadRowDto selectDownloadRowById(@org.apache.ibatis.annotations.Param("dailyFileId") Long dailyFileId);
/** 某一累计对象 key 还被多少 daily_file 行引用。 */
@Select("""
SELECT COUNT(1) FROM biz_shop_data_crawl_daily_file
WHERE result_file_url = #{objectKey}
""")
long countDailyFileObjectReferences(@org.apache.ibatis.annotations.Param("objectKey") String objectKey);
class SqlProvider {
@@ -2,8 +2,6 @@ package com.nanri.aiimage.modules.shopdatacrawl.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.shopdatacrawl.model.entity.ShopDataCrawlItemEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -13,10 +11,6 @@ import java.util.List;
public interface ShopDataCrawlItemMapper extends BaseMapper<ShopDataCrawlItemEntity> {
/** 删除某店某批次(每日快照替换语义:先删后插)。 */
@Delete("""
DELETE FROM biz_shop_data_crawl_item
WHERE shop_name = #{shopName} AND business_date = #{businessDate}
""")
int deleteBatch(@Param("shopName") String shopName, @Param("businessDate") java.time.LocalDate businessDate);
/**
@@ -26,18 +20,9 @@ public interface ShopDataCrawlItemMapper extends BaseMapper<ShopDataCrawlItemEnt
* 撞款扫描只读每店最新批次,历史批次仅用于追溯,过期即可删;分批删避免长事务与大范围行锁,
* 走 idx_shop_data_crawl_item_biz_date 范围扫描。
*/
@Delete("""
DELETE FROM biz_shop_data_crawl_item
WHERE business_date < #{cutoff}
LIMIT #{batchSize}
""")
int deleteOlderThanBatch(@Param("cutoff") java.time.LocalDate cutoff, @Param("batchSize") int batchSize);
/** 删除归属某累计档的全部明细行(管理端真删店铺数据记录时清理,避免留下悬空引用)。 */
@Delete("""
DELETE FROM biz_shop_data_crawl_item
WHERE daily_file_id = #{dailyFileId}
""")
int deleteByDailyFileId(@Param("dailyFileId") Long dailyFileId);
/**
@@ -47,18 +32,5 @@ public interface ShopDataCrawlItemMapper extends BaseMapper<ShopDataCrawlItemEnt
* 此前逐行 insert 会产生同数量网络往返并全部持在一个事务里,是采集落库耗时与主从延迟的主要来源。
* 刻意不写 created_at:该列有 DB 默认值 CURRENT_TIMESTAMP,显式传 null 会覆盖掉默认值。
*/
@Insert("""
<script>
INSERT INTO biz_shop_data_crawl_item
(shop_id, shop_name, group_name, business_date, country, asin, brand, price,
item_date, units_sold, item_json, result_id, task_id, daily_file_id)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.shopId}, #{row.shopName}, #{row.groupName}, #{row.businessDate}, #{row.country},
#{row.asin}, #{row.brand}, #{row.price}, #{row.itemDate}, #{row.unitsSold},
#{row.itemJson}, #{row.resultId}, #{row.taskId}, #{row.dailyFileId})
</foreach>
</script>
""")
int insertBatch(@Param("rows") List<ShopDataCrawlItemEntity> rows);
}
@@ -4,10 +4,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.shopduplicatecheck.model.dto.ScanLightRowDto;
import com.nanri.aiimage.modules.shopduplicatecheck.model.dto.ScanFullRowDto;
import com.nanri.aiimage.modules.shopduplicatecheck.model.entity.ShopDataDuplicateScanEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
@@ -15,27 +13,15 @@ import java.time.LocalDateTime;
public interface ShopDataDuplicateScanMapper extends BaseMapper<ShopDataDuplicateScanEntity> {
/** 轻量判活:仅取最新 SUCCESS 行 id/created_at,避免每次请求读大 payload。 */
@Select("SELECT id, created_at AS createdAt FROM shop_data_duplicate_scan "
+ "WHERE status = 'SUCCESS' ORDER BY id DESC LIMIT 1")
ScanLightRowDto selectLatestLightRow();
/** 全量读取最新 SUCCESS 行(含 summary/payload 反序列化)。 */
@Select("SELECT id, summary_json AS summaryJson, payload_json AS payloadJson, "
+ "created_at AS createdAt FROM shop_data_duplicate_scan "
+ "WHERE status = 'SUCCESS' ORDER BY id DESC LIMIT 1")
ScanFullRowDto selectLatestFullRow();
/** 第 N 新的行 id(offset 从 0 计);行数不足时返回 null,供保留清理计算保护线。 */
@Select("SELECT id FROM shop_data_duplicate_scan ORDER BY id DESC LIMIT 1 OFFSET #{offset}")
Long selectNthNewestId(@Param("offset") int offset);
/** 分批删除保留期外、且早于保护线的历史扫描行。 */
@Delete("""
DELETE FROM shop_data_duplicate_scan
WHERE created_at < #{cutoff}
AND id < #{protectFromId}
LIMIT #{batchSize}
""")
int deleteOlderThanBatch(@Param("cutoff") LocalDateTime cutoff,
@Param("protectFromId") long protectFromId,
@Param("batchSize") int batchSize);
@@ -4,7 +4,6 @@ import com.nanri.aiimage.modules.shopduplicatecheck.model.dto.ShopDailyFileMigra
import com.nanri.aiimage.modules.shopduplicatecheck.model.dto.ShopLatestItemRowDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
@@ -20,7 +19,6 @@ public interface ShopDuplicateCheckItemMapper {
* 全量店铺名(去重,走 idx_shop_data_crawl_item_shop_date 的索引扫描,不读行数据)。
* 撞款扫描按店逐批取数用,避免一次性把全部明细拉进堆。
*/
@Select("SELECT DISTINCT shop_name FROM biz_shop_data_crawl_item")
List<String> selectDistinctShopNames();
/**
@@ -28,57 +26,14 @@ public interface ShopDuplicateCheckItemMapper {
* 内层 MAX(business_date) 走 (shop_name, business_date) 索引(松散索引扫描),
* 外层按索引前缀取该批次,只读目标店的行,不再全表 GROUP BY + JOIN。
*/
@Select("""
SELECT i.shop_name AS shopName,
i.country AS country,
i.asin AS asin,
i.item_date AS itemDate,
i.price AS price,
i.brand AS brand
FROM biz_shop_data_crawl_item i
WHERE i.shop_name = #{shopName}
AND i.business_date = (
SELECT MAX(business_date) FROM biz_shop_data_crawl_item WHERE shop_name = #{shopName}
)
""")
List<ShopLatestItemRowDto> selectLatestItemsByShop(@Param("shopName") String shopName);
/** 幂等判定:该店该日是否已有明细批次。 */
@Select("""
SELECT COUNT(1) FROM biz_shop_data_crawl_item
WHERE shop_name = #{shopName} AND business_date = #{businessDate}
""")
long countShopBatch(@Param("shopName") String shopName, @Param("businessDate") LocalDate businessDate);
/** 存量迁移数据源:每店最新已补偿 daily_file(join 最新结果行拿店名/文件地址)。 */
@Select("""
SELECT ranked.daily_file_id AS dailyFileId,
ranked.business_date AS businessDate,
ranked.latest_result_id AS latestResultId,
fr.source_filename AS shopName,
fr.result_file_url AS resultFileUrl,
fr.task_id AS taskId
FROM (
SELECT df.id AS daily_file_id,
df.business_date,
df.latest_result_id,
ROW_NUMBER() OVER (
PARTITION BY df.shop_key
ORDER BY df.business_date DESC, df.id DESC) AS row_no
FROM biz_shop_data_crawl_daily_file df
WHERE df.compensation_done = 1
) ranked
JOIN biz_file_result fr ON fr.id = ranked.latest_result_id
WHERE ranked.row_no = 1
AND fr.module_type = 'SHOP_DATA_CRAWL'
""")
List<ShopDailyFileMigrationDto> selectLegacyMigrationFiles();
/** 取每日成员行(row_payload 快照 JSON),无 payload 时返回 null。 */
@Select("""
SELECT row_payload FROM biz_shop_data_crawl_daily_member
WHERE daily_file_id = #{dailyFileId} AND result_id = #{resultId}
LIMIT 1
""")
String selectMemberPayload(@Param("dailyFileId") Long dailyFileId, @Param("resultId") Long resultId);
}
@@ -4,7 +4,6 @@ import com.nanri.aiimage.modules.shopduplicatecheck.model.dto.ShopGroupLabelDto;
import com.nanri.aiimage.modules.shopduplicatecheck.model.dto.ShopSourceRowDto;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@@ -20,81 +19,21 @@ public interface ShopDuplicateCheckSourceMapper {
* 每家店铺最新一条 biz_file_result 行(shop 最新累计结果文件,含该店全部国家/记录)。
* 结果文件 URL 非空即参与,不要求 task/result 成功(与 Python 语义一致)。
*/
@Select("""
SELECT ranked.result_id AS resultId,
ranked.shop_name AS shopName,
ranked.result_file_url AS resultFileUrl,
ranked.result_file_size AS resultFileSize,
ranked.request_json AS requestJson,
ranked.country_codes_json AS countryCodesJson
FROM (
SELECT r.id AS result_id,
r.source_filename AS shop_name,
r.result_file_url,
r.result_file_size,
t.request_json,
df.country_codes_json,
COALESCE(df.last_success_at, df.updated_at, t.finished_at,
t.updated_at, t.created_at) AS latest_file_updated_at,
ROW_NUMBER() OVER (
PARTITION BY TRIM(COALESCE(r.source_filename, ''))
ORDER BY COALESCE(df.last_success_at, df.updated_at,
t.finished_at, t.updated_at, t.created_at) DESC,
r.id DESC) AS row_no
FROM biz_file_result r
JOIN biz_file_task t ON t.id = r.task_id
LEFT JOIN biz_shop_data_crawl_daily_file df ON df.latest_result_id = r.id
WHERE r.module_type = 'SHOP_DATA_CRAWL'
AND t.module_type = 'SHOP_DATA_CRAWL'
AND TRIM(COALESCE(r.result_file_url, '')) <> ''
) ranked
WHERE ranked.row_no = 1
ORDER BY ranked.latest_file_updated_at DESC, ranked.result_id DESC
""")
List<ShopSourceRowDto> selectLatestResultRows();
/** 店铺 → 分组名(g.group_name 优先,多分组用「、」连接;店铺名 trim 精确匹配)。 */
@Select("""
<script>
SELECT TRIM(sm.shop_name) AS shopName,
GROUP_CONCAT(DISTINCT COALESCE(NULLIF(g.group_name, ''), NULLIF(sm.group_name, ''))
ORDER BY sm.id SEPARATOR '、') AS groupName
FROM biz_shop_manage sm
LEFT JOIN biz_shop_manage_group g ON g.id = sm.group_id
WHERE TRIM(sm.shop_name) IN
<foreach collection='shopNames' item='name' open='(' separator=',' close=')'>#{name}</foreach>
GROUP BY TRIM(sm.shop_name)
</script>
""")
List<ShopGroupLabelDto> selectGroupLabels(@Param("shopNames") List<String> shopNames);
/**
* 全量店铺名+分组标签(biz_shop_manage 全集):扫描基线对齐店铺表——
* 无结果文件的店以空态(0 记录)参与扫描,保证撞款控制台超管可见全部分组店铺。
*/
@Select("""
SELECT TRIM(sm.shop_name) AS shopName,
GROUP_CONCAT(DISTINCT COALESCE(NULLIF(g.group_name, ''), NULLIF(sm.group_name, ''))
ORDER BY sm.id SEPARATOR '、') AS groupName
FROM biz_shop_manage sm
LEFT JOIN biz_shop_manage_group g ON g.id = sm.group_id
WHERE TRIM(COALESCE(sm.shop_name, '')) <> ''
GROUP BY TRIM(sm.shop_name)
""")
List<ShopGroupLabelDto> selectAllShopLabels();
/**
* 主管(admin 角色)可管店铺:自己创建或指派给自己的分组(created_by_id=me OR user_id=me)
* 下的店铺名,trim 后去重。无分组时返回空集合(什么都看不到),与 Python 语义一致。
*/
@Select("""
SELECT DISTINCT TRIM(sm.shop_name) AS shopName
FROM biz_shop_manage sm
WHERE sm.group_id IN (
SELECT id FROM biz_shop_manage_group
WHERE created_by_id = #{operatorId} OR user_id = #{operatorId}
)
""")
List<String> selectManagedShopNames(@Param("operatorId") Long operatorId);
/**
@@ -107,14 +46,5 @@ public interface ShopDuplicateCheckSourceMapper {
*
* <p>空成员集不在这条 SQL 里兜:IN () 是语法错,调用方必须在 memberIds 为空时直接给空集合。</p>
*/
@Select("""
<script>
SELECT DISTINCT TRIM(sm.shop_name)
FROM biz_shop_manage sm
WHERE TRIM(COALESCE(sm.shop_name, '')) &lt;&gt; ''
AND sm.created_by_id IN
<foreach collection="memberIds" item="mid" open="(" separator="," close=")">#{mid}</foreach>
</script>
""")
List<String> selectShopNamesOwnedBy(@Param("memberIds") List<Long> memberIds);
}
@@ -4,8 +4,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.shopkey.model.entity.QueryAsinEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import java.util.Collection;
import java.util.List;
@@ -13,43 +11,10 @@ import java.util.List;
@Mapper
public interface QueryAsinMapper extends BaseMapper<QueryAsinEntity> {
@Select("""
<script>
SELECT id, group_id, shop_name, asin_de, asin_uk, asin_fr, asin_it, asin_es, created_at, updated_at
FROM biz_query_asin
WHERE group_id IN
<foreach collection="groupIds" item="groupId" open="(" separator="," close=")">
#{groupId}
</foreach>
AND shop_name IN
<foreach collection="shopNames" item="shopName" open="(" separator="," close=")">
#{shopName}
</foreach>
</script>
""")
List<QueryAsinEntity> selectByGroupIdsAndShopNames(@Param("groupIds") Collection<Long> groupIds,
@Param("shopNames") Collection<String> shopNames);
@Select("""
<script>
SELECT id, group_id, shop_name, asin_de, asin_uk, asin_fr, asin_it, asin_es, created_at, updated_at
FROM biz_query_asin
WHERE shop_name IN
<foreach collection="shopNames" item="shopName" open="(" separator="," close=")">
#{shopName}
</foreach>
</script>
""")
List<QueryAsinEntity> selectByShopNames(@Param("shopNames") Collection<String> shopNames);
@Insert("""
<script>
INSERT INTO biz_query_asin (group_id, shop_name, asin_de, asin_uk, asin_fr, asin_it, asin_es)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.groupId}, #{row.shopName}, #{row.asinDe}, #{row.asinUk}, #{row.asinFr}, #{row.asinIt}, #{row.asinEs})
</foreach>
</script>
""")
int insertBatch(@Param("rows") List<QueryAsinEntity> rows);
}
@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.softwareversion.model.entity.SoftwareVersionEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
@Mapper
public interface SoftwareVersionMapper extends BaseMapper<SoftwareVersionEntity> {
@@ -17,6 +16,5 @@ public interface SoftwareVersionMapper extends BaseMapper<SoftwareVersionEntity>
* 零标记的中间态,而本方法要在 MinIO 上传流程之后调用,不值得为 62 行的表
* 拉一个事务、还要为此绕开 Spring 自调用不走代理的问题。
*/
@Update("UPDATE web_config SET is_latest = (id = #{id})")
int markLatestById(@Param("id") Long id);
}
@@ -5,7 +5,6 @@ import com.nanri.aiimage.modules.task.model.dto.TaskRowsVersionDto;
import com.nanri.aiimage.modules.task.model.entity.FileResultEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@@ -20,16 +19,6 @@ public interface FileResultMapper extends BaseMapper<FileResultEntity> {
* 前端据 (latestRowAt, rowCount) 判断该任务的行数据是否变化,未变就不必再拉带行明细的重型 batch。
* GROUP BY 走 (task_id) 索引,每任务一行。
*/
@Select("""
<script>
SELECT task_id AS taskId, MAX(updated_at) AS latestRowAt, COUNT(*) AS rowCount
FROM biz_file_result
WHERE module_type = #{moduleType}
AND task_id IN
<foreach item="taskId" collection="taskIds" open="(" separator="," close=")">#{taskId}</foreach>
GROUP BY task_id
</script>
""")
List<TaskRowsVersionDto> selectRowsUpdatedAtByTaskIds(@Param("moduleType") String moduleType,
@Param("taskIds") List<Long> taskIds);
}
@@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
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;
@@ -23,14 +22,6 @@ public interface FileTaskMapper extends BaseMapper<FileTaskEntity> {
* 且能命中 V120 的 idx_biz_file_task_status_updated(status, updated_at) 索引,避免每轮全表扫描。
* 注意不要在这里包 COALESCE(finished_at, updated_at):表达式会让该索引失效。
*/
@Select("""
SELECT id FROM biz_file_task
WHERE module_type = #{moduleType}
AND status IN ('SUCCESS', 'FAILED', 'CANCELLED', 'CANCELED')
AND updated_at < #{cutoff}
ORDER BY id ASC
LIMIT #{batchSize}
""")
List<Long> selectExpiredTerminalTaskIds(@Param("moduleType") String moduleType,
@Param("cutoff") LocalDateTime cutoff,
@Param("batchSize") int batchSize);
@@ -2,7 +2,6 @@ package com.nanri.aiimage.modules.task.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.task.model.entity.TaskResultItemEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -15,26 +14,5 @@ public interface TaskResultItemMapper extends BaseMapper<TaskResultItemEntity> {
* 按唯一键 uk_task_scope_item (task_id, module_type, scope_hash, item_key)
* 批量 upsert:命中唯一键时更新行内容(含 id 保留),否则插入。
*/
@Insert("""
<script>
INSERT INTO biz_task_result_item
(task_id, module_type, result_id, scope_key, scope_hash, item_key, asin, status,
payload_json, payload_hash, created_at, updated_at)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.taskId}, #{row.moduleType}, #{row.resultId}, #{row.scopeKey}, #{row.scopeHash},
#{row.itemKey}, #{row.asin}, #{row.status}, #{row.payloadJson}, #{row.payloadHash},
#{row.createdAt}, #{row.updatedAt})
</foreach>
ON DUPLICATE KEY UPDATE
result_id = VALUES(result_id),
scope_key = VALUES(scope_key),
asin = VALUES(asin),
status = VALUES(status),
payload_json = VALUES(payload_json),
payload_hash = VALUES(payload_hash),
updated_at = VALUES(updated_at)
</script>
""")
int upsertBatch(@Param("rows") List<TaskResultItemEntity> rows);
}
@@ -5,7 +5,6 @@ import com.nanri.aiimage.modules.task.model.dto.TaskScopeLastChunkDto;
import com.nanri.aiimage.modules.task.model.entity.TaskScopeStateEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@@ -20,15 +19,5 @@ public interface TaskScopeStateMapper extends BaseMapper<TaskScopeStateEntity> {
* HAVING 过滤掉全 NULL 的组:从未上报的任务不出现在结果里,调用方据此跳过(不判死)。
* GROUP BY 走 uk_task_scope 的 task_id 前缀。
*/
@Select("""
<script>
SELECT task_id AS taskId, MAX(last_chunk_at) AS lastChunkAt
FROM biz_task_scope_state
WHERE task_id IN
<foreach item="taskId" collection="taskIds" open="(" separator="," close=")">#{taskId}</foreach>
GROUP BY task_id
HAVING MAX(last_chunk_at) IS NOT NULL
</script>
""")
List<TaskScopeLastChunkDto> selectLastChunkAtByTaskIds(@Param("taskIds") List<Long> taskIds);
}
@@ -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 &lt; #{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 &lt; #{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);
}
@@ -2,8 +2,6 @@ package com.nanri.aiimage.modules.usersecret.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.usersecret.model.entity.UserSecretUsageEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -16,21 +14,11 @@ public interface UserSecretUsageMapper extends BaseMapper<UserSecretUsageEntity>
* 按唯一键 uk_user_module_date 原子累加计次:命中则 call_count 自增,否则插入。
* 并发下无需应用层加锁(引擎层唯一键冲突转更新)。
*/
@Insert("""
INSERT INTO biz_user_secret_usage_daily (user_id, module_key, business_date, call_count, created_at, updated_at)
VALUES (#{userId}, #{moduleKey}, #{businessDate}, #{count}, NOW(), NOW())
ON DUPLICATE KEY UPDATE call_count = call_count + #{count}, updated_at = NOW()
""")
int increment(@Param("userId") Long userId,
@Param("moduleKey") String moduleKey,
@Param("businessDate") LocalDate businessDate,
@Param("count") int count);
/** 分批删除保留期外的用量日统计行(该表此前只增不删)。 */
@Delete("""
DELETE FROM biz_user_secret_usage_daily
WHERE business_date < #{cutoff}
LIMIT #{batchSize}
""")
int deleteBefore(@Param("cutoff") LocalDate cutoff, @Param("batchSize") int batchSize);
}