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