public static List<String> genInviteCode(HashMap<String, String> codes,int length, int num) {
List<String> strList = new ArrayList<String> ();
char[] d = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9'};
int len = 35;
Random random = new Random();
while(num > 0) {
int l = length;
StringBuffer sb = new StringBuffer();
while(l > 0) {
sb.append(d[random.nextInt(len)]);
l --;
}
String inviteCode = sb.toString();
if(codes.containsKey(inviteCode)) {
continue;
}
codes.put(inviteCode, "A");
strList.add(inviteCode);
num--;
}
return strList;
}
/**
* 获取6895900个不能重复的邀请码
*
* @param list
*/
private void creatAndCheckInviteCode(List<String> list) {
//要发送的邀请码总数
int count = 66666;
HashMap<String, String> hash = new HashMap<String,String> ();
while (count > 0) {
List<String> inviteCodeList = RandomUtil.genInviteCode(hash, 5, 10);
System.out.println(inviteCodeList.toString());
list.addAll(inviteCodeList);
count--;
}
}
//每个用户发十个邀请码
@PostMapping("index")
private void index() {
List<String> inviteCodes = new ArrayList<>();
inviteCodes.add("1");
Long addTime = System.currentTimeMillis();
long l = 1570550399000L - addTime;
creatAndCheckInviteCode(inviteCodes);
if (inviteCodes != null && inviteCodes.size() > 0) {
EntityWrapper<Organization> userEntityWrapper = new EntityWrapper<>();
userEntityWrapper.eq("organization_type", 3);
userEntityWrapper.eq("organization_state", 1);
userEntityWrapper.eq("state", 1);
userEntityWrapper.eq("group_id", 2);
List<Organization> organizationList = organizationService.selectList(userEntityWrapper);
int size = organizationList.size();
for (int i = 0; i < size; i++) {
List<InviteCodeCopy1> inviteCodeCopy1List = new ArrayList<>();
int count = 0;
Organization organization = organizationList.get(i);
int orgId = organization.getOrganizationId();
while (count <= 9) {
String ic = inviteCodes.get(i*10 + count);
InviteCodeCopy1 inviteCodeCopy1 = new InviteCodeCopy1(null, 2, ic, orgId, "", 7, "", BigDecimal.valueOf(0), null, addTime, l, 2, 1, 0L, 0L, "");
inviteCodeCopy1.setRemark("国庆特派");
inviteCodeCopy1.setCmsUserId(null);
inviteCodeCopy1List.add(inviteCodeCopy1);
System.out.println(inviteCodeCopy1.getOrganizationId());
count++;
}
inviteCodeCopy1Service.insertBatch(inviteCodeCopy1List);
}
}
}
不重复随机数生成工具类
最新推荐文章于 2021-06-06 13:08:52 发布