field.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package entity
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/tag/check"
  4. "git.sxidc.com/go-tools/utils/strutils"
  5. "github.com/pkg/errors"
  6. "time"
  7. )
  8. const (
  9. // FieldID ID字段名
  10. FieldID = "ID"
  11. // FieldTenantID 租户ID字段名
  12. FieldTenantID = "TenantID"
  13. // FieldCreateUserID 创建用户ID字段名
  14. FieldCreateUserID = "CreateUserID"
  15. // FieldLastUpdateUserID 最近更新用户ID字段名
  16. FieldLastUpdateUserID = "LastUpdateUserID"
  17. // FieldCreatedTime 创建时间字段名
  18. FieldCreatedTime = "CreatedTime"
  19. // FieldLastUpdatedTime 最近更新时间字段名
  20. FieldLastUpdatedTime = "LastUpdatedTime"
  21. )
  22. const (
  23. // ColumnID ID列名
  24. ColumnID = "id"
  25. // ColumnTenantID 租户ID列名
  26. ColumnTenantID = "tenant_id"
  27. // ColumnCreateUserID 创建用户ID列名
  28. ColumnCreateUserID = "create_user_id"
  29. // ColumnLastUpdateUserID 最新更新用户ID列名
  30. ColumnLastUpdateUserID = "last_update_user_id"
  31. // ColumnCreatedTime 创建时间列名
  32. ColumnCreatedTime = "created_time"
  33. // ColumnLastUpdatedTime 最近更新时间列名
  34. ColumnLastUpdatedTime = "last_updated_time"
  35. )
  36. const (
  37. idLen = 32
  38. )
  39. // TenantIDField 租户ID字段
  40. type TenantIDField struct {
  41. TenantID string `sqlmapping:"column:tenant_id;" sqlresult:"column:tenant_id;" check:"required,len=32"`
  42. }
  43. // UserIDFields 用户ID相关字段
  44. type UserIDFields struct {
  45. CreateUserID string `sqlmapping:"column:create_user_id;" sqlresult:"column:create_user_id;" check:"required,len=32"`
  46. LastUpdateUserID string `sqlmapping:"column:last_update_user_id;" sqlresult:"column:last_update_user_id;" check:"required,len=32"`
  47. }
  48. // TimeFields 时间相关字段
  49. type TimeFields struct {
  50. CreatedTime time.Time `sqlmapping:"column:created_time;" sqlresult:"column:created_time;"`
  51. LastUpdatedTime time.Time `sqlmapping:"column:last_updated_time;" sqlresult:"column:last_updated_time;"`
  52. }
  53. // CheckFieldID 校验领域实体的ID字段是否合规
  54. // 参数:
  55. // - entity: 领域实体
  56. // 返回值:
  57. // - 错误
  58. func CheckFieldID(entity Entity) error {
  59. return checkIDTypeField(entity, FieldID, "ID")
  60. }
  61. // CheckFieldTenantID 校验领域实体的租户ID字段是否合规
  62. // 参数:
  63. // - entity: 领域实体
  64. // 返回值:
  65. // - 错误
  66. func CheckFieldTenantID(entity Entity) error {
  67. return checkIDTypeField(entity, FieldTenantID, "租户ID")
  68. }
  69. // CheckFieldCreateUserID 校验领域实体的创建用户ID字段是否合规
  70. // 参数:
  71. // - entity: 领域实体
  72. // 返回值:
  73. // - 错误
  74. func CheckFieldCreateUserID(entity Entity) error {
  75. return checkIDTypeField(entity, FieldCreateUserID, "创建用户ID")
  76. }
  77. // CheckFieldLastUpdateUserID 校验领域实体的最近更新用户ID字段是否合规
  78. // 参数:
  79. // - entity: 领域实体
  80. // 返回值:
  81. // - 错误
  82. func CheckFieldLastUpdateUserID(entity Entity) error {
  83. return checkIDTypeField(entity, FieldLastUpdateUserID, "最近更新用户ID")
  84. }
  85. // CheckFieldIDResult 利用check.Struct函数返回的结果校验领域实体的ID字段是否合规
  86. // 参数:
  87. // - checkResult: check.Struct函数返回的结果
  88. // 返回值:
  89. // - 错误
  90. func CheckFieldIDResult(checkResult check.Result) error {
  91. return checkIDTypeResult(checkResult, FieldID, FieldID)
  92. }
  93. // CheckFieldTenantIDResult 利用check.Struct函数返回的结果校验领域实体的租户ID字段是否合规
  94. // 参数:
  95. // - checkResult: check.Struct函数返回的结果
  96. // 返回值:
  97. // - 错误
  98. func CheckFieldTenantIDResult(checkResult check.Result) error {
  99. return checkIDTypeResult(checkResult, FieldTenantID, FieldTenantID)
  100. }
  101. // CheckFieldCreateUserIDResult 利用check.Struct函数返回的结果校验领域实体的创建用户ID字段是否合规
  102. // 参数:
  103. // - checkResult: check.Struct函数返回的结果
  104. // 返回值:
  105. // - 错误
  106. func CheckFieldCreateUserIDResult(checkResult check.Result) error {
  107. return checkIDTypeResult(checkResult, FieldCreateUserID, FieldCreateUserID)
  108. }
  109. // CheckFieldLastUpdateUserIDResult 利用check.Struct函数返回的结果校验领域实体的最近更新用户ID字段是否合规
  110. // 参数:
  111. // - checkResult: check.Struct函数返回的结果
  112. // 返回值:
  113. // - 错误
  114. func CheckFieldLastUpdateUserIDResult(checkResult check.Result) error {
  115. return checkIDTypeResult(checkResult, FieldLastUpdateUserID, FieldLastUpdateUserID)
  116. }
  117. // CheckIDTypeValue 校验ID类型的字段,如ID,租户ID,用户ID等,特点是字符串类型,不能为空且长度严格为32字节
  118. // 参数:
  119. // - domainCNName: 领域中文名,可以使用DomainCNName()方法获得
  120. // - fieldCNName: 字段的中文名称(用于构造报错信息)
  121. // - id: id字段的值
  122. // 返回值:
  123. // - 错误
  124. func CheckIDTypeValue(domainCNName string, fieldCNName string, id string) error {
  125. if strutils.IsStringEmpty(id) {
  126. return errors.New(domainCNName + fieldCNName + "为空")
  127. }
  128. if len(id) != idLen {
  129. return errors.New(domainCNName + fieldCNName + "长度不正确")
  130. }
  131. return nil
  132. }
  133. func checkIDTypeField(entity Entity, fieldName string, fieldCNName string) error {
  134. checkResult := check.Struct(entity, map[string]string{
  135. fieldName: entity.DomainCNName() + fieldCNName,
  136. })
  137. err := checkResult.CheckFields(fieldName)
  138. if err != nil {
  139. return errors.New(entity.DomainCNName() + ": " + err.Error())
  140. }
  141. return nil
  142. }
  143. func checkIDTypeResult(checkResult check.Result, domainCNName string, fieldName string) error {
  144. err := checkResult.CheckFields(fieldName)
  145. if err != nil {
  146. return errors.New(domainCNName + ": " + err.Error())
  147. }
  148. return nil
  149. }