entity.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package user
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/domain"
  4. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  5. "git.sxidc.com/go-framework/baize/framework/core/tag/check"
  6. "git.sxidc.com/go-tools/utils/strutils"
  7. "github.com/pkg/errors"
  8. "time"
  9. )
  10. const (
  11. FieldUserName = "UserName"
  12. FieldPassword = "Password"
  13. FieldName = "Name"
  14. FieldPhone = "Phone"
  15. FieldEmail = "Email"
  16. FieldToken = "Token"
  17. FieldLastLoginTime = "LastLoginTime"
  18. )
  19. var (
  20. ColumnUserName = domain.ColumnName(FieldUserName)
  21. ColumnPassword = domain.ColumnName(FieldPassword)
  22. ColumnName = domain.ColumnName(FieldName)
  23. ColumnPhone = domain.ColumnName(FieldPhone)
  24. ColumnEmail = domain.ColumnName(FieldEmail)
  25. ColumnToken = domain.ColumnName(FieldToken)
  26. ColumnLastLoginTime = domain.ColumnName(FieldLastLoginTime)
  27. )
  28. var fieldMap = map[string]string{
  29. FieldUserName: "用户名",
  30. FieldPassword: "密码",
  31. FieldName: "姓名",
  32. FieldPhone: "手机号",
  33. FieldEmail: "邮箱",
  34. FieldToken: "token",
  35. FieldLastLoginTime: "最近登录时间",
  36. }
  37. type Entity struct {
  38. entity.Base
  39. UserName string `sqlmapping:"column:user_name;notUpdate" sqlresult:"column:user_name" check:"required,lte=256"`
  40. Password string `sqlmapping:"column:password" sqlresult:"column:password" check:"required,lte=256"`
  41. Name string `sqlmapping:"column:name;updateClear" sqlresult:"column:name" check:"lte=256"`
  42. Phone string `sqlmapping:"column:phone;updateClear" sqlresult:"column:phone" check:"lte=128"`
  43. Email string `sqlmapping:"column:email;updateClear" sqlresult:"column:email" check:"lte=512"`
  44. Token string `sqlmapping:"column:token;notUpdate" sqlresult:"column:token"`
  45. LastLoginTime time.Time `sqlmapping:"column:last_login_time;notUpdate" sqlresult:"column:last_login_time" check:"required,timezero"`
  46. RoleIDs []string `sqlmapping:"-" sqlresult:"-"`
  47. entity.TimeFields
  48. }
  49. func (e *Entity) DomainCNName() string {
  50. return "用户"
  51. }
  52. func (e *Entity) DomainCamelName() string {
  53. return "User"
  54. }
  55. func (e *Entity) ForCreate() error {
  56. checkResult := check.Struct(e, fieldMap)
  57. err := entity.CheckFieldIDResult(checkResult)
  58. if err != nil {
  59. return err
  60. }
  61. err = domain.CheckField(checkResult, e.DomainCNName(), FieldUserName)
  62. if err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. func (e *Entity) ForDelete() error {
  68. checkResult := check.Struct(e, fieldMap)
  69. err := entity.CheckFieldIDResult(checkResult)
  70. if err != nil {
  71. return err
  72. }
  73. return nil
  74. }
  75. func (e *Entity) ForUpdate() error {
  76. checkResult := check.Struct(e, fieldMap)
  77. err := entity.CheckFieldIDResult(checkResult)
  78. if err != nil {
  79. return err
  80. }
  81. err = e.checkUpdateFields()
  82. if err != nil {
  83. return err
  84. }
  85. return nil
  86. }
  87. func (e *Entity) checkUpdateFields() error {
  88. if strutils.AllBlank(e.Password) && e.LastLoginTime.IsZero() {
  89. return errors.New(e.DomainCNName() + "没有传递需要更新的字段")
  90. }
  91. return nil
  92. }