Browse Source

更改部门显示

abiao 3 years ago
parent
commit
dab7010fe0

+ 8 - 2
go/gopath/src/fohow.com/apps/controllers/product_controller/product_controller.go

@@ -349,8 +349,14 @@ func (self *ProductController) GetProductsByCat() {
 		List      []*product_model.Product `json:"list"`
 		ListCount int64                    `json:"list_count"`
 	}
-	list := product_model.GetProductsByCatId(catId, sale_zone, page, perPage, price_sort, sale_sort, words, cache)
-	count := product_model.GetProductCountByCatId(catId, sale_zone, words, cache)
+
+	wxUId := self.GetCurrentWxUserIdByToken()
+	wxUser := user_model.GetWxUserById(wxUId, true)
+	if wxUser == nil {
+		self.ReturnError(403, apps.NoExist, "", nil)
+	}
+	list := product_model.DepartGetProductsByCatId(catId, sale_zone, page, perPage, price_sort, sale_sort, wxUser.Depart, words, cache)
+	count := product_model.DepartGetProductCountByCatId(catId, sale_zone, wxUser.Depart, words, cache)
 	for _, pd := range list {
 		pd.SoldCount = pd.SaleNums
 		if pd.Count > pd.SoldCount {

+ 108 - 0
go/gopath/src/fohow.com/apps/models/product_model/product.go

@@ -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
+}