ResponseCodeConst.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package net.lab1024.smartadmin.common.constant;
  2. import java.lang.reflect.Field;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import lombok.extern.slf4j.Slf4j;
  6. import net.lab1024.smartadmin.module.business.log.orderoperatelog.constant.OrderOperateLogOperateTypeConst;
  7. import net.lab1024.smartadmin.module.support.file.constant.FileResponseCodeConst;
  8. import net.lab1024.smartadmin.module.system.department.DepartmentResponseCodeConst;
  9. import net.lab1024.smartadmin.module.system.employee.constant.EmployeeResponseCodeConst;
  10. import net.lab1024.smartadmin.module.system.login.LoginResponseCodeConst;
  11. import net.lab1024.smartadmin.module.system.position.PositionResponseCodeConst;
  12. import net.lab1024.smartadmin.module.system.privilege.constant.PrivilegeResponseCodeConst;
  13. import net.lab1024.smartadmin.module.system.role.basic.RoleResponseCodeConst;
  14. import net.lab1024.smartadmin.module.system.systemconfig.constant.SystemConfigResponseCodeConst;
  15. /**
  16. * 每个业务,100个范围值就够了.
  17. */
  18. @Slf4j
  19. public class ResponseCodeConst {
  20. // 范围声明
  21. static {
  22. // 系统功能,从0开始,step=1000
  23. ResponseCodeContainer.register(ResponseCodeConst.class, 0, 1000);
  24. ResponseCodeContainer.register(LoginResponseCodeConst.class, 1001, 1999);
  25. ResponseCodeContainer.register(DepartmentResponseCodeConst.class, 2001, 2999);
  26. ResponseCodeContainer.register(EmployeeResponseCodeConst.class, 3001, 3999);
  27. ResponseCodeContainer.register(FileResponseCodeConst.class, 4001, 4999);
  28. ResponseCodeContainer.register(SystemConfigResponseCodeConst.class, 5001, 5999);
  29. ResponseCodeContainer.register(RoleResponseCodeConst.class, 6001, 6999);
  30. ResponseCodeContainer.register(PrivilegeResponseCodeConst.class, 7001, 7999);
  31. ResponseCodeContainer.register(OrderOperateLogOperateTypeConst.class, 8001, 8999);
  32. ResponseCodeContainer.register(PositionResponseCodeConst.class, 13000, 13999);
  33. }
  34. public static final ResponseCodeConst SUCCESS = new ResponseCodeConst(1, "操作成功!", true);
  35. public static final ResponseCodeConst ERROR_PARAM = new ResponseCodeConst(101, "参数异常!");
  36. public static final ResponseCodeConst ERROR_PARAM_ANY = new ResponseCodeConst(102, "%s参数异常!");
  37. public static final ResponseCodeConst SYSTEM_ERROR = new ResponseCodeConst(111, "系统错误");
  38. public static final ResponseCodeConst DEVELOPMENT = new ResponseCodeConst(112, "此功能正在开发中");
  39. public static final ResponseCodeConst NOT_EXISTS = new ResponseCodeConst(113, "数据不存在");
  40. public static ResponseCodeConst REQUEST_METHOD_ERROR = new ResponseCodeConst(114, "请求方式错误");
  41. public static ResponseCodeConst JSON_FORMAT_ERROR = new ResponseCodeConst(115, "JSON格式错误");
  42. protected int code;
  43. protected String msg;
  44. protected boolean success;
  45. public ResponseCodeConst() {
  46. }
  47. public ResponseCodeConst(int code, String msg) {
  48. super();
  49. this.code = code;
  50. this.msg = msg;
  51. ResponseCodeContainer.put(this);
  52. }
  53. //shortFlag 0
  54. public ResponseCodeConst(int code, String msg, String shortFlag) {
  55. super();
  56. this.code = code;
  57. this.msg = msg;
  58. }
  59. protected ResponseCodeConst(int code, String msg, boolean success) {
  60. super();
  61. this.code = code;
  62. this.msg = msg;
  63. this.success = success;
  64. ResponseCodeContainer.put(this);
  65. }
  66. protected ResponseCodeConst(int code) {
  67. super();
  68. this.code = code;
  69. ResponseCodeContainer.put(this);
  70. }
  71. public int getCode() {
  72. return code;
  73. }
  74. public void setCode(int code) {
  75. this.code = code;
  76. }
  77. public String getMsg() {
  78. return msg;
  79. }
  80. public void setMsg(String msg) {
  81. this.msg = msg;
  82. }
  83. public boolean isSuccess() {
  84. return success;
  85. }
  86. public void setSuccess(boolean success) {
  87. this.success = success;
  88. }
  89. public static void init() {
  90. log.info("ResponseCodeConst init....");
  91. }
  92. // =======================分割=======================
  93. /**
  94. * 内部类,用于检测code范围
  95. *
  96. * @author Anders
  97. */
  98. @Slf4j
  99. private static class ResponseCodeContainer {
  100. private static final Map<Integer, ResponseCodeConst> RESPONSE_CODE_MAP = new HashMap<>();
  101. private static final Map<Class<? extends ResponseCodeConst>, int[]> RESPONSE_CODE_RANGE_MAP = new HashMap<>();
  102. /**
  103. * id的范围:[start, end]左闭右闭
  104. *
  105. * @param clazz
  106. * @param start
  107. * @param end
  108. */
  109. private static void register(Class<? extends ResponseCodeConst> clazz, int start, int end) {
  110. if (start > end) {
  111. throw new IllegalArgumentException("<ResponseDTO> start > end!");
  112. }
  113. if (RESPONSE_CODE_RANGE_MAP.containsKey(clazz)) {
  114. throw new IllegalArgumentException(String.format("<ResponseDTO> Class:%s already exist!", clazz.getSimpleName()));
  115. }
  116. RESPONSE_CODE_RANGE_MAP.forEach((k, v) -> {
  117. if ((start >= v[0] && start <= v[1]) || (end >= v[0] && end <= v[1])) {
  118. throw new IllegalArgumentException(String.format("<ResponseDTO> Class:%s 's id range[%d,%d] has " + "intersection with " + "class:%s", clazz.getSimpleName(), start, end,
  119. k.getSimpleName()));
  120. }
  121. });
  122. RESPONSE_CODE_RANGE_MAP.put(clazz, new int[]{start, end});
  123. // 提前初始化static变量,进行范围检测
  124. Field[] fields = clazz.getFields();
  125. if (fields.length != 0) {
  126. try {
  127. fields[0].get(clazz);
  128. } catch (IllegalArgumentException | IllegalAccessException e) {
  129. log.error("", e);
  130. }
  131. }
  132. }
  133. public static void put(ResponseCodeConst codeConst) {
  134. int[] idRange = RESPONSE_CODE_RANGE_MAP.get(codeConst.getClass());
  135. if (idRange == null) {
  136. throw new IllegalArgumentException(String.format("<ResponseDTO> Class:%s has not been registered!", codeConst.getClass().getSimpleName()));
  137. }
  138. int code = codeConst.code;
  139. if (code < idRange[0] || code > idRange[1]) {
  140. throw new IllegalArgumentException(String.format("<ResponseDTO> Id(%d) out of range[%d,%d], " + "class:%s", code, idRange[0], idRange[1], codeConst.getClass().getSimpleName()));
  141. }
  142. if (RESPONSE_CODE_MAP.keySet().contains(code)) {
  143. log.error(String.format("<ResponseDTO> Id(%d) out of range[%d,%d], " + "class:%s code is repeat!", code, idRange[0], idRange[1], codeConst.getClass().getSimpleName()));
  144. System.exit(0);
  145. }
  146. RESPONSE_CODE_MAP.put(code, codeConst);
  147. }
  148. }
  149. }