field.go 5.8 KB

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