animeic 2 years ago
parent
commit
8258fef918
2 changed files with 107 additions and 114 deletions
  1. 0 15
      queencount/api/servcie-test.go
  2. 107 99
      queencount/api/service-asset-count.go

+ 0 - 15
queencount/api/servcie-test.go

@@ -1,15 +0,0 @@
-package api
-
-import (
-	"fmt"
-
-	"github.com/gin-gonic/gin"
-)
-
-// ServiceTestHttp JWT授权的http处理函数
-func ServiceTestHttp(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
-
-	fmt.Println("test")
-
-	return "hello world " + apictx.Svc.DebugUserPhone, nil
-}

+ 107 - 99
queencount/api/service-asset-count.go

@@ -255,9 +255,25 @@ func GetInfo(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	timeSet := GetCountTimeSet(form)
 
 	query := make(map[string]interface{})
-	query["dbId"], _ = primitive.ObjectIDFromHex(form.DbId)
+	// query["dbId"], _ = primitive.ObjectIDFromHex(form.DbId)
+	dbId, _ := primitive.ObjectIDFromHex(form.DbId)
 
+	// ??? 需要修改
+	// 根据id查询数据库名
+	var databaseDoc model.Database
+	dbParam := &repo.DocSearchOptions{
+		CollectName: repo.CollectionDatabase,
+		Query:       map[string]interface{}{"_id": dbId},
+		Project:     []string{"_id", "name", "assets"},
+	}
+	is, err := repo.RepoSeachDoc(ctx, dbParam, &databaseDoc)
+	if !is || err != nil {
+		return nil, errors.New("资产数据为空")
+	}
+
+	// 查数据名下对应的assetcount
 	parm := &repo.DocSearchOptions{
+		Db:          databaseDoc.Name,
 		CollectName: repo.CollectionAssetCount,
 		Project:     []string{"_id", "defId", "createTime"},
 		Sort:        bson.M{"createTime": -1}, // 降序 小于等于createTime中取时间最近的 第一条
@@ -265,123 +281,115 @@ func GetInfo(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 	var assetCount model.AssetCount
 
-	// 返回的数据
-	var countInfo CountInfo
+	// 循环asset.id ->defId,defKey,defName
 
-	// 获取分类数据
-	defCateInfo, err := GetDefCate(form.DbId, ctx)
-	if err != nil {
-		return nil, err
+	if databaseDoc.Assets == nil {
+		return nil, errors.New("资产不存在")
 	}
-
+	// 返回的数据
+	var countInfo CountInfo
+	// 各项数据
 	var meshCountTime CountTime
 	var meshDefItem DefItem
 
-	counti := 0
-	countj := 0
-
-	for _, defMesh := range defCateInfo.Mesh {
-		counti++
-		query["type"] = 10
-		meshDefItem.DefKey = defMesh.DefKey
-		meshDefItem.DefName = defMesh.DefName
-		for _, timePoint := range timeSet {
-			// 拼接条件 查询各个增量时间段的数据
-			query["defKey"] = defMesh.DefKey
-			parm.Query = query
-			countj++
-			bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
-			if !bl || err != nil {
-				meshCountTime.Count = 0
-			} else {
-				meshCountTime.Count = assetCount.Count
-			}
-			meshCountTime.TimePoint = timePoint
-			meshDefItem.Time = append(meshDefItem.Time, meshCountTime)
+	var imageCountTime CountTime
+	var imageDefItem DefItem
 
-		}
-		countInfo.Mesh = append(countInfo.Mesh, meshDefItem)
+	var materialCountTime CountTime
+	var materialDefItem DefItem
 
-	}
-	fmt.Println(counti) // 29
-	fmt.Println("---------------------------")
-	fmt.Println(countj) // 725
+	var env3dCountTime CountTime
+	var env3dDefItem DefItem
 
-	var imageCountTime CountTime
-	var imageDefItem DefItem
-	for _, defImage := range defCateInfo.Image {
-		query["type"] = 20
-		imageDefItem.DefKey = defImage.DefKey
-		imageDefItem.DefName = defImage.DefName
-		for _, timePoint := range timeSet {
-			// 拼接条件 查询各个增量时间段的数据
-			query["createTime"] = bson.M{"$lte": timePoint}
-			query["defKey"] = defImage.DefKey
-			parm.Query = query
-			bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
-
-			if !bl || err != nil {
-				imageCountTime.Count = 0
-			} else {
-				imageCountTime.Count = assetCount.Count
-			}
-			imageCountTime.TimePoint = timePoint
-			imageDefItem.Time = append(imageDefItem.Time, imageCountTime)
+	for _, asset := range databaseDoc.Assets {
+		// mesh
+		if asset.Type == 10 {
+			meshDefItem.DefKey = asset.Collection
+			meshDefItem.DefName = asset.Label
+
+			for _, timePoint := range timeSet {
+				// 拼接条件 查询各个增量时间段的数据
+				query["defId"] = asset.Id
+				parm.Query = query
+				bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
+				if !bl || err != nil {
+					meshCountTime.Count = 0
+				} else {
+					meshCountTime.Count = assetCount.Count
+				}
+				meshCountTime.TimePoint = timePoint
+				meshDefItem.Time = append(meshDefItem.Time, meshCountTime)
 
+			}
+			countInfo.Mesh = append(countInfo.Mesh, meshDefItem)
 		}
-		countInfo.Image = append(countInfo.Image, imageDefItem)
 
-	}
+		// image
 
-	var materialCountTime CountTime
-	var materialDefItem DefItem
-	for _, defMaterial := range defCateInfo.Material {
-		query["type"] = 30
-		materialDefItem.DefKey = defMaterial.DefKey
-		materialDefItem.DefName = defMaterial.DefName
-		for _, timePoint := range timeSet {
-			// 拼接条件 查询各个增量时间段的数据
-			query["createTime"] = bson.M{"$lte": timePoint}
-			query["defKey"] = defMaterial.DefKey
-			parm.Query = query
-			bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
-
-			if !bl || err != nil {
-				materialCountTime.Count = 0
-			} else {
-				materialCountTime.Count = assetCount.Count
-			}
-			materialCountTime.TimePoint = timePoint
-			materialDefItem.Time = append(materialDefItem.Time, materialCountTime)
+		if asset.Type == 20 {
+			imageDefItem.DefKey = asset.Collection
+			imageDefItem.DefName = asset.Label
+
+			for _, timePoint := range timeSet {
+				// 拼接条件 查询各个增量时间段的数据
+				query["defId"] = asset.Id
+				parm.Query = query
+				bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
+				if !bl || err != nil {
+					imageCountTime.Count = 0
+				} else {
+					imageCountTime.Count = assetCount.Count
+				}
+				imageCountTime.TimePoint = timePoint
+				imageDefItem.Time = append(imageDefItem.Time, imageCountTime)
 
+			}
+			countInfo.Image = append(countInfo.Image, imageDefItem)
 		}
-		countInfo.Material = append(countInfo.Material, materialDefItem)
 
-	}
+		// material
+		if asset.Type == 30 {
+			materialDefItem.DefKey = asset.Collection
+			materialDefItem.DefName = asset.Label
+
+			for _, timePoint := range timeSet {
+				// 拼接条件 查询各个增量时间段的数据
+				query["defId"] = asset.Id
+				parm.Query = query
+				bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
+				if !bl || err != nil {
+					imageCountTime.Count = 0
+				} else {
+					materialCountTime.Count = assetCount.Count
+				}
+				materialCountTime.TimePoint = timePoint
+				materialDefItem.Time = append(materialDefItem.Time, materialCountTime)
 
-	var env3dCountTime CountTime
-	var env3dDefItem DefItem
-	for _, defEnv3d := range defCateInfo.Env3d {
-		query["type"] = 40
-		env3dDefItem.DefKey = defEnv3d.DefKey
-		env3dDefItem.DefName = defEnv3d.DefName
-		for _, timePoint := range timeSet {
-			// 拼接条件 查询各个增量时间段的数据
-			query["createTime"] = bson.M{"$lte": timePoint}
-			query["defKey"] = defEnv3d.DefKey
-			parm.Query = query
-			bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
-
-			if !bl || err != nil {
-				env3dCountTime.Count = 0
-			} else {
-				env3dCountTime.Count = assetCount.Count
 			}
-			env3dCountTime.TimePoint = timePoint
-			env3dDefItem.Time = append(env3dDefItem.Time, env3dCountTime)
+			countInfo.Material = append(countInfo.Material, materialDefItem)
+		}
+
+		// env3d
+		if asset.Type == 40 {
+			env3dDefItem.DefKey = asset.Collection
+			env3dDefItem.DefName = asset.Label
+
+			for _, timePoint := range timeSet {
+				// 拼接条件 查询各个增量时间段的数据
+				query["defId"] = asset.Id
+				parm.Query = query
+				bl, err := repo.RepoSeachDoc(ctx, parm, &assetCount)
+				if !bl || err != nil {
+					env3dCountTime.Count = 0
+				} else {
+					env3dCountTime.Count = assetCount.Count
+				}
+				env3dCountTime.TimePoint = timePoint
+				env3dDefItem.Time = append(env3dDefItem.Time, env3dCountTime)
 
+			}
+			countInfo.Env3d = append(countInfo.Env3d, env3dDefItem)
 		}
-		countInfo.Env3d = append(countInfo.Env3d, env3dDefItem)
 
 	}