fusenapi/model/gmodel/fs_product_logic.go
laodaming 9ff4468778 fix
2023-07-20 14:56:28 +08:00

128 lines
4.1 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` = ? ", id).First(&resp).Error
return resp, err
}
func (p *FsProductModel) FindOneBySn(ctx context.Context, sn string, fields ...string) (resp *FsProduct, err error) {
db := p.db.WithContext(ctx).Model(&FsProduct{}).Where("`sn` = ? ", sn)
if len(fields) != 0 {
db = db.Select(fields[0])
}
err = db.Take(&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, page int, limit int, sort string) (resp []FsProduct, total int64, 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.Count(&total).Error
if err != nil {
return nil, 0, err
}
offset := (page - 1) * limit
err = db.Offset(offset).Limit(limit).Find(&resp).Error
return resp, total, err
}
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
return resp, err
}
func (p *FsProductModel) GetIgnoreRandomProductList(ctx context.Context, limit int, notInProductIds []int64) (resp []FsProduct, err error) {
err = p.db.WithContext(ctx).Model(&FsProduct{}).
Where("`is_del` =? and `is_shelf` = ? and `id` not in(?)", 0, 1, notInProductIds).Order("RAND()").Limit(limit).Find(&resp).Error
return resp, err
}
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
}
func (p *FsProductModel) GetRandomProductListInIds(ctx context.Context, ids []int64, limit int, fields ...string) (resp []FsProduct, err error) {
if len(ids) == 0 {
return
}
db := p.db.WithContext(ctx).Model(&FsProduct{}).
Where("`id` in (?) and `is_del` =? and `is_shelf` = ?", ids, 0, 1).Order("RAND()").Limit(limit)
if len(fields) != 0 {
db = db.Select(fields[0])
}
err = db.Find(&resp).Error
return resp, err
}
type GetProductListByParamsReq struct {
Type []int64
IsDel *int64
IsShelf *int64
Status *int64
OrderBy string
}
func (p *FsProductModel) GetProductListByParams(ctx context.Context, req GetProductListByParamsReq) (resp []FsProduct, err error) {
db := p.db.WithContext(ctx).Model(&FsProduct{})
if len(req.Type) > 0 {
db = db.Where("`type` in (?)", req.Type)
}
if req.IsDel != nil {
db = db.Where("`is_del` = ?", *req.IsDel)
}
if req.IsShelf != nil {
db = db.Where("`is_shelf` = ?", *req.IsShelf)
}
if req.Status != nil {
db = db.Where("`status` = ?", *req.Status)
}
switch req.OrderBy {
case "":
db = db.Order("`id` DESC")
default:
db = db.Order(req.OrderBy)
}
err = db.Find(&resp).Error
return resp, err
}