瀏覽代碼

清理资源库数据

xyxie 1 月之前
父節點
當前提交
171e39bc58
共有 4 個文件被更改,包括 86 次插入1 次删除
  1. 2 1
      main.go
  2. 8 0
      models/mgodb/edb_data_base.go
  3. 8 0
      models/mgodb/edb_data_calculate.go
  4. 68 0
      services/system.go

+ 2 - 1
main.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	_ "eta/eta_forum_admin/routers"
+
 	beego "github.com/beego/beego/v2/server/web"
 )
 
@@ -10,6 +11,6 @@ func main() {
 		beego.BConfig.WebConfig.DirectoryIndex = true
 		beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
 	}
-	//services.InitEtaForumAdminId() // 初始化账号
+	//services.InitDeleteData() // 清理资源库数据
 	beego.Run()
 }

+ 8 - 0
models/mgodb/edb_data_base.go

@@ -58,3 +58,11 @@ func GetEdbDataList(endInfoId int, startDate, endDate time.Time) (list []*EdbDat
 	}
 	return
 }
+
+// 删除
+func DeleteEdbInfoBaseDataAll() (err error) {
+	filter := bson.D{}
+	db := NewMgo(utils.MgoDataDbName, "edb_data_base", utils.MgoDataCli)
+	_, err = db.DeleteMany(filter)
+	return
+}

+ 8 - 0
models/mgodb/edb_data_calculate.go

@@ -45,3 +45,11 @@ func GetEdbCalculateDataList(endInfoId int, startDate, endDate time.Time) (list
 	}
 	return
 }
+
+// 删除
+func DeleteEdbInfoCalculateDataAll() (err error) {
+	filter := bson.D{}
+	db := NewMgo(utils.MgoDataDbName, "edb_data_calculate", utils.MgoDataCli)
+	_, err = db.DeleteMany(filter)
+	return
+}

+ 68 - 0
services/system.go

@@ -1,5 +1,14 @@
 package services
 
+import (
+	"context"
+	"eta/eta_forum_admin/models/mgodb"
+	"eta/eta_forum_admin/utils"
+	"fmt"
+
+	"github.com/olivere/elastic/v7"
+)
+
 /*func GetCompanyNameByAdmins(adminIds []int) (adminCompanyMap map[int]string, err error) {
 	// 查询所有机构
 	ob := new(system.EtaBusiness)
@@ -36,3 +45,62 @@ package services
 	}
 	return
 }*/
+
+// 写一个初始化的方法清空所有的es数据,表数据,mongodb数据
+func InitDeleteData() {
+	// 清空es数据
+	var err error
+	defer func() {
+		if err != nil {
+			fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
+		}
+	}()
+	client := utils.EsClient
+	// 删除索引下的所有数据
+	deleteQuery := elastic.NewMatchAllQuery()
+	
+	// 删除图表索引数据
+	_, err = client.DeleteByQuery().
+		Index(utils.CHART_INDEX_NAME).
+		Query(deleteQuery).
+		Refresh("true").  // 添加刷新使操作立即生效
+		Do(context.Background())
+	if err != nil {
+		fmt.Printf("删除图表索引(%s)失败: %v\n", utils.CHART_INDEX_NAME, err)
+		err = nil  // 清空错误继续执行后续清理
+	}
+
+	// 删除指标索引数据 
+	_, err = client.DeleteByQuery().
+		Index(utils.DATA_INDEX_NAME).
+		Query(deleteQuery).
+		Refresh("true").
+		Do(context.Background())
+	if err != nil {
+		fmt.Printf("删除指标索引(%s)失败: %v\n", utils.DATA_INDEX_NAME, err)
+		err = nil
+	}
+
+	// 删除收藏索引数据
+	_, err = client.DeleteByQuery().
+		Index(utils.CHART_COLLECT_INDEX_NAME).
+		Query(deleteQuery).
+		Refresh("true").
+		Do(context.Background())
+	if err != nil {
+		fmt.Printf("删除收藏索引(%s)失败: %v\n", utils.CHART_COLLECT_INDEX_NAME, err)
+		err = nil
+	}
+	fmt.Println("删除es数据成功")
+	// 删除mongodb数据
+	err = mgodb.DeleteEdbInfoBaseDataAll()
+	if err != nil {
+		return
+	}
+	err = mgodb.DeleteEdbInfoCalculateDataAll()
+	if err != nil {
+		return
+	}
+	fmt.Println("删除mongodb数据成功")
+	
+}