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.

92 lines
3.0 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. "gin-vue-admin/global"
  4. "gin-vue-admin/model/common/request"
  5. "gin-vue-admin/model/system"
  6. "gin-vue-admin/utils"
  7. "strings"
  8. "go.uber.org/zap"
  9. )
  10. type AutoCodeHistoryService struct {
  11. }
  12. var AutoCodeHistoryServiceApp = new(AutoCodeHistoryService)
  13. // CreateAutoCodeHistory RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
  14. func (autoCodeHistoryService *AutoCodeHistoryService) CreateAutoCodeHistory(meta, structName, structCNName, autoCodePath string, injectionMeta string, tableName string, apiIds string) error {
  15. return global.GVA_DB.Create(&system.SysAutoCodeHistory{
  16. RequestMeta: meta,
  17. AutoCodePath: autoCodePath,
  18. InjectionMeta: injectionMeta,
  19. StructName: structName,
  20. StructCNName: structCNName,
  21. TableName: tableName,
  22. ApiIDs: apiIds,
  23. }).Error
  24. }
  25. // RollBack 回滚
  26. func (autoCodeHistoryService *AutoCodeHistoryService) RollBack(id uint) error {
  27. md := system.SysAutoCodeHistory{}
  28. if err := global.GVA_DB.First(&md, id).Error; err != nil {
  29. return err
  30. }
  31. // 清除API表
  32. err := ApiServiceApp.DeleteApiByIds(strings.Split(md.ApiIDs, ";"))
  33. if err != nil {
  34. global.GVA_LOG.Error("ClearTag DeleteApiByIds:", zap.Error(err))
  35. }
  36. // 获取全部表名
  37. err, dbNames := AutoCodeServiceApp.GetTables(global.GVA_CONFIG.Mysql.Dbname)
  38. if err != nil {
  39. global.GVA_LOG.Error("ClearTag GetTables:", zap.Error(err))
  40. }
  41. // 删除表
  42. for _, name := range dbNames {
  43. if strings.Contains(strings.ToUpper(strings.Replace(name.TableName, "_", "", -1)), strings.ToUpper(md.TableName)) {
  44. // 删除表
  45. if err = AutoCodeServiceApp.DropTable(name.TableName); err != nil {
  46. global.GVA_LOG.Error("ClearTag DropTable:", zap.Error(err))
  47. }
  48. }
  49. }
  50. // 删除文件
  51. for _, path := range strings.Split(md.AutoCodePath, ";") {
  52. _ = utils.DeLFile(path)
  53. }
  54. // 清除注入
  55. for _, v := range strings.Split(md.InjectionMeta, ";") {
  56. // RouterPath@functionName@RouterString
  57. meta := strings.Split(v, "@")
  58. if len(meta) == 3 {
  59. _ = utils.AutoClearCode(meta[0], meta[2])
  60. }
  61. }
  62. md.Flag = 1
  63. return global.GVA_DB.Save(&md).Error
  64. }
  65. func (autoCodeHistoryService *AutoCodeHistoryService) GetMeta(id uint) (string, error) {
  66. var meta string
  67. return meta, global.GVA_DB.Model(system.SysAutoCodeHistory{}).Select("request_meta").First(&meta, id).Error
  68. }
  69. // GetSysHistoryPage 获取系统历史数据
  70. func (autoCodeHistoryService *AutoCodeHistoryService) GetSysHistoryPage(info request.PageInfo) (err error, list interface{}, total int64) {
  71. limit := info.PageSize
  72. offset := info.PageSize * (info.Page - 1)
  73. db := global.GVA_DB
  74. var fileLists []system.SysAutoCodeHistory
  75. err = db.Find(&fileLists).Count(&total).Error
  76. 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
  77. return err, fileLists, total
  78. }
  79. // DeletePage 删除历史数据
  80. func (autoCodeHistoryService *AutoCodeHistoryService) DeletePage(id uint) error {
  81. return global.GVA_DB.Delete(system.SysAutoCodeHistory{}, id).Error
  82. }