Commit e8a403ca authored by zhaobiyan's avatar zhaobiyan

新增会员编码字段,刷新历史数据

parent 31cd04db
......@@ -1409,4 +1409,8 @@ public class RedisHelper {
public boolean execute(DefaultRedisScript<Boolean> defaultRedisScript, List<String> keys, Object... args) {
return operations.execute(defaultRedisScript, keys, args);
}
public Long execute4Long(DefaultRedisScript<Long> defaultRedisScript, List<String> keys, Object... args) {
return operations.execute(defaultRedisScript, keys, args);
}
}
\ No newline at end of file
......@@ -459,4 +459,11 @@ public class MemberUserController {
}
return success(null);
}
@PutMapping("/memberCode/flush")
@ApiOperation("刷新历史会员编码")
public CommonResult<String> memberCodeFlush() {
userService.historyCodeFlush();
return success(null);
}
}
......@@ -49,6 +49,7 @@ import com.google.common.annotations.VisibleForTesting;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -215,6 +216,7 @@ public class MemberUserServiceImpl implements MemberUserService {
}
user.setEnglishName(englishName);
user.setNickname(englishName);
user.setCode(getNextMemberCode());
memberUserMapper.insert(user);
//站内信
InternalMessageCreateDto dto = InternalMessageCreateDto.builder().toIdList(Arrays.asList(user.getId()))
......@@ -565,14 +567,23 @@ public class MemberUserServiceImpl implements MemberUserService {
private String getNextMemberCode() {
String key = "memberCode:max:number";
Boolean hasMemberCodeMaxNumber = redisHelper.hasKey(key);
if (hasMemberCodeMaxNumber) {
return MemberUserCodeUtils.generateMemberCode(redisHelper.incrBy(key, 1));
String incrIfExistScript = "if redis.call(\"exists\", KEYS[1]) == 1 then\n" +
" return redis.call(\"incr\", KEYS[1])\n" +
"else\n" +
" return nil\n" +
"end";
DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
redisScript.setScriptText(incrIfExistScript);
redisScript.setResultType(Long.class);
Long nextMemberCodeNumber = redisHelper.execute4Long(redisScript, Collections.singletonList(key));
if (nextMemberCodeNumber != null) {
return MemberUserCodeUtils.generateMemberCode(nextMemberCodeNumber);
}
synchronized(this) {
hasMemberCodeMaxNumber = redisHelper.hasKey(key);
if (hasMemberCodeMaxNumber) {
return MemberUserCodeUtils.generateMemberCode(redisHelper.incrBy(key, 1));
nextMemberCodeNumber = redisHelper.execute4Long(redisScript, Collections.singletonList(key));
if (nextMemberCodeNumber != null) {
return MemberUserCodeUtils.generateMemberCode(nextMemberCodeNumber);
}
String currentMaxMemberCode = memberUserMapper.getCurrentMaxMemberCode();
Long memberCodeMaxNumber = MemberUserCodeUtils.getMemberCodeNumber(currentMaxMemberCode);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment