Browse Source

获取用户勋章增加补偿,批量删除用户徽章,将keys操作放在管道外,否则返回null,具体原因还在查。。。

dev_shibei_match
wangchao 5 years ago
parent
commit
6bb97351e1
  1. 8
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisUtils.java
  2. 2
      epmet-user/epmet-user-client/src/main/java/com/epmet/dto/form/UserBadgeUnitFormDTO.java
  3. 19
      epmet-user/epmet-user-server/src/main/java/com/epmet/controller/BadgeController.java
  4. 11
      epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserBadgeDao.java
  5. 32
      epmet-user/epmet-user-server/src/main/java/com/epmet/redis/UserBadgeRedis.java
  6. 10
      epmet-user/epmet-user-server/src/main/java/com/epmet/service/UserBadgeService.java
  7. 14
      epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserBadgeServiceImpl.java
  8. 45
      epmet-user/epmet-user-server/src/main/resources/mapper/UserBadgeDao.xml

8
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);
}

2
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

19
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();
}
}

11
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<com.epmet.dto.form.UserBadgeUnitFormDTO>
* @author wangc
* @date 2020.11.09 14:14
*/
List<UserBadgeUnitFormDTO> selectUserSortedBadge(@Param("userId")String userId,@Param("customerId")String customerId);
}

32
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<UserBadgeUnitFormDTO> obtainUserBadge2List(String userId,String customerId) {
//TODO 补偿
return redisUtils.lrange(UserRedisKeys.getResiUserBadgeKey(customerId,userId), NumConstant.ZERO, NumConstant.ONE_NEG, UserBadgeUnitFormDTO.class);
List<UserBadgeUnitFormDTO> 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<UserBadgeUnitFormDTO> sortedBadges = cache;
redisTemplate.executePipelined((RedisCallback<List<UserBadgeUnitFormDTO>>) 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<String> key = redisUtils.keys(UserRedisKeys.getResiUserBadgeKey(customerId, null));
if(CollectionUtils.isEmpty(key)) return;
final Set<String> keys = key;
redisTemplate.executePipelined((RedisCallback<String>) connection ->{
Set<byte[]> 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;
});

10
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<com.epmet.dto.form.UserBadgeUnitFormDTO>
* @author wangc
* @date 2020.11.09 13:45
*/
List<UserBadgeUnitFormDTO> getUserSortedBadge(String userId,String customerId);
}

14
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<com.epmet.dto.form.UserBadgeUnitFormDTO>
* @author wangc
* @date 2020.11.09 13:45
*/
@Override
public List<UserBadgeUnitFormDTO> getUserSortedBadge(String userId, String customerId) {
return userBadgeDao.selectUserSortedBadge(userId,customerId);
}
}

45
epmet-user/epmet-user-server/src/main/resources/mapper/UserBadgeDao.xml

@ -180,4 +180,49 @@
)
</insert>
<!-- 查找用户全部的显示徽章(排序) -->
<select id="selectUserSortedBadge" resultType="com.epmet.dto.form.UserBadgeUnitFormDTO">
SELECT
userBadge.badge_id,
badgeInfo.BADGE_NAME,
badgeInfo.BADGE_ICON
FROM
resi_user_badge userBadge
LEFT JOIN (
SELECT
id AS badgeId,
CUSTOMER_ID,
BADGE_NAME,
BADGE_ICON
FROM
(
SELECT
*
FROM
badge
WHERE
CUSTOMER_ID = #{customerId}
AND DEL_FLAG = '0'
AND BADGE_STATUS = 'online'
UNION ALL
SELECT
*
FROM
badge a
WHERE
CUSTOMER_ID = 'default'
AND a.DEL_FLAG = '0'
AND BADGE_STATUS = 'online'
AND NOT EXISTS ( SELECT ID FROM badge b WHERE CUSTOMER_ID = #{customerId} AND a.ID = b.ID )
) temp
) badgeInfo ON userBadge.badge_id = badgeInfo.badgeId
WHERE
userBadge.del_flag = '0'
AND userBadge.user_id = #{userId}
AND userBadge.customer_id = #{customerId}
AND userBadge.is_opened = 1
AND userBadge.certification_autid_status = 'approved'
ORDER BY
userBadge.updated_time DESC
</select>
</mapper>

Loading…
Cancel
Save