Преглед на файлове

Merge branch 'develop' of http://git.hiwavo.com/Fohow/fohow_api into develop

abiao преди 3 седмици
родител
ревизия
cd599e39ec

+ 51 - 0
git_commit_push.sh

@@ -0,0 +1,51 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+cd "$SCRIPT_DIR"
+
+REMOTE="${GIT_REMOTE:-origin}"
+BRANCH="${GIT_BRANCH:-$(git branch --show-current)}"
+
+if [ -z "$BRANCH" ]; then
+  echo "ERROR: 当前不是普通分支,不能在 detached HEAD 状态下提交。" >&2
+  exit 1
+fi
+
+if [ -d .git/rebase-merge ] || [ -d .git/rebase-apply ] || [ -f .git/MERGE_HEAD ]; then
+  echo "ERROR: 当前仓库存在未完成的 merge/rebase,请先处理后再执行。" >&2
+  exit 1
+fi
+
+MESSAGE="${*:-}"
+if [ -z "$MESSAGE" ]; then
+  MESSAGE="chore: update code $(date '+%Y-%m-%d %H:%M:%S')"
+fi
+
+echo "Repo:   $(git rev-parse --show-toplevel)"
+echo "Branch: $BRANCH"
+echo "Remote: $REMOTE"
+echo "Msg:    $MESSAGE"
+
+git add -A
+
+if git diff --cached --quiet; then
+  echo "没有需要提交的修改。"
+  exit 0
+fi
+
+git commit -m "$MESSAGE"
+
+if git ls-remote --exit-code --heads "$REMOTE" "$BRANCH" >/dev/null 2>&1; then
+  git fetch "$REMOTE" "$BRANCH"
+  REMOTE_REF="refs/remotes/$REMOTE/$BRANCH"
+  if git show-ref --verify --quiet "$REMOTE_REF"; then
+    if ! git merge-base --is-ancestor "$REMOTE_REF" HEAD; then
+      echo "远端 $REMOTE/$BRANCH 有本地没有的提交,开始 rebase。"
+      git pull --rebase "$REMOTE" "$BRANCH"
+    fi
+  fi
+fi
+
+git push "$REMOTE" "HEAD:$BRANCH"
+echo "提交并推送完成:$(git rev-parse --short HEAD)"

+ 9 - 3
go/gopath/src/fohow.com/apps/controllers/product_controller/product_controller.go

@@ -389,6 +389,7 @@ func (self *ProductController) GetProductsByCat() {
 	price_sort, _ := self.GetInt64("price_sort")
 	sale_sort, _ := self.GetInt64("sale_sort")
 	sale_zone, _ := self.GetInt64("sale_zone")
+	lang := self.GetString("lang")
 
 	if page <= 0 {
 		page = 1
@@ -406,9 +407,12 @@ func (self *ProductController) GetProductsByCat() {
 	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)
+	baseList := 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 {
+	list := make([]*product_model.Product, 0, len(baseList))
+	for _, basePd := range baseList {
+		pd := basePd.Clone()
+		pd.ApplyLanguage(lang)
 		pd.SoldCount = pd.SaleNums
 		if pd.Count > pd.SoldCount {
 			pd.LeftCount = pd.Count - pd.SoldCount
@@ -455,6 +459,7 @@ func (self *ProductController) GetProductsByCat() {
 				pd.DeliverState = product_model.DELIVER_STATE_2
 			}
 		}
+		list = append(list, pd)
 	}
 	self.Data["json"] = &Ret{List: list, ListCount: count}
 	self.ServeJSON()
@@ -610,6 +615,7 @@ func (self *ProductController) GetCommendWords() {
 
 	perPage, _ := self.GetInt64("per_page")
 	cache, _ := self.GetBool("cache", true)
+	lang := self.GetString("lang")
 
 	if perPage <= 0 {
 		perPage = 10
@@ -617,7 +623,7 @@ func (self *ProductController) GetCommendWords() {
 	type Ret struct {
 		List []*product_model.CommendWord `json:"list"`
 	}
-	list := product_model.GetCommendWordsList(perPage, cache)
+	list := product_model.GetCommendWordsList(perPage, lang, cache)
 
 	self.Data["json"] = &Ret{List: list}
 	self.ServeJSON()

+ 42 - 7
go/gopath/src/fohow.com/apps/models/product_model/commend_words.go

@@ -5,6 +5,7 @@ import (
 	"fohow.com/cache"
 	"github.com/astaxie/beego"
 	"github.com/astaxie/beego/orm"
+	"strings"
 	"time"
 )
 
@@ -13,19 +14,50 @@ const (
 )
 
 type CommendWord struct {
-	Id    int64  `orm:"column(id);pk"                json:"id"`    // int(11)
-	Sort  int64  `orm:"column(sort)"           json:"sort"`        // int(11)
-	Title string `orm:"column(title);null"           json:"title"` // varchar(64)
-	State bool   `orm:"column(state);null"         json:"-"`       // tinyint(1)
-
+	Id      int64  `orm:"column(id);pk" json:"id"`         // int(11)
+	Sort    int64  `orm:"column(sort)" json:"sort"`        // int(11)
+	Title   string `orm:"column(title);null" json:"title"` // varchar(255)
+	TitleEn string `orm:"column(title_en);null" json:"-"`  // varchar(255)
+	TitleRu string `orm:"column(title_ru);null" json:"-"`  // varchar(255)
+	TitleTw string `orm:"column(title_tw);null" json:"-"`  // varchar(255)
+	TitleVi string `orm:"column(title_vi);null" json:"-"`  // varchar(255)
+	State   bool   `orm:"column(state);null" json:"-"`     // tinyint(1)
 }
 
 func (self *CommendWord) TableName() string {
 	return commend_words_tablename
 }
 
-func GetCommendWordsList(perSize int64, useCache bool) (items []*CommendWord) {
-	k := fmt.Sprintf("product_model.GetCommendWordsList(%d)", perSize)
+func (self *CommendWord) ApplyLanguage(lang string) {
+	if self == nil {
+		return
+	}
+	switch NormalizeLanguage(lang) {
+	case LanguageEn:
+		if strings.TrimSpace(self.TitleEn) != "" {
+			self.Title = self.TitleEn
+		}
+	case LanguageRu:
+		if strings.TrimSpace(self.TitleRu) != "" {
+			self.Title = self.TitleRu
+		}
+	case LanguageZhTW:
+		if strings.TrimSpace(self.TitleTw) != "" {
+			self.Title = self.TitleTw
+		}
+	default:
+		if strings.HasPrefix(strings.ToLower(strings.TrimSpace(lang)), "vi") && strings.TrimSpace(self.TitleVi) != "" {
+			self.Title = self.TitleVi
+		}
+	}
+}
+
+func GetCommendWordsList(perSize int64, lang string, useCache bool) (items []*CommendWord) {
+	normalizedLang := NormalizeLanguage(lang)
+	if strings.HasPrefix(strings.ToLower(strings.TrimSpace(lang)), "vi") {
+		normalizedLang = "vi"
+	}
+	k := fmt.Sprintf("product_model.GetCommendWordsList(%d,%s)", perSize, normalizedLang)
 
 	if useCache {
 		if ret, ok := cache.Cache.Get(k).([]*CommendWord); ok {
@@ -38,6 +70,9 @@ func GetCommendWordsList(perSize int64, useCache bool) (items []*CommendWord) {
 	if err != nil {
 		beego.BeeLogger.Debug("GetCommendWordsList err=%s", err)
 	}
+	for _, item := range items {
+		item.ApplyLanguage(lang)
+	}
 
 	cache.Cache.Put(k, items, 10*time.Minute)
 	return items

+ 43 - 20
go/gopath/src/fohow.com/apps/models/product_model/product.go

@@ -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)