package goods
import (
"recook/configs"
"recook/internal/v2/lib/db"
"recook/internal/v2/model/flashsale"
"strconv"
"github.com/jinzhu/gorm"
"github.com/shopspring/decimal"
)
type RecookGoodsSkuModel struct {
db . BaseModel
Id uint ` gorm:"column:id;primary_key" json:"id" `
GoodsId uint ` gorm:"column:goods_id" json:"goodsId" `
Name string ` gorm:"column:name" json:"name" `
CombineId string ` gorm:"column:combine_id" json:"combineId" `
PicURL string ` gorm:"column:pic_url" json:"picUrl" `
Code string ` gorm:"column:code" json:"code" `
PurchasePrice decimal . Decimal ` gorm:"column:purchase_price" json:"purchasePrice" `
OriginalPrice decimal . Decimal ` gorm:"column:original_price" json:"originalPrice" ` // 原价
DiscountPrice decimal . Decimal ` gorm:"column:discount_price" json:"discountPrice" ` // 折扣价
CommissionRate decimal . Decimal ` gorm:"column:commission_rate" json:"commissionRate" ` // 佣金比
Commission decimal . Decimal ` gorm:"column:commission" json:"commission" ` // 佣金
ControlPrice decimal . Decimal ` gorm:"column:control_price" json:"controlPrice" ` // 供应商最低控价
SalesVolume uint ` gorm:"column:sales_volume" json:"salesVolume" ` // 销售量 销售量只简单累加
Inventory uint ` gorm:"column:inventory" json:"inventory" ` // 库存量 可以直接补货
SalesVolumeInc uint ` gorm:"column:sales_volume_inc" json:"salesVolumeInc" ` // 销售量的增量
Coupon decimal . Decimal ` gorm:"column:coupon" json:"coupon" `
GoodsNum string ` gorm:"column:goods_num" json:"goodsNum" ` //商品货号
BmSkuId string ` gorm:"column:bm_sku_id" json:"bm_sku_id" ` //舶茂的skuid
BmShopId string ` gorm:"column:bm_shop_id" json:"bm_shop_id" ` //舶茂的商家id
ThirdPartySkuId string ` gorm:"column:third_party_sku_id" json:"thirdPartySkuId" ` //第三方skuid
ThirdPartyType int ` gorm:"column:third_party_type" json:"thirdPartySkuType" ` //第三方类型
TMallPrice string ` gorm:"column:tmall_price" json:"tmallPrice" `
TMallUrl string ` gorm:"column:tmall_url" json:"tmallUrl" `
JdPrice string ` gorm:"column:jd_price" json:"jdPrice" `
JdUrl string ` gorm:"column:jd_url" json:"jdUrl" `
GysSkuID uint ` gorm:"gys_sku_id" json:"gys_sku_id" `
Invoice RecookSkuInvoiceModel ` gorm:"foreignKey:SkuId" json:"invoice" `
DetailPhoto [ ] RecookGoodsDetailPhotoModel ` gorm:"foreignKey:GoodsID;references:GoodsId" `
Goods RecookGoodsInfoModel ` gorm:"foreignKey:GoodsId" `
SecKill flashsale . RecookSecKillGoodsModel ` gorm:"foreignKey:Id;references:GoodsSkuId" `
SecKillTemp flashsale . RecookSecKillGoodsModelTemp ` gorm:"foreignKey:Id;references:GoodsSkuId" `
Length float64 ` json:"length" `
Width float64 ` json:"width" `
Height float64 ` json:"height" `
Weight float64 ` json:"weight" `
Limit int ` json:"limit" gorm:"column:limit_num" `
Min int ` json:"min" gorm:"column:min_num" `
SaleInventory uint ` json:"sale_inventory" `
GoodsInfo * RecookGoodsInfoModel ` json:"-" gorm:"foreignKey:goods_id" `
SalePurchasePrice decimal . Decimal ` json:"sale_purchase_price" gorm:"column:sale_purchase_price" `
SaleVolume2 uint ` json:"sale_volume2" gorm:"column:sale_volume2" `
SaleVolumeInc2 uint ` json:"sale_volume_inc2" gorm:"column:sale_volume_inc2" `
EffectTime int ` json:"effect_time" `
EffectDayType int ` json:"effect_day_type" `
ExtraPrice decimal . Decimal ` json:"extra_price" `
SaleExtraPrice decimal . Decimal ` json:"sale_extra_price" `
}
func ( r * RecookGoodsSkuModel ) GetSalePrice ( level int ) decimal . Decimal {
if r . SalePurchasePrice . IsZero ( ) {
return decimal . Zero
}
if level == 10 {
if ! r . SaleExtraPrice . IsZero ( ) {
return r . SaleExtraPrice . Add ( r . SaleExtraPrice )
}
return r . SalePurchasePrice . Mul ( decimal . NewFromFloat ( 1.03 ) ) . Round ( 2 )
}
cost := r . SalePurchasePrice . Mul ( decimal . NewFromFloat ( 1.03 ) )
if ! r . SaleExtraPrice . IsZero ( ) {
cost = r . SaleExtraPrice . Add ( r . SaleExtraPrice )
}
s1 := cost . Round ( 2 ) . Mul ( decimal . NewFromFloat ( 1.2 ) ) . Round ( 2 )
return s1 . Round ( 2 )
}
// TableName sets the insert table name for this struct type
func ( r * RecookGoodsSkuModel ) TableName ( ) string {
return "recook_goods_sku"
}
// @Style 添加
func ( r * RecookGoodsSkuModel ) Create ( data * RecookGoodsSkuModel ) {
r . GetDb ( ) . Create ( data )
}
// @Style 批量添加
func ( r * RecookGoodsSkuModel ) CreateAll ( datas * [ ] RecookGoodsSkuModel ) int64 {
valueStr := ""
values := [ ] interface { } { }
for _ , item := range * datas {
valueStr += ",(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
values = append ( values ,
item . GoodsId ,
item . Name ,
item . CombineId ,
item . PicURL ,
item . Code ,
item . PurchasePrice ,
item . OriginalPrice ,
item . DiscountPrice ,
item . CommissionRate ,
item . Commission ,
item . ControlPrice ,
item . SalesVolume ,
item . Inventory ,
item . SalesVolumeInc ,
item . Coupon ,
item . GoodsNum ,
item . TMallPrice ,
item . TMallUrl ,
item . JdPrice ,
item . JdUrl ,
)
}
if len ( values ) > 0 {
return r . GetDb ( ) . Exec ( "insert into recook_goods_sku(goods_id,name,combine_id,pic_url,code,purchase_price,original_price,discount_price,commission_rate,commission,control_price,sales_volume,inventory,sales_volume_inc,coupon,goods_num,tmall_price,tmall_url,jd_price,jd_url) values " + valueStr [ 1 : ] , values ... ) . RowsAffected
}
return 0
}
// @Style 添加
func ( r * RecookGoodsSkuModel ) UpdateByGoodsId ( goodsId uint , data * RecookGoodsSkuModel ) int64 {
return r . GetDb ( ) . Model ( & RecookGoodsSkuModel { } ) . Where ( "goods_id = ?" , goodsId ) . Update ( data ) . RowsAffected
}
// @Style 添加
func ( r * RecookGoodsSkuModel ) Update ( date map [ string ] interface { } , query interface { } , queryArgs ... interface { } ) ( result error ) {
return r . GetDb ( ) . Model ( & RecookGoodsSkuModel { } ) . Where ( query , queryArgs ... ) . Updates ( date ) . Error
}
// @Style 查询
func ( r * RecookGoodsSkuModel ) FindByCodes ( codes [ ] string ) ( result RecookGoodsSkuModel ) {
r . GetDb ( ) . First ( & result , "code in (?)" , codes )
return
}
// @Style 查询
func ( r * RecookGoodsSkuModel ) FindByGoodsId ( goodsId uint ) ( result [ ] RecookGoodsSkuModel ) {
r . GetDb ( ) . Find ( & result , "goods_id = ?" , goodsId )
return
}
// @Style 查询售价为0的商品
func ( r * RecookGoodsSkuModel ) FindByGoods ( goodsId uint ) ( result [ ] RecookGoodsSkuModel ) {
r . GetDb ( ) . Find ( & result , "goods_id = ? and discount_price = 0" , goodsId )
return
}
// @Style 查询
func ( r * RecookGoodsSkuModel ) FindByGoodsIds ( goodsIds [ ] uint ) ( result [ ] RecookGoodsSkuModel ) {
r . GetDb ( ) . Find ( & result , "goods_id in (?)" , goodsIds )
return
}
// @Style 获取列表数量
func ( r * RecookGoodsSkuModel ) GetGoodsSql ( query interface { } , args ... interface { } ) ( result * gorm . SqlExpr ) {
return r . GetDb ( ) . Model ( & RecookGoodsSkuModel { } ) . Select ( "goods_id" ) . Where ( query , args ... ) . SubQuery ( )
}
// FindByGoodsID 根据goodsID 查询skus.
func ( r * RecookGoodsSkuModel ) FindByGoodsID ( by uint ) ( result [ ] RecookGoodsSkuModel ) {
r . GetDb ( ) . Find ( & result , "goods_id = ?" , by )
return
}
// GetSkuID 获取sku_id.
func ( r * RecookGoodsSkuModel ) GetSkuID ( ) string {
return r . Code
}
// GetShopSkuID 获取shop_sku_id.
func ( r * RecookGoodsSkuModel ) GetShopSkuID ( ) string {
return configs . ConfigJSTPrefix + "#" + strconv . Itoa ( int ( r . Id ) )
}
// FindByID 根据goodsID 查询sku.
func ( r * RecookGoodsSkuModel ) FindByID ( by uint ) ( result RecookGoodsSkuModel ) {
r . GetDb ( ) . Find ( & result , "id = ?" , by )
return
}
// FindBySkuCode 根据skuCode 查询sku.
func ( r * RecookGoodsSkuModel ) FindBySkuCode ( by string ) ( result RecookGoodsSkuModel ) {
r . GetDb ( ) . Find ( & result , "code = ?" , by )
return
}
//根据id来找到资料, 并修改库存字段
func ( r * RecookGoodsSkuModel ) SetInventoryById ( id int , inventory int ) {
r . GetDb ( ) . Model ( & RecookGoodsSkuModel { } ) . Where ( "id=?" , id ) . Update ( "inventory" , inventory )
}
//根据code来查找RecookGoodsSkuModel
func ( r * RecookGoodsSkuModel ) FindByCode ( code string ) ( result RecookGoodsSkuModel ) {
r . GetDb ( ) . Find ( & result , "code=?" , code )
return
}