From f5dbd2288679dcb439fdf370777cf26cb0dbf9ac Mon Sep 17 00:00:00 2001 From: chenjin Date: Sat, 26 Feb 2022 23:17:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=92=E8=A1=8C=E6=A6=9C?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F=E4=B8=BAredis=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/controller/ds/DsCommonController.java | 57 +++++++++++-- .../com/ruoyi/common/constant/Constants.java | 5 ++ .../com/ruoyi/common/utils/RedisUtil.java | 85 +++++++++++++++++++ .../ruoyi/system/mapper/SysUserMapper.java | 2 + .../system/service/IDsCommonService.java | 20 +++++ .../ruoyi/system/service/ISysUserService.java | 3 + .../service/impl/DSCommonServiceImpl.java | 46 ++++++++++ .../service/impl/DsRecordServiceImpl.java | 7 ++ .../service/impl/SysUserServiceImpl.java | 7 ++ .../resources/mapper/system/SysUserMapper.xml | 13 ++- 10 files changed, 239 insertions(+), 6 deletions(-) create mode 100644 ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtil.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/service/IDsCommonService.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DSCommonServiceImpl.java diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/ds/DsCommonController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/ds/DsCommonController.java index 0ff3d39..1924d3d 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/ds/DsCommonController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/ds/DsCommonController.java @@ -1,8 +1,11 @@ package com.ruoyi.web.controller.ds; +import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.entity.SysUser; +import com.ruoyi.common.utils.RedisUtil; import com.ruoyi.system.domain.DsRecord; +import com.ruoyi.system.service.IDsCommonService; import com.ruoyi.system.service.IDsRecordService; import com.ruoyi.system.service.ISysUserService; import io.swagger.annotations.Api; @@ -13,8 +16,10 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.net.UnknownServiceException; import java.util.ArrayList; import java.util.List; +import java.util.Set; /** * @author ChenJin @@ -32,6 +37,12 @@ public class DsCommonController { @Autowired private ISysUserService userService; + + @Autowired + private IDsCommonService commonService; + + @Autowired + private RedisUtil redisUtil; /** * 根据用户id查询用户信息答题记录的一周内的积分总和 * 查询登录用户排名和排行榜前50 @@ -49,15 +60,51 @@ public class DsCommonController { score+=record.getScore(); } ajax.put("weekScore",score); - //排行榜前50,积分大于等于0才加入排行,管理员积分设置为-10 - List userList = userService.selectUserListByTop50(); - ajax.put("top50List",userList); + //当前用户排名 - Integer myRanking= userService.selectUserRankingByUserId(userId); - ajax.put("userRanking",myRanking); + Long myRanking= commonService.getUserRankingById(Constants.RANKING_KEY,userId); + if(myRanking==-1){ + ajax.put("userRanking","无"); + }else { + ajax.put("userRanking",myRanking); + } + //获取用户信息 SysUser user = userService.selectUserById(userId); ajax.put("user",user); return ajax; } + + + @ApiOperation("获取用户排名存入redis") + @GetMapping("/getTopRankingToRedis") + public AjaxResult getTopRankingToRedis(){ + //查询前50名用户 + List userList = userService.selectUserList(new SysUser()); + for (SysUser user:userList){ + redisUtil.opsForZSetAdd(Constants.RANKING_KEY,user.getUserId(),user.getScore()); + } + + return AjaxResult.success(); + } + + @ApiOperation("获取排行榜") + @GetMapping("/getRanking") + public AjaxResult getRanking(@RequestParam("userId") Long userId){ + AjaxResult ajax = AjaxResult.success(); + //排行榜前50 + List userIdList = commonService.getRankingTop(Constants.RANKING_KEY,0,50); + List userList = userService.selectByUserIds(userIdList); + ajax.put("top50List",userList); + //当前用户排名 + Long myRanking= commonService.getUserRankingById(Constants.RANKING_KEY,userId); + if(myRanking==-1){ + ajax.put("userRanking","无"); + }else { + ajax.put("userRanking",myRanking); + } + return ajax; + } + + } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java b/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java index 6dccbf9..9d20666 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java @@ -143,4 +143,9 @@ public class Constants * INSERT */ public static final String INSERT = "user"; + + /** + * cache 排名key + */ + public static final String RANKING_KEY = "scoreRanking"; } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtil.java new file mode 100644 index 0000000..182fa29 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtil.java @@ -0,0 +1,85 @@ +package com.ruoyi.common.utils; + +import io.lettuce.core.dynamic.annotation.CommandNaming; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +/** + * @author ChenJin + * @version 1.0 + * @date 2022/2/26 19:18 + * @remark redis工具类 + */ +@Component +public final class RedisUtil { + + @Resource + private RedisTemplate redisTemplate; + + + public Object get(String key){ + return redisTemplate.opsForValue().get(key); + } + + /** + * 设置过期时间 + * @param key key + * @param time 过期时间 + * @return boolean + */ + public boolean expire(String key,long time){ + try { + if(time>0){ + redisTemplate.expire(key,time, TimeUnit.SECONDS); + } + return true; + }catch (Exception e){ + e.printStackTrace(); + return false; + } + + } + + public boolean opsForZSetAdd(String key, Object member, double score){ + try { + redisTemplate.opsForZSet().add(key, member, score); + return true; + }catch (Exception e){ + e.printStackTrace(); + return false; + } + } + + public boolean opsForZSetIncrBy(String key, Object member, double score){ + try { + redisTemplate.opsForZSet().incrementScore(key,member,score); + return true; + }catch (Exception e){ + e.printStackTrace(); + return false; + } + } + + public Set opsForZSetReRang(String key, long m, long n){ + Set set = new HashSet<>(); + set = redisTemplate.opsForZSet().reverseRange(key, m, n); + return set; + } + + + public Long opsForZSetReRank(String key, Object member){ + Long aLong = redisTemplate.opsForZSet().reverseRank(key, member); + if(aLong==null){ + return -1L; + }else { + return aLong+1; + } + } + +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java index 7a97ff0..835e40f 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java @@ -138,4 +138,6 @@ public interface SysUserMapper * @return */ SysUser selectUserBYPhoneNumber(@Param("phonenumber") String phonenumber); + + List selectByUserIds(@Param("ids") List ids); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/IDsCommonService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/IDsCommonService.java new file mode 100644 index 0000000..c47dc8a --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/IDsCommonService.java @@ -0,0 +1,20 @@ +package com.ruoyi.system.service; + +import com.ruoyi.system.domain.model.RankInfo; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Set; + +/** + * @author ChenJin + * @version 1.0 + * @date 2022/2/26 20:20 + * @remark 备注 + */ +public interface IDsCommonService { + //获取排名前m-n + List getRankingTop(String key, long m, long n); + + Long getUserRankingById(String key, long id); +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java index a59aae4..64ffd99 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java @@ -199,4 +199,7 @@ public interface ISysUserService */ SysUser selectUserByPhoneNumber(String phonenumber); + + + List selectByUserIds(List ids); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DSCommonServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DSCommonServiceImpl.java new file mode 100644 index 0000000..e4fbf43 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DSCommonServiceImpl.java @@ -0,0 +1,46 @@ +package com.ruoyi.system.service.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.ruoyi.common.constant.Constants; +import com.ruoyi.common.utils.RedisUtil; +import com.ruoyi.system.domain.model.RankInfo; +import com.ruoyi.system.service.IDsCommonService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @author ChenJin + * @version 1.0 + * @date 2022/2/26 20:23 + * @remark 备注 + */ +@Service +public class DSCommonServiceImpl implements IDsCommonService { + + @Autowired + private RedisUtil redisUtil; + + + @Override + public List getRankingTop(String key, long m, long n) { + Set set = redisUtil.opsForZSetReRang(key, m, n); + List ids = new ArrayList<>(); + for (Object o : set){ + ids.add((Long) o); + } + return ids; + } + + @Override + public Long getUserRankingById(String key, long id) { + return redisUtil.opsForZSetReRank(key,id); + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DsRecordServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DsRecordServiceImpl.java index 71d40f5..18409a4 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DsRecordServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/DsRecordServiceImpl.java @@ -2,8 +2,10 @@ package com.ruoyi.system.service.impl; import java.util.List; +import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.common.utils.RedisUtil; import com.ruoyi.system.mapper.SysUserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -26,6 +28,9 @@ public class DsRecordServiceImpl implements IDsRecordService @Autowired private SysUserMapper sysUserMapper; + + @Autowired + private RedisUtil redisUtil; /** * 查询答题记录 * @@ -71,6 +76,8 @@ public class DsRecordServiceImpl implements IDsRecordService //执行修改用户积分 sysUserMapper.updateUser(user); dsRecord.setCreateTime(DateUtils.getNowDate()); + //redis排行榜覆盖积分 + redisUtil.opsForZSetAdd(Constants.RANKING_KEY,user.getUserId(),user.getScore()); //新增记录 return dsRecordMapper.insertDsRecord(dsRecord); } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java index 53fc362..83f5f7d 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java @@ -2,6 +2,8 @@ package com.ruoyi.system.service.impl; import java.util.ArrayList; import java.util.List; + +import org.apache.ibatis.annotations.Param; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -511,4 +513,9 @@ public class SysUserServiceImpl implements ISysUserService public SysUser selectUserByPhoneNumber(String phonenumber) { return userMapper.selectUserBYPhoneNumber(phonenumber); } + + @Override + public List selectByUserIds( List ids) { + return userMapper.selectByUserIds(ids); + } } diff --git a/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml b/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml index 9d912d1..e4e6bf6 100644 --- a/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml +++ b/ruoyi-system/src/main/resources/mapper/system/SysUserMapper.xml @@ -163,8 +163,19 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where phonenumber=#{phonenumber} + - + insert into sys_user( user_id, dept_id, -- Gitee