2
0

sys_auto_code.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package request
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/flipped-aurora/gin-vue-admin/server/global"
  6. model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  7. "github.com/pkg/errors"
  8. "go/token"
  9. "strings"
  10. )
  11. type AutoCode struct {
  12. Package string `json:"package"`
  13. PackageT string `json:"-"`
  14. TableName string `json:"tableName" example:"表名"` // 表名
  15. BusinessDB string `json:"businessDB" example:"业务数据库"` // 业务数据库
  16. StructName string `json:"structName" example:"Struct名称"` // Struct名称
  17. PackageName string `json:"packageName" example:"文件名称"` // 文件名称
  18. Description string `json:"description" example:"Struct中文名称"` // Struct中文名称
  19. Abbreviation string `json:"abbreviation" example:"Struct简称"` // Struct简称
  20. HumpPackageName string `json:"humpPackageName" example:"go文件名称"` // go文件名称
  21. GvaModel bool `json:"gvaModel" example:"false"` // 是否使用gva默认Model
  22. AutoMigrate bool `json:"autoMigrate" example:"false"` // 是否自动迁移表结构
  23. AutoCreateResource bool `json:"autoCreateResource" example:"false"` // 是否自动创建资源标识
  24. AutoCreateApiToSql bool `json:"autoCreateApiToSql" example:"false"` // 是否自动创建api
  25. AutoCreateMenuToSql bool `json:"autoCreateMenuToSql" example:"false"` // 是否自动创建menu
  26. AutoCreateBtnAuth bool `json:"autoCreateBtnAuth" example:"false"` // 是否自动创建按钮权限
  27. OnlyTemplate bool `json:"onlyTemplate" example:"false"` // 是否只生成模板
  28. IsTree bool `json:"isTree" example:"false"` // 是否树形结构
  29. TreeJson string `json:"treeJson" example:"展示的树json字段"` // 展示的树json字段
  30. IsAdd bool `json:"isAdd" example:"false"` // 是否新增
  31. Fields []*AutoCodeField `json:"fields"`
  32. GenerateWeb bool `json:"generateWeb" example:"true"` // 是否生成web
  33. GenerateServer bool `json:"generateServer" example:"true"` // 是否生成server
  34. Module string `json:"-"`
  35. DictTypes []string `json:"-"`
  36. PrimaryField *AutoCodeField `json:"primaryField"`
  37. DataSourceMap map[string]*DataSource `json:"-"`
  38. HasPic bool `json:"-"`
  39. HasFile bool `json:"-"`
  40. HasTimer bool `json:"-"`
  41. NeedSort bool `json:"-"`
  42. NeedJSON bool `json:"-"`
  43. HasRichText bool `json:"-"`
  44. HasDataSource bool `json:"-"`
  45. HasSearchTimer bool `json:"-"`
  46. HasArray bool `json:"-"`
  47. HasExcel bool `json:"-"`
  48. }
  49. type DataSource struct {
  50. DBName string `json:"dbName"`
  51. Table string `json:"table"`
  52. Label string `json:"label"`
  53. Value string `json:"value"`
  54. Association int `json:"association"` // 关联关系 1 一对一 2 一对多
  55. HasDeletedAt bool `json:"hasDeletedAt"`
  56. }
  57. func (r *AutoCode) Apis() []model.SysApi {
  58. return []model.SysApi{
  59. {
  60. Path: "/" + r.Abbreviation + "/" + "create" + r.StructName,
  61. Description: "新增" + r.Description,
  62. ApiGroup: r.Description,
  63. Method: "POST",
  64. },
  65. {
  66. Path: "/" + r.Abbreviation + "/" + "delete" + r.StructName,
  67. Description: "删除" + r.Description,
  68. ApiGroup: r.Description,
  69. Method: "DELETE",
  70. },
  71. {
  72. Path: "/" + r.Abbreviation + "/" + "delete" + r.StructName + "ByIds",
  73. Description: "批量删除" + r.Description,
  74. ApiGroup: r.Description,
  75. Method: "DELETE",
  76. },
  77. {
  78. Path: "/" + r.Abbreviation + "/" + "update" + r.StructName,
  79. Description: "更新" + r.Description,
  80. ApiGroup: r.Description,
  81. Method: "PUT",
  82. },
  83. {
  84. Path: "/" + r.Abbreviation + "/" + "find" + r.StructName,
  85. Description: "根据ID获取" + r.Description,
  86. ApiGroup: r.Description,
  87. Method: "GET",
  88. },
  89. {
  90. Path: "/" + r.Abbreviation + "/" + "get" + r.StructName + "List",
  91. Description: "获取" + r.Description + "列表",
  92. ApiGroup: r.Description,
  93. Method: "GET",
  94. },
  95. }
  96. }
  97. func (r *AutoCode) Menu(template string) model.SysBaseMenu {
  98. component := fmt.Sprintf("view/%s/%s/%s.vue", r.Package, r.PackageName, r.PackageName)
  99. if template != "package" {
  100. component = fmt.Sprintf("plugin/%s/view/%s.vue", r.Package, r.PackageName)
  101. }
  102. return model.SysBaseMenu{
  103. ParentId: 0,
  104. Path: r.Abbreviation,
  105. Name: r.Abbreviation,
  106. Component: component,
  107. Meta: model.Meta{
  108. Title: r.Description,
  109. },
  110. }
  111. }
  112. // Pretreatment 预处理
  113. // Author [SliverHorn](https://github.com/SliverHorn)
  114. func (r *AutoCode) Pretreatment() error {
  115. r.Module = global.GVA_CONFIG.AutoCode.Module
  116. if token.IsKeyword(r.Abbreviation) {
  117. r.Abbreviation = r.Abbreviation + "_"
  118. } // go 关键字处理
  119. if strings.HasSuffix(r.HumpPackageName, "test") {
  120. r.HumpPackageName = r.HumpPackageName + "_"
  121. } // test
  122. length := len(r.Fields)
  123. dict := make(map[string]string, length)
  124. r.DataSourceMap = make(map[string]*DataSource, length)
  125. for i := 0; i < length; i++ {
  126. if r.Fields[i].Excel {
  127. r.HasExcel = true
  128. }
  129. if r.Fields[i].DictType != "" {
  130. dict[r.Fields[i].DictType] = ""
  131. }
  132. if r.Fields[i].Sort {
  133. r.NeedSort = true
  134. }
  135. switch r.Fields[i].FieldType {
  136. case "file":
  137. r.HasFile = true
  138. r.NeedJSON = true
  139. case "json":
  140. r.NeedJSON = true
  141. case "array":
  142. r.NeedJSON = true
  143. r.HasArray = true
  144. case "video":
  145. r.HasPic = true
  146. case "richtext":
  147. r.HasRichText = true
  148. case "picture":
  149. r.HasPic = true
  150. case "pictures":
  151. r.HasPic = true
  152. r.NeedJSON = true
  153. case "time.Time":
  154. r.HasTimer = true
  155. if r.Fields[i].FieldSearchType != "" {
  156. r.HasSearchTimer = true
  157. }
  158. }
  159. if r.Fields[i].DataSource != nil {
  160. if r.Fields[i].DataSource.Table != "" && r.Fields[i].DataSource.Label != "" && r.Fields[i].DataSource.Value != "" {
  161. r.HasDataSource = true
  162. r.Fields[i].CheckDataSource = true
  163. r.DataSourceMap[r.Fields[i].FieldJson] = r.Fields[i].DataSource
  164. }
  165. }
  166. if !r.GvaModel && r.PrimaryField == nil && r.Fields[i].PrimaryKey {
  167. r.PrimaryField = r.Fields[i]
  168. } // 自定义主键
  169. }
  170. {
  171. for key := range dict {
  172. r.DictTypes = append(r.DictTypes, key)
  173. }
  174. } // DictTypes => 字典
  175. {
  176. if r.GvaModel {
  177. r.PrimaryField = &AutoCodeField{
  178. FieldName: "ID",
  179. FieldType: "uint",
  180. FieldDesc: "ID",
  181. FieldJson: "ID",
  182. DataTypeLong: "20",
  183. Comment: "主键ID",
  184. ColumnName: "id",
  185. }
  186. }
  187. } // GvaModel
  188. {
  189. if r.IsAdd && r.PrimaryField == nil {
  190. r.PrimaryField = new(AutoCodeField)
  191. }
  192. } // 新增字段模式下不关注主键
  193. if r.Package == "" {
  194. return errors.New("Package为空!")
  195. } // 增加判断:Package不为空
  196. packages := []rune(r.Package)
  197. if len(packages) > 0 {
  198. if packages[0] >= 97 && packages[0] <= 122 {
  199. packages[0] = packages[0] - 32
  200. }
  201. r.PackageT = string(packages)
  202. } // PackageT 是 Package 的首字母大写
  203. return nil
  204. }
  205. func (r *AutoCode) History() SysAutoHistoryCreate {
  206. bytes, _ := json.Marshal(r)
  207. return SysAutoHistoryCreate{
  208. Table: r.TableName,
  209. Package: r.Package,
  210. Request: string(bytes),
  211. StructName: r.StructName,
  212. BusinessDB: r.BusinessDB,
  213. Description: r.Description,
  214. }
  215. }
  216. type AutoCodeField struct {
  217. FieldName string `json:"fieldName"` // Field名
  218. FieldDesc string `json:"fieldDesc"` // 中文名
  219. FieldType string `json:"fieldType"` // Field数据类型
  220. FieldJson string `json:"fieldJson"` // FieldJson
  221. DataTypeLong string `json:"dataTypeLong"` // 数据库字段长度
  222. Comment string `json:"comment"` // 数据库字段描述
  223. ColumnName string `json:"columnName"` // 数据库字段
  224. FieldSearchType string `json:"fieldSearchType"` // 搜索条件
  225. FieldSearchHide bool `json:"fieldSearchHide"` // 是否隐藏查询条件
  226. DictType string `json:"dictType"` // 字典
  227. //Front bool `json:"front"` // 是否前端可见
  228. Form bool `json:"form"` // 是否前端新建/编辑
  229. Table bool `json:"table"` // 是否前端表格列
  230. Desc bool `json:"desc"` // 是否前端详情
  231. Excel bool `json:"excel"` // 是否导入/导出
  232. Require bool `json:"require"` // 是否必填
  233. DefaultValue string `json:"defaultValue"` // 是否必填
  234. ErrorText string `json:"errorText"` // 校验失败文字
  235. Clearable bool `json:"clearable"` // 是否可清空
  236. Sort bool `json:"sort"` // 是否增加排序
  237. PrimaryKey bool `json:"primaryKey"` // 是否主键
  238. DataSource *DataSource `json:"dataSource"` // 数据源
  239. CheckDataSource bool `json:"checkDataSource"` // 是否检查数据源
  240. FieldIndexType string `json:"fieldIndexType"` // 索引类型
  241. }
  242. type AutoFunc struct {
  243. Package string `json:"package"`
  244. FuncName string `json:"funcName"` // 方法名称
  245. Router string `json:"router"` // 路由名称
  246. FuncDesc string `json:"funcDesc"` // 方法介绍
  247. BusinessDB string `json:"businessDB"` // 业务库
  248. StructName string `json:"structName"` // Struct名称
  249. PackageName string `json:"packageName"` // 文件名称
  250. Description string `json:"description"` // Struct中文名称
  251. Abbreviation string `json:"abbreviation"` // Struct简称
  252. HumpPackageName string `json:"humpPackageName"` // go文件名称
  253. Method string `json:"method"` // 方法
  254. IsPlugin bool `json:"isPlugin"` // 是否插件
  255. IsAuth bool `json:"isAuth"` // 是否鉴权
  256. IsPreview bool `json:"isPreview"` // 是否预览
  257. IsAi bool `json:"isAi"` // 是否AI
  258. ApiFunc string `json:"apiFunc"` // API方法
  259. ServerFunc string `json:"serverFunc"` // 服务方法
  260. JsFunc string `json:"jsFunc"` // JS方法
  261. }
  262. type InitMenu struct {
  263. PlugName string `json:"plugName"`
  264. ParentMenu string `json:"parentMenu"`
  265. Menus []uint `json:"menus"`
  266. }
  267. type InitApi struct {
  268. PlugName string `json:"plugName"`
  269. APIs []uint `json:"apis"`
  270. }
  271. type LLMAutoCode struct {
  272. Prompt string `json:"prompt" form:"prompt" gorm:"column:prompt;comment:提示语;type:text;"` //提示语
  273. Mode string `json:"mode" form:"mode" gorm:"column:mode;comment:模式;type:text;"` //模式
  274. }