123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package com.sd.business.util;
- 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.ruoyi.common.exception.ServiceException;
- import com.sd.business.service.apply.ApplyBuyService;
- import com.sd.business.service.in.InOutStorageService;
- import com.sd.business.service.lend.LendService;
- import com.sd.business.service.order.OrderExchangeService;
- import com.sd.business.service.order.OrderService;
- import com.sd.business.service.purchase.PurchaseService;
- import com.sd.business.service.statement.StatementOfAccountService;
- import lombok.Getter;
- import java.util.Date;
- import java.util.Map;
- @Getter
- public enum CodeEnum {
- // 申购编号
- APPLY_BUY_CODE("PA", "-yyMMdd-", "code", 6, ApplyBuyService.class),
- // 借出编号
- LEND_CODE("BL", "-yyMMdd-", "code", 6, LendService.class),
- // 入库编号
- IN_CODE("DTS", "-yyMMdd-", "code", 6, InOutStorageService.class),
- // 入库编号
- OUT_CODE("OS", "-yyMMdd-", "code", 6, InOutStorageService.class),
- // 对账单号
- STATEMENT_OF_ACCOUNT_CODE("SOA", "-yyMMdd-", "code", 6, StatementOfAccountService.class),
- // 采购单号
- PURCHASE_CODE("PA", "-yyMMdd-", "code", 6, PurchaseService.class),
- // 退货单号
- TH_CODE("TH", "-yyMMdd-", "code", 6, OrderExchangeService.class),
- // 换货单号
- HH_CODE("HH", "-yyMMdd-", "code", 6, OrderExchangeService.class),
- // 送货单号
- SH_CODE("SH", "-yyMMddHH-", "delivery_code", 3, OrderService.class);
- // 编码前缀
- private final String prefix;
- // 编码加日期规则
- private final String dateFormat;
- // 长度
- private final Integer length;
- // 编码字段名
- private final String codeFieldName;
- // service
- private final IService<?> service;
- 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);
- }
- /**
- * 不够位数的在前面补0,保留num的长度位数字
- */
- private static String autoGenericCode(int length, Integer codeNum) {
- return String.format("%0" + length + "d", codeNum + 1);
- }
- // /**
- // * 获取键值对
- // */
- // 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();
- // }
- // }
- /**
- * 获取键值对
- */
- 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);
- }
- }
|