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.

75 lines
2.1 KiB

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