Commit e8a403ca authored by zhaobiyan's avatar zhaobiyan

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

parent 31cd04db
...@@ -1409,4 +1409,8 @@ public class RedisHelper { ...@@ -1409,4 +1409,8 @@ public class RedisHelper {
public boolean execute(DefaultRedisScript<Boolean> defaultRedisScript, List<String> keys, Object... args) { public boolean execute(DefaultRedisScript<Boolean> defaultRedisScript, List<String> keys, Object... args) {
return operations.execute(defaultRedisScript, keys, 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 { ...@@ -459,4 +459,11 @@ public class MemberUserController {
} }
return success(null); 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; ...@@ -49,6 +49,7 @@ import com.google.common.annotations.VisibleForTesting;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -215,6 +216,7 @@ public class MemberUserServiceImpl implements MemberUserService { ...@@ -215,6 +216,7 @@ public class MemberUserServiceImpl implements MemberUserService {
} }
user.setEnglishName(englishName); user.setEnglishName(englishName);
user.setNickname(englishName); user.setNickname(englishName);
user.setCode(getNextMemberCode());
memberUserMapper.insert(user); memberUserMapper.insert(user);
//站内信 //站内信
InternalMessageCreateDto dto = InternalMessageCreateDto.builder().toIdList(Arrays.asList(user.getId())) InternalMessageCreateDto dto = InternalMessageCreateDto.builder().toIdList(Arrays.asList(user.getId()))
...@@ -565,14 +567,23 @@ public class MemberUserServiceImpl implements MemberUserService { ...@@ -565,14 +567,23 @@ public class MemberUserServiceImpl implements MemberUserService {
private String getNextMemberCode() { private String getNextMemberCode() {
String key = "memberCode:max:number"; String key = "memberCode:max:number";
Boolean hasMemberCodeMaxNumber = redisHelper.hasKey(key); String incrIfExistScript = "if redis.call(\"exists\", KEYS[1]) == 1 then\n" +
if (hasMemberCodeMaxNumber) { " return redis.call(\"incr\", KEYS[1])\n" +
return MemberUserCodeUtils.generateMemberCode(redisHelper.incrBy(key, 1)); "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) { synchronized(this) {
hasMemberCodeMaxNumber = redisHelper.hasKey(key); nextMemberCodeNumber = redisHelper.execute4Long(redisScript, Collections.singletonList(key));
if (hasMemberCodeMaxNumber) { if (nextMemberCodeNumber != null) {
return MemberUserCodeUtils.generateMemberCode(redisHelper.incrBy(key, 1)); return MemberUserCodeUtils.generateMemberCode(nextMemberCodeNumber);
} }
String currentMaxMemberCode = memberUserMapper.getCurrentMaxMemberCode(); String currentMaxMemberCode = memberUserMapper.getCurrentMaxMemberCode();
Long memberCodeMaxNumber = MemberUserCodeUtils.getMemberCodeNumber(currentMaxMemberCode); 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