CodeEnum.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.apply.ApplyBuyService;
  10. import com.sd.business.service.in.InOutStorageService;
  11. import com.sd.business.service.lend.LendService;
  12. import com.sd.business.service.order.OrderExchangeService;
  13. import com.sd.business.service.order.OrderService;
  14. import com.sd.business.service.purchase.PurchaseService;
  15. import com.sd.business.service.statement.StatementOfAccountService;
  16. import lombok.Getter;
  17. import java.util.Date;
  18. import java.util.Map;
  19. @Getter
  20. public enum CodeEnum {
  21. // 申购编号
  22. APPLY_BUY_CODE("PA", "-yyMMdd-", "code", 6, ApplyBuyService.class),
  23. // 借出编号
  24. LEND_CODE("BL", "-yyMMdd-", "code", 6, LendService.class),
  25. // 入库编号
  26. IN_CODE("DTS", "-yyMMdd-", "code", 6, InOutStorageService.class),
  27. // 入库编号
  28. OUT_CODE("OS", "-yyMMdd-", "code", 6, InOutStorageService.class),
  29. // 对账单号
  30. STATEMENT_OF_ACCOUNT_CODE("SOA", "-yyMMdd-", "code", 6, StatementOfAccountService.class),
  31. // 采购单号
  32. PURCHASE_CODE("PA", "-yyMMdd-", "code", 6, PurchaseService.class),
  33. // 退货单号
  34. TH_CODE("TH", "-yyMMdd-", "code", 6, OrderExchangeService.class),
  35. // 换货单号
  36. HH_CODE("HH", "-yyMMdd-", "code", 6, OrderExchangeService.class),
  37. // 送货单号
  38. SH_CODE("SH", "-yyMMddHH-", "delivery_code", 3, OrderService.class);
  39. // 编码前缀
  40. private final String prefix;
  41. // 编码加日期规则
  42. private final String dateFormat;
  43. // 长度
  44. private final Integer length;
  45. // 编码字段名
  46. private final String codeFieldName;
  47. // service
  48. private final IService<?> service;
  49. CodeEnum(String prefix, String dateFormat, String codeFieldName, Integer length, Class<? extends IService<?>> serviceCls) {
  50. this.prefix = prefix;
  51. this.dateFormat = dateFormat;
  52. this.length = length;
  53. this.codeFieldName = codeFieldName;
  54. this.service = SpringUtil.getBean(serviceCls);
  55. }
  56. /**
  57. * 不够位数的在前面补0,保留num的长度位数字
  58. */
  59. private static String autoGenericCode(int length, Integer codeNum) {
  60. return String.format("%0" + length + "d", codeNum + 1);
  61. }
  62. // /**
  63. // * 获取键值对
  64. // */
  65. // public String getCode(String code) {
  66. // if (ObjectUtil.isNotEmpty(code)) {
  67. // Long count = service.query().eq(codeFieldName, code).count();
  68. // if (count != 0) {
  69. // throw new ServiceException("编码已存在");
  70. // }
  71. // return code;
  72. // } else {
  73. // return getCode();
  74. // }
  75. // }
  76. /**
  77. * 获取键值对
  78. */
  79. public String getCode() {
  80. String itemPrefix;
  81. if (ObjectUtil.isNotEmpty(dateFormat)) {
  82. Date date = new Date();
  83. String format = DateUtil.format(date, dateFormat);
  84. itemPrefix = prefix + format;
  85. } else {
  86. itemPrefix = prefix;
  87. }
  88. Object obj = service.query()
  89. .likeRight(codeFieldName, itemPrefix)
  90. .orderByDesc(codeFieldName)
  91. .last("limit 1")
  92. .one();
  93. if (obj == null) {
  94. return itemPrefix + autoGenericCode(length, 0);
  95. }
  96. Map<String, Object> map = Convert.toMap(String.class, Object.class, obj);
  97. String code = Convert.toStr(map.get(CharSequenceUtil.toCamelCase(codeFieldName)));
  98. Integer codeNum = Convert.toInt(code.substring(itemPrefix.length()));
  99. if (ObjectUtil.isEmpty(codeNum)) {
  100. throw new ServiceException("自定义编码与系统编码生成规则冲突,暂时无法生成编码,请联系管理员");
  101. }
  102. return itemPrefix + autoGenericCode(length, codeNum);
  103. }
  104. }