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.

104 lines
3.5 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. nPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root,
  57. "rm_file", time.Now().Format("20060102"), filepath.Base(filepath.Dir(filepath.Dir(path))), filepath.Base(filepath.Dir(path)), filepath.Base(path))
  58. err = utils.FileMove(path, nPath)
  59. if err != nil {
  60. fmt.Println(">>>>>>>>>>>>>>>>>>>", err)
  61. }
  62. //_ = utils.DeLFile(path)
  63. }
  64. // 清除注入
  65. for _, v := range strings.Split(md.InjectionMeta, ";") {
  66. // RouterPath@functionName@RouterString
  67. meta := strings.Split(v, "@")
  68. if len(meta) == 3 {
  69. _ = utils.AutoClearCode(meta[0], meta[2])
  70. }
  71. }
  72. md.Flag = 1
  73. return global.GVA_DB.Save(&md).Error
  74. }
  75. func (autoCodeHistoryService *AutoCodeHistoryService) GetMeta(id uint) (string, error) {
  76. var meta string
  77. return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").First(&meta, id).Error
  78. }
  79. // GetSysHistoryPage 获取系统历史数据
  80. func (autoCodeHistoryService *AutoCodeHistoryService) GetSysHistoryPage(info request.PageInfo) (err error, list interface{}, total int64) {
  81. limit := info.PageSize
  82. offset := info.PageSize * (info.Page - 1)
  83. db := global.GVA_DB
  84. var fileLists []system.SysAutoCodeHistory
  85. err = db.Find(&fileLists).Count(&total).Error
  86. 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
  87. return err, fileLists, total
  88. }
  89. // DeletePage 删除历史数据
  90. func (autoCodeHistoryService *AutoCodeHistoryService) DeletePage(id uint) error {
  91. return global.GVA_DB.Delete(system.SysAutoCodeHistory{}, id).Error
  92. }