2023-06-16 07:11:37 +00:00
|
|
|
package gmodel
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
2023-06-20 09:28:28 +00:00
|
|
|
func (p *FsProductModel) FindOne(ctx context.Context, id int64) (resp *FsProduct, err error) {
|
2023-07-04 08:48:56 +00:00
|
|
|
err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` = ? ", id).First(&resp).Error
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
func (p *FsProductModel) FindOneBySn(ctx context.Context, sn string) (resp *FsProduct, err error) {
|
|
|
|
err = p.db.WithContext(ctx).Model(&FsProduct{}).Where("`sn` = ? ", sn).Take(&resp).Error
|
2023-06-20 09:28:28 +00:00
|
|
|
return resp, err
|
2023-06-19 06:47:54 +00:00
|
|
|
}
|
2023-06-16 07:11:37 +00:00
|
|
|
func (p *FsProductModel) GetProductListByIds(ctx context.Context, productIds []int64, sort string) (resp []FsProduct, err error) {
|
|
|
|
if len(productIds) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2023-06-20 09:28:28 +00:00
|
|
|
db := p.db.Model(&FsProduct{}).WithContext(ctx).
|
2023-06-16 07:11:37 +00:00
|
|
|
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
|
|
|
|
}
|
2023-06-27 09:04:58 +00:00
|
|
|
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
|
|
|
|
}
|
2023-06-16 07:11:37 +00:00
|
|
|
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
|
2023-07-10 11:42:55 +00:00
|
|
|
return resp,err
|
2023-06-16 07:11:37 +00:00
|
|
|
}
|
2023-06-26 10:19:51 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2023-07-10 11:42:55 +00:00
|
|
|
func (p *FsProductModel) GetRandomProductListInIds(ctx context.Context,ids []int64, limit int) (resp []FsProduct, err error) {
|
|
|
|
err = p.db.WithContext(ctx).Model(&FsProduct{}).
|
|
|
|
Where("`id` in (?) and `is_del` =? and `is_shelf` = ?", ids,0, 1).Order("RAND()").Limit(limit).Find(&resp).Error
|
|
|
|
return resp,err
|
|
|
|
}
|