热词功能完善

This commit is contained in:
Chopper 2022-04-25 18:08:06 +08:00
parent bb50510ecb
commit 8dc86e0482
10 changed files with 123 additions and 66 deletions

View File

@ -4,13 +4,15 @@ ALTER TABLE li_member_sign DROP INDEX uk_member_day;
ALTER TABLE li_member_sign add unique uk_member_day (member_id, day) COMMENT 'uk_member_day';
/** 添加历史热词表 **/
-- ----------------------------
-- Table structure for li_hot_words_history
-- ----------------------------
DROP TABLE IF EXISTS `li_hot_words_history`;
CREATE TABLE `li_hot_words_history` (
`id` bigint NOT NULL COMMENT 'ID',
`create_by` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '创建者',
`create_time` datetime(6) DEFAULT NULL COMMENT '创建时间',
`delete_flag` bit(1) DEFAULT NULL COMMENT '是否删除',
`update_by` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '修改者',
`update_time` datetime(6) DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
`id` bigint NOT NULL COMMENT 'ID',
`create_time` datetime(6) DEFAULT NULL COMMENT '创建时间',
`keywords` varchar(255) COLLATE utf8_bin DEFAULT NULL COMMENT '热词',
`score` int DEFAULT NULL COMMENT '热词分数',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin;

View File

@ -29,9 +29,9 @@ public class DateUtil {
return DateUtil.getDateline()/(60*24*60);
}
/**
* 当天的开始时间
* 指定日的开始时间
*
* @return 今天开始时间
* @return 指定日时间
*/
public static Long getDayOfStart(Date date) {
return date.getTime()/(60*24*60);

View File

@ -1,21 +1,15 @@
package cn.lili.modules.search.entity.dos;
import cn.lili.mybatis.BaseEntity;
import cn.lili.mybatis.BaseIdEntity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
/**
@ -29,7 +23,7 @@ import java.util.Date;
@AllArgsConstructor
@NoArgsConstructor
@TableName("li_hot_words_history")
public class HotWordsHistory extends BaseIdEntity {
public class HotWordsHistory extends BaseIdEntity implements Comparable<HotWordsHistory>, Serializable {
/**
*
@ -41,7 +35,18 @@ public class HotWordsHistory extends BaseIdEntity {
*/
private Integer score;
@ApiModelProperty(value = "创建时间")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createTime;
public HotWordsHistory(String keywords, Integer score) {
this.keywords = keywords;
this.score = score;
}
@Override
public int compareTo(HotWordsHistory hotWordsHistory) {
return hotWordsHistory.getScore() - this.score;
}
}

View File

@ -52,6 +52,9 @@ public class HotWordsSearchParams extends PageVO {
private Date endTime;
//搜索热词数量
private Integer top = 50;
public <T> QueryWrapper<T> queryWrapper() {
//组织查询时间
QueryWrapper<T> queryWrapper = new QueryWrapper<>();

View File

@ -26,7 +26,7 @@ public interface HotWordsHistoryMapper extends BaseMapper<HotWordsHistory> {
* @param queryWrapper 查询条件
* @return 订单统计列表
*/
@Select("SELECT score,keywords,DATE_FORMAT(create_time,'%Y-%m-%d') AS create_time FROM li_hot_words_history " +" ${ew.customSqlSegment}")
@Select("SELECT sum(score) as score,keywords FROM li_hot_words_history " +" ${ew.customSqlSegment}")
List<HotWordsHistory> statistics(@Param(Constants.WRAPPER) Wrapper queryWrapper);
}

View File

@ -1,5 +1,6 @@
package cn.lili.modules.search.service;
import cn.lili.modules.search.entity.dos.HotWordsHistory;
import cn.lili.modules.search.entity.dto.HotWordsDTO;
import java.util.List;
@ -21,6 +22,14 @@ public interface HotWordsService {
*/
List<String> getHotWords(Integer count);
/**
* 获取热门关键词
*
* @param count 热词数量
* @return 热词集合
*/
List<HotWordsHistory> getHotWordsVO(Integer count);
/**
* 设置热门关键词
*

View File

@ -1,11 +1,16 @@
package cn.lili.modules.search.serviceimpl;
import cn.lili.common.utils.DateUtil;
import cn.lili.modules.search.entity.dos.HotWordsHistory;
import cn.lili.modules.search.entity.dto.HotWordsSearchParams;
import cn.lili.modules.search.mapper.HotWordsHistoryMapper;
import cn.lili.modules.search.service.HotWordsHistoryService;
import cn.lili.modules.search.service.HotWordsService;
import cn.lili.modules.statistics.entity.enums.SearchTypeEnum;
import cn.lili.modules.statistics.util.StatisticsDateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
@ -19,55 +24,30 @@ import java.util.*;
@Service
public class HotWordsHistoryServiceImpl extends ServiceImpl<HotWordsHistoryMapper, HotWordsHistory> implements HotWordsHistoryService {
@Autowired
private HotWordsService hotWordsService;
@Override
public List<HotWordsHistory> statistics(HotWordsSearchParams hotWordsSearchParams) {
if (hotWordsSearchParams.getSearchType().equals(SearchTypeEnum.TODAY.name())) {
return hotWordsService.getHotWordsVO(hotWordsSearchParams.getTop());
}
QueryWrapper queryWrapper = hotWordsSearchParams.queryWrapper();
queryWrapper.groupBy("keywords");
queryWrapper.orderByDesc("score");
queryWrapper.last("limit " + hotWordsSearchParams.getTop());
List<HotWordsHistory> list = baseMapper.statistics(queryWrapper);
return initData(list, hotWordsSearchParams);
return list;
}
@Override
public List<HotWordsHistory> queryByDay(Date queryTime) {
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("create_time", queryTime);
Date[] dates = StatisticsDateUtil.getDateArray(queryTime);
queryWrapper.between("create_time", dates[0], dates[1]);
return list(queryWrapper);
}
/**
* 历史统计查询
*
* @param source
* @return
*/
private List<HotWordsHistory> initData(List<HotWordsHistory> source, HotWordsSearchParams hotWordsSearchParams) {
//结果集
List<HotWordsHistory> onlineMemberVOS = new ArrayList<>();
//时间初始化
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT-8:00"));
calendar.setTime(hotWordsSearchParams.getStartTIme());
//判定是否超出时间 如果结束时间在循环时间之后则跳出循环
while (hotWordsSearchParams.getEndTime().after(calendar.getTime())) {
// //筛选时间相等的查询结果
Optional<HotWordsHistory> first = source.stream().filter(item -> item.getCreateTime().equals(calendar.getTime())).findFirst();
if (first.isPresent()) {
onlineMemberVOS.add(first.get());
} else {
onlineMemberVOS.add(new HotWordsHistory(hotWordsSearchParams.getKeywords(), 0, calendar.getTime()));
}
// for (HotWordsHistory hotWordsHistory : source) {
// System.out.println(hotWordsHistory.getCreateTime().getTime() + "-" + calendar.getTime().getTime());
// if (hotWordsHistory.getCreateTime().equals(calendar.getTime())) {
// onlineMemberVOS.add(hotWordsHistory);
// } else {
// onlineMemberVOS.add(new HotWordsHistory(hotWordsSearchParams.getKeywords(), 0, calendar.getTime()));
// }
// }
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
}
return onlineMemberVOS;
}
}

View File

@ -2,20 +2,19 @@ package cn.lili.modules.search.serviceimpl;
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.modules.search.entity.dos.HotWordsHistory;
import cn.lili.modules.search.entity.dto.HotWordsDTO;
import cn.lili.modules.search.mapper.HotWordsHistoryMapper;
import cn.lili.modules.search.service.HotWordsService;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.entity.enums.SettingEnum;
import cn.lili.modules.system.service.SettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
/**
* HotWordsServiceImpl
@ -24,6 +23,7 @@ import java.util.Set;
* @version v1.0
* 2022-04-14 09:35
*/
@Slf4j
@Service
public class HotWordsServiceImpl implements HotWordsService {
@ -32,6 +32,7 @@ public class HotWordsServiceImpl implements HotWordsService {
*/
@Autowired
private Cache<Object> cache;
@Override
public List<String> getHotWords(Integer count) {
if (count == null) {
@ -50,6 +51,33 @@ public class HotWordsServiceImpl implements HotWordsService {
return hotWords;
}
@Override
public List<HotWordsHistory> getHotWordsVO(Integer count) {
if (count == null) {
count = 50;
}
List<HotWordsHistory> hotWords = new ArrayList<>();
// redis 排序中下标从0开始所以这里需要 -1 处理
count = count - 1;
Set<ZSetOperations.TypedTuple<Object>> set = cache.reverseRangeWithScores(CachePrefix.HOT_WORD.getPrefix(), count);
if (set == null || set.isEmpty()) {
return new ArrayList<>();
}
for (ZSetOperations.TypedTuple<Object> defaultTypedTuple : set) {
try {
hotWords.add(new HotWordsHistory(defaultTypedTuple.getValue().toString(),
defaultTypedTuple.getScore().intValue()));
} catch (Exception e) {
log.error("读取热词错误", e);
}
}
Collections.sort(hotWords);
return hotWords;
}
@Override
public void setHotWords(HotWordsDTO hotWords) {
cache.incrementScore(CachePrefix.HOT_WORD.getPrefix(), hotWords.getKeywords(), hotWords.getPoint());

View File

@ -121,4 +121,30 @@ public class StatisticsDateUtil {
}
}
/**
* 根据一个日期获取这一天的开始时间和结束时间
*
* @param queryDate
* @return
*/
public static Date[] getDateArray(Date queryDate) {
Date[] dateArray = new Date[2];
Calendar calendar = Calendar.getInstance();
calendar.setTime(queryDate);
//时间归到今天凌晨0点
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
dateArray[0] = calendar.getTime();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 1);
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) - 1);
dateArray[1] = calendar.getTime();
return dateArray;
}
}

View File

@ -2,6 +2,7 @@ package cn.lili.controller.other;
import cn.lili.common.enums.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.search.entity.dos.HotWordsHistory;
import cn.lili.modules.search.entity.dto.HotWordsDTO;
import cn.lili.modules.search.entity.dto.HotWordsSearchParams;
import cn.lili.modules.search.service.HotWordsHistoryService;
@ -15,7 +16,10 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
/**
* 管理端,热词管理
@ -58,7 +62,9 @@ public class HotWordsManagerController {
@ApiOperation(value = "历史热词")
@GetMapping("/history")
public ResultMessage<Object> deleteWords(HistorySearchParams historySearchParams) {
return ResultUtil.data(hotWordsHistoryService.queryByDay(historySearchParams.getDate()));
List<HotWordsHistory> hotWordsHistoryList = hotWordsHistoryService.queryByDay(historySearchParams.getDate());
Collections.sort(hotWordsHistoryList);
return ResultUtil.data(hotWordsHistoryList);
}
@ApiOperation(value = "热词统计")
@ -66,8 +72,6 @@ public class HotWordsManagerController {
public ResultMessage<Object> deleteWords(HotWordsSearchParams hotWordsSearchParams) {
return ResultUtil.data(hotWordsHistoryService.statistics(hotWordsSearchParams));
}
}
@Data