schema.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package rest
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "errors"
  6. )
  7. var (
  8. errDbTypeUnsupported = errors.New("database type unsupported")
  9. )
  10. const (
  11. ScenarioCreate = "create"
  12. ScenarioUpdate = "update"
  13. ScenarioDelete = "delete"
  14. ScenarioSearch = "search"
  15. ScenarioExport = "export"
  16. ScenarioList = "list"
  17. ScenarioView = "view"
  18. ScenarioMapping = "mapping"
  19. )
  20. const (
  21. MatchExactly = "exactly" //精确匹配
  22. MatchFuzzy = "fuzzy" //模糊匹配
  23. )
  24. const (
  25. LiveTypeDropdown = "dropdown"
  26. LiveTypeCascader = "cascader"
  27. )
  28. type (
  29. LiveValue struct {
  30. Enable bool `json:"enable"`
  31. Type string `json:"type"`
  32. Url string `json:"url"`
  33. Columns []string `json:"columns"`
  34. }
  35. EnumValue struct {
  36. Label string `json:"label"`
  37. Value string `json:"value"`
  38. Color string `json:"color"`
  39. }
  40. VisibleCondition struct {
  41. Column string `json:"column"`
  42. Values []interface{} `json:"values"`
  43. }
  44. Rule struct {
  45. Min int `json:"min"`
  46. Max int `json:"max"`
  47. Type string `json:"type"`
  48. Unique bool `json:"unique"`
  49. Required []string `json:"required"`
  50. Regular string `json:"regular"`
  51. }
  52. Attribute struct {
  53. Match string `json:"match"` //匹配模式
  54. PrimaryKey bool `json:"primary_key"` //是否为主键
  55. DefaultValue string `json:"default_value"` //默认值
  56. Readonly []string `json:"readonly"` //只读场景
  57. Disable []string `json:"disable"` //禁用场景
  58. Visible []VisibleCondition `json:"visible"` //可见条件
  59. Values []EnumValue `json:"values"` //值
  60. Live LiveValue `json:"live"` //延时加载配置
  61. Icon string `json:"icon"` //显示图标
  62. Sort bool `json:"sort"` //是否允许排序
  63. Suffix string `json:"suffix"` //追加内容
  64. Tooltip string `json:"tooltip"` //字段提示信息
  65. Description string `json:"description"` //字段说明信息
  66. }
  67. Scenarios []string
  68. Schema struct {
  69. Id uint64 `json:"id" gorm:"primary_key"`
  70. CreatedAt int64 `json:"created_at" gorm:"autoCreateTime"` //创建时间
  71. UpdatedAt int64 `json:"updated_at" gorm:"autoUpdateTime"` //更新时间
  72. Namespace string `json:"namespace" gorm:"column:namespace;type:char(60);index"` //域
  73. ModuleName string `json:"module_name" gorm:"column:module_name;type:varchar(60);index"` //模块名称
  74. TableName string `json:"table_name" gorm:"column:table_name;type:varchar(120);index"` //表名称
  75. Enable uint8 `json:"enable" gorm:"column:enable;type:int(1)"` //是否启用
  76. Column string `json:"column" gorm:"type:varchar(120)"` //字段名称
  77. Label string `json:"label" gorm:"type:varchar(120)"` //显示名称
  78. Type string `json:"type" gorm:"type:varchar(120)"` //字段类型
  79. Format string `json:"format" gorm:"type:varchar(120)"` //字段格式
  80. Native uint8 `json:"native" gorm:"type:int(1)"` //是否为原生字段
  81. IsPrimaryKey uint8 `json:"is_primary_key" gorm:"type:int(1)"` //是否为主键
  82. Expression string `json:"expression" gorm:"type:varchar(526)"` //计算规则
  83. Scenarios Scenarios `json:"scenarios" gorm:"type:varchar(120)"` //场景
  84. Rule Rule `json:"rule" gorm:"type:varchar(2048)"` //字段规则
  85. Attribute Attribute `json:"attribute" gorm:"type:varchar(4096)"` //字段属性
  86. Position int `json:"position"` //字段排序位置
  87. }
  88. )
  89. func (n Scenarios) Has(str string) bool {
  90. for _, v := range n {
  91. if v == str {
  92. return true
  93. }
  94. }
  95. return false
  96. }
  97. // Scan implements the Scanner interface.
  98. func (n *Scenarios) Scan(value interface{}) error {
  99. if value == nil {
  100. return nil
  101. }
  102. switch s := value.(type) {
  103. case string:
  104. return json.Unmarshal([]byte(s), n)
  105. case []byte:
  106. return json.Unmarshal(s, n)
  107. }
  108. return errDbTypeUnsupported
  109. }
  110. // Value implements the driver Valuer interface.
  111. func (n Scenarios) Value() (driver.Value, error) {
  112. return json.Marshal(n)
  113. }
  114. // Scan implements the Scanner interface.
  115. func (n *Attribute) Scan(value interface{}) error {
  116. if value == nil {
  117. return nil
  118. }
  119. switch s := value.(type) {
  120. case string:
  121. return json.Unmarshal([]byte(s), n)
  122. case []byte:
  123. return json.Unmarshal(s, n)
  124. }
  125. return errDbTypeUnsupported
  126. }
  127. // Value implements the driver Valuer interface.
  128. func (n Attribute) Value() (driver.Value, error) {
  129. return json.Marshal(n)
  130. }
  131. // Scan implements the Scanner interface.
  132. func (n *Rule) Scan(value interface{}) error {
  133. if value == nil {
  134. return nil
  135. }
  136. switch s := value.(type) {
  137. case string:
  138. return json.Unmarshal([]byte(s), n)
  139. case []byte:
  140. return json.Unmarshal(s, n)
  141. }
  142. return errDbTypeUnsupported
  143. }
  144. // Value implements the driver Valuer interface.
  145. func (n Rule) Value() (driver.Value, error) {
  146. return json.Marshal(n)
  147. }