fusenapi/model/gmodel/fsproductpricemodel.go

85 lines
3.0 KiB
Go
Raw Normal View History

2023-06-12 08:47:48 +00:00
package gmodel
import (
"context"
2023-06-13 04:15:06 +00:00
"errors"
2023-06-12 08:47:48 +00:00
"gorm.io/gorm"
)
2023-06-12 06:05:35 +00:00
type FsProductPrice struct {
2023-06-12 06:35:36 +00:00
Id int64 `gorm:"primary_key" json:"id"`
Sn *string `gorm:"default:''" json:"sn"` // 唯一编码
Title *string `gorm:"default:''" json:"title"` // 标题描述
ProductId *int64 `gorm:"default:0" json:"product_id"` // 产品ID
MaterialId *int64 `gorm:"default:0" json:"material_id"` // 材质ID
SizeId *int64 `gorm:"default:0" json:"size_id"` // 尺寸ID
EachBoxNum *int64 `gorm:"default:0" json:"each_box_num"` // 每一箱的个数
EachBoxWeight *float64 `gorm:"default:0" json:"each_box_weight"` // 每一箱的重量 单位KG
MinBuyNum *int64 `gorm:"default:0" json:"min_buy_num"` // 最少购买量
StepNum *string `gorm:"default:''" json:"step_num"` // 数量阶梯 eg:10,20,30
StepPrice *string `gorm:"default:''" json:"step_price"` // 价格阶梯 eg:100,50,25
Status *int64 `gorm:"default:1" json:"status"` // 是否可用
IsDefault *int64 `gorm:"default:0" json:"is_default"` // 是否默认
2023-06-12 06:05:35 +00:00
}
2023-06-12 08:47:48 +00:00
type FsProductPriceModel struct {
db *gorm.DB
}
func NewFsProductPriceModel(db *gorm.DB) *FsProductPriceModel {
return &FsProductPriceModel{db}
}
type GetPriceListByProductIdsRsp struct {
ProductId int64 `json:"product_id"`
Price string `json:"price"`
}
func (p *FsProductPriceModel) GetPriceListByProductIds(ctx context.Context, productIds []int64) (resp []GetPriceListByProductIdsRsp, err error) {
if len(productIds) == 0 {
return nil, nil
}
2023-06-12 09:47:20 +00:00
db := p.db.WithContext(ctx).Model(&FsProductPrice{}).Select("product_id,group_concat(step_price) as price").
2023-06-12 08:47:48 +00:00
Where("`product_id` in (?) and `status` = ? group by product_id", productIds, 1)
if err = db.Find(&resp).Error; err != nil {
return nil, err
}
return
}
func (p *FsProductPriceModel) GetPriceListBySizeIds(ctx context.Context, sizeIds []int64) (resp []FsProductPrice, err error) {
2023-06-13 04:15:06 +00:00
if len(sizeIds) == 0 {
return
}
2023-06-12 09:47:20 +00:00
err = p.db.WithContext(ctx).Model(&FsProductPrice{}).Where("`size_id` in (?) and `status` = ? ", sizeIds, 1).Find(&resp).Error
2023-06-12 08:47:48 +00:00
if err != nil {
return nil, err
}
return
}
2023-06-13 04:15:06 +00:00
type FindOneProductPriceByParamsReq struct {
ProductId *int64
MaterialId *int64
SizeId *int64
Status *int64
}
func (p *FsProductPriceModel) FindOneProductPriceByParams(ctx context.Context, req FindOneProductPriceByParamsReq) (resp FsProductPrice, err error) {
db := p.db.WithContext(ctx).Model(&FsProductPrice{})
if req.ProductId != nil {
db = db.Where("`product_id` = ?", *req.ProductId)
}
if req.MaterialId != nil {
db = db.Where("`material_id` = ?", *req.MaterialId)
}
if req.SizeId != nil {
db = db.Where("`size_id` = ?", *req.SizeId)
}
if req.Status != nil {
db = db.Where("`status` = ?", *req.Status)
}
if err = db.First(&resp).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return FsProductPrice{}, err
}
2023-06-13 05:07:09 +00:00
return resp, nil
2023-06-13 04:15:06 +00:00
}