entity.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package role
  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. )
  9. const (
  10. FieldName = "Name"
  11. FieldDescription = "Description"
  12. )
  13. var (
  14. ColumnName = domain.ColumnName(FieldName)
  15. ColumnDescription = domain.ColumnName(FieldDescription)
  16. )
  17. var fieldMap = map[string]string{
  18. FieldName: "角色名称",
  19. FieldDescription: "角色描述",
  20. }
  21. type Entity struct {
  22. entity.Base
  23. Name string `sqlmapping:"column:name" sqlresult:"column:name" check:"required,lte=256"`
  24. Description string `sqlmapping:"column:description;updateClear" sqlresult:"column:description"`
  25. UserIDs []string `sqlmapping:"-" sqlresult:"-"`
  26. PermissionIDs []string `sqlmapping:"-" sqlresult:"-"`
  27. entity.UserIDFields
  28. entity.TimeFields
  29. }
  30. func (e *Entity) DomainCNName() string {
  31. return "角色"
  32. }
  33. func (e *Entity) DomainCamelName() string {
  34. return "Role"
  35. }
  36. func (e *Entity) ForCreate() error {
  37. checkResult := check.Struct(e, fieldMap)
  38. err := entity.CheckFieldIDResult(checkResult)
  39. if err != nil {
  40. return err
  41. }
  42. err = domain.CheckField(checkResult, e.DomainCNName(), FieldName)
  43. if err != nil {
  44. return err
  45. }
  46. err = entity.CheckFieldCreateUserIDResult(checkResult)
  47. if err != nil {
  48. return err
  49. }
  50. e.LastUpdateUserID = e.CreateUserID
  51. return nil
  52. }
  53. func (e *Entity) ForDelete() error {
  54. checkResult := check.Struct(e, fieldMap)
  55. err := entity.CheckFieldIDResult(checkResult)
  56. if err != nil {
  57. return err
  58. }
  59. return nil
  60. }
  61. func (e *Entity) ForUpdate() error {
  62. checkResult := check.Struct(e, fieldMap)
  63. err := entity.CheckFieldIDResult(checkResult)
  64. if err != nil {
  65. return err
  66. }
  67. err = entity.CheckFieldLastUpdateUserIDResult(checkResult)
  68. if err != nil {
  69. return err
  70. }
  71. err = e.checkUpdateFields(checkResult)
  72. if err != nil {
  73. return err
  74. }
  75. return nil
  76. }
  77. func (e *Entity) checkUpdateFields(checkResult check.Result) error {
  78. if strutils.AllBlank(e.Name) {
  79. return errors.New(e.DomainCNName() + "没有传递需要更新的字段")
  80. }
  81. if strutils.IsStringNotEmpty(e.Name) {
  82. err := domain.CheckField(checkResult, e.DomainCNName(), FieldName)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }