sys_autocode_history.go 5.1 KB

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