sys_auto_code_history.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package system
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "gorm.io/gorm"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "strings"
  9. )
  10. // SysAutoCodeHistory 自动迁移代码记录,用于回滚,重放使用
  11. type SysAutoCodeHistory struct {
  12. global.GVA_MODEL
  13. Table string `json:"tableName" gorm:"column:table_name;comment:表名"`
  14. Package string `json:"package" gorm:"column:package;comment:模块名/插件名"`
  15. Request string `json:"request" gorm:"type:text;column:request;comment:前端传入的结构化信息"`
  16. StructName string `json:"structName" gorm:"column:struct_name;comment:结构体名称"`
  17. Abbreviation string `json:"abbreviation" gorm:"column:abbreviation;comment:结构体名称缩写"`
  18. BusinessDB string `json:"businessDb" gorm:"column:business_db;comment:业务库"`
  19. Description string `json:"description" gorm:"column:description;comment:Struct中文名称"`
  20. Templates map[string]string `json:"template" gorm:"serializer:json;type:text;column:templates;comment:模板信息"`
  21. Injections map[string]string `json:"injections" gorm:"serializer:json;type:text;column:Injections;comment:注入路径"`
  22. Flag int `json:"flag" gorm:"column:flag;comment:[0:创建,1:回滚]"`
  23. ApiIDs []uint `json:"apiIDs" gorm:"serializer:json;column:api_ids;comment:api表注册内容"`
  24. MenuID uint `json:"menuId" gorm:"column:menu_id;comment:菜单ID"`
  25. ExportTemplateID uint `json:"exportTemplateID" gorm:"column:export_template_id;comment:导出模板ID"`
  26. AutoCodePackage SysAutoCodePackage `json:"autoCodePackage" gorm:"foreignKey:ID;references:PackageID"`
  27. PackageID uint `json:"packageID" gorm:"column:package_id;comment:包ID"`
  28. }
  29. func (s *SysAutoCodeHistory) BeforeCreate(db *gorm.DB) error {
  30. templates := make(map[string]string, len(s.Templates))
  31. for key, value := range s.Templates {
  32. server := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server)
  33. {
  34. hasServer := strings.Index(key, server)
  35. if hasServer != -1 {
  36. key = strings.TrimPrefix(key, server)
  37. keys := strings.Split(key, string(os.PathSeparator))
  38. key = path.Join(keys...)
  39. }
  40. } // key
  41. web := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.WebRoot())
  42. hasWeb := strings.Index(value, web)
  43. if hasWeb != -1 {
  44. value = strings.TrimPrefix(value, web)
  45. values := strings.Split(value, string(os.PathSeparator))
  46. value = path.Join(values...)
  47. templates[key] = value
  48. continue
  49. }
  50. hasServer := strings.Index(value, server)
  51. if hasServer != -1 {
  52. value = strings.TrimPrefix(value, server)
  53. values := strings.Split(value, string(os.PathSeparator))
  54. value = path.Join(values...)
  55. templates[key] = value
  56. continue
  57. }
  58. }
  59. s.Templates = templates
  60. return nil
  61. }
  62. func (s *SysAutoCodeHistory) TableName() string {
  63. return "sys_auto_code_histories"
  64. }