2023-06-12 08:47:48 +00:00
package gmodel
import (
"context"
2023-06-13 04:15:06 +00:00
"errors"
2023-06-19 02:12:58 +00:00
2023-06-12 08:47:48 +00:00
"gorm.io/gorm"
)
2023-06-12 06:05:35 +00:00
2023-06-12 08:47:48 +00:00
type GetPriceListByProductIdsRsp struct {
ProductId int64 ` json:"product_id" `
Price string ` json:"price" `
}
2023-06-13 09:47:48 +00:00
func ( p * FsProductPriceModel ) GetSimplePriceListByProductIds ( ctx context . Context , productIds [ ] int64 ) ( resp [ ] GetPriceListByProductIdsRsp , err error ) {
2023-06-12 08:47:48 +00:00
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
}
2023-09-15 08:30:01 +00:00
func ( p * FsProductPriceModel ) GetPriceListByProductIdsSizeIds ( ctx context . Context , productIds , sizeIds [ ] int64 ) ( resp [ ] FsProductPrice , err error ) {
2023-09-20 07:52:34 +00:00
2023-09-15 08:30:01 +00:00
if len ( sizeIds ) == 0 || len ( productIds ) == 0 {
2023-06-13 04:15:06 +00:00
return
}
2023-09-15 08:30:01 +00:00
err = p . db . WithContext ( ctx ) . Model ( & FsProductPrice { } ) . Where ( "`size_id` in (?) and `product_id` in (?) and `status` = ? " , sizeIds , productIds , 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 )
}
2023-06-16 11:26:48 +00:00
if err = db . First ( & resp ) . Error ; err != nil && ! errors . Is ( err , gorm . ErrRecordNotFound ) {
2023-06-13 04:15:06 +00:00
return FsProductPrice { } , err
}
2023-06-13 05:07:09 +00:00
return resp , nil
2023-06-13 04:15:06 +00:00
}
2023-06-13 09:47:48 +00:00
func ( p * FsProductPriceModel ) GetPriceListByProductIds ( ctx context . Context , productIds [ ] int64 ) ( resp [ ] FsProductPrice , err error ) {
if len ( productIds ) == 0 {
return nil , nil
}
db := p . db . WithContext ( ctx ) . Model ( & FsProductPrice { } ) .
Where ( "`product_id` in (?) and `status` = ?" , productIds , 1 )
if err = db . Find ( & resp ) . Error ; err != nil {
return nil , err
}
return
}
2023-09-15 07:27:39 +00:00
func ( p * FsProductPriceModel ) FindOneBySizeId ( ctx context . Context , sizeId int64 ) ( resp * FsProductPrice , err error ) {
err = p . db . WithContext ( ctx ) . Model ( & FsProductPrice { } ) .
Where ( "`size_id` = ? and `status` = ?" , sizeId , 1 ) . Take ( & resp ) . Error
return resp , err
}
2023-06-26 10:19:51 +00:00
// 产品价格
type ProductPrice struct {
Id int64 ` json:"id" `
MinBuyNum int64 ` json:"min_buy_num" `
StepNum interface { } ` json:"step_num" `
StepPrice interface { } ` json:"step_price" `
ProductId int64 ` json:"product_id" `
SizeId int64 ` json:"size_id" `
EachBoxNum int64 ` json:"each_box_num" `
}
func ( c * FsProductPriceModel ) GetAllSelectBySizeId ( ctx context . Context , sizeIds [ ] int64 ) ( prices [ ] * ProductPrice , err error ) {
2023-06-27 10:09:56 +00:00
var pprices [ ] * FsProductPrice
err = c . db . WithContext ( ctx ) . Model ( & FsProductPrice { } ) . Where ( "size_id IN (?) AND status = ?" , sizeIds , 1 ) . Select ( "id, min_buy_num, step_num, step_price, product_id, size_id, each_box_num" ) . Find ( & pprices ) . Error
if err != nil {
return nil , err
}
for _ , p := range pprices {
prices = append ( prices , & ProductPrice {
Id : p . Id ,
MinBuyNum : * p . MinBuyNum ,
StepNum : * p . StepNum ,
StepPrice : * p . StepPrice ,
ProductId : * p . ProductId ,
SizeId : * p . SizeId ,
EachBoxNum : * p . EachBoxNum ,
} )
}
2023-06-26 10:19:51 +00:00
return prices , err
}
2023-07-06 09:43:07 +00:00
func ( p * FsProductPriceModel ) GetAllByIdsWithoutStatus ( ctx context . Context , Ids [ ] int64 ) ( resp [ ] FsProductPrice , err error ) {
2023-07-03 09:33:59 +00:00
if len ( Ids ) == 0 {
return nil , nil
}
2023-07-06 09:43:07 +00:00
err = p . db . WithContext ( ctx ) . Model ( & FsProductPrice { } ) . Where ( "`id` in (?)" , Ids ) . Find ( & resp ) . Error
return resp , err
}
func ( p * FsProductPriceModel ) GetAllByProductIdStatus ( ctx context . Context , productId int64 , status int64 ) ( resp [ ] FsProductPrice , err error ) {
err = p . db . WithContext ( ctx ) . Model ( & FsProductPrice { } ) . Where ( "`product_id` = ? and `status` = ?" , productId , status ) . Find ( & resp ) . Error
return resp , err
2023-07-03 09:33:59 +00:00
}