CodeEnum.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.sd.business.util;
  2. import cn.hutool.core.convert.Convert;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.text.CharSequenceUtil;
  5. import cn.hutool.core.util.ObjectUtil;
  6. import cn.hutool.extra.spring.SpringUtil;
  7. import com.baomidou.mybatisplus.extension.service.IService;
  8. import com.ruoyi.common.exception.ServiceException;
  9. import com.sd.business.service.order.OrderService;
  10. import lombok.AllArgsConstructor;
  11. import lombok.Getter;
  12. import java.util.Date;
  13. import java.util.Map;
  14. @Getter
  15. @AllArgsConstructor
  16. public enum CodeEnum {
  17. // 订单编号
  18. ORDER_CODE("KX", "yyMMdd", "code", 6, OrderService.class);
  19. // 编码前缀
  20. private final String prefix;
  21. // 编码加日期规则
  22. private final String dateFormat;
  23. // 长度
  24. private final Integer length;
  25. // 编码字段名
  26. private final String codeFieldName;
  27. // service
  28. private final IService<?> service;
  29. CodeEnum(String prefix, String dateFormat, String codeFieldName, Integer length, Class<? extends IService<?>> serviceCls) {
  30. this.prefix = prefix;
  31. this.dateFormat = dateFormat;
  32. this.length = length;
  33. this.codeFieldName = codeFieldName;
  34. this.service = SpringUtil.getBean(serviceCls);
  35. }
  36. /**
  37. * 不够位数的在前面补0,保留num的长度位数字
  38. */
  39. private static String autoGenericCode(int length, Integer codeNum) {
  40. return String.format("%0" + length + "d", codeNum + 1);
  41. }
  42. // /**
  43. // * 获取键值对
  44. // */
  45. // public String getCode(String code) {
  46. // if (ObjectUtil.isNotEmpty(code)) {
  47. // Long count = service.query().eq(codeFieldName, code).count();
  48. // if (count != 0) {
  49. // throw new ServiceException("编码已存在");
  50. // }
  51. // return code;
  52. // } else {
  53. // return getCode();
  54. // }
  55. // }
  56. /**
  57. * 获取键值对
  58. */
  59. public String getCode() {
  60. String itemPrefix;
  61. if (ObjectUtil.isNotEmpty(dateFormat)) {
  62. Date date = new Date();
  63. String format = DateUtil.format(date, dateFormat);
  64. itemPrefix = prefix + format;
  65. } else {
  66. itemPrefix = prefix;
  67. }
  68. Object obj = service.query()
  69. .likeRight(codeFieldName, itemPrefix)
  70. .orderByDesc(codeFieldName)
  71. .last("limit 1")
  72. .one();
  73. if (obj == null) {
  74. return itemPrefix + autoGenericCode(length, 0);
  75. }
  76. Map<String, Object> map = Convert.toMap(String.class, Object.class, obj);
  77. String code = Convert.toStr(map.get(CharSequenceUtil.toCamelCase(codeFieldName)));
  78. Integer codeNum = Convert.toInt(code.substring(itemPrefix.length()));
  79. if (ObjectUtil.isEmpty(codeNum)) {
  80. throw new ServiceException("自定义编码与系统编码生成规则冲突,暂时无法生成编码,请联系管理员");
  81. }
  82. return itemPrefix + autoGenericCode(length, codeNum);
  83. }
  84. }