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 0ff3d39932a2290deb54ff6204ae697b6e331651..1924d3df8dfef4d039ff27f1e620c25399dddf40 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 6dccbf9f56d8ae59a2e4274a9aded40cad71c537..9d206669c4ce1674333c8c19e1a06dbe35988aec 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 0000000000000000000000000000000000000000..182fa292d0424474951b1ead6d1d6b2b08c5ecb0 --- /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 7a97ff05ded2ef5a700eb0644ef205c883d161ee..835e40f6d2d2ecc44dbc3e3e845dd86f4b8374e0 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 0000000000000000000000000000000000000000..c47dc8a3682ecbc15c84f1e3a9c06dc407a1bfd1 --- /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 a59aae407c866ea9d8ed9f7b7c78a733246c96e6..64ffd9926a5273bc4b6cc647df89e565dd0ee66e 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 0000000000000000000000000000000000000000..e4fbf431d0ed3773264bab00bd63359c5cee1055 --- /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 71d40f5037b9b051fcc2982c6b39d1237500700d..18409a456ff079490d5cfe7737afb99a386f05ad 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 53fc362639b4fd39afe345539ceebde2f217c752..83f5f7d3aaab15f23ae5cc2550dd80c231c5643d 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 9d912d1fa11b7fc7cf29fe1dddc1dd976a6d58c0..e4e6bf60f7ed14e2c91a7253ea44d227e6b43b45 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,