123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package com.fjhx.customer.utils.code;
- import cn.hutool.core.convert.Convert;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.text.CharSequenceUtil;
- import cn.hutool.core.util.ObjectUtil;
- import cn.hutool.extra.spring.SpringUtil;
- import com.baomidou.mybatisplus.extension.service.IService;
- import com.fjhx.customer.service.customer.CustomerService;
- import com.obs.services.internal.ServiceException;
- import lombok.Getter;
- import java.util.Date;
- import java.util.Map;
- @Getter
- public enum CodeEnum {
- // 客户
- CUSTOMER("CH", "yyMM-", "code", 3, CustomerService.class),
- ;
- CodeEnum(String prefix, String dateFormat, String codeFieldName, Integer length, Class<? extends IService<?>> serviceCls) {
- this.prefix = prefix;
- this.dateFormat = dateFormat;
- this.length = length;
- this.codeFieldName = codeFieldName;
- this.service = SpringUtil.getBean(serviceCls);
- }
- // 编码前缀
- private final String prefix;
- // 编码加日期规则
- private final String dateFormat;
- // 长度
- private final Integer length;
- // 编码字段名
- private final String codeFieldName;
- // service
- private final IService<?> service;
- /**
- * 获取键值对
- */
- public String getCode() {
- String itemPrefix;
- if (ObjectUtil.isNotEmpty(dateFormat)) {
- Date date = new Date();
- String format = DateUtil.format(date, dateFormat);
- itemPrefix = prefix + format;
- } else {
- itemPrefix = prefix;
- }
- Object obj = service.query()
- .likeRight(codeFieldName, itemPrefix)
- .orderByDesc(codeFieldName)
- .last("limit 1")
- .one();
- if (obj == null) {
- return itemPrefix + autoGenericCode(length, 0);
- }
- Map<String, Object> map = Convert.toMap(String.class, Object.class, obj);
- String code = Convert.toStr(map.get(CharSequenceUtil.toCamelCase(codeFieldName)));
- Integer codeNum = Convert.toInt(code.substring(itemPrefix.length()));
- if (ObjectUtil.isEmpty(codeNum)) {
- throw new ServiceException("自定义编码与系统编码生成规则冲突,暂时无法生成编码,请联系管理员");
- }
- return itemPrefix + autoGenericCode(length, codeNum);
- }
- /**
- * 获取键值对
- */
- public String getCode(String code) {
- if (ObjectUtil.isNotEmpty(code)) {
- Long count = service.query().eq(codeFieldName, code).count();
- if (count != 0) {
- throw new ServiceException("编码已存在");
- }
- return code;
- } else {
- return getCode();
- }
- }
- /**
- * 不够位数的在前面补0,保留num的长度位数字
- */
- private static String autoGenericCode(int length, Integer codeNum) {
- return String.format("%0" + length + "d", codeNum + 1);
- }
- }
|