field.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package entity
  2. import (
  3. "git.sxidc.com/go-tools/utils/strutils"
  4. "github.com/pkg/errors"
  5. "time"
  6. )
  7. const (
  8. // FieldID ID字段名
  9. FieldID = "ID"
  10. // FieldTenantID 租户ID字段名
  11. FieldTenantID = "TenantID"
  12. // FieldCreateUserID 创建用户ID字段名
  13. FieldCreateUserID = "CreateUserID"
  14. // FieldLastUpdateUserID 最近更新用户ID字段名
  15. FieldLastUpdateUserID = "LastUpdateUserID"
  16. // FieldDeleteUserID 删除用户ID字段名
  17. FieldDeleteUserID = "DeleteUserID"
  18. // FieldOperatorUserName 操作者用户名
  19. FieldOperatorUserName = "OperatorUserName"
  20. // FieldCreatedTime 创建时间字段名
  21. FieldCreatedTime = "CreatedTime"
  22. // FieldLastUpdatedTime 最近更新时间字段名
  23. FieldLastUpdatedTime = "LastUpdatedTime"
  24. )
  25. const (
  26. // ColumnID ID列名
  27. ColumnID = "id"
  28. // ColumnTenantID 租户ID列名
  29. ColumnTenantID = "tenant_id"
  30. // ColumnCreateUserID 创建用户ID列名
  31. ColumnCreateUserID = "create_user_id"
  32. // ColumnLastUpdateUserID 最新更新用户ID列名
  33. ColumnLastUpdateUserID = "last_update_user_id"
  34. // ColumnDeleteUserID 删除用户ID列名
  35. ColumnDeleteUserID = "delete_user_id"
  36. // ColumnOperatorUserName 操作者用户名列名
  37. ColumnOperatorUserName = "operator_user_name"
  38. // ColumnCreatedTime 创建时间列名
  39. ColumnCreatedTime = "created_time"
  40. // ColumnLastUpdatedTime 最近更新时间列名
  41. ColumnLastUpdatedTime = "last_updated_time"
  42. )
  43. const (
  44. idLen = 32
  45. )
  46. // TenantIDField 租户ID字段
  47. type TenantIDField struct {
  48. TenantID string `sqlmapping:"column:tenant_id;" sqlresult:"column:tenant_id;" check:"required,len=32,when=create/delete/update"`
  49. }
  50. // UserIDFields 用户ID相关字段
  51. type UserIDFields struct {
  52. CreateUserID string `sqlmapping:"column:create_user_id;" sqlresult:"column:create_user_id;" check:"required,len=32,when=create"`
  53. LastUpdateUserID string `sqlmapping:"column:last_update_user_id;" sqlresult:"column:last_update_user_id;" check:"required,len=32,when=update"`
  54. DeleteUserID string `sqlmapping:"-" sqlresult:"-" check:"required,len=32,when=delete"`
  55. }
  56. // OperatorUserNameField 操作者用户名
  57. type OperatorUserNameField struct {
  58. OperatorUserName string `sqlmapping:"-" sqlresult:"-" check:"required,when=create/delete/update"`
  59. }
  60. // TimeFields 时间相关字段
  61. type TimeFields struct {
  62. CreatedTime time.Time `sqlmapping:"column:created_time;" sqlresult:"column:created_time;"`
  63. LastUpdatedTime time.Time `sqlmapping:"column:last_updated_time;" sqlresult:"column:last_updated_time;"`
  64. }
  65. // CheckIDTypeValue 校验ID类型的字段,如ID,租户ID,用户ID等,特点是字符串类型,不能为空且长度严格为32字节
  66. // 参数:
  67. // - domainCNName: 领域中文名,可以使用DomainCNName()方法获得
  68. // - fieldCNName: 字段的中文名称(用于构造报错信息)
  69. // - id: id字段的值
  70. // 返回值:
  71. // - 错误
  72. func CheckIDTypeValue(domainCNName string, fieldCNName string, id string) error {
  73. if strutils.IsStringEmpty(id) {
  74. return errors.New(domainCNName + fieldCNName + "为空")
  75. }
  76. if len(id) != idLen {
  77. return errors.New(domainCNName + fieldCNName + "长度不正确")
  78. }
  79. return nil
  80. }