sys_autocode_history.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package system
  2. import (
  3. "errors"
  4. "fmt"
  5. systemReq "log-server/model/system/request"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "log-server/model/system/response"
  10. "log-server/global"
  11. "log-server/model/common/request"
  12. "log-server/model/system"
  13. "log-server/utils"
  14. "go.uber.org/zap"
  15. )
  16. var RepeatErr = errors.New("重复创建")
  17. type AutoCodeHistoryService struct{}
  18. var AutoCodeHistoryServiceApp = new(AutoCodeHistoryService)
  19. // CreateAutoCodeHistory 创建代码生成器历史记录
  20. // RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
  21. // Author [SliverHorn](https://github.com/SliverHorn)
  22. // Author [songzhibin97](https://github.com/songzhibin97)
  23. func (autoCodeHistoryService *AutoCodeHistoryService) CreateAutoCodeHistory(meta, structName, structCNName, autoCodePath string, injectionMeta string, tableName string, apiIds string, Package string) error {
  24. return global.GVA_DB.Create(&system.SysAutoCodeHistory{
  25. Package: Package,
  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. // First 根据id获取代码生成器历史的数据
  36. // Author [SliverHorn](https://github.com/SliverHorn)
  37. // Author [songzhibin97](https://github.com/songzhibin97)
  38. func (autoCodeHistoryService *AutoCodeHistoryService) First(info *request.GetById) (string, error) {
  39. var meta string
  40. return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").Where("id = ?", info.Uint()).First(&meta).Error
  41. }
  42. // Repeat 检测重复
  43. // Author [SliverHorn](https://github.com/SliverHorn)
  44. // Author [songzhibin97](https://github.com/songzhibin97)
  45. func (autoCodeHistoryService *AutoCodeHistoryService) Repeat(structName string, Package string) bool {
  46. var count int64
  47. global.GVA_DB.Model(&system.SysAutoCodeHistory{}).Where("struct_name = ? and package = ? and flag = 0", structName, Package).Count(&count)
  48. return count > 0
  49. }
  50. // RollBack 回滚
  51. // Author [SliverHorn](https://github.com/SliverHorn)
  52. // Author [songzhibin97](https://github.com/songzhibin97)
  53. func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(info *systemReq.RollBack) error {
  54. md := system.SysAutoCodeHistory{}
  55. if err := global.GVA_DB.Where("id = ?", info.ID).First(&md).Error; err != nil {
  56. return err
  57. }
  58. // 清除API表
  59. err := ApiServiceApp.DeleteApiByIds(strings.Split(md.ApiIDs, ";"))
  60. if err != nil {
  61. global.GVA_LOG.Error("ClearTag DeleteApiByIds:", zap.Error(err))
  62. }
  63. // 删除表
  64. if info.DeleteTable {
  65. if err = AutoCodeServiceApp.DropTable(md.TableName); err != nil {
  66. global.GVA_LOG.Error("ClearTag DropTable:", zap.Error(err))
  67. }
  68. }
  69. // 删除文件
  70. for _, path := range strings.Split(md.AutoCodePath, ";") {
  71. // 增加安全判断补丁:
  72. _path, err := filepath.Abs(path)
  73. if err != nil || _path != path {
  74. continue
  75. }
  76. // 迁移
  77. nPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root,
  78. "rm_file", time.Now().Format("20060102"), filepath.Base(filepath.Dir(filepath.Dir(path))), filepath.Base(filepath.Dir(path)), filepath.Base(path))
  79. // 判断目标文件是否存在
  80. for utils.FileExist(nPath) {
  81. fmt.Println("文件已存在:", nPath)
  82. nPath += fmt.Sprintf("_%d", time.Now().Nanosecond())
  83. }
  84. err = utils.FileMove(path, nPath)
  85. if err != nil {
  86. global.GVA_LOG.Error("file move err ", zap.Error(err))
  87. }
  88. //_ = utils.DeLFile(path)
  89. }
  90. // 清除注入
  91. for _, v := range strings.Split(md.InjectionMeta, ";") {
  92. // RouterPath@functionName@RouterString
  93. meta := strings.Split(v, "@")
  94. if len(meta) == 3 {
  95. _ = utils.AutoClearCode(meta[0], meta[2])
  96. }
  97. }
  98. md.Flag = 1
  99. return global.GVA_DB.Save(&md).Error
  100. }
  101. // Delete 删除历史数据
  102. // Author [SliverHorn](https://github.com/SliverHorn)
  103. // Author [songzhibin97](https://github.com/songzhibin97)
  104. func (autoCodeHistoryService *AutoCodeHistoryService) Delete(info *request.GetById) error {
  105. return global.GVA_DB.Where("id = ?", info.Uint()).Delete(&system.SysAutoCodeHistory{}).Error
  106. }
  107. // GetList 获取系统历史数据
  108. // Author [SliverHorn](https://github.com/SliverHorn)
  109. // Author [songzhibin97](https://github.com/songzhibin97)
  110. func (autoCodeHistoryService *AutoCodeHistoryService) GetList(info request.PageInfo) (list []response.AutoCodeHistory, total int64, err error) {
  111. limit := info.PageSize
  112. offset := info.PageSize * (info.Page - 1)
  113. db := global.GVA_DB.Model(&system.SysAutoCodeHistory{})
  114. var entities []response.AutoCodeHistory
  115. err = db.Count(&total).Error
  116. if err != nil {
  117. return nil, total, err
  118. }
  119. err = db.Limit(limit).Offset(offset).Order("updated_at desc").Find(&entities).Error
  120. return entities, total, err
  121. }