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.

129 lines
4.1 KiB

3 years ago
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. // 判断目标文件是否存在
  71. for utils.FileExist(nPath) {
  72. fmt.Println("文件已存在:", nPath)
  73. nPath += fmt.Sprintf("_%d", time.Now().Nanosecond())
  74. }
  75. err = utils.FileMove(path, nPath)
  76. if err != nil {
  77. fmt.Println(">>>>>>>>>>>>>>>>>>>", err)
  78. }
  79. //_ = utils.DeLFile(path)
  80. }
  81. // 清除注入
  82. for _, v := range strings.Split(md.InjectionMeta, ";") {
  83. // RouterPath@functionName@RouterString
  84. meta := strings.Split(v, "@")
  85. if len(meta) == 3 {
  86. _ = utils.AutoClearCode(meta[0], meta[2])
  87. }
  88. }
  89. md.Flag = 1
  90. return global.GVA_DB.Save(&md).Error
  91. }
  92. func (autoCodeHistoryService *AutoCodeHistoryService) GetMeta(id uint) (string, error) {
  93. var meta string
  94. return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").First(&meta, id).Error
  95. }
  96. // GetSysHistoryPage 获取系统历史数据
  97. func (autoCodeHistoryService *AutoCodeHistoryService) GetSysHistoryPage(info request.PageInfo) (err error, list interface{}, total int64) {
  98. limit := info.PageSize
  99. offset := info.PageSize * (info.Page - 1)
  100. db := global.GVA_DB.Model(&system.SysAutoCodeHistory{})
  101. var fileLists []system.SysAutoCodeHistory
  102. err = db.Count(&total).Error
  103. if err != nil {
  104. return
  105. }
  106. 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
  107. return err, fileLists, total
  108. }
  109. // DeletePage 删除历史数据
  110. func (autoCodeHistoryService *AutoCodeHistoryService) DeletePage(id uint) error {
  111. return global.GVA_DB.Delete(system.SysAutoCodeHistory{}, id).Error
  112. }