abiao лет назад: 5
Родитель
Сommit
6a3a33a4c0

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

@@ -16,8 +16,8 @@ import (
 
 var (
 	//需要校验用户登录的Action
-	exceptCheckUserLoginAction   = []string{"Latest", "Get", "Categories", "GetProductsByCat", "GetNeedShare"}
-	exceptCheckWxUserLoginAction = []string{"Latest", "Get", "Categories", "GetProductsByCat"}
+	exceptCheckUserLoginAction   = []string{"GetProductSizesByPid", "Latest", "Get", "Categories", "GetProductsByCat", "GetNeedShare"}
+	exceptCheckWxUserLoginAction = []string{"GetProductSizesByPid", "Latest", "Get", "Categories", "GetProductsByCat"}
 )
 
 type ProductController struct {

+ 36 - 0
go/gopath/src/fohow.com/apps/controllers/product_controller/product_controller.go

@@ -104,6 +104,10 @@ func (self *ProductController) Get() {
 			pd.SeckillStateCn = product_model.SECKILL_END_STATE_CN
 		}
 	}
+	pd.HaveSize = false
+	if product_model.GetProductAttrConfig(pd.Id) != nil {
+		pd.HaveSize = true
+	}
 
 	pd.DeliverStartTime = pd.DeliverStartAt.Unix()
 	pd.DeliverStopTime = pd.DeliverStopAt.Unix()
@@ -229,3 +233,35 @@ func (self *ProductController) GetNeedShare() {
 	self.Data["json"] = &Ret{NeedShare: needShare}
 	self.ServeJSON()
 }
+
+//获取某个商品关联规格列表
+func (self *ProductController) GetProductSizesByPid() {
+	_id := self.Ctx.Input.Param(":product_id")
+	pId, _ := strconv.ParseInt(_id, 10, 64)
+
+	type Ret struct {
+		Size  *product_model.ProductAttrKey `json:"size_list"`
+		Color *product_model.ProductAttrKey `json:"color_list"`
+	}
+	product := product_model.GetProductById(pId, false)
+	if product == nil {
+		self.ReturnError(403, apps.NoExist, "", nil)
+	}
+	ret := &Ret{}
+	list := product_model.GetProductConfigAttrsByPId(pId)
+	for _, item := range list {
+		productKeyConfig := product_model.GetProductAttrKey(item.AttrKeyId)
+		if productKeyConfig != nil {
+			if item.SizeType == product_model.SIZE_TYPE_1 {
+				productKeyConfig.ProductAttr = product_model.GetProductAttrsByKId(productKeyConfig.Id)
+				ret.Size = productKeyConfig
+			}
+			if item.SizeType == product_model.SIZE_TYPE_2 {
+				productKeyConfig.ProductAttr = product_model.GetProductAttrsByKId(productKeyConfig.Id)
+				ret.Color = productKeyConfig
+			}
+		}
+	}
+	self.Data["json"] = ret
+	self.ServeJSON()
+}

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

@@ -29,6 +29,10 @@ const (
 	SECKILL_UNDER_STATE_CN     = "正在秒杀"
 	SECKILL_END_STATE          = "end"
 	SECKILL_END_STATE_CN       = "秒杀结束"
+
+	//规格类型
+	SIZE_TYPE_1 = "size"  //类型
+	SIZE_TYPE_2 = "color" //颜色
 )
 
 func (self *Product) TableName() string {
@@ -93,6 +97,7 @@ type Product struct {
 	ShowFlag        bool   `orm:"column(show_flag)"                          json:"-"`                         // varchar(255)
 	SizeId          string `orm:"column(size_id)"                          json:"size_id"`                     // varchar(255)
 	ColorId         string `orm:"column(color_id)"                          json:"color_id"`                   // varchar(255)
+	HaveSize        bool   `orm:"column(have_size)"                         json:"have_size"`                  // bool
 }
 
 //获取最新推荐商品

+ 98 - 0
go/gopath/src/fohow.com/apps/models/product_model/product_attr.go

@@ -0,0 +1,98 @@
+package product_model
+
+import (
+	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/orm"
+	"time"
+)
+
+const (
+	product_attrs_tablename        = "product_attrs"
+	product_attr_keys_tablename    = "product_attr_keys"
+	product_attr_configs_tablename = "product_attr_configs"
+)
+
+type ProductAttrKey struct {
+	Id          int64          `orm:"column(id);pk"                                  json:"id"`   // int(11)
+	Name        string         `orm:"column(name)"                                   json:"name"` // varchar(20)
+	Status      int64          `orm:"column(status);null"                            json:"-"`    // tinyint(1)
+	CreatedAt   time.Time      `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"`    // datetime
+	UpdatedAt   time.Time      `orm:"column(updated_at);auto_now;type(datetime)"     json:"-"`    // datetime
+	ProductAttr []*ProductAttr `orm:"-"                            json:"attr_values"`
+}
+
+func (self *ProductAttrKey) TableName() string {
+	return product_attr_keys_tablename
+}
+
+type ProductAttr struct {
+	Id        int64     `orm:"column(id);pk"                json:"id"`                  // int(11)
+	AttrKeyId int64     `orm:"column(attr_key_id)"          json:"attr_key_id"`         // int(11)
+	Name      string    `orm:"column(name);null"            json:"name"`                // varchar(64)
+	IsSelect  bool      `orm:"-"                            json:"is_select"`           // tinyint(1)
+	Recommend int64     `orm:"column(recommend);null"                        json:"-"`  // tinyint(1)
+	CreatedAt time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"` // datetime
+	UpdatedAt time.Time `orm:"column(updated_at);auto_now;type(datetime)"     json:"-"` // datetime
+}
+
+func (self *ProductAttr) TableName() string {
+	return product_attrs_tablename
+}
+
+type ProductAttrConfig struct {
+	Id        int64     `orm:"column(id);pk"                          json:"id,omitempty"` // int(11)
+	AttrKeyId int64     `orm:"column(attr_key_id)"                    json:"attr_key_id"`  // int(11)
+	ProductId int64     `orm:"column(product_id)"                    json:"product_id"`    // int(11)
+	SizeType  string    `orm:"column(name);null"            json:"name"`                   // varchar(64)
+	CreatedAt time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"`    // datetime
+	UpdatedAt time.Time `orm:"column(updated_at);auto_now;type(datetime)"     json:"-"`    // datetime
+}
+
+func (self *ProductAttrConfig) TableName() string {
+	return product_attr_configs_tablename
+}
+
+// 根据product_attr_configs表product_Id查找记录
+func GetProductAttrConfig(productId int64) (pa *ProductAttrConfig) {
+	pa = &ProductAttrConfig{}
+	if err := orm.NewOrm().QueryTable(pa).Filter("product_id", productId).Limit(1).
+		One(pa); err != nil {
+		beego.BeeLogger.Error("get product attr config by record_id=%s err=%s", productId, err)
+		return nil
+	}
+	return pa
+}
+
+// 根据商品I找出商品对应某个属性Key的所有属性记录
+func GetProductConfigAttrsByPId(pId int64) (items []*ProductAttrConfig) {
+	o := orm.NewOrm()
+	item := new(ProductAttrConfig)
+	_, err := o.QueryTable(item).Filter("product_id", pId).Filter("status", 1).
+		All(&items)
+	if err != nil {
+		beego.BeeLogger.Error("GetProductAttrs(%d) err=%s", pId, err)
+	}
+	return items
+}
+
+// 根据KeyId找出商品对应某个属性Key的所有属性记录
+func GetProductAttrsByKId(kId int64) (items []*ProductAttr) {
+	o := orm.NewOrm()
+	item := new(ProductAttr)
+	_, err := o.QueryTable(item).Filter("attr_key_id", kId).All(&items)
+	if err != nil {
+		beego.BeeLogger.Error("GetProductAttrsByKId(%d) err=%s", kId, err)
+	}
+	return items
+}
+
+// 根据product_attr_configs表product_Id查找记录
+func GetProductAttrKey(kId int64) (pk *ProductAttrKey) {
+	pk = &ProductAttrKey{}
+	if err := orm.NewOrm().QueryTable(pk).Filter("id", kId).Limit(1).
+		One(pk); err != nil {
+		beego.BeeLogger.Error("get product attr key by record_id=%s err=%s", kId, err)
+		return nil
+	}
+	return pk
+}

+ 0 - 54
go/gopath/src/fohow.com/apps/models/product_model/product_attrl.go

@@ -1,54 +0,0 @@
-package product_model
-
-import (
-	"time"
-)
-
-const (
-	product_attrs_tablename        = "product_attrs"
-	product_attr_keys_tablename    = "product_attr_keys"
-	product_attr_configs_tablename = "product_attr_configs"
-)
-
-type ProductAttrKey struct {
-	Id        int64     `orm:"column(id);pk"                                  json:"id"`     // int(11)
-	Name      string    `orm:"column(name)"                                   json:"name"`   // varchar(20)
-	Status    int64     `orm:"column(status);null"                            json:"status"` // tinyint(1)
-	CreatedAt time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"`      // datetime
-	UpdatedAt time.Time `orm:"column(updated_at);auto_now;type(datetime)"     json:"-"`      // datetime
-}
-
-func (self *ProductAttrKey) TableName() string {
-	return product_attr_keys_tablename
-}
-
-type ProductAttr struct {
-	Id               int64             `orm:"column(id);pk"                json:"id"`                           // int(11)
-	AttrKeyId        int64             `orm:"column(attr_key_id)"          json:"attr_key_id"`                  // int(11)
-	Name             string            `orm:"column(name);null"            json:"name"`                         // varchar(64)
-	Recommend        int64             `orm:"column(use_custom);null"                        json:"use_custom"` // tinyint(1)
-	CreatedAt        time.Time         `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"`          // datetime
-	UpdatedAt        time.Time         `orm:"column(updated_at);auto_now;type(datetime)"     json:"-"`          // datetime
-	ProductAttrKey   *ProductAttrKey   `orm:"-"                            json:"attr_key"`
-	ProductAttrValue *ProductAttrValue `orm:"-"                            json:"attr_value"`
-}
-
-func (self *ProductAttr) TableName() string {
-	return product_attrs_tablename
-}
-
-type ProductAttrValue struct {
-	Id        int64     `orm:"column(id);pk"                          json:"id,omitempty"`       // int(11)
-	AttrKeyId int64     `orm:"column(attr_key_id)"                    json:"attr_key_id"`        // int(11)
-	Name      string    `orm:"column(name)"                                   json:"name"`       // varchar(64)
-	Pic       string    `orm:"column(pic)"                                    json:"pic"`        // varchar(255)
-	Color     string    `orm:"column(color)"                                  json:"color"`      // varchar(64)
-	CreatedBy int64     `orm:"column(created_by);null"                        json:"created_by"` // int(11)
-	Status    int64     `orm:"column(status);null"                            json:"status"`     // tinyint(1)
-	CreatedAt time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"`          // datetime
-	UpdatedAt time.Time `orm:"column(updated_at);auto_now;type(datetime)"     json:"-"`          // datetime
-}
-
-func (self *ProductAttrValue) TableName() string {
-	return product_attr_configs_tablename
-}

+ 3 - 0
go/gopath/src/fohow.com/routers/routes.go

@@ -84,6 +84,9 @@ func init() {
 	beego.Router("/v1/cat/:cat_id([0-9]+)/products", &product_controller.ProductController{}, "get:GetProductsByCat")
 	//商品详情
 	beego.Router("/v1/product/:id([0-9]+)", &product_controller.ProductController{}, "get:Get")
+	//商品规格明细
+	beego.Router("/v1/product_size/:id([0-9]+)", &product_controller.ProductController{}, "get:GetProductSizesByPid")
+
 	//----------- 订单相关 -----------
 	//商品下单
 	beego.Router("/v1/product/:id([0-9]+)/order/:count", &order_controller.OrderController{}, "post:Create")