fusenapi/model/gmodel/fs_product_logic.go
laodaming 66ed487249 fix
2023-06-27 17:04:58 +08:00

73 lines
2.3 KiB
Go
Executable File

package gmodel
import "context"
func (p *FsProductModel) FindOne(ctx context.Context, id int64) (resp *FsProduct, err error) {
err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` = ? and `is_del` =? and `is_shelf` = ? and `status` =?", id, 0, 1, 1).First(&resp).Error
return resp, err
}
func (p *FsProductModel) GetProductListByIds(ctx context.Context, productIds []int64, sort string) (resp []FsProduct, err error) {
if len(productIds) == 0 {
return
}
db := p.db.Model(&FsProduct{}).WithContext(ctx).
Where("`id` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?", productIds, 0, 1, 1)
switch sort {
case "sort-asc":
db = db.Order("`sort` ASC")
case "sort-desc":
db = db.Order("`sort` DESC")
}
if err = db.Find(&resp).Error; err != nil {
return nil, err
}
return
}
func (p *FsProductModel) GetProductListByIdsWithoutStatus(ctx context.Context, productIds []int64, sort string) (resp []FsProduct, err error) {
if len(productIds) == 0 {
return
}
db := p.db.Model(&FsProduct{}).WithContext(ctx).
Where("`id` in (?) ", productIds)
switch sort {
case "sort-asc":
db = db.Order("`sort` ASC")
case "sort-desc":
db = db.Order("`sort` DESC")
}
if err = db.Find(&resp).Error; err != nil {
return nil, err
}
return
}
func (p *FsProductModel) GetProductListByTypeIds(ctx context.Context, productTypes []int64, sort string) (resp []FsProduct, err error) {
if len(productTypes) == 0 {
return
}
db := p.db.WithContext(ctx).Model(&FsProduct{}).Where("`type` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?", productTypes, 0, 1, 1)
switch sort {
case "sort-asc":
db = db.Order("`sort` ASC")
case "sort-desc":
db = db.Order("`sort` DESC")
}
err = db.Find(&resp).Error
if err != nil {
return nil, err
}
return
}
func (p *FsProductModel) GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error) {
err = p.db.WithContext(ctx).Model(&FsProduct{}).
Where("`is_del` =? and `is_shelf` = ?", 0, 1).Order("RAND()").Limit(limit).Find(&resp).Error
if err != nil {
return nil, err
}
return
}
func (p *FsProductModel) FindAllOnlyByIds(ctx context.Context, ids []int64) (resp []*FsProduct, err error) {
err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` IN (?)", ids).Find(&resp).Error
return resp, err
}