前言:记录一个生成有规则的编码,并且带有顺序
规则:生成一个PO开头的编号 并且是有序不重复的
private String getProductCode(String pType) {
String newCode = null;
if ("1".equals(pType)) {
QueryWrapper<MallProductInfo> queryWrapper = new QueryWrapper<>();
queryWrapper.select("max(product_code) as product_code");
queryWrapper.like("product_code", "PO%");
// 查询结果
MallProductInfo productInfo = productInfoDao.selectOne(queryWrapper);
// 如果查询结果为空,直接返回默认值
if (productInfo == null || productInfo.getProductCode() == null) {
newCode = "PO00001"; // 默认值
} else {
String pCode = productInfo.getProductCode();
// 提取数字部分,去除非数字字符
String numericPart = pCode.replaceAll("[^0-9]", "");
// 转换为整数并加1
int numericValue = Integer.parseInt(numericPart) + 1;
// 保持原有位数,并转换回字符串
String newNumericPart = String.format("%0" + numericPart.length() + "d", numericValue);
// 拼接成新的字符串
newCode = "PO" + newNumericPart;
}
}
return newCode;
}