Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
[随机查询产品] API 待完成
Browse files Browse the repository at this point in the history
  • Loading branch information
Thxzzzzz committed May 20, 2019
1 parent 7dfd551 commit 19b1dc3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
11 changes: 11 additions & 0 deletions controllers/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,14 @@ func (c *ProductController) GetCommentInfoByProductId() {
}
c.ResponseSuccessJson(results)
}

// @Title 根据产品类型 和数量 获取随机数量的指定类型产品
// @Description 根据产品类型 和数量 获取随机数量的指定类型产品
// @Param product_type query int true "产品类型"
// @Param num query int true "查询数量"
// @Success 200
// @Failure 400
// @router /getCommentInfoByProductId [get]
func (c *ProductController) GetProductsRandByTypeAndNum() {

}
18 changes: 17 additions & 1 deletion models/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func GetAllProduct() ([]*Product, error) {

// 获取产品类型列表
func GetProductTypeList() ([]ProductType, error) {
results := []ProductType{}
var results []ProductType
err := db.Find(&results).Error
return results, err
}
Expand Down Expand Up @@ -251,6 +251,22 @@ func GetAllProductCountInfo() (resultModels.ProductCountInfo, error) {
return countInfo, err
}

// 根据类型随机获取指定数量的产品 ProductContent
func GetProductsRandByTypeAndNum(productType int, num int) ([]resultModels.ProductContent, error) {

var result []resultModels.ProductContent
cDb := db
if productType > 0 {
cDb = cDb.Where("product_type = ?", productType)
}
if num > 0 {
cDb = cDb.Limit(num)
}
err := cDb.Select(resultModels.ProductContentField).Table("products").Order("RAND()").Scan(&result).Error

return result, err
}

// 获取产品的截止日期,这个可以用作购物车的失效处理,或者在获取购物车列表的时候就处理?
//func GetEndTimeListInProductId(productIds []uint64) ([]time.Time, error) {
// results := []time.Time{}
Expand Down
11 changes: 10 additions & 1 deletion tests/models_product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestGetProductList(t *testing.T) {
//Type: 1,
Name: "手环",
}
resutlt, err := models.GetProductList(form)
resutlt, err := models.GetProductList(form, 1)
if err != nil {
t.Failed()
}
Expand Down Expand Up @@ -63,3 +63,12 @@ func TestGetAllProductCountInfo(t *testing.T) {
}
t.Log(result)
}

func TestGetProductsRandByTypeAndNum(t *testing.T) {
result, err := models.GetProductsRandByTypeAndNum(1, 5)
if err != nil {
t.Fail()
t.Log(err)
}
t.Log(result)
}

0 comments on commit 19b1dc3

Please sign in to comment.