|
|
@@ -322,6 +322,27 @@ func (self *Product) ApplyLanguage(lang string) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func productKeywordSQL(tableAlias, words string) string {
|
|
|
+ keyword := strings.TrimSpace(words)
|
|
|
+ if keyword == "" {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ keyword = strings.ReplaceAll(keyword, "'", "''")
|
|
|
+ prefix := ""
|
|
|
+ if strings.TrimSpace(tableAlias) != "" {
|
|
|
+ prefix = strings.TrimSpace(tableAlias) + "."
|
|
|
+ }
|
|
|
+ like := "%" + keyword + "%"
|
|
|
+ return fmt.Sprintf(
|
|
|
+ " and (%sname like '%s' or %sname_en like '%s' or %sname_ru like '%s' or %sname_tw like '%s' or %skey_words like '%s')",
|
|
|
+ prefix, like,
|
|
|
+ prefix, like,
|
|
|
+ prefix, like,
|
|
|
+ prefix, like,
|
|
|
+ prefix, like,
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
// 获取最新推荐商品
|
|
|
func GetLatest(page, perPage, recommend, priceSort, saleSort, saleZone, depart int64, ptype string, onlyApp bool, useCache bool) (products []*Product) {
|
|
|
k := productCacheKey(fmt.Sprintf("product_model.GetLatest.page(%d).perPage(%d).recommend(%d).priceSort(%d).saleSort(%d).saleZone(%d).depart(%d).ptype(%s).onlyApp(%t)", page, perPage, recommend, priceSort, saleSort, saleZone, depart, ptype, onlyApp))
|
|
|
@@ -546,9 +567,7 @@ func GetProductsByCatId(cId, saleZone, page, perPage, priceSort, saleSort int64,
|
|
|
cidSql = cidSql + fmt.Sprintf(" and sale_zone=%d", saleZone)
|
|
|
}
|
|
|
|
|
|
- if len(words) > 0 {
|
|
|
- cidSql = cidSql + fmt.Sprintf(" and key_words like '%s'", "%"+words+"%")
|
|
|
- }
|
|
|
+ cidSql = cidSql + productKeywordSQL("", words)
|
|
|
|
|
|
sql := `
|
|
|
select * from products
|
|
|
@@ -576,24 +595,32 @@ func GetProductCountByCatId(cId, saleZone int64, words string, useCache bool) in
|
|
|
return ret
|
|
|
}
|
|
|
}
|
|
|
- item := new(Product)
|
|
|
- o := orm.NewOrm()
|
|
|
- qs := o.QueryTable(item)
|
|
|
+
|
|
|
+ type Ret struct {
|
|
|
+ Count int64 `json:"count"`
|
|
|
+ }
|
|
|
+ ret := &Ret{}
|
|
|
+ whereSQL := ""
|
|
|
if cId != 0 {
|
|
|
- qs = qs.Filter("category_id", cId)
|
|
|
+ whereSQL = whereSQL + fmt.Sprintf(" and category_id=%d", cId)
|
|
|
}
|
|
|
-
|
|
|
if saleZone != 0 {
|
|
|
- qs = qs.Filter("sale_zone", saleZone)
|
|
|
+ whereSQL = whereSQL + fmt.Sprintf(" and sale_zone=%d", saleZone)
|
|
|
}
|
|
|
+ whereSQL = whereSQL + productKeywordSQL("", words)
|
|
|
|
|
|
- if len(words) > 0 {
|
|
|
- qs = qs.Filter("key_words__icontains", words)
|
|
|
+ sql := fmt.Sprintf(`
|
|
|
+ select count(*) as count from products
|
|
|
+ where status = ? and show_flag=? and ptype=? %s
|
|
|
+ `, whereSQL)
|
|
|
+ err := orm.NewOrm().Raw(sql, 1, true, TYPE_DIRECT_SALE).QueryRow(ret)
|
|
|
+ if err != nil {
|
|
|
+ beego.BeeLogger.Error("GetProductCountByCatId err=[%s]", err)
|
|
|
+ return 0
|
|
|
}
|
|
|
- count, _ := qs.Filter("status", 1).Filter("show_flag", true).Filter("ptype", TYPE_DIRECT_SALE).Count()
|
|
|
|
|
|
- cache.Cache.Put(k, count, 10*time.Minute)
|
|
|
- return count
|
|
|
+ cache.Cache.Put(k, ret.Count, 10*time.Minute)
|
|
|
+ return ret.Count
|
|
|
}
|
|
|
|
|
|
func GetSeckillProducts(queryDate time.Time, useCache bool) (list []*Product) {
|
|
|
@@ -813,9 +840,7 @@ func DepartGetProductsByCatId(cId, saleZone, page, perPage, priceSort, saleSort,
|
|
|
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+"%")
|
|
|
- }
|
|
|
+ cidSql = cidSql + productKeywordSQL("a", words)
|
|
|
|
|
|
sql := `
|
|
|
select a.* from products a left join depart_records_products b on a.id=b.product_id
|
|
|
@@ -862,9 +887,7 @@ func DepartGetProductCountByCatId(cId, saleZone, depart int64, words string, use
|
|
|
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+"%")
|
|
|
- }
|
|
|
+ saleZoneSql = saleZoneSql + productKeywordSQL("a", words)
|
|
|
|
|
|
sql = fmt.Sprintf(sql, saleZoneSql)
|
|
|
|