VppAuditHelper.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.usky.vpp.util;
  2. import com.usky.common.security.utils.SecurityUtils;
  3. import org.springframework.util.StringUtils;
  4. import java.lang.reflect.Method;
  5. import java.time.LocalDateTime;
  6. import java.time.format.DateTimeFormatter;
  7. import java.util.UUID;
  8. /**
  9. * VPP 审计与编号工具
  10. */
  11. public final class VppAuditHelper {
  12. public static final int NOT_DELETED = 0;
  13. public static final int DELETED = 1;
  14. private VppAuditHelper() {
  15. }
  16. public static void fillCreate(Object entity) {
  17. LocalDateTime now = LocalDateTime.now();
  18. invokeSetter(entity, "setCreateTime", now);
  19. invokeSetter(entity, "setUpdateTime", now);
  20. invokeSetter(entity, "setDeleteFlag", NOT_DELETED);
  21. String auditUser = currentUsername();
  22. if (StringUtils.hasText(auditUser)) {
  23. invokeSetter(entity, "setCreatedBy", auditUser);
  24. invokeSetter(entity, "setUpdatedBy", auditUser);
  25. }
  26. }
  27. public static void fillUpdate(Object entity) {
  28. invokeSetter(entity, "setUpdateTime", LocalDateTime.now());
  29. String auditUser = currentUsername();
  30. if (StringUtils.hasText(auditUser)) {
  31. invokeSetter(entity, "setUpdatedBy", auditUser);
  32. }
  33. }
  34. public static void fillSoftDelete(Object entity) {
  35. invokeSetter(entity, "setDeleteFlag", DELETED);
  36. invokeSetter(entity, "setDeletedAt", LocalDateTime.now());
  37. fillUpdate(entity);
  38. }
  39. public static boolean isDeletedAt(LocalDateTime deletedAt) {
  40. return deletedAt != null;
  41. }
  42. public static void fillSoftDeleteAt(Object entity) {
  43. fillSoftDelete(entity);
  44. }
  45. public static boolean isActive(Integer deleteFlag, LocalDateTime deletedAt) {
  46. return !isDeleted(deleteFlag) && !isDeletedAt(deletedAt);
  47. }
  48. public static boolean isDeleted(Integer deleteFlag) {
  49. return deleteFlag != null && deleteFlag == DELETED;
  50. }
  51. public static String nextApplyNo() {
  52. return "ACC" + System.currentTimeMillis() + UUID.randomUUID().toString().substring(0, 4).toUpperCase();
  53. }
  54. public static String nextEventId() {
  55. return "EVE_" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now())
  56. + UUID.randomUUID().toString().substring(0, 4).toUpperCase();
  57. }
  58. public static String nextInvitationNo() {
  59. return "INV_" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now())
  60. + UUID.randomUUID().toString().substring(0, 4).toUpperCase();
  61. }
  62. public static String nextBillNo() {
  63. return "BILL_" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now())
  64. + UUID.randomUUID().toString().substring(0, 4).toUpperCase();
  65. }
  66. private static String currentUsername() {
  67. try {
  68. return SecurityUtils.getUsername();
  69. } catch (Exception ex) {
  70. return null;
  71. }
  72. }
  73. public static Integer resolveTenantId(String dnId) {
  74. if (StringUtils.hasText(dnId)) {
  75. Integer tenantId = VppSiteResourceHelper.resolveTenantIdByUnResourceId(dnId);
  76. if (tenantId != null) {
  77. return tenantId;
  78. }
  79. }
  80. return currentTenantId();
  81. }
  82. private static String invokeGetter(Object entity, String method) {
  83. try {
  84. Method m = entity.getClass().getMethod(method);
  85. Object value = m.invoke(entity);
  86. return value != null ? value.toString() : null;
  87. } catch (Exception ignored) {
  88. return null;
  89. }
  90. }
  91. private static Integer currentTenantId() {
  92. try {
  93. return SecurityUtils.getTenantId();
  94. } catch (Exception ex) {
  95. return null;
  96. }
  97. }
  98. private static void invokeSetter(Object entity, String method, Object value) {
  99. try {
  100. Method m = entity.getClass().getMethod(method, value.getClass());
  101. m.invoke(entity, value);
  102. } catch (Exception ignored) {
  103. // 部分实体无审计字段时忽略
  104. }
  105. }
  106. }