package com.epmet.redis; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSON; import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.redis.RedisUtils; import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.constant.BadgeConstant; import com.epmet.constant.UserRedisKeys; import com.epmet.dao.BadgeDao; import com.epmet.dto.form.UserBadgeUnitFormDTO; import com.epmet.dto.result.BadgeListResultDTO; import com.epmet.dto.result.UserBadgeListResultDTO; import com.epmet.service.UserBadgeService; 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.stereotype.Component; import java.util.List; import java.util.Optional; import static com.epmet.commons.tools.redis.RedisUtils.MINUTE_THIRTY_EXPIRE; /** * @Author zxc * @DateTime 2020/11/3 2:21 下午 */ @Component @Slf4j public class UserBadgeRedis { @Autowired private RedisUtils redisUtils; @Autowired private BadgeDao badgeDao; @Autowired private UserBadgeService badgeService; /** * @Description 获取客户徽章信息 * @Param customerId * @author zxc * @date 2020/11/3 2:50 下午 */ public Object getCustomerBadge(String customerId) { Object userBadge = redisUtils.hGet(BadgeConstant.BADGE_KEY + customerId, BadgeConstant.BADGE); return userBadge; } /** * @Description 存放客户徽章信息 * @Param userBadge * @Param customerId * @author zxc * @date 2020/11/3 2:51 下午 */ public void setCustomerBadge(List userBadge, String customerId) { redisUtils.hSet(BadgeConstant.BADGE_KEY + customerId, BadgeConstant.BADGE, JSON.toJSON(userBadge).toString(), -1); } /** * @Description 删除客户徽章信息 * @Param customerId * @author zxc * @date 2020/11/5 3:06 下午 */ public void delCustomerBadge(String customerId) { redisUtils.hDel(BadgeConstant.BADGE_KEY + customerId, BadgeConstant.BADGE); } /** * @Description 存放徽章审核 手机验证码 * @Param mobile * @author zxc * @date 2020/11/5 10:30 上午 */ public void saveBadgeSmsCode(String mobile, String smsCode) { String smsCodeKey = BadgeConstant.SMS_CODE_KEY + mobile; redisUtils.set(smsCodeKey, smsCode, MINUTE_THIRTY_EXPIRE); } /** * @Description 获取徽章审核 手机验证码 * @Param mobile * @author zxc * @date 2020/11/5 10:30 上午 */ public String getBadgeSmsCode(String mobile) { String smsCodeKey = BadgeConstant.SMS_CODE_KEY + mobile; String smsCode = (String) redisUtils.get(smsCodeKey); return smsCode; } /** * @param userId * @return java.util.List * @Description 获取用户的全部勋章信息(点亮的 、 排序从前到后) * @author wangc * @date 2020.11.05 13:26 */ public List obtainUserBadge2List(String userId) { //TODO 补偿 return redisUtils.lrange(UserRedisKeys.getResiUserKey(userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class); } /** * 用户点亮或取消徽章 * @param * @return int * @Description 入栈时使用lpush 出栈时使用lrem * @author wangc * @date 2020.11.05 13:37 */ public long pushOrRemoveUserBadge4List(String userId, String badgeId, String customerId) { List orient = obtainUserBadge2List(userId); UserBadgeUnitFormDTO unit = null; if (!CollectionUtils.isEmpty(orient)) { Optional opt = orient.stream().filter(badge -> StringUtils.equals(badgeId, badge.getBadgeId())).findFirst(); if (opt.isPresent()) { unit = opt.get(); return redisUtils.lrem(UserRedisKeys.getResiUserBadgeKey(customerId, userId), NumConstant.ONE, unit); } } //徽章池 Object badgeObj = getCustomerBadge(customerId); List badgePool = null == badgeObj ? null : Optional.ofNullable(JSON.parseArray(badgeObj.toString(), UserBadgeListResultDTO.class)) .orElse(badgeDao.selectCustomerBadgePool(customerId)); if (CollectionUtils.isEmpty(badgePool)) { log.error("com.epmet.redis.UserBadgeRedis.pushOrRemoveUserBadge4List,缓存中客户{}下的徽章池为空", customerId); return NumConstant.ZERO; } if (null == unit) { Optional poolOpt = badgePool.stream().filter(badge -> StringUtils.equals(badge.getBadgeId(), badgeId)).findFirst(); if (poolOpt.isPresent()) unit = ConvertUtils.sourceToTarget(poolOpt.get(), UserBadgeUnitFormDTO.class); else return NumConstant.ZERO; } redisUtils.leftPush(UserRedisKeys.getResiUserBadgeKey(customerId, userId), unit); return NumConstant.ONE; } }