feat(crm): 添加客户编码自动生成功能

- 新增 selectMaxCustomerCode 方法查询最大客户编码
- 实现客户编码自动生成逻辑,支持纯数字和带前缀格式
- 添加正则表达式解析编码规则并递增末尾数字
- 集成编码生成功能到客户插入业务流程中
This commit is contained in:
2026-06-12 13:17:33 +08:00
parent 7a0d7e1b12
commit 2559dc27cb
3 changed files with 27 additions and 0 deletions

View File

@@ -12,4 +12,9 @@ import com.klp.common.core.mapper.BaseMapperPlus;
*/
public interface CrmCustomerMapper extends BaseMapperPlus<CrmCustomerMapper, CrmCustomer, CrmCustomerVo> {
/**
* 查询当前最大的customer_code用于自动生成新编码
*/
String selectMaxCustomerCode();
}

View File

@@ -91,6 +91,25 @@ public class CrmCustomerServiceImpl implements ICrmCustomerService {
@Override
public Boolean insertByBo(CrmCustomerBo bo) {
CrmCustomer add = BeanUtil.toBean(bo, CrmCustomer.class);
// 自动生成customer_code查询最大编码并加1
if (StringUtils.isBlank(add.getCustomerCode())) {
String maxCode = baseMapper.selectMaxCustomerCode();
if (StringUtils.isBlank(maxCode)) {
add.setCustomerCode("00001");
} else {
// 提取末尾数字后缀并递增,支持纯数字("00001")和带前缀("KH00001")两种格式
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("^(.*?)(\\d+)$");
java.util.regex.Matcher matcher = pattern.matcher(maxCode);
if (matcher.matches()) {
String prefix = matcher.group(1);
String numStr = matcher.group(2);
long num = Long.parseLong(numStr) + 1;
add.setCustomerCode(prefix + String.format("%0" + numStr.length() + "d", num));
} else {
add.setCustomerCode(maxCode + "1");
}
}
}
validEntityBeforeSave(add);
boolean flag = baseMapper.insert(add) > 0;
if (flag) {

View File

@@ -23,5 +23,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="delFlag" column="del_flag"/>
</resultMap>
<select id="selectMaxCustomerCode" resultType="String">
SELECT customer_code FROM crm_customer WHERE del_flag = 0 ORDER BY LENGTH(customer_code) DESC, customer_code DESC LIMIT 1
</select>
</mapper>