XieXing преди 3 месеца
родител
ревизия
9b8a3acf8e
променени са 1 файла, в които са добавени 45 реда и са изтрити 3 реда
  1. 45 3
      shudao-go-backend/controllers/total.go

+ 45 - 3
shudao-go-backend/controllers/total.go

@@ -138,8 +138,29 @@ func (c *TotalController) GetPolicyFile() {
 func (c *TotalController) GetFunctionCard() {
 	//问题类型,0为AI问答,1为安全培训
 	functionType, _ := c.GetInt("function_type")
+	limit := 4
+
+	var count int64
+	models.DB.Model(&models.FunctionCard{}).Where("function_type = ? AND is_deleted = ?", functionType, 0).Count(&count)
+
 	var functionCard []models.FunctionCard
-	models.DB.Model(&models.FunctionCard{}).Order("rand()").Limit(4).Where("function_type = ? AND is_deleted = ?", functionType, 0).Find(&functionCard)
+	if count == 0 {
+		c.Data["json"] = map[string]interface{}{
+			"statusCode": 200,
+			"msg":        "success",
+			"data":       functionCard,
+		}
+		c.ServeJSON()
+		return
+	}
+
+	if int64(limit) >= count {
+		models.DB.Model(&models.FunctionCard{}).Where("function_type = ? AND is_deleted = ?", functionType, 0).Find(&functionCard)
+	} else {
+		offset := rand.Intn(int(count) - limit + 1)
+		models.DB.Model(&models.FunctionCard{}).Where("function_type = ? AND is_deleted = ?", functionType, 0).Offset(offset).Limit(limit).Find(&functionCard)
+	}
+
 	c.Data["json"] = map[string]interface{}{
 		"statusCode": 200,
 		"msg":        "success",
@@ -148,12 +169,33 @@ func (c *TotalController) GetFunctionCard() {
 	c.ServeJSON()
 }
 
-// 随机返回条热点问题
+// 随机返回条热点问题
 func (c *TotalController) GetHotQuestion() {
 	//问题类型,0为AI问答,1为安全培训
 	questionType, _ := c.GetInt("question_type")
+	limit := 4
+
+	var count int64
+	models.DB.Model(&models.HotQuestion{}).Where("question_type = ? AND is_deleted = ?", questionType, 0).Count(&count)
+
 	var hotQuestion []models.HotQuestion
-	models.DB.Model(&models.HotQuestion{}).Order("rand()").Limit(4).Where("question_type = ? AND is_deleted = ?", questionType, 0).Find(&hotQuestion)
+	if count == 0 {
+		c.Data["json"] = map[string]interface{}{
+			"statusCode": 200,
+			"msg":        "success",
+			"data":       hotQuestion,
+		}
+		c.ServeJSON()
+		return
+	}
+
+	if int64(limit) >= count {
+		models.DB.Model(&models.HotQuestion{}).Where("question_type = ? AND is_deleted = ?", questionType, 0).Find(&hotQuestion)
+	} else {
+		offset := rand.Intn(int(count) - limit + 1)
+		models.DB.Model(&models.HotQuestion{}).Where("question_type = ? AND is_deleted = ?", questionType, 0).Offset(offset).Limit(limit).Find(&hotQuestion)
+	}
+
 	c.Data["json"] = map[string]interface{}{
 		"statusCode": 200,
 		"msg":        "success",