sys_auto_code.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package system
  2. import (
  3. "errors"
  4. "go/token"
  5. "strings"
  6. "server/global"
  7. )
  8. // AutoCodeStruct 初始版本自动化代码工具
  9. type AutoCodeStruct struct {
  10. StructName string `json:"structName"` // Struct名称
  11. TableName string `json:"tableName"` // 表名
  12. PackageName string `json:"packageName"` // 文件名称
  13. HumpPackageName string `json:"humpPackageName"` // go文件名称
  14. Abbreviation string `json:"abbreviation"` // Struct简称
  15. Description string `json:"description"` // Struct中文名称
  16. AutoCreateApiToSql bool `json:"autoCreateApiToSql"` // 是否自动创建api
  17. AutoCreateMenuToSql bool `json:"autoCreateMenuToSql"` // 是否自动创建menu
  18. AutoCreateResource bool `json:"autoCreateResource"` // 是否自动创建资源标识
  19. AutoMoveFile bool `json:"autoMoveFile"` // 是否自动移动文件
  20. BusinessDB string `json:"businessDB"` // 业务数据库
  21. GvaModel bool `json:"gvaModel"` // 是否使用gva默认Model
  22. Fields []*Field `json:"fields"`
  23. PrimaryField *Field `json:"primaryField"`
  24. HasTimer bool `json:"-"`
  25. HasSearchTimer bool `json:"-"`
  26. DictTypes []string `json:"-"`
  27. Package string `json:"package"`
  28. PackageT string `json:"-"`
  29. NeedSort bool `json:"-"`
  30. HasPic bool `json:"-"`
  31. HasRichText bool `json:"-"`
  32. HasFile bool `json:"-"`
  33. NeedJSON bool `json:"-"`
  34. FrontFields []*Field `json:"-"`
  35. HasDataSource bool `json:"-"`
  36. DataSourceMap map[string]*DataSource `json:"-"`
  37. }
  38. type DataSource struct {
  39. Table string `json:"table"`
  40. Label string `json:"label"`
  41. Value string `json:"value"`
  42. }
  43. func (a *AutoCodeStruct) Pretreatment() {
  44. a.KeyWord()
  45. a.SuffixTest()
  46. }
  47. // KeyWord 是go关键字的处理加上 _ ,防止编译报错
  48. // Author [SliverHorn](https://github.com/SliverHorn)
  49. func (a *AutoCodeStruct) KeyWord() {
  50. if token.IsKeyword(a.Abbreviation) {
  51. a.Abbreviation = a.Abbreviation + "_"
  52. }
  53. }
  54. // SuffixTest 处理_test 后缀
  55. // Author [SliverHorn](https://github.com/SliverHorn)
  56. func (a *AutoCodeStruct) SuffixTest() {
  57. if strings.HasSuffix(a.HumpPackageName, "test") {
  58. a.HumpPackageName = a.HumpPackageName + "_"
  59. }
  60. }
  61. type Field struct {
  62. FieldName string `json:"fieldName"` // Field名
  63. FieldDesc string `json:"fieldDesc"` // 中文名
  64. FieldType string `json:"fieldType"` // Field数据类型
  65. FieldJson string `json:"fieldJson"` // FieldJson
  66. DataTypeLong string `json:"dataTypeLong"` // 数据库字段长度
  67. Comment string `json:"comment"` // 数据库字段描述
  68. ColumnName string `json:"columnName"` // 数据库字段
  69. FieldSearchType string `json:"fieldSearchType"` // 搜索条件
  70. DictType string `json:"dictType"` // 字典
  71. Front bool `json:"front"` // 是否前端可见
  72. Require bool `json:"require"` // 是否必填
  73. DefaultValue string `json:"defaultValue"` // 是否必填
  74. ErrorText string `json:"errorText"` // 校验失败文字
  75. Clearable bool `json:"clearable"` // 是否可清空
  76. Sort bool `json:"sort"` // 是否增加排序
  77. PrimaryKey bool `json:"primaryKey"` // 是否主键
  78. DataSource *DataSource `json:"dataSource"` // 数据源
  79. CheckDataSource bool `json:"checkDataSource"` // 是否检查数据源
  80. }
  81. var ErrAutoMove error = errors.New("创建代码成功并移动文件成功")
  82. type SysAutoCode struct {
  83. global.GVA_MODEL
  84. PackageName string `json:"packageName" gorm:"comment:包名"`
  85. Label string `json:"label" gorm:"comment:展示名"`
  86. Desc string `json:"desc" gorm:"comment:描述"`
  87. }
  88. type AutoPlugReq struct {
  89. PlugName string `json:"plugName"` // 必然大写开头
  90. Snake string `json:"snake"` // 后端自动转为 snake
  91. RouterGroup string `json:"routerGroup"`
  92. HasGlobal bool `json:"hasGlobal"`
  93. HasRequest bool `json:"hasRequest"`
  94. HasResponse bool `json:"hasResponse"`
  95. NeedModel bool `json:"needModel"`
  96. Global []AutoPlugInfo `json:"global,omitempty"`
  97. Request []AutoPlugInfo `json:"request,omitempty"`
  98. Response []AutoPlugInfo `json:"response,omitempty"`
  99. }
  100. func (a *AutoPlugReq) CheckList() {
  101. a.Global = bind(a.Global)
  102. a.Request = bind(a.Request)
  103. a.Response = bind(a.Response)
  104. }
  105. func bind(req []AutoPlugInfo) []AutoPlugInfo {
  106. var r []AutoPlugInfo
  107. for _, info := range req {
  108. if info.Effective() {
  109. r = append(r, info)
  110. }
  111. }
  112. return r
  113. }
  114. type AutoPlugInfo struct {
  115. Key string `json:"key"`
  116. Type string `json:"type"`
  117. Desc string `json:"desc"`
  118. }
  119. func (a AutoPlugInfo) Effective() bool {
  120. return a.Key != "" && a.Type != "" && a.Desc != ""
  121. }