|
|
@@ -122,17 +122,36 @@ type Product struct {
|
|
|
}
|
|
|
|
|
|
//获取最新推荐商品
|
|
|
-func GetLatest(page, perPage, recommend int64, ptype string, useCache bool) (products []*Product) {
|
|
|
+func GetLatest(page, perPage, recommend, priceSort, saleSort int64, ptype string, useCache bool) (products []*Product) {
|
|
|
k := fmt.Sprintf("product_model.GetLatest.page(%d).perPage(%d).recommend(%d)", page, perPage, recommend)
|
|
|
if useCache {
|
|
|
if ret, ok := cache.Cache.Get(k).([]*Product); ok {
|
|
|
return ret
|
|
|
}
|
|
|
}
|
|
|
+ var priceSql, saleSql string
|
|
|
+ if priceSort == int64(1) {
|
|
|
+ //降序
|
|
|
+ priceSql = "-price"
|
|
|
+ } else if priceSort == int64(2) {
|
|
|
+ //升序
|
|
|
+ priceSql = "price"
|
|
|
+ }
|
|
|
+ if saleSort == int64(1) {
|
|
|
+ //降序
|
|
|
+ saleSql = "-(virtual_sold_count+sale_nums)"
|
|
|
+ } else if priceSort == int64(2) {
|
|
|
+ saleSql = "+(virtual_sold_count+sale_nums)"
|
|
|
+ }
|
|
|
+ if len(priceSql) <= 0 && len(saleSql) <= 0 {
|
|
|
+ saleSql = "-created_at"
|
|
|
+ priceSql = "-recommend"
|
|
|
+ }
|
|
|
+ sortSql := fmt.Sprintf("%s,%s", priceSql, saleSql)
|
|
|
o := orm.NewOrm()
|
|
|
_, err := o.QueryTable(new(Product)).Filter("status", 1).
|
|
|
Filter("recommend__gt", recommend).Filter("show_flag", true).Filter("ptype", ptype).
|
|
|
- OrderBy("-recommend", "-created_at").
|
|
|
+ OrderBy(sortSql).
|
|
|
Limit(perPage, (page-1)*perPage).
|
|
|
All(&products)
|
|
|
if err != nil {
|
|
|
@@ -183,13 +202,34 @@ func GetProductById(id int64, useCache bool) *Product {
|
|
|
return item
|
|
|
}
|
|
|
|
|
|
-func GetProductsByCatId(cId, page, perPage int64, words string, useCache bool) (products []*Product) {
|
|
|
+func GetProductsByCatId(cId, page, perPage, priceSort, saleSort int64, words string, useCache bool) (products []*Product) {
|
|
|
k := fmt.Sprintf("product_model.GetProductsByCatId(%d).page(%d).perPage(%d).words(%d)", cId, page, perPage, words)
|
|
|
if useCache {
|
|
|
if ret, ok := cache.Cache.Get(k).([]*Product); ok {
|
|
|
return ret
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ var priceSql, saleSql string
|
|
|
+ if priceSort == int64(1) {
|
|
|
+ //降序
|
|
|
+ priceSql = "-price"
|
|
|
+ } else if priceSort == int64(2) {
|
|
|
+ //升序
|
|
|
+ priceSql = "price"
|
|
|
+ }
|
|
|
+ if saleSort == int64(1) {
|
|
|
+ //降序
|
|
|
+ saleSql = "-(virtual_sold_count+sale_nums)"
|
|
|
+ } else if priceSort == int64(2) {
|
|
|
+ saleSql = "+(virtual_sold_count+sale_nums)"
|
|
|
+ }
|
|
|
+ if len(priceSql) <= 0 && len(saleSql) <= 0 {
|
|
|
+ saleSql = "-created_at"
|
|
|
+ priceSql = "-recommend"
|
|
|
+ }
|
|
|
+ sortSql := fmt.Sprintf("%s,%s", priceSql, saleSql)
|
|
|
+
|
|
|
o := orm.NewOrm()
|
|
|
qs := o.QueryTable(new(Product)).Filter("status", 1).Filter("show_flag", true).Filter("ptype", TYPE_DIRECT_SALE)
|
|
|
if cId != 0 {
|
|
|
@@ -199,7 +239,7 @@ func GetProductsByCatId(cId, page, perPage int64, words string, useCache bool) (
|
|
|
qs = qs.Filter("key_words__icontains", words)
|
|
|
}
|
|
|
_, err := qs.
|
|
|
- OrderBy("-recommend", "-created_at").
|
|
|
+ OrderBy(sortSql).
|
|
|
Limit(perPage, (page-1)*perPage).
|
|
|
All(&products)
|
|
|
if err != nil {
|