add Tag useCount
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.imyeyu.api.modules.common.entity;
|
package com.imyeyu.api.modules.common.entity;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
import com.imyeyu.api.annotation.MultilingualField;
|
import com.imyeyu.api.annotation.MultilingualField;
|
||||||
import com.imyeyu.spring.annotation.table.Transient;
|
import com.imyeyu.spring.annotation.table.Transient;
|
||||||
import com.imyeyu.spring.entity.UUIDEntity;
|
import com.imyeyu.spring.entity.UUIDEntity;
|
||||||
@@ -40,4 +41,9 @@ public class Tag extends UUIDEntity {
|
|||||||
/// 中文名称
|
/// 中文名称
|
||||||
@Transient
|
@Transient
|
||||||
protected String zhCN;
|
protected String zhCN;
|
||||||
|
|
||||||
|
/// 当前查询业务下的使用量
|
||||||
|
@Transient
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
protected Long useCount;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.imyeyu.api.modules.common.mapper;
|
package com.imyeyu.api.modules.common.mapper;
|
||||||
|
|
||||||
import com.imyeyu.api.modules.common.entity.Tag;
|
import com.imyeyu.api.modules.common.entity.Tag;
|
||||||
|
import com.imyeyu.api.modules.common.entity.TagApply;
|
||||||
|
import com.imyeyu.spring.bean.Page;
|
||||||
import com.imyeyu.spring.mapper.BaseMapper;
|
import com.imyeyu.spring.mapper.BaseMapper;
|
||||||
import org.apache.ibatis.annotations.Param;
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
@@ -33,5 +35,23 @@ public interface TagMapper extends BaseMapper<Tag, String> {
|
|||||||
/// @return 标签列表
|
/// @return 标签列表
|
||||||
List<Tag> selectByOwnerAndKeyword(@Param("ownerType") Tag.OwnerType ownerType, @Param("ownerId") String ownerId, @Param("keyword") String keyword, @Param("offset") long offset, @Param("limit") long limit);
|
List<Tag> selectByOwnerAndKeyword(@Param("ownerType") Tag.OwnerType ownerType, @Param("ownerId") String ownerId, @Param("keyword") String keyword, @Param("offset") long offset, @Param("limit") long limit);
|
||||||
|
|
||||||
|
/// 按归属和名称关键词分页查询标签,并统计指定业务的使用量
|
||||||
|
///
|
||||||
|
/// @param ownerType 归属类型
|
||||||
|
/// @param ownerId 归属 ID
|
||||||
|
/// @param keyword 名称关键词
|
||||||
|
/// @param bizType 业务类型
|
||||||
|
/// @param offset 分页偏移量
|
||||||
|
/// @param limit 分页数量
|
||||||
|
/// @return 标签列表
|
||||||
|
List<Tag> selectByOwnerAndKeywordWithUseCount(@Param("page") Page<Tag> page, @Param("ownerType") Tag.OwnerType ownerType, @Param("ownerId") String ownerId, @Param("keyword") String keyword, @Param("bizType") TagApply.BizType bizType, @Param("offset") long offset, @Param("limit") long limit);
|
||||||
|
|
||||||
|
/// 查询指定标签在指定业务下的使用量
|
||||||
|
///
|
||||||
|
/// @param tagId 标签 ID
|
||||||
|
/// @param bizType 业务类型
|
||||||
|
/// @return 使用量
|
||||||
|
long countUseByTagIdAndBizType(@Param("tagId") String tagId, @Param("bizType") TagApply.BizType bizType);
|
||||||
|
|
||||||
Tag selectByOwnerAndZhCN(@Param("ownerType") Tag.OwnerType ownerType, @Param("ownerId") String ownerId, @Param("zhCN") String zhCN);
|
Tag selectByOwnerAndZhCN(@Param("ownerType") Tag.OwnerType ownerType, @Param("ownerId") String ownerId, @Param("zhCN") String zhCN);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,23 @@ public interface TagService extends BaseService<Tag, String> {
|
|||||||
/// @return 标签分页
|
/// @return 标签分页
|
||||||
PageResult<Tag> pageByOwnerAndKeyword(Page<Tag> page, Tag.OwnerType ownerType, String ownerId, String keyword);
|
PageResult<Tag> pageByOwnerAndKeyword(Page<Tag> page, Tag.OwnerType ownerType, String ownerId, String keyword);
|
||||||
|
|
||||||
|
/// 按归属和名称关键词分页查询标签,并按指定业务的使用量排序
|
||||||
|
///
|
||||||
|
/// @param page 分页参数
|
||||||
|
/// @param ownerType 归属类型
|
||||||
|
/// @param ownerId 归属 ID
|
||||||
|
/// @param keyword 名称关键词
|
||||||
|
/// @param bizType 业务类型
|
||||||
|
/// @return 标签分页
|
||||||
|
PageResult<Tag> pageByOwnerAndKeywordWithUseCount(Page<Tag> page, Tag.OwnerType ownerType, String ownerId, String keyword, TagApply.BizType bizType);
|
||||||
|
|
||||||
|
/// 查询指定标签在指定业务下的使用量
|
||||||
|
///
|
||||||
|
/// @param tagId 标签 ID
|
||||||
|
/// @param bizType 业务类型
|
||||||
|
/// @return 使用量
|
||||||
|
long countUseByTagIdAndBizType(String tagId, TagApply.BizType bizType);
|
||||||
|
|
||||||
/// 按业务查询标签列表
|
/// 按业务查询标签列表
|
||||||
///
|
///
|
||||||
/// @param bizType 业务类型
|
/// @param bizType 业务类型
|
||||||
|
|||||||
+16
@@ -7,6 +7,7 @@ import com.imyeyu.api.modules.common.mapper.TagApplyMapper;
|
|||||||
import com.imyeyu.api.modules.common.mapper.TagMapper;
|
import com.imyeyu.api.modules.common.mapper.TagMapper;
|
||||||
import com.imyeyu.api.modules.common.service.MultilingualService;
|
import com.imyeyu.api.modules.common.service.MultilingualService;
|
||||||
import com.imyeyu.api.modules.common.service.TagService;
|
import com.imyeyu.api.modules.common.service.TagService;
|
||||||
|
import com.imyeyu.java.TimiJava;
|
||||||
import com.imyeyu.java.bean.timi.TimiException;
|
import com.imyeyu.java.bean.timi.TimiException;
|
||||||
import com.imyeyu.spring.bean.Page;
|
import com.imyeyu.spring.bean.Page;
|
||||||
import com.imyeyu.spring.bean.PageResult;
|
import com.imyeyu.spring.bean.PageResult;
|
||||||
@@ -82,6 +83,21 @@ public class TagServiceImplement extends AbstractEntityService<Tag, String> impl
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PageResult<Tag> pageByOwnerAndKeywordWithUseCount(Page<Tag> page, Tag.OwnerType ownerType, String ownerId, String keyword, TagApply.BizType bizType) {
|
||||||
|
PageResult<Tag> result = new PageResult<>();
|
||||||
|
result.setTotal(TimiJava.isNotEmpty(keyword)
|
||||||
|
? mapper.countByOwnerAndKeyword(ownerType, ownerId, keyword)
|
||||||
|
: mapper.countByPage(page));
|
||||||
|
result.setList(mapper.selectByOwnerAndKeywordWithUseCount(page, ownerType, ownerId, keyword, bizType, page.getOffset(), page.getLimit()));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long countUseByTagIdAndBizType(String tagId, TagApply.BizType bizType) {
|
||||||
|
return mapper.countUseByTagIdAndBizType(tagId, bizType);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Tag> listByBizType(TagApply.BizType bizType, String bizId) {
|
public List<Tag> listByBizType(TagApply.BizType bizType, String bizId) {
|
||||||
return listByIdList(listApply(bizType, bizId).stream().map(TagApply::getTagId).toList());
|
return listByIdList(listApply(bizType, bizId).stream().map(TagApply::getTagId).toList());
|
||||||
|
|||||||
@@ -50,18 +50,21 @@ public class GaoTagController {
|
|||||||
@PostMapping("/list")
|
@PostMapping("/list")
|
||||||
public PageResult<Tag> list(@RequestBody Page<Tag> page) {
|
public PageResult<Tag> list(@RequestBody Page<Tag> page) {
|
||||||
TimiException.required(page, "未找到分页参数");
|
TimiException.required(page, "未找到分页参数");
|
||||||
String ownerId = null;
|
|
||||||
page.setEqualsExample(TimiJava.defaultIfNull(page.getEqualsExample(), new Tag()));
|
page.setEqualsExample(TimiJava.defaultIfNull(page.getEqualsExample(), new Tag()));
|
||||||
page.getEqualsExample().setOwnerType(Tag.OwnerType.GAO_STORE);
|
page.getEqualsExample().setOwnerType(Tag.OwnerType.GAO_STORE);
|
||||||
|
String ownerId = page.getEqualsExample().getOwnerId();
|
||||||
if (!roleChecker.hasAny(ModuleCode.GAO, GaoRoleCode.GLOBAL_MANAGER.name())) {
|
if (!roleChecker.hasAny(ModuleCode.GAO, GaoRoleCode.GLOBAL_MANAGER.name())) {
|
||||||
ownerId = storeService.getBelongIdByRequiredLoginUserId();
|
ownerId = storeService.getBelongIdByRequiredLoginUserId();
|
||||||
page.getEqualsExample().setOwnerId(ownerId);
|
page.getEqualsExample().setOwnerId(ownerId);
|
||||||
}
|
}
|
||||||
String keyword = page.getLikesExample() == null ? null : page.getLikesExample().getName();
|
String keyword = page.getLikesExample() == null ? null : page.getLikesExample().getName();
|
||||||
if (TimiJava.isNotEmpty(keyword)) {
|
return service.pageByOwnerAndKeywordWithUseCount(
|
||||||
return service.pageByOwnerAndKeyword(page, Tag.OwnerType.GAO_STORE, ownerId, keyword.trim());
|
page,
|
||||||
}
|
Tag.OwnerType.GAO_STORE,
|
||||||
return service.page(page);
|
ownerId,
|
||||||
|
TimiJava.isNotEmpty(keyword) ? keyword.trim() : null,
|
||||||
|
TagApply.BizType.GAO_CUSTOMER
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 查询标签详情
|
/// 查询标签详情
|
||||||
@@ -73,7 +76,11 @@ public class GaoTagController {
|
|||||||
@RequireCorePermission(CorePermissionCode.TAG_READ)
|
@RequireCorePermission(CorePermissionCode.TAG_READ)
|
||||||
@PostMapping("/detail")
|
@PostMapping("/detail")
|
||||||
public Tag detail(@RequestParam String id) {
|
public Tag detail(@RequestParam String id) {
|
||||||
return service.get(id);
|
Tag tag = service.get(id);
|
||||||
|
if (tag != null) {
|
||||||
|
tag.setUseCount(service.countUseByTagIdAndBizType(id, TagApply.BizType.GAO_CUSTOMER));
|
||||||
|
}
|
||||||
|
return tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 创建标签
|
/// 创建标签
|
||||||
|
|||||||
@@ -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
|
ORDER BY COALESCE(`t`.`updated_at`, `t`.`created_at`) DESC
|
||||||
LIMIT #{offset}, #{limit}
|
LIMIT #{offset}, #{limit}
|
||||||
</select>
|
</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 id="selectByOwnerAndZhCN" resultType="com.imyeyu.api.modules.common.entity.Tag">
|
||||||
SELECT
|
SELECT
|
||||||
`t`.*
|
`t`.*
|
||||||
|
|||||||
Reference in New Issue
Block a user