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.

143 lines
5.0 KiB

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