Browse Source

查询标签列表的方法 IssueProjectTagDictRedis.getTagsOrderByRank(*)

dev_shibei_match
wangchao 5 years ago
parent
commit
d89b66a1a8
  1. 11
      epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/dao/IssueProjectCategoryDictDao.java
  2. 11
      epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/dao/IssueProjectTagDictDao.java
  3. 126
      epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/redis/IssueProjectTagDictRedis.java
  4. 2
      epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/service/impl/IssueTagsServiceImpl.java
  5. 12
      epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/utils/ModuleConstants.java
  6. 22
      epmet-module/gov-issue/gov-issue-server/src/main/resources/mapper/IssueProjectCategoryDictDao.xml
  7. 32
      epmet-module/gov-issue/gov-issue-server/src/main/resources/mapper/IssueProjectTagDictDao.xml

11
epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/dao/IssueProjectCategoryDictDao.java

@ -61,4 +61,15 @@ public interface IssueProjectCategoryDictDao extends BaseDao<IssueProjectCategor
* @Author sun
**/
List<IssueProjectCategoryDictDTO> selectCategoryList(@Param("cstegoryIdList") List<String> cstegoryIdList);
/**
* @Description 根据客户Id类别名称类型查询单个类别 默认查询二级分类
* @param customerId
* @param categoryName
* @param categoryType
* @return com.epmet.entity.IssueProjectCategoryDictEntity
* @author wangc
* @date 2020.12.10 13:37
*/
IssueProjectCategoryDictEntity selectSingleByCustomerIdAndCategoryNameAndCategoryType(@Param("customerId")String customerId,@Param("categoryName")String categoryName,@Param("categoryType")String categoryType);
}

11
epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/dao/IssueProjectTagDictDao.java

@ -71,4 +71,15 @@ public interface IssueProjectTagDictDao extends BaseDao<IssueProjectTagDictEntit
* @return java.util.List<com.epmet.entity.IssueProjectTagDictEntity>
*/
List<IssueProjectTagDictEntity> selectTagListByCustomer(@Param("customerId") String customerId);
/**
* @Description 根据类别查询客户下标签
* @param categories
* @param isDefault
* @param customerId
* @return java.util.List<com.epmet.entity.IssueProjectTagDictEntity>
* @author wangc
* @date 2020.12.10 10:07
*/
List<IssueProjectTagDictEntity> selectTagByCategory(@Param("categories") List<String> categories,@Param("isDefault") String isDefault,@Param("customerId") String customerId);
}

126
epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/redis/IssueProjectTagDictRedis.java

@ -20,8 +20,13 @@ package com.epmet.redis;
import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.redis.RedisUtils;
import com.epmet.constant.GovIssueRedisKeys;
import com.epmet.dao.IssueProjectCategoryDictDao;
import com.epmet.dao.IssueProjectTagDictDao;
import com.epmet.dto.result.IssueCategoryTagResultDTO;
import com.epmet.entity.IssueProjectCategoryDictEntity;
import com.epmet.entity.IssueProjectTagDictEntity;
import com.epmet.utils.ModuleConstants;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
@ -51,6 +56,10 @@ public class IssueProjectTagDictRedis {
private RedisUtils redisUtils;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private IssueProjectTagDictDao poolDao;
@Autowired
private IssueProjectCategoryDictDao categoryDictDao;
public void delete(Object[] ids) {
}
@ -74,66 +83,103 @@ public class IssueProjectTagDictRedis {
* @author wangc
* @date 2020.12.09 11:16
*/
public List<IssueCategoryTagResultDTO> getTagsOrderByRank(String customerId,List<String> category){
if(StringUtils.isBlank(customerId)) {
public Map<String,List<IssueCategoryTagResultDTO>> getTagsOrderByRank(String customerId,List<String> category) {
if (StringUtils.isBlank(customerId)) {
log.error("customerId can not be null when obtain govern tags.");
return null;
}
IssueProjectCategoryDictEntity otherCategory = categoryDictDao.selectSingleByCustomerIdAndCategoryNameAndCategoryType(customerId, ModuleConstants.CATEGORY_NAME_OTHERS, null);
boolean ifOther = null == otherCategory ? false : (!CollectionUtils.isEmpty(category) && category.size() == NumConstant.ONE && category.contains(otherCategory.getId()) ? true : false);
Set<String> keys;
if(CollectionUtils.isEmpty(category))
keys = redisUtils.keys(GovIssueRedisKeys.getGovernmentTagKey(customerId,null));
else keys = category.stream().map(key -> {return GovIssueRedisKeys.getGovernmentTagKey(customerId,key);}).collect(Collectors.toSet());
List<Set<ZSetOperations.TypedTuple<Object>>> heatTuple = redisTemplate.executePipelined(new RedisCallback<Set<ZSetOperations.TypedTuple<Object>>>() {
@Nullable
@Override
public Set<ZSetOperations.TypedTuple<Object>> doInRedis(RedisConnection connection) throws DataAccessException {
connection.openPipeline();
keys.forEach(key -> {
connection.zSetCommands().zRangeByScoreWithScores(redisTemplate.getKeySerializer().serialize(key), NumConstant.ZERO_L, (long) (NumConstant.ONE_NEG));
if (CollectionUtils.isEmpty(category) || ifOther)
keys = redisUtils.keys(GovIssueRedisKeys.getGovernmentTagKey(customerId, null));
else keys = category.stream().map(key -> {
return GovIssueRedisKeys.getGovernmentTagKey(customerId, key);
}).collect(Collectors.toSet());
//如果keys集合为空,说明缓存中没有当前客户的标签排行信息,直接进行补偿
if (!CollectionUtils.isEmpty(keys)){
keys.remove(GovIssueRedisKeys.getGovernmentTagKey(customerId,ModuleConstants.CUSTOMIZED_TAG_CATEGORY_NAME));
List<Set<ZSetOperations.TypedTuple<Object>>> heatTuple = redisTemplate.executePipelined(new RedisCallback<Set<ZSetOperations.TypedTuple<Object>>>() {
@Nullable
@Override
public Set<ZSetOperations.TypedTuple<Object>> doInRedis(RedisConnection connection) throws DataAccessException {
connection.openPipeline();
keys.forEach(key -> {
connection.zSetCommands().zRangeByScoreWithScores(redisTemplate.getKeySerializer().serialize(key), NumConstant.ZERO_L, (long) (NumConstant.ONE_NEG));
});
return null;
}
}, redisTemplate.getValueSerializer());
//热度Map 分数为key
Map<Double, List<IssueCategoryTagResultDTO>> heatMap = new HashMap<>();
if (!CollectionUtils.isEmpty(heatTuple)) {
heatTuple.forEach(tupleSet -> {
tupleSet.forEach(tuple -> {
Double heatScore = tuple.getScore();
List<IssueCategoryTagResultDTO> queue = heatMap.get(heatScore);
if (CollectionUtils.isEmpty(queue)) queue = new LinkedList<>();
queue.add(parseObject(tuple.getValue(), IssueCategoryTagResultDTO.class));
heatMap.put(heatScore,queue);
});
});
return null;
List<IssueCategoryTagResultDTO> defaultResult = new LinkedList<>();
heatMap.keySet().stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()).forEach(count -> {
defaultResult.addAll(heatMap.get(count));
});
Map<String,List<IssueCategoryTagResultDTO>> result = new HashMap<>();
result.put(ModuleConstants.DEFAULT_TAG_CATEGORY_NAME,defaultResult);
Set<ZSetOperations.TypedTuple<Object>> customizedTuples = redisUtils.zReverseRangeWithScores(GovIssueRedisKeys.getGovernmentTagKey(customerId, null), NumConstant.ZERO_L, (long) (NumConstant.ONE_NEG));
if(!CollectionUtils.isEmpty(customizedTuples)){
Map<Double, List<IssueCategoryTagResultDTO>> customizedHeatMap = new HashMap<>();
customizedTuples.forEach(tuple -> {
List<IssueCategoryTagResultDTO> queue = customizedHeatMap.get(tuple.getValue());
if((CollectionUtils.isEmpty(queue))) queue = new LinkedList<>();
queue.add(parseObject(tuple.getValue(), IssueCategoryTagResultDTO.class));
heatMap.put((Double) tuple.getValue(),queue);
});
List<IssueCategoryTagResultDTO> customizedResult = new LinkedList<>();
customizedHeatMap.keySet().stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()).forEach(count -> {
customizedResult.addAll(customizedHeatMap.get(count));
});
result.put(ModuleConstants.CUSTOMIZED_TAG_CATEGORY_NAME,customizedResult);
}
return result;
}
},redisTemplate.getValueSerializer());
//热度Map 分数为key
Map<Double,List<IssueCategoryTagResultDTO>> heatMap = new HashMap<>();
if(!CollectionUtils.isEmpty(heatTuple)){
heatTuple.forEach(tupleSet -> {
tupleSet.forEach( tuple -> {
IssueCategoryTagResultDTO entity = parseObject(tuple.getValue(),IssueCategoryTagResultDTO.class);
Double heatScore = tuple.getScore();
List<IssueCategoryTagResultDTO> queue = heatMap.get(heatScore);
if(CollectionUtils.isEmpty(queue)) queue = new LinkedList<>();
queue.add(entity);
} );
});
List<IssueCategoryTagResultDTO> result = new LinkedList<>();
heatMap.keySet().stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()).forEach(count -> {
result.addAll(heatMap.get(count));
});
return result;
}
log.warn("fetch customer govern tag cache blankly,customerId:{},now begin compensation...", customerId);
if(CollectionUtils.isEmpty(keys) || !keys.iterator().next().contains(ModuleConstants.CUSTOMIZED_TAG_CATEGORY_ID)) {
log.warn("fetch customer govern tag cache blankly , customerId:{} , now begin compensation...", customerId);
List<IssueCategoryTagResultDTO> compensate = compensate(customerId, category);
Map<String,List<IssueCategoryTagResultDTO>> compensate = compensate(customerId, category, ifOther);
if (CollectionUtils.isEmpty(compensate)) {
log.error("compensation failure or there is no default tag data in database !!");
log.error("compensation failure or there is no default tag data in database!!");
return null;
} else {
log.warn("compensation completed !!");
log.warn("compensation completed!!");
return compensate;
}
}return null;
}
public List<IssueCategoryTagResultDTO> compensate(String customerId,List<String> category){
/**
* @Description category为空则补偿全部返回全部
* category不为空则补偿集合中的类别标签以及自定义标签
* category为"customize"只补偿自定义标签
* 只需查询标签库(issue_project_tag_dict)中的issueUseCount和projectUseCount即可
* @param customerId
* @param category
* @return java.util.List<com.epmet.dto.result.IssueCategoryTagResultDTO>
* @author wangc
* @date 2020.12.10 09:34
*/
public Map<String,List<IssueCategoryTagResultDTO>> compensate(String customerId,List<String> category,boolean ifOther){
//TODO 补偿机制
List<IssueProjectTagDictEntity> _default = poolDao.selectTagByCategory(ifOther || CollectionUtils.isEmpty(category) ? null : category, NumConstant.ZERO_STR, customerId);//默认
List<IssueProjectTagDictEntity> _customized = poolDao.selectTagByCategory(null,NumConstant.ONE_STR,customerId);//自定义
return null;
}

2
epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/service/impl/IssueTagsServiceImpl.java

@ -149,7 +149,7 @@ public class IssueTagsServiceImpl extends BaseServiceImpl<IssueTagsDao, IssueTag
}
IssueProjectTagDictEntity entity = new IssueProjectTagDictEntity();
entity.setCustomerId(form.getCustomerId());
entity.setCategoryId(ModuleConstants.CUSTOMIZED_TAG_CATEGORY_ID);
entity.setCategoryId(ModuleConstants.CUSTOMIZED_TAG_CATEGORY_NAME);
entity.setTagName(form.getTagName());
entity.setIsDefault(NumConstant.ONE_STR);
entity.setIssueUseCount(NumConstant.ZERO);

12
epmet-module/gov-issue/gov-issue-server/src/main/java/com/epmet/utils/ModuleConstants.java

@ -159,5 +159,15 @@ public interface ModuleConstants {
/**
* 自定义标签类别缺省值
*/
String CUSTOMIZED_TAG_CATEGORY_ID = "customize";
String CUSTOMIZED_TAG_CATEGORY_NAME = "customize";
/**
* 默认标签类型
*/
String DEFAULT_TAG_CATEGORY_NAME = "default";
/**
* 类别名称其他
*/
String CATEGORY_NAME_OTHERS = "其他";
}

22
epmet-module/gov-issue/gov-issue-server/src/main/resources/mapper/IssueProjectCategoryDictDao.xml

@ -67,4 +67,26 @@
sort ASC
</select>
<!-- 根据客户Id、类别名称、类型查询单个类别 -->
<select id="selectSingleByCustomerIdAndCategoryNameAndCategoryType" resultType="com.epmet.entity.IssueProjectCategoryDictEntity">
SELECT
*
FROM
issue_project_category_dict
WHERE
del_flag = '0'
AND
customer_id = #{customerId}
AND
category_name = #{categoryName}
<choose>
<when test='null != categoryType and "" != categoryType'>
AND category_type = #{categoryType}
</when>
<otherwise>
AND category_type = '2'
</otherwise>
</choose>
LIMIT 1
</select>
</mapper>

32
epmet-module/gov-issue/gov-issue-server/src/main/resources/mapper/IssueProjectTagDictDao.xml

@ -70,5 +70,37 @@
DEL_FLAG = '0'
AND CUSTOMER_ID = #{customerId}
</select>
<!-- 根据类别查询客户下标签 -->
<select id="selectTagByCategory" resultType="com.epmet.entity.IssueProjectTagDictEntity">
SELECT
tag.ID,
tag.TAG_NAME,
tag.CATEGORY_ID,
tag.IS_DEFAULT,
(IFNULL(tag.ISSUE_USE_COUNT,0) + IFNULL(tag.PROJECT_USE_COUNT,0)) AS issueUseCount
FROM
ISSUE_PROJECT_TAG_DICT tag LEFT JOIN ISSUE_PROJECT_CATEGORY_DICT type ON tag.CATEGORY_ID = type.ID
WHERE
tag.DEL_FLAG = '0'
AND tag.CUSTOMER_ID = #{customerId}
AND tag.IS_DEFAULT = #{isDefault}
<choose>
<when test="null != categories and categories.size > 0">
<foreach collection="categories" item="id" open="AND ( " separator=" OR " close=" )">
tag.CATEGORY_ID = #{id}
</foreach>
ORDER BY FIELD(
tag.CATEGORY_ID,
<foreach collection="categories" item="id" separator=" , ">
#{id}
</foreach>
)
</when>
<otherwise>
ORDER BY type.SORT
</otherwise>
</choose>
, tag.TAG_NAME
</select>
</mapper>
Loading…
Cancel
Save