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.

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