You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package system
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "time"
  7. "github.com/flipped-aurora/gin-vue-admin/server/global"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  9. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  10. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  11. "go.uber.org/zap"
  12. )
  13. type AutoCodeHistoryService struct {
  14. }
  15. var AutoCodeHistoryServiceApp = new(AutoCodeHistoryService)
  16. // CreateAutoCodeHistory RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
  17. func (autoCodeHistoryService *AutoCodeHistoryService) CreateAutoCodeHistory(meta, structName, structCNName, autoCodePath string, injectionMeta string, tableName string, apiIds string) error {
  18. return global.GVA_DB.Create(&system.SysAutoCodeHistory{
  19. RequestMeta: meta,
  20. AutoCodePath: autoCodePath,
  21. InjectionMeta: injectionMeta,
  22. StructName: structName,
  23. StructCNName: structCNName,
  24. TableName: tableName,
  25. ApiIDs: apiIds,
  26. }).Error
  27. }
  28. // RollBack 回滚
  29. func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(id uint) error {
  30. md := system.SysAutoCodeHistory{}
  31. if err := global.GVA_DB.First(&md, id).Error; err != nil {
  32. return err
  33. }
  34. // 清除API表
  35. err := ApiServiceApp.DeleteApiByIds(strings.Split(md.ApiIDs, ";"))
  36. if err != nil {
  37. global.GVA_LOG.Error("ClearTag DeleteApiByIds:", zap.Error(err))
  38. }
  39. // 获取全部表名
  40. err, dbNames := AutoCodeServiceApp.GetTables(global.GVA_CONFIG.Mysql.Dbname)
  41. if err != nil {
  42. global.GVA_LOG.Error("ClearTag GetTables:", zap.Error(err))
  43. }
  44. // 删除表
  45. for _, name := range dbNames {
  46. if strings.Contains(strings.ToUpper(strings.Replace(name.TableName, "_", "", -1)), strings.ToUpper(md.TableName)) {
  47. // 删除表
  48. if err = AutoCodeServiceApp.DropTable(name.TableName); err != nil {
  49. global.GVA_LOG.Error("ClearTag DropTable:", zap.Error(err))
  50. }
  51. }
  52. }
  53. // 删除文件
  54. for _, path := range strings.Split(md.AutoCodePath, ";") {
  55. // 增加安全判断补丁:
  56. _path, err := filepath.Abs(path)
  57. if err != nil || _path != path {
  58. continue
  59. }
  60. // 迁移
  61. nPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root,
  62. "rm_file", time.Now().Format("20060102"), filepath.Base(filepath.Dir(filepath.Dir(path))), filepath.Base(filepath.Dir(path)), filepath.Base(path))
  63. err = utils.FileMove(path, nPath)
  64. if err != nil {
  65. fmt.Println(">>>>>>>>>>>>>>>>>>>", err)
  66. }
  67. //_ = utils.DeLFile(path)
  68. }
  69. // 清除注入
  70. for _, v := range strings.Split(md.InjectionMeta, ";") {
  71. // RouterPath@functionName@RouterString
  72. meta := strings.Split(v, "@")
  73. if len(meta) == 3 {
  74. _ = utils.AutoClearCode(meta[0], meta[2])
  75. }
  76. }
  77. md.Flag = 1
  78. return global.GVA_DB.Save(&md).Error
  79. }
  80. func (autoCodeHistoryService *AutoCodeHistoryService) GetMeta(id uint) (string, error) {
  81. var meta string
  82. return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").First(&meta, id).Error
  83. }
  84. // GetSysHistoryPage 获取系统历史数据
  85. func (autoCodeHistoryService *AutoCodeHistoryService) GetSysHistoryPage(info request.PageInfo) (err error, list interface{}, total int64) {
  86. limit := info.PageSize
  87. offset := info.PageSize * (info.Page - 1)
  88. db := global.GVA_DB
  89. var fileLists []system.SysAutoCodeHistory
  90. err = db.Find(&fileLists).Count(&total).Error
  91. err = db.Limit(limit).Offset(offset).Order("updated_at desc").Select("id,created_at,updated_at,struct_name,struct_cn_name,flag,table_name").Find(&fileLists).Error
  92. return err, fileLists, total
  93. }
  94. // DeletePage 删除历史数据
  95. func (autoCodeHistoryService *AutoCodeHistoryService) DeletePage(id uint) error {
  96. return global.GVA_DB.Delete(system.SysAutoCodeHistory{}, id).Error
  97. }