entity.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package rule
  2. import (
  3. "encoding/json"
  4. "git.sxidc.com/go-framework/baize/framework/core/domain"
  5. "git.sxidc.com/go-framework/baize/framework/core/domain/entity"
  6. "git.sxidc.com/go-framework/baize/framework/core/tag/check"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. FieldScope = "Scope"
  11. FieldDomainName = "DomainName"
  12. FieldRule = "Rule"
  13. FieldEnabled = "Enabled"
  14. )
  15. var (
  16. ColumnScope = domain.ColumnName(FieldScope)
  17. ColumnDomainName = domain.ColumnName(FieldDomainName)
  18. ColumnRule = domain.ColumnName(FieldRule)
  19. ColumnEnabled = domain.ColumnName(FieldEnabled)
  20. )
  21. var fieldMap = map[string]string{
  22. FieldScope: "范围",
  23. FieldDomainName: "领域名",
  24. FieldRule: "规则",
  25. }
  26. type Entity struct {
  27. entity.Base
  28. Scope string `sqlmapping:"column:scope;notUpdate;" sqlresult:"column:scope;" check:"required,lte=256"`
  29. DomainName string `sqlmapping:"column:domain_name;notUpdate;" sqlresult:"column:domain_name;" check:"required,lte=256"`
  30. Rule string `sqlmapping:"column:rule;notUpdate;" sqlresult:"column:rule;" check:"required"`
  31. Enabled bool `sqlresult:"column:enabled;" sqlresult:"column:enabled;"`
  32. }
  33. func (e *Entity) DomainCNName() string {
  34. return "查询规则"
  35. }
  36. func (e *Entity) DomainCamelName() string {
  37. return "QueryRule"
  38. }
  39. func (e *Entity) ForCreate() error {
  40. checkResult := check.Struct(e, fieldMap)
  41. err := entity.CheckFieldIDResult(checkResult)
  42. if err != nil {
  43. return err
  44. }
  45. err = domain.CheckField(checkResult, e.DomainCNName(), FieldScope)
  46. if err != nil {
  47. return err
  48. }
  49. err = domain.CheckField(checkResult, e.DomainCNName(), FieldDomainName)
  50. if err != nil {
  51. return err
  52. }
  53. err = domain.CheckField(checkResult, e.DomainCNName(), FieldRule)
  54. if err != nil {
  55. return err
  56. }
  57. err = e.lintRule()
  58. if err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. func (e *Entity) ForDelete() error {
  64. checkResult := check.Struct(e, fieldMap)
  65. err := entity.CheckFieldIDResult(checkResult)
  66. if err != nil {
  67. return err
  68. }
  69. return nil
  70. }
  71. func (e *Entity) ForUpdate() error {
  72. return nil
  73. }
  74. func (e *Entity) lintRule() error {
  75. r := new(Rule)
  76. err := json.Unmarshal([]byte(e.Rule), r)
  77. if err != nil {
  78. return errors.New(err.Error())
  79. }
  80. return r.check()
  81. }