entity.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package configuration
  2. import (
  3. "git.sxidc.com/go-tools/utils/strutils"
  4. "git.sxidc.com/service-supports/fserr"
  5. )
  6. const (
  7. ColumnScope = "scope"
  8. ColumnGroup = "group"
  9. ColumnValue = "value"
  10. )
  11. const (
  12. domainCNName = "配置"
  13. )
  14. const (
  15. tableName = "configurations"
  16. )
  17. const (
  18. fieldScopeMaxLen = 256
  19. fieldGroupMaxLen = 256
  20. fieldValueMaxLen = 256
  21. )
  22. type Entity struct {
  23. Scope string `sqlmapping:"column:scope;key;notUpdate;" sqlresult:"column:group;"`
  24. Group string `sqlmapping:"column:group;key;notUpdate;" sqlresult:"column:group;"`
  25. Value string `sqlmapping:"column:value;notUpdate;" sqlresult:"column:value;"`
  26. }
  27. func (e *Entity) DomainCNName() string {
  28. return domainCNName
  29. }
  30. func (e *Entity) CheckKeyFields() error {
  31. err := e.checkFieldScope()
  32. if err != nil {
  33. return err
  34. }
  35. err = e.checkFieldGroup()
  36. if err != nil {
  37. return err
  38. }
  39. return nil
  40. }
  41. func (e *Entity) ForCreate() error {
  42. err := e.checkFieldScope()
  43. if err != nil {
  44. return err
  45. }
  46. err = e.checkFieldGroup()
  47. if err != nil {
  48. return err
  49. }
  50. err = e.checkFieldValue()
  51. if err != nil {
  52. return err
  53. }
  54. return nil
  55. }
  56. func (e *Entity) checkFieldScope() error {
  57. if strutils.IsStringEmpty(e.Scope) {
  58. return fserr.New(domainCNName + "范围为空")
  59. }
  60. if len(e.Scope) > fieldScopeMaxLen {
  61. return fserr.New(domainCNName + "范围超出限定长度")
  62. }
  63. return nil
  64. }
  65. func (e *Entity) checkFieldGroup() error {
  66. if strutils.IsStringEmpty(e.Group) {
  67. return fserr.New(domainCNName + "组为空")
  68. }
  69. if len(e.Group) > fieldGroupMaxLen {
  70. return fserr.New(domainCNName + "组超出限定长度")
  71. }
  72. return nil
  73. }
  74. func (e *Entity) checkFieldValue() error {
  75. if strutils.IsStringEmpty(e.Value) {
  76. return fserr.New(domainCNName + "值为空")
  77. }
  78. if len(e.Value) > fieldValueMaxLen {
  79. return fserr.New(domainCNName + "值超出限定长度")
  80. }
  81. return nil
  82. }