diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java index 19d8e73374..af891dc817 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java @@ -166,6 +166,14 @@ public class RedisUtils { redisTemplate.opsForHash().delete(key, fields); } + public void rightPush(String key, Object value, long expire){ + redisTemplate.opsForList().rightPush(key, value); + + if (expire != NOT_EXPIRE) { + expire(key, expire); + } + } + public void leftPush(String key, Object value) { leftPush(key, value, DEFAULT_EXPIRE); } diff --git a/epmet-user/epmet-user-client/src/main/java/com/epmet/dto/form/UserBadgeUnitFormDTO.java b/epmet-user/epmet-user-client/src/main/java/com/epmet/dto/form/UserBadgeUnitFormDTO.java index 406f527bb9..a68dd2157e 100644 --- a/epmet-user/epmet-user-client/src/main/java/com/epmet/dto/form/UserBadgeUnitFormDTO.java +++ b/epmet-user/epmet-user-client/src/main/java/com/epmet/dto/form/UserBadgeUnitFormDTO.java @@ -6,7 +6,7 @@ import java.io.Serializable; import java.util.Objects; /** - * @Description 用户-徽章缓存单元 + * @Description 用户-徽章缓存单元(专为redis操作提供) 尽量不要使用这个DTO,因为重写了hashcode和equals!!!!!!! * @ClassName UserBadgeUnitFormDTO * @Auth wangc * @Date 2020-11-05 10:00 diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/BadgeController.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/BadgeController.java index 12042c4fbd..c284939571 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/BadgeController.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/BadgeController.java @@ -12,12 +12,10 @@ import com.epmet.dto.result.BadgeAuditRecordResultDTO; import com.epmet.dto.result.BadgeAuditingResultDTO; import com.epmet.dto.result.BadgeDetailResultDTO; import com.epmet.dto.result.BadgeListResultDTO; +import com.epmet.redis.UserBadgeRedis; import com.epmet.service.BadgeService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -150,4 +148,17 @@ public class BadgeController { return new Result(); } + @Autowired + private UserBadgeRedis redis; + @PostMapping("compensation-mechanism") + public Result compensationMechanism(@RequestParam("userId")String userId,@RequestParam("customerId")String customerId,@RequestParam("b1")String b1, + @RequestParam("b2")String b2){ + redis.pushOrRemoveUserBadge4List(userId,b1,customerId); + redis.pushOrRemoveUserBadge4List(userId,b1,customerId); + redis.pushOrRemoveUserBadge4List(userId,b1,customerId); + redis.pushOrRemoveUserBadge4List(userId,b2,customerId); + redis.batchClearUserBadgeCache(customerId); + redis.obtainUserBadge2List(userId,customerId); + return new Result(); + } } diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserBadgeDao.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserBadgeDao.java index 65910f22cd..b304118d3d 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserBadgeDao.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserBadgeDao.java @@ -4,6 +4,7 @@ import com.epmet.dto.UserBadgeCertificateRecordDTO; import com.epmet.dto.form.AuthFieldFormDTO; import com.epmet.dto.form.OpenedOrClosedFormDTO; import com.epmet.dto.form.UserBadgeListFormDTO; +import com.epmet.dto.form.UserBadgeUnitFormDTO; import com.epmet.dto.result.AuthFieldResultDTO; import com.epmet.dto.result.CertificationDetailResultDTO; import com.epmet.dto.result.UserBadgeListResultDTO; @@ -92,4 +93,14 @@ public interface UserBadgeDao { * @date 2020/11/4 6:04 下午 */ void updateIsOpen(OpenedOrClosedFormDTO openedOrClosedFormDTO); + + /** + * @Description 查找用户全部的显示徽章(排序) + * @param userId + * @param customerId + * @return java.util.List + * @author wangc + * @date 2020.11.09 14:14 + */ + List selectUserSortedBadge(@Param("userId")String userId,@Param("customerId")String customerId); } diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBadgeRedis.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBadgeRedis.java index f5c22ca54a..759ae00192 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBadgeRedis.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBadgeRedis.java @@ -16,10 +16,14 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataAccessException; +import org.springframework.data.redis.connection.RedisConnection; +import org.springframework.data.redis.connection.StringRedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; +import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.Set; @@ -106,8 +110,23 @@ public class UserBadgeRedis { * @date 2020.11.05 13:26 */ public List obtainUserBadge2List(String userId,String customerId) { - //TODO 补偿 - return redisUtils.lrange(UserRedisKeys.getResiUserBadgeKey(customerId,userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class); + List cache = + redisUtils.lrange(UserRedisKeys.getResiUserBadgeKey(customerId,userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class); + if(!CollectionUtils.isEmpty(cache)) return cache; + //补偿 + cache = badgeService.getUserSortedBadge(userId,customerId); + if(CollectionUtils.isEmpty(cache)) return cache; + final List sortedBadges = cache; + redisTemplate.executePipelined((RedisCallback>) connection ->{ + sortedBadges.forEach(badge -> { + + connection.listCommands().rPush(UserRedisKeys.getResiUserBadgeKey(customerId, userId).getBytes(), + redisTemplate.getValueSerializer().serialize(badge)); + }); + return null; + }); + + return cache; } /** @@ -160,12 +179,11 @@ public class UserBadgeRedis { * @date 2020.11.09 10:02 */ public void batchClearUserBadgeCache(String customerId){ + Set key = redisUtils.keys(UserRedisKeys.getResiUserBadgeKey(customerId, null)); + if(CollectionUtils.isEmpty(key)) return; + final Set keys = key; redisTemplate.executePipelined((RedisCallback) connection ->{ - Set keys = - connection.keys(redisTemplate.getKeySerializer().serialize(UserRedisKeys.getResiUserBadgeKey(customerId, null))); - if(!CollectionUtils.isEmpty(keys)){ - keys.forEach( ser -> {connection.del(ser);}); - } + keys.forEach( ser -> {connection.del(redisTemplate.getKeySerializer().serialize(ser));}); return null; }); diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/UserBadgeService.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/UserBadgeService.java index 81213550b9..65e9494cc8 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/UserBadgeService.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/UserBadgeService.java @@ -81,4 +81,14 @@ public interface UserBadgeService { */ void reloadCustomerBadge(String customerId); + /** + * @Description 得到用户全部的显示徽章(排序) + * @param userId + * @param customerId + * @return java.util.List + * @author wangc + * @date 2020.11.09 13:45 + */ + List getUserSortedBadge(String userId,String customerId); + } diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserBadgeServiceImpl.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserBadgeServiceImpl.java index 2f3f51ed86..4d71f25c04 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserBadgeServiceImpl.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserBadgeServiceImpl.java @@ -10,6 +10,7 @@ import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.PhoneValidatorUtils; import com.epmet.constant.BadgeConstant; import com.epmet.constant.SmsTemplateConstant; +import com.epmet.dao.ResiUserBadgeDao; import com.epmet.dao.UserBadgeDao; import com.epmet.dto.UserBadgeCertificateRecordDTO; import com.epmet.dto.form.*; @@ -254,4 +255,17 @@ public class UserBadgeServiceImpl implements UserBadgeService { userBadgeRedis.setCustomerBadge(userBadgeListResultDTOS, customerId); } + /** + * @Description 得到用户全部的显示徽章(排序) + * @param userId + * @param customerId + * @return java.util.List + * @author wangc + * @date 2020.11.09 13:45 + */ + @Override + public List getUserSortedBadge(String userId, String customerId) { + return userBadgeDao.selectUserSortedBadge(userId,customerId); + } + } diff --git a/epmet-user/epmet-user-server/src/main/resources/mapper/UserBadgeDao.xml b/epmet-user/epmet-user-server/src/main/resources/mapper/UserBadgeDao.xml index a20a9c0e61..33b3c4eabb 100644 --- a/epmet-user/epmet-user-server/src/main/resources/mapper/UserBadgeDao.xml +++ b/epmet-user/epmet-user-server/src/main/resources/mapper/UserBadgeDao.xml @@ -180,4 +180,49 @@ ) + +