|
|
package goods
|
|
|
|
|
|
import (
|
|
|
"recook/internal/v2/lib/db"
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
"github.com/shopspring/decimal"
|
|
|
)
|
|
|
|
|
|
type GysGoodsSkuModel struct {
|
|
|
db.BaseModel
|
|
|
Id uint `gorm:"column:id;primary_key" json:"id"`
|
|
|
GoodsId uint `json:"goodsId"`
|
|
|
Name string `json:"name"`
|
|
|
CombineId string `json:"combineId"`
|
|
|
PicUrl string `json:"picUrl"`
|
|
|
Code string `json:"code"`
|
|
|
PurchasePrice decimal.Decimal `json:"purchasePrice"`
|
|
|
OriginalPrice decimal.Decimal `json:"originalPrice"`
|
|
|
DiscountPrice decimal.Decimal `json:"discountPrice"`
|
|
|
RealDiscountPrice decimal.Decimal `json:"realDiscountPrice"`
|
|
|
Inventory uint `json:"inventory"`
|
|
|
CommissionRate decimal.Decimal `json:"commissionRate"`
|
|
|
SalesVolumeInc uint `json:"salesVolumeInc"`
|
|
|
Coupon decimal.Decimal `json:"coupon"`
|
|
|
GoodsNum string `json:"goodsNum"`
|
|
|
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"`
|
|
|
}
|
|
|
|
|
|
func (r GysGoodsSkuModel) TableName() string {
|
|
|
return "gys_goods_sku"
|
|
|
}
|
|
|
|
|
|
type GysGoodsSkuTempModel struct {
|
|
|
GysGoodsSkuModel
|
|
|
}
|
|
|
|
|
|
func (r GysGoodsSkuTempModel) TableName() string {
|
|
|
return "gys_goods_sku_temp"
|
|
|
}
|
|
|
|
|
|
//调整库存,通过code来匹配
|
|
|
func (r *GysGoodsSkuModel) SetInventoryByCode(code string, inventory uint) {
|
|
|
r.GetDb().Model(&GysGoodsSkuModel{}).Where("code=?", code).Update("inventory", inventory)
|
|
|
}
|
|
|
|
|
|
func (r *GysGoodsSkuModel) Create(data *GysGoodsSkuModel) {
|
|
|
r.GetDb().Create(data)
|
|
|
}
|
|
|
|
|
|
func (r *GysGoodsSkuModel) CreateAll(datas *[]GysGoodsSkuModel) 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.Inventory, item.GoodsNum, item.JdUrl, item.JdPrice, item.TMallUrl, item.TMallPrice)
|
|
|
}
|
|
|
if len(values) > 0 {
|
|
|
return r.GetDb().Exec("insert into gys_goods_sku(goods_id,name,combine_id,pic_url,code,purchase_price,original_price,discount_price,inventory,goods_num,jd_url,jd_price,tmall_url,tmall_price) values "+valueStr[1:], values...).RowsAffected
|
|
|
}
|
|
|
return 0
|
|
|
}
|
|
|
|
|
|
func (r *GysGoodsSkuModel) DeleteByGoodsId(goodsId uint) int64 {
|
|
|
return r.GetDb().Delete(&GysGoodsSkuModel{}, "goods_id = ?", goodsId).RowsAffected
|
|
|
}
|
|
|
|
|
|
func (r *GysGoodsSkuModel) GetByGoodsId(goodsId uint) (result []GysGoodsSkuModel) {
|
|
|
if goodsId < 0 {
|
|
|
return []GysGoodsSkuModel{}
|
|
|
}
|
|
|
r.GetDb().Model(&GysGoodsSkuModel{}).Find(&result, "goods_id = ?", goodsId)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func (r *GysGoodsSkuModel) SumInventoryByGoodsIds(goodsIds []uint) (result []GysGoodsSkuModel) {
|
|
|
if len(goodsIds) == 0 {
|
|
|
return []GysGoodsSkuModel{}
|
|
|
}
|
|
|
r.GetDb().Model(&GysGoodsSkuModel{}).Select("goods_id,sum(inventory) as inventory").Group("goods_id").Find(&result, "goods_id in (?)", goodsIds)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func (r *GysGoodsSkuModel) SubGoodsIds(query string, args ...interface{}) (result *gorm.SqlExpr) {
|
|
|
return r.GetDb().Model(&GysGoodsSkuModel{}).Select("goods_id").Where(query, args...).SubQuery()
|
|
|
}
|
|
|
|
|
|
// @Style 更新
|
|
|
func (r *GysGoodsSkuModel) Update(data *GysGoodsSkuModel, query string, args ...interface{}) (result int64) {
|
|
|
return r.GetDb().Model(&GysGoodsSkuModel{}).Where(query, args...).Update(data).RowsAffected
|
|
|
}
|
|
|
|
|
|
// FindByID 根据id.
|
|
|
func (r *GysGoodsSkuModel) FindByID(by uint) (result *GysGoodsSkuModel) {
|
|
|
r.GetDb().Model(&GysGoodsSkuModel{}).Where("id = ?", by).First(&result)
|
|
|
return
|
|
|
}
|