entity.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package permission_group
  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. PermissionIDs []string `sqlmapping:"-" sqlresult:"-"`
  26. entity.UserIDFields
  27. entity.TimeFields
  28. }
  29. func (e *Entity) DomainCNName() string {
  30. return "权限组"
  31. }
  32. func (e *Entity) DomainCamelName() string {
  33. return "PermissionGroup"
  34. }
  35. func (e *Entity) ForCreate() error {
  36. checkResult := check.Struct(e, fieldMap)
  37. err := entity.CheckFieldIDResult(checkResult)
  38. if err != nil {
  39. return err
  40. }
  41. err = domain.CheckField(checkResult, e.DomainCNName(), FieldName)
  42. if err != nil {
  43. return err
  44. }
  45. err = entity.CheckFieldCreateUserIDResult(checkResult)
  46. if err != nil {
  47. return err
  48. }
  49. e.LastUpdateUserID = e.CreateUserID
  50. return nil
  51. }
  52. func (e *Entity) ForDelete() error {
  53. checkResult := check.Struct(e, fieldMap)
  54. err := entity.CheckFieldIDResult(checkResult)
  55. if err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. func (e *Entity) ForUpdate() error {
  61. checkResult := check.Struct(e, fieldMap)
  62. err := entity.CheckFieldIDResult(checkResult)
  63. if err != nil {
  64. return err
  65. }
  66. err = entity.CheckFieldLastUpdateUserIDResult(checkResult)
  67. if err != nil {
  68. return err
  69. }
  70. err = e.checkUpdateFields(checkResult)
  71. if err != nil {
  72. return err
  73. }
  74. return nil
  75. }
  76. func (e *Entity) checkUpdateFields(checkResult check.Result) error {
  77. if strutils.AllBlank(e.Name) {
  78. return errors.New(e.DomainCNName() + "没有传递需要更新的字段")
  79. }
  80. if strutils.IsStringNotEmpty(e.Name) {
  81. err := domain.CheckField(checkResult, e.DomainCNName(), FieldName)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return nil
  87. }