|
@@ -120,32 +120,41 @@ type Product struct {
|
|
|
PackageList []*ProductItem `orm:"-" json:"package_list"` // varchar(255)
|
|
PackageList []*ProductItem `orm:"-" json:"package_list"` // varchar(255)
|
|
|
Size *ProductAttrKey `orm:"-" json:"size_list"` // varchar(255)
|
|
Size *ProductAttrKey `orm:"-" json:"size_list"` // varchar(255)
|
|
|
Color *ProductAttrKey `orm:"-" json:"color_list"` // varchar(255)
|
|
Color *ProductAttrKey `orm:"-" json:"color_list"` // varchar(255)
|
|
|
|
|
+ UseQuan bool `orm:"column(use_quan)" json:"use_quan"` // varchar(255)
|
|
|
|
|
+ Silver int64 `orm:"column(silver)" json:"silver"` // varchar(255)
|
|
|
|
|
+ SaleZone int64 `orm:"column(sale_zone)" json:"sale_zone"` // 销售区
|
|
|
|
|
+ SaleZoneName string `orm:"-" json:"sale_zone_name"` // 销售区名称
|
|
|
|
|
+ CanBuy bool `orm:"-" json:"can_buy"` // bool
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//获取最新推荐商品
|
|
//获取最新推荐商品
|
|
|
-func GetLatest(page, perPage, recommend, priceSort, saleSort int64, ptype string, useCache bool) (products []*Product) {
|
|
|
|
|
|
|
+func GetLatest(page, perPage, recommend, priceSort, saleSort, saleZone, depart int64, ptype string, useCache bool) (products []*Product) {
|
|
|
k := fmt.Sprintf("product_model.GetLatest.page(%d).perPage(%d).recommend(%d)", page, perPage, recommend)
|
|
k := fmt.Sprintf("product_model.GetLatest.page(%d).perPage(%d).recommend(%d)", page, perPage, recommend)
|
|
|
if useCache {
|
|
if useCache {
|
|
|
if ret, ok := cache.Cache.Get(k).([]*Product); ok {
|
|
if ret, ok := cache.Cache.Get(k).([]*Product); ok {
|
|
|
return ret
|
|
return ret
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- var priceSql, saleSql, orderSql string
|
|
|
|
|
|
|
+ var saleZoneSql, priceSql, saleSql, orderSql string
|
|
|
|
|
+ if saleZone > int64(0) {
|
|
|
|
|
+ saleZoneSql = fmt.Sprintf(" and a.sale_zone=%d", saleZone)
|
|
|
|
|
+ }
|
|
|
if priceSort == int64(1) {
|
|
if priceSort == int64(1) {
|
|
|
//降序
|
|
//降序
|
|
|
- priceSql = "price desc"
|
|
|
|
|
|
|
+ priceSql = "a.price desc"
|
|
|
} else if priceSort == int64(2) {
|
|
} else if priceSort == int64(2) {
|
|
|
//升序
|
|
//升序
|
|
|
- priceSql = "price asc"
|
|
|
|
|
|
|
+ priceSql = "a.price asc"
|
|
|
}
|
|
}
|
|
|
if saleSort == int64(1) {
|
|
if saleSort == int64(1) {
|
|
|
//降序
|
|
//降序
|
|
|
- saleSql = "sale_nums desc"
|
|
|
|
|
|
|
+ saleSql = "a.sale_nums desc"
|
|
|
} else if priceSort == int64(2) {
|
|
} else if priceSort == int64(2) {
|
|
|
- saleSql = "sale_nums asc"
|
|
|
|
|
|
|
+ saleSql = "a.sale_nums asc"
|
|
|
}
|
|
}
|
|
|
if len(priceSql) <= 0 && len(saleSql) <= 0 {
|
|
if len(priceSql) <= 0 && len(saleSql) <= 0 {
|
|
|
- orderSql = "recommend desc,created_at desc"
|
|
|
|
|
|
|
+ //orderSql = "recommend desc,created_at desc"
|
|
|
|
|
+ orderSql = " (a.virtual_sold_count + a.sale_nums) desc"
|
|
|
} else if len(priceSql) <= 0 {
|
|
} else if len(priceSql) <= 0 {
|
|
|
orderSql = saleSql
|
|
orderSql = saleSql
|
|
|
} else if len(saleSql) <= 0 {
|
|
} else if len(saleSql) <= 0 {
|
|
@@ -154,13 +163,13 @@ func GetLatest(page, perPage, recommend, priceSort, saleSort int64, ptype string
|
|
|
orderSql = fmt.Sprintf("%s,%s", priceSql, saleSql)
|
|
orderSql = fmt.Sprintf("%s,%s", priceSql, saleSql)
|
|
|
}
|
|
}
|
|
|
sql := `
|
|
sql := `
|
|
|
- select * from products
|
|
|
|
|
- where status = ? and recommend > ? and show_flag=? and ptype=?
|
|
|
|
|
|
|
+ select a.* from products a left join depart_records_products b on a.id=b.product_id
|
|
|
|
|
+ where a.status = ? and a.recommend > ? 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;
|
|
order by %s limit %d, %d;
|
|
|
`
|
|
`
|
|
|
- sql = fmt.Sprintf(sql, orderSql, (page-1)*perPage, perPage)
|
|
|
|
|
|
|
+ sql = fmt.Sprintf(sql, saleZoneSql, orderSql, (page-1)*perPage, perPage)
|
|
|
//beego.BeeLogger.Warn("sql=%s", sql)
|
|
//beego.BeeLogger.Warn("sql=%s", sql)
|
|
|
- _, err := orm.NewOrm().Raw(sql, 1, recommend, true, ptype).QueryRows(&products)
|
|
|
|
|
|
|
+ _, err := orm.NewOrm().Raw(sql, 1, recommend, true, ptype, depart).QueryRows(&products)
|
|
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
beego.BeeLogger.Debug("GetLatest err=%s", err)
|
|
beego.BeeLogger.Debug("GetLatest err=%s", err)
|
|
@@ -176,7 +185,41 @@ func GetLatest(page, perPage, recommend, priceSort, saleSort int64, ptype string
|
|
|
return products
|
|
return products
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func GetLatestCount(recommend int64, ptype string, useCache bool) int64 {
|
|
|
|
|
|
|
+func GetNewLatestCount(recommend, saleZone, depart int64, ptype string, useCache bool) int64 {
|
|
|
|
|
+ k := fmt.Sprintf("product_model.GetNewLatestCount.recommend(%d)(%d)(%d)", recommend, 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.recommend > ? 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)
|
|
|
|
|
+ }
|
|
|
|
|
+ sql = fmt.Sprintf(sql, saleZoneSql)
|
|
|
|
|
+
|
|
|
|
|
+ err := o.Raw(sql, true, recommend, true, ptype, 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
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func GetLatestCount(recommend, saleZone int64, ptype string, useCache bool) int64 {
|
|
|
k := fmt.Sprintf("product_model.GetLatestCount.recommend(%d)", recommend)
|
|
k := fmt.Sprintf("product_model.GetLatestCount.recommend(%d)", recommend)
|
|
|
if useCache {
|
|
if useCache {
|
|
|
if ret, ok := cache.Cache.Get(k).(int64); ok {
|
|
if ret, ok := cache.Cache.Get(k).(int64); ok {
|
|
@@ -185,7 +228,11 @@ func GetLatestCount(recommend int64, ptype string, useCache bool) int64 {
|
|
|
}
|
|
}
|
|
|
item := new(Product)
|
|
item := new(Product)
|
|
|
o := orm.NewOrm()
|
|
o := orm.NewOrm()
|
|
|
- count, _ := o.QueryTable(item).Filter("recommend__gt", recommend).Filter("show_flag", true).Filter("ptype", ptype).Filter("status", 1).Count()
|
|
|
|
|
|
|
+ qs := o.QueryTable(item).Filter("recommend__gt", recommend).Filter("show_flag", true).Filter("ptype", ptype).Filter("status", 1)
|
|
|
|
|
+ if saleZone > int64(0) {
|
|
|
|
|
+ qs = qs.Filter("sale_zone", saleZone)
|
|
|
|
|
+ }
|
|
|
|
|
+ count, _ := qs.Count()
|
|
|
|
|
|
|
|
cache.Cache.Put(k, count, 10*time.Minute)
|
|
cache.Cache.Put(k, count, 10*time.Minute)
|
|
|
return count
|
|
return count
|
|
@@ -258,7 +305,7 @@ func GetNoDeliveryPrd(id int64, province string, useCache bool) *Product {
|
|
|
return item
|
|
return item
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func GetProductsByCatId(cId, page, perPage, priceSort, saleSort int64, words string, useCache bool) (products []*Product) {
|
|
|
|
|
|
|
+func GetProductsByCatId(cId, saleZone, 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)
|
|
k := fmt.Sprintf("product_model.GetProductsByCatId(%d).page(%d).perPage(%d).words(%d)", cId, page, perPage, words)
|
|
|
if useCache {
|
|
if useCache {
|
|
|
if ret, ok := cache.Cache.Get(k).([]*Product); ok {
|
|
if ret, ok := cache.Cache.Get(k).([]*Product); ok {
|
|
@@ -280,7 +327,8 @@ func GetProductsByCatId(cId, page, perPage, priceSort, saleSort int64, words str
|
|
|
saleSql = "sale_nums asc"
|
|
saleSql = "sale_nums asc"
|
|
|
}
|
|
}
|
|
|
if len(priceSql) <= 0 && len(saleSql) <= 0 {
|
|
if len(priceSql) <= 0 && len(saleSql) <= 0 {
|
|
|
- orderSql = "recommend desc,created_at desc"
|
|
|
|
|
|
|
+ //orderSql = "recommend desc,created_at desc"
|
|
|
|
|
+ orderSql = " (virtual_sold_count + sale_nums) desc"
|
|
|
} else if len(priceSql) <= 0 {
|
|
} else if len(priceSql) <= 0 {
|
|
|
orderSql = saleSql
|
|
orderSql = saleSql
|
|
|
} else if len(saleSql) <= 0 {
|
|
} else if len(saleSql) <= 0 {
|
|
@@ -291,7 +339,10 @@ func GetProductsByCatId(cId, page, perPage, priceSort, saleSort int64, words str
|
|
|
|
|
|
|
|
var cidSql string
|
|
var cidSql string
|
|
|
if cId != 0 {
|
|
if cId != 0 {
|
|
|
- cidSql = fmt.Sprintf("and category_id=%d", cId)
|
|
|
|
|
|
|
+ cidSql = fmt.Sprintf(" and category_id=%d", cId)
|
|
|
|
|
+ }
|
|
|
|
|
+ if saleZone != 0 {
|
|
|
|
|
+ cidSql = cidSql + fmt.Sprintf(" and sale_zone=%d", saleZone)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if len(words) > 0 {
|
|
if len(words) > 0 {
|
|
@@ -317,7 +368,7 @@ func GetProductsByCatId(cId, page, perPage, priceSort, saleSort int64, words str
|
|
|
return products
|
|
return products
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func GetProductCountByCatId(cId int64, words string, useCache bool) int64 {
|
|
|
|
|
|
|
+func GetProductCountByCatId(cId, saleZone int64, words string, useCache bool) int64 {
|
|
|
k := fmt.Sprintf("product_model.GetProductCountByCatId(%d)", cId)
|
|
k := fmt.Sprintf("product_model.GetProductCountByCatId(%d)", cId)
|
|
|
if useCache {
|
|
if useCache {
|
|
|
if ret, ok := cache.Cache.Get(k).(int64); ok {
|
|
if ret, ok := cache.Cache.Get(k).(int64); ok {
|
|
@@ -330,6 +381,11 @@ func GetProductCountByCatId(cId int64, words string, useCache bool) int64 {
|
|
|
if cId != 0 {
|
|
if cId != 0 {
|
|
|
qs = qs.Filter("category_id", cId)
|
|
qs = qs.Filter("category_id", cId)
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ if saleZone != 0 {
|
|
|
|
|
+ qs = qs.Filter("sale_zone", saleZone)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if len(words) > 0 {
|
|
if len(words) > 0 {
|
|
|
qs = qs.Filter("key_words__icontains", words)
|
|
qs = qs.Filter("key_words__icontains", words)
|
|
|
}
|
|
}
|
|
@@ -513,3 +569,134 @@ func GetProductCountByRelateId(relateId int64, useCache bool) int64 {
|
|
|
return ret.Count
|
|
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
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func DecideCanBuy(pid, depart int64) bool {
|
|
|
|
|
+
|
|
|
|
|
+ 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.id = ? and (b.depart_record_id=? or b.depart_record_id is null )
|
|
|
|
|
+
|
|
|
|
|
+ `
|
|
|
|
|
+ err := o.Raw(sql, pid, depart).QueryRow(ret)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ beego.BeeLogger.Error("GetLatestCount, depart:%d, err=[%s]", depart, err)
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+ if ret.Count <= 0 {
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+ return true
|
|
|
|
|
+}
|