16位卡号
工具类(规则可自己修改生成)
package com.meditrusthealth.mth.equity.service.util;
import org.apache.commons.lang3.StringUtils;
import java.util.Random;
public class CodeNumberUtil {
private static int i = 0;
public static final String NUMBERCHAR = "0123456789";
public static final String ENGILSHCHAR = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
public static String getCodeNumber(String prefix) {
if (!StringUtils.isEmpty(prefix)) {
String num = generateNumString(6);
String st = prefix + num + getUnixTime();
return st + getCardCheckCode(st);
}
return "prefix不能为空";
}
public static String getCodeNumber(String prefix, String code) {
if (!StringUtils.isEmpty(prefix) && !StringUtils.isEmpty(code)) {
String st = prefix + code + getUnixTime();
return st + getCardCheckCode(st);
}
return "prefix和code不能为空";
}
public static boolean checkCode(String code) {
char bit = getCardCheckCode(code.substring(0, code.length() - 1));
if (bit == 'N') {
return false;
}
return code.charAt(code.length() - 1) == bit;
}
private static char getCardCheckCode(String nonCheckCodeCardNo) {
if (nonCheckCodeCardNo == null
|| nonCheckCodeCardNo.trim().length() == 0
|| !nonCheckCodeCardNo.matches("\\d+")) {
return 'N';
}
char[] chs = nonCheckCodeCardNo.trim().toCharArray();
int luhmSum = 0;
for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
int k = chs[i] - '0';
if (j % 2 == 0) {
k *= 2;
k = k / 10 + k % 10;
}
luhmSum += k;
}
return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
}
private static String getUnixTime() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
i = i > 100 ? i % 10 : i;
return ((System.currentTimeMillis() / 100) + "").substring(5)
+ (i % 10);
}
private static String generateNumString(int length) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(NUMBERCHAR.charAt(random.nextInt(NUMBERCHAR.length())));
}
return sb.toString();
}
public static String getCodeEnglish(String prefix) {
if (!StringUtils.isEmpty(prefix)) {
String num = generateEngString(2);
String st = prefix + num + getUnixTime();
return st + getCardCheckCode(st);
}
return "prefix不能为空";
}
public static String getCodeEnglish(String prefix, String code) {
if (!StringUtils.isEmpty(prefix) && !StringUtils.isEmpty(code)) {
String st = prefix + code + getUnixTime();
return st + getCardCheckCode(st);
}
return "prefix和code不能为空";
}
private static String generateEngString(int length) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(ENGILSHCHAR.charAt(random.nextInt(ENGILSHCHAR.length())));
}
return sb.toString();
}
}
用例
@Transactional
@Async
public EquityCardBatchnoResp makeCardAndActivation(EquityCardBatchnoReq equityCardBatchnoReq) throws CommonException {
Date date = new Date();
long time = date.getTime();
EquityCardBatchno equityCardBatchno = new EquityCardBatchno();
BeanUtils.copyProperties(equityCardBatchnoReq,equityCardBatchno);
equityCardBatchnoReq.setCreateTime(date);
EquityCardBatchnoDetail equityCardBatchnoDetail = new EquityCardBatchnoDetail();
equityCardBatchnoDetail.setBatchNo(String.valueOf(time));
equityCardBatchno.setBacthCount(2000);
for (int i = 0; i < equityCardBatchno.getBacthCount(); i++) {
String cardId = CodeNumberUtil.getCodeNumber("99");
String activationCode = CodeNumberUtil.getCodeEnglish("MD");
boolean checkCode = CodeNumberUtil.checkCode(cardId);
if (checkCode) {
cardId=cardId.replaceAll("(.{4})","$1-");
cardId = cardId.substring(0, cardId.length() - 1);
equityCardBatchnoDetail.setCardNo(cardId);
equityCardBatchnoDetail.setActivationCode(activationCode);
int saveId = equityCardBatchnoDetailMapper.insert(equityCardBatchnoDetail);
if (saveId == 1) {
log.warn(
"{卡号:" + " " + cardId + "}" + " " + "保存成功" + "}" +
"{激活码:" + " " + activationCode + "}" + " " + "保存成功" + "}"
);
} else {
log.warn(
"{卡号:" + " " + cardId + "}" + " " + "保存失败" +
"{激活码:" + " " + activationCode + "}" + " " + "保存失败" + "}"
);
}
} else {
log.error(
"{卡号:" + " " + cardId + "}" + " " + "制卡失败,不符合编卡规则" + "}"
);
}
}
EquityCardBatchno cardBatchno = equityCardBatchnoMapper.selectById(equityCardBatchno.getId());
LambdaQueryWrapper<EquityCardBatchnoDetail> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(EquityCardBatchnoDetail::getBatchNo,cardBatchno.getBatchNo());
List<EquityCardBatchnoDetail> list = equityCardBatchnoDetailMapper.selectList(wrapper);
EquityCardBatchnoResp resp = new EquityCardBatchnoResp();
BeanUtils.copyProperties(cardBatchno,resp);
resp.setEquityCardBatchnoDetailList(list);
log.warn(
"{本次制卡数量:" + " " + equityCardBatchnoReq.getBacthCount() + "}" + " "+"已完成}"
);
return resp;
}
卡号(16位数字,每4位之间用-隔开)
String cardId = CodeNumberUtil.getCodeNumber("99");
String activationCode = CodeNumberUtil.getCodeEnglish("MD");
boolean checkCode = CodeNumberUtil.checkCode(cardId);
cardId=cardId.replaceAll("(.{4})","$1-");
cardId = cardId.substring(0, cardId.length() - 1);
System.out.println(cardId);
激活码(12位数字+随机字母)
String activationCode = CodeNumberUtil.getCodeEnglish("MD");