|
|
@@ -568,3 +568,111 @@ func GetProductCountByRelateId(relateId int64, useCache bool) int64 {
|
|
|
return ret.Count
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+func DepartGetProductsByCatId(cId, saleZone, page, perPage, priceSort, saleSort, depart 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, orderSql string
|
|
|
+ if priceSort == int64(1) {
|
|
|
+ //降序
|
|
|
+ priceSql = "a.price desc"
|
|
|
+ } else if priceSort == int64(2) {
|
|
|
+ //升序
|
|
|
+ priceSql = "a.price asc"
|
|
|
+ }
|
|
|
+ if saleSort == int64(1) {
|
|
|
+ //降序
|
|
|
+ saleSql = "a.sale_nums desc"
|
|
|
+ } else if priceSort == int64(2) {
|
|
|
+ saleSql = "a.sale_nums asc"
|
|
|
+ }
|
|
|
+ if len(priceSql) <= 0 && len(saleSql) <= 0 {
|
|
|
+ //orderSql = "recommend desc,created_at desc"
|
|
|
+ orderSql = " (a.virtual_sold_count + a.sale_nums) desc"
|
|
|
+ } else if len(priceSql) <= 0 {
|
|
|
+ orderSql = saleSql
|
|
|
+ } else if len(saleSql) <= 0 {
|
|
|
+ orderSql = priceSql
|
|
|
+ } else {
|
|
|
+ orderSql = fmt.Sprintf("%s,%s", priceSql, saleSql)
|
|
|
+ }
|
|
|
+
|
|
|
+ var cidSql string
|
|
|
+ if cId != 0 {
|
|
|
+ cidSql = fmt.Sprintf(" and a.category_id=%d", cId)
|
|
|
+ }
|
|
|
+ if saleZone != 0 {
|
|
|
+ cidSql = cidSql + fmt.Sprintf(" and a.sale_zone=%d", saleZone)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(words) > 0 {
|
|
|
+ cidSql = cidSql + fmt.Sprintf(" and a.key_words like '%s'", "%"+words+"%")
|
|
|
+ }
|
|
|
+
|
|
|
+ sql := `
|
|
|
+ select a.* from products a left join depart_records_products b on a.id=b.product_id
|
|
|
+ where a.status = ? and a.show_flag=? and a.ptype=? and (b.depart_record_id=? or b.depart_record_id is null ) %s
|
|
|
+ order by %s limit %d, %d ;
|
|
|
+ `
|
|
|
+ sql = fmt.Sprintf(sql, cidSql, orderSql, (page-1)*perPage, perPage)
|
|
|
+ _, err := orm.NewOrm().Raw(sql, 1, true, TYPE_DIRECT_SALE, depart).QueryRows(&products)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ beego.BeeLogger.Debug("GetProductsByCatId err=%s", err)
|
|
|
+ }
|
|
|
+ for _, pd := range products {
|
|
|
+ pd.Cover = GetCoverByPId(pd.Id, useCache)
|
|
|
+ }
|
|
|
+ cache.Cache.Put(k, products, 10*time.Minute)
|
|
|
+ return products
|
|
|
+}
|
|
|
+
|
|
|
+func DepartGetProductCountByCatId(cId, saleZone, depart int64, words string, useCache bool) int64 {
|
|
|
+ k := fmt.Sprintf("product_model.DepartGetProductCountByCatId(%d)saleZone(%d)depart(%d)", cId, saleZone, depart)
|
|
|
+ if useCache {
|
|
|
+ if ret, ok := cache.Cache.Get(k).(int64); ok {
|
|
|
+ return ret
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ type Ret struct {
|
|
|
+ Count int64 `json:"count"` //总数
|
|
|
+ }
|
|
|
+ ret := &Ret{}
|
|
|
+ o := orm.NewOrm()
|
|
|
+ sql := `
|
|
|
+ select count(DISTINCT a.id) as count from products a left join depart_records_products b on a.id=b.product_id
|
|
|
+ where a.status = ? and a.show_flag=? and a.ptype=? and (b.depart_record_id=? or b.depart_record_id is null ) %s
|
|
|
+
|
|
|
+ `
|
|
|
+ var saleZoneSql string
|
|
|
+ if saleZone > int64(0) {
|
|
|
+ saleZoneSql = fmt.Sprintf(" and a.sale_zone=%d", saleZone)
|
|
|
+ }
|
|
|
+
|
|
|
+ if cId != 0 {
|
|
|
+ saleZoneSql = saleZoneSql + fmt.Sprintf(" and a.category_id=%d", cId)
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(words) > 0 {
|
|
|
+ saleZoneSql = saleZoneSql + fmt.Sprintf(" and a.key_words like '%s'", "%"+words+"%")
|
|
|
+ }
|
|
|
+
|
|
|
+ sql = fmt.Sprintf(sql, saleZoneSql)
|
|
|
+
|
|
|
+ err := o.Raw(sql, 1, true, TYPE_DIRECT_SALE, depart).QueryRow(ret)
|
|
|
+ if err != nil {
|
|
|
+ beego.BeeLogger.Error("GetLatestCount, depart:%d, err=[%s]", depart, err)
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ if ret.Count < 0 {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+
|
|
|
+ cache.Cache.Put(k, ret.Count, 10*time.Minute)
|
|
|
+ return ret.Count
|
|
|
+}
|