sys_autocode_history.go 2.6 KB

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