add Tag useCount

This commit is contained in:
Timi
2026-10-21 01:01:34 +08:00
parent 618d91c6aa
commit b3db103494
7 changed files with 162 additions and 6 deletions
@@ -0,0 +1,2 @@
ALTER TABLE `tag_apply`
ADD KEY `idx_tag_apply_type_deleted_tag_biz` (`biz_type`, `deleted_at`, `tag_id`, `biz_id`);
@@ -58,6 +58,94 @@
ORDER BY COALESCE(`t`.`updated_at`, `t`.`created_at`) DESC
LIMIT #{offset}, #{limit}
</select>
<select id="selectByOwnerAndKeywordWithUseCount" resultType="com.imyeyu.api.modules.common.entity.Tag">
SELECT
`t`.*,
COALESCE(`usage`.`use_count`, 0) AS `useCount`
FROM `tag` `t`
INNER JOIN `multilingual` `m`
ON `m`.`id` = `t`.`name_lang_id`
LEFT JOIN (
SELECT
`ta`.`tag_id`,
COUNT(DISTINCT `ta`.`biz_id`) AS `use_count`
FROM `tag_apply` `ta`
INNER JOIN `tag` `usage_tag`
ON `usage_tag`.`id` = `ta`.`tag_id`
AND `usage_tag`.`owner_type` = #{ownerType}
AND `usage_tag`.`deleted_at` IS NULL
<if test="ownerId != null and ownerId != ''">
AND `usage_tag`.`owner_id` = #{ownerId}
</if>
WHERE
`ta`.`biz_type` = #{bizType}
AND `ta`.`deleted_at` IS NULL
GROUP BY `ta`.`tag_id`
) `usage`
ON `usage`.`tag_id` = `t`.`id`
WHERE
<include refid="ownerKeywordWhere" />
<if test="keyword == null or keyword.trim() == ''">
<if test="page.equalsExample != null">
<trim prefix="AND (" suffix=")" prefixOverrides="AND |OR ">
<if test="page.equalsExample.id != null and page.equalsExample.id != ''">AND `t`.`id` = #{page.equalsExample.id}</if>
<if test="page.equalsExample.createdAt != null">
<choose><when test="page.equalsLogic != null and page.equalsLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`created_at` = #{page.equalsExample.createdAt}
</if>
<if test="page.equalsExample.updatedAt != null">
<choose><when test="page.equalsLogic != null and page.equalsLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`updated_at` = #{page.equalsExample.updatedAt}
</if>
<if test="page.equalsExample.deletedAt != null">
<choose><when test="page.equalsLogic != null and page.equalsLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`deleted_at` = #{page.equalsExample.deletedAt}
</if>
<if test="page.equalsExample.ownerType != null">
<choose><when test="page.equalsLogic != null and page.equalsLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`owner_type` = #{page.equalsExample.ownerType}
</if>
<if test="page.equalsExample.ownerId != null and page.equalsExample.ownerId != ''">
<choose><when test="page.equalsLogic != null and page.equalsLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`owner_id` = #{page.equalsExample.ownerId}
</if>
<if test="page.equalsExample.nameLangId != null and page.equalsExample.nameLangId != ''">
<choose><when test="page.equalsLogic != null and page.equalsLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`name_lang_id` = #{page.equalsExample.nameLangId}
</if>
</trim>
</if>
<if test="page.likesExample != null">
<trim prefix="AND (" suffix=")" prefixOverrides="AND |OR ">
<if test="page.likesExample.id != null and page.likesExample.id != ''">
<choose><when test="page.likesLogic != null and page.likesLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`id` LIKE CONCAT('%', #{page.likesExample.id}, '%')
</if>
<if test="page.likesExample.createdAt != null">
<choose><when test="page.likesLogic != null and page.likesLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`created_at` LIKE CONCAT('%', #{page.likesExample.createdAt}, '%')
</if>
<if test="page.likesExample.updatedAt != null">
<choose><when test="page.likesLogic != null and page.likesLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`updated_at` LIKE CONCAT('%', #{page.likesExample.updatedAt}, '%')
</if>
<if test="page.likesExample.deletedAt != null">
<choose><when test="page.likesLogic != null and page.likesLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`deleted_at` LIKE CONCAT('%', #{page.likesExample.deletedAt}, '%')
</if>
<if test="page.likesExample.ownerType != null">
<choose><when test="page.likesLogic != null and page.likesLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`owner_type` LIKE CONCAT('%', #{page.likesExample.ownerType}, '%')
</if>
<if test="page.likesExample.ownerId != null and page.likesExample.ownerId != ''">
<choose><when test="page.likesLogic != null and page.likesLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`owner_id` LIKE CONCAT('%', #{page.likesExample.ownerId}, '%')
</if>
<if test="page.likesExample.nameLangId != null and page.likesExample.nameLangId != ''">
<choose><when test="page.likesLogic != null and page.likesLogic.name() == 'OR'">OR</when><otherwise>AND</otherwise></choose> `t`.`name_lang_id` LIKE CONCAT('%', #{page.likesExample.nameLangId}, '%')
</if>
</trim>
</if>
</if>
ORDER BY COALESCE(`usage`.`use_count`, 0) DESC, COALESCE(`t`.`updated_at`, `t`.`created_at`) DESC, `t`.`id` DESC
LIMIT #{offset}, #{limit}
</select>
<select id="countUseByTagIdAndBizType" resultType="long">
SELECT COUNT(DISTINCT `biz_id`)
FROM `tag_apply`
WHERE
`tag_id` = #{tagId}
AND `biz_type` = #{bizType}
AND `deleted_at` IS NULL
</select>
<select id="selectByOwnerAndZhCN" resultType="com.imyeyu.api.modules.common.entity.Tag">
SELECT
`t`.*