You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.1 KiB

5 years ago
package com.epmet.redis;
5 years ago
import com.alibaba.fastjson.JSON;
import com.epmet.commons.tools.constant.NumConstant;
5 years ago
import com.epmet.commons.tools.redis.RedisUtils;
import com.epmet.commons.tools.utils.ConvertUtils;
5 years ago
import com.epmet.constant.BadgeConstant;
import com.epmet.constant.UserRedisKeys;
import com.epmet.dao.BadgeDao;
import com.epmet.dto.form.UserBadgeUnitFormDTO;
5 years ago
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;
5 years ago
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Optional;
5 years ago
import static com.epmet.commons.tools.redis.RedisUtils.MINUTE_THIRTY_EXPIRE;
5 years ago
/**
* @Author zxc
* @DateTime 2020/11/3 2:21 下午
*/
@Component
@Slf4j
5 years ago
public class UserBadgeRedis {
@Autowired
private RedisUtils redisUtils;
@Autowired
private BadgeDao badgeDao;
@Autowired
private UserBadgeService badgeService;
5 years ago
/**
* @Description 获取客户徽章信息
5 years ago
* @Param customerId
* @author zxc
* @date 2020/11/3 2:50 下午
*/
public Object getCustomerBadge(String customerId) {
5 years ago
Object userBadge = redisUtils.hGet(BadgeConstant.BADGE_KEY + customerId, BadgeConstant.BADGE);
5 years ago
return userBadge;
5 years ago
}
/**
* @Description 存放客户徽章信息
5 years ago
* @Param userBadge
* @Param customerId
* @author zxc
* @date 2020/11/3 2:51 下午
*/
public void setCustomerBadge(List<UserBadgeListResultDTO> userBadge, String customerId) {
redisUtils.hSet(BadgeConstant.BADGE_KEY + customerId, BadgeConstant.BADGE, JSON.toJSON(userBadge).toString(), -1);
5 years ago
}
/**
* @Description 删除客户徽章信息
* @Param customerId
* @author zxc
* @date 2020/11/5 3:06 下午
*/
public void delCustomerBadge(String customerId) {
redisUtils.hDel(BadgeConstant.BADGE_KEY + customerId, BadgeConstant.BADGE);
}
5 years ago
/**
* @Description 存放徽章审核 手机验证码
5 years ago
* @Param mobile
* @author zxc
* @date 2020/11/5 10:30 上午
*/
public void saveBadgeSmsCode(String mobile, String smsCode) {
5 years ago
String smsCodeKey = BadgeConstant.SMS_CODE_KEY + mobile;
redisUtils.set(smsCodeKey, smsCode, MINUTE_THIRTY_EXPIRE);
}
5 years ago
/**
* @Description 获取徽章审核 手机验证码
5 years ago
* @Param mobile
* @author zxc
* @date 2020/11/5 10:30 上午
*/
public String getBadgeSmsCode(String mobile) {
5 years ago
String smsCodeKey = BadgeConstant.SMS_CODE_KEY + mobile;
String smsCode = (String) redisUtils.get(smsCodeKey);
return smsCode;
}
/**
* @param userId
* @return java.util.List<com.epmet.dto.form.UserBadgeUnitFormDTO>
* @Description 获取用户的全部勋章信息(点亮的 排序从前到后)
* @author wangc
* @date 2020.11.05 13:26
*/
public List<UserBadgeUnitFormDTO> obtainUserBadge2List(String userId,String customerId) {
//TODO 补偿
return redisUtils.lrange(UserRedisKeys.getResiUserBadgeKey(customerId,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<UserBadgeUnitFormDTO> orient = obtainUserBadge2List(userId,customerId);
UserBadgeUnitFormDTO unit = null;
if (!CollectionUtils.isEmpty(orient)) {
Optional<UserBadgeUnitFormDTO> 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<UserBadgeListResultDTO> badgePool =
null == badgeObj ? badgeDao.selectCustomerBadgePool(customerId) : JSON.parseArray(badgeObj.toString(), UserBadgeListResultDTO.class);
if (CollectionUtils.isEmpty(badgePool)) {
log.error("com.epmet.redis.UserBadgeRedis.pushOrRemoveUserBadge4List,缓存中客户{}下的徽章池为空", customerId);
return NumConstant.ZERO;
}
if (null == unit) {
Optional<UserBadgeListResultDTO> 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;
}
}