Bladeren bron

优化查询总学习时长

sunsheng 1 jaar geleden
bovenliggende
commit
6ae4598dbd
4 gewijzigde bestanden met toevoegingen van 20 en 164 verwijderingen
  1. BIN
      package/copter-train.exe
  2. 9 4
      src/api/exeamLog.go
  3. 11 4
      src/api/learnLog.go
  4. 0 156
      src/api/tmp

BIN
package/copter-train.exe


+ 9 - 4
src/api/exeamLog.go

@@ -117,7 +117,11 @@ func LatestExamExportExcel(c *gin.Context, apictx *ApiSession) (interface{}, err
 }
 
 func getTotalLearnTime(apictx *ApiSession, db string, uid string) (int32, error) {
-	colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
+
+	// 可以查询learn_log_statistics,数据量小些
+	colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLogStatistics)
+
+	// colls := apictx.CreateRepoCtx().Client.GetDbCollection(db, repo.CollectionLearnLog)
 
 	// 定义聚合管道
 	pipeline := mongo.Pipeline{
@@ -188,8 +192,9 @@ func getHandleLatestExamLog(apictx *ApiSession, db string, examType string) ([]*
 			fmt.Println(err)
 		}
 		// 查询用户对应总时长
-		totalLearnTime, _ := getTotalLearnTime(apictx, db, examLog.Uid)
-		_completeRate, _ := getLearnLogStatistics(apictx, db, examLog.Uid)
+		// !优化,总学习时长可以从learn_log_statistics中查询
+		// totalLearnTime, _ := getTotalLearnTime(apictx, db, examLog.Uid)
+		totalLearnTime, _completeRate, _ := getLearnLogStatistics(apictx, db, examLog.Uid)
 		completeRate := int(_completeRate * 100)
 		score := 0
 		if examLog.Score != nil {
@@ -202,7 +207,7 @@ func getHandleLatestExamLog(apictx *ApiSession, db string, examType string) ([]*
 			Type:         examLog.Type,
 			Score:        score,
 			CompleteRate: completeRate,
-			LearnTime:    int(totalLearnTime),
+			LearnTime:    totalLearnTime,
 		})
 
 	}

+ 11 - 4
src/api/learnLog.go

@@ -85,10 +85,11 @@ func LearnLogStatistics(c *gin.Context, apictx *ApiSession) (interface{}, error)
 	if len(uid) == 0 {
 		uid = apictx.User.ID
 	}
-	return getLearnLogStatistics(apictx, db, uid)
+	_, a, err := getLearnLogStatistics(apictx, db, uid)
+	return a, err
 }
 
-func getLearnLogStatistics(apictx *ApiSession, db string, uid string) (float64, error) {
+func getLearnLogStatistics(apictx *ApiSession, db string, uid string) (int, float64, error) {
 	learnLogStatistics := make([]*model.LearnLogStatistics, 0)
 	err := repo.RepoSeachDocs(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
 		Db:          db,
@@ -98,12 +99,14 @@ func getLearnLogStatistics(apictx *ApiSession, db string, uid string) (float64,
 	}, &learnLogStatistics)
 	if err != nil {
 		log.Error(err)
-		return 0.00, err
+		return 0, 0.00, err
 	}
 	completeRate := 0.00
 	total := len(LearnLogSettings)
+	totalLearnTime := 0
 	if len(learnLogStatistics) > 0 {
 		for _, v := range learnLogStatistics {
+			totalLearnTime += v.LearnTime
 			limit, ok := LearnLogSettings[v.Cid]
 			if !ok {
 				limit = 10
@@ -112,10 +115,14 @@ func getLearnLogStatistics(apictx *ApiSession, db string, uid string) (float64,
 			if rate > 1 {
 				rate = 1
 			}
+			fmt.Println("pre_rate:", rate)
 			completeRate += rate / float64(total)
 		}
 	}
-	return completeRate, nil
+	fmt.Println("completeRate:", completeRate)
+	fmt.Println("uid_totalLearnTime:", totalLearnTime)
+	fmt.Println("----------------------------------------")
+	return totalLearnTime, completeRate, nil
 
 }
 

+ 0 - 156
src/api/tmp

@@ -1,156 +0,0 @@
-func DownLoadCompBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-
-	f := excelize.NewFile()
-	f.SetDefaultFont("宋体")
-	flagIndex := -1
-	// 采购 加工 加工-印刷 加工-覆膜 成品采购
-	typeRows := []int{0, 0, 0, 0, 0}
-	for _, tId := range typeBillIds {
-		tidArr := strings.Split(tId, "_")
-		var billExcel IExcel
-		// 采购
-		billId, _ := primitive.ObjectIDFromHex(tidArr[1])
-		if tidArr[0] == "1" {
-			purchase := model.PurchaseBill{}
-			found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-				CollectName: repo.CollectionBillPurchase,
-				Query:       repo.Map{"_id": billId},
-			}, &purchase)
-			if !found {
-				continue
-			}
-			sheetName := "采购单"
-			index := f.NewSheet(sheetName)
-			if flagIndex < 0 {
-				flagIndex = index
-			}
-			// index := f.NewSheet("采购单")
-			// f.SetActiveSheet(index)
-			billExcel = NewPurchaseBill(f)
-			billExcel.SetSheetName(sheetName)
-			if purchase.Reviewed == 1 {
-				if len(purchase.SignUsers) > 0 {
-					signs := []*model.Signature{}
-					repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
-						CollectName: repo.CollectionSignature,
-						Query:       repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
-						Sort:        bson.M{"sort": 1},
-					}, &signs)
-					billExcel.SetSignatures(signs)
-				}
-
-			}
-			billExcel.SetContent(&purchase)
-			billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
-
-			billExcel.SetRow(typeRows[0])
-			billExcel.Draws()
-			typeRows[0] = billExcel.GetRow() + 5
-
-		}
-		// 工艺
-		if tidArr[0] == "2" {
-			produce := model.ProduceBill{}
-			found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-				CollectName: repo.CollectionBillProduce,
-				Query:       repo.Map{"_id": billId},
-			}, &produce)
-			if !found {
-				continue
-			}
-			sheetName := "加工单"
-			if produce.IsPrint {
-				sheetName = "加工单-印刷"
-			} else if produce.IsLam {
-				sheetName = "加工单-覆膜"
-			}
-			index := f.NewSheet(sheetName)
-			if flagIndex < 0 {
-				flagIndex = index
-			}
-			billExcel = NewProduceBill(f)
-			billExcel.SetSheetName(sheetName)
-			if produce.Reviewed == 1 {
-				if len(produce.SignUsers) > 0 {
-					signs := []*model.Signature{}
-					repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
-						CollectName: repo.CollectionSignature,
-						Query:       repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
-						Sort:        bson.M{"sort": 1},
-					}, &signs)
-					billExcel.SetSignatures(signs)
-				}
-
-			}
-			billExcel.SetContent(&produce)
-			billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
-
-			if produce.IsPrint {
-				billExcel.SetRow(typeRows[2])
-				billExcel.Draws()
-				typeRows[2] = billExcel.GetRow() + 5
-			} else if produce.IsLam {
-				billExcel.SetRow(typeRows[3])
-				billExcel.Draws()
-				typeRows[3] = billExcel.GetRow() + 5
-			} else {
-				billExcel.SetRow(typeRows[1])
-				billExcel.Draws()
-				typeRows[1] = billExcel.GetRow() + 5
-
-			}
-
-		}
-		// 成品采购
-		if tidArr[0] == "3" {
-			product := model.ProductBill{}
-			found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-				CollectName: repo.CollectionBillProduct,
-				Query:       repo.Map{"_id": billId},
-			}, &product)
-			if !found {
-				continue
-			}
-			sheetName := "成品采购单"
-			index := f.NewSheet(sheetName)
-			if flagIndex < 0 {
-				flagIndex = index
-			}
-			billExcel = NewProductBill(f)
-			billExcel.SetSheetName(sheetName)
-			if product.Reviewed == 1 {
-				if len(product.SignUsers) > 0 {
-					signs := []*model.Signature{}
-					repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
-						CollectName: repo.CollectionSignature,
-						Query:       repo.Map{"_id": bson.M{"$in": product.SignUsers}},
-						Sort:        bson.M{"sort": 1},
-					}, &signs)
-					billExcel.SetSignatures(signs)
-				}
-
-			}
-			billExcel.SetContent(&product)
-			billExcel.SetTitle(companyName)
-
-			billExcel.SetRow(typeRows[4])
-			billExcel.Draws()
-			typeRows[4] = billExcel.GetRow() + 5
-		}
-
-	}
-
-	// 设置活跃sheet
-	f.SetActiveSheet(flagIndex)
-	// 删除默认Sheet1
-	f.DeleteSheet("Sheet1")
-
-	c.Header("Content-Type", "application/octet-stream")
-	c.Header("Content-Disposition", "attachment; filename="+fmt.Sprintf("%s.xlsx", compNameId))
-	c.Header("Content-Transfer-Encoding", "binary")
-
-	f.Write(c.Writer)
-
-	return nil, nil
-
-}