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.

79 lines
2.3 KiB

  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, 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. 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.AutoCodePath, ";") {
  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. _ = utils.AutoClearCode(meta[0], meta[2])
  55. }
  56. }
  57. md.Flag = 1
  58. return global.GVA_DB.Save(&md).Error
  59. }
  60. func GetMeta(id uint) (string, error) {
  61. var meta string
  62. return meta, global.GVA_DB.Model(model.SysAutoCodeHistory{}).Select("request_meta").First(&meta, id).Error
  63. }
  64. func GetSysHistoryPage(info request.PageInfo) (err error, list interface{}, total int64) {
  65. limit := info.PageSize
  66. offset := info.PageSize * (info.Page - 1)
  67. db := global.GVA_DB
  68. var fileLists []model.SysAutoCodeHistory
  69. err = db.Find(&fileLists).Count(&total).Error
  70. err = db.Limit(limit).Offset(offset).Order("updated_at desc").Select("id,created_at,updated_at,table_name,auto_code_path,injection_meta,api_ids,flag").Find(&fileLists).Error
  71. return err, fileLists, total
  72. }