private Set<Integer> getCouponCode(Integer count) {
Set<Integer> set = new HashSet<>();
while (true) {
String code = "";
//随机生成1位数,判断code是8位还是9位
Integer random = Integer.valueOf(getRandom(1));
if (random % 2 == 0) {//8
code = getRandom(3) + getRandom(3) + getRandom(2);
} else {
code = getRandom(3) + getRandom(3) + getRandom(2) + getRandom(1);
}
Integer v = Integer.valueOf(code);
if (v > 10000000) {
set.add(Integer.valueOf(code));
}
if (set.size() >= count) {
break;
}
}
//查数据库是否有重复的
List<UserCouponExchangeEntity> list = this.baseMapper.selectList(new EntityWrapper<UserCouponExchangeEntity>().in("exchange_code", StringUtils.join(set.toArray(), ",")));
if (list.size() > 0) {
for (UserCouponExchangeEntity userCouponExchange : list) {
set.remove(userCouponExchange.getExchangeCode());
}
Set<Integer> setNew = getCouponCode(list.size());
set.addAll(setNew);
return set;
}
return set;
}
private String getRandom(int length) {
String val = "";
Random random = new Random();
for (int i = 0; i < length; i++) {
val += String.valueOf(random.nextInt(10));
}
return val;
}