45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package gmodel
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
// TODO: 使用model的属性做你想做的
|
|
type GetStockListReq struct {
|
|
UserId int64
|
|
Ids []int64
|
|
Status *int64
|
|
Page int
|
|
Limit int
|
|
}
|
|
|
|
func (s *FsUserStockModel) GetStockList(ctx context.Context, req GetStockListReq) (resp []FsUserStock, total int64, err error) {
|
|
db := s.db.Debug().WithContext(ctx).Model(&FsUserStock{})
|
|
if req.UserId > 0 {
|
|
db = db.Where("`user_id` = ?", req.UserId)
|
|
}
|
|
if len(req.Ids) > 0 {
|
|
db = db.Where("`id` in (?)", req.Ids)
|
|
}
|
|
if req.Status != nil {
|
|
db = db.Where("`status` = ?", *req.Status)
|
|
}
|
|
if err = db.Limit(1).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
offset := (req.Page - 1) * req.Limit
|
|
err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return
|
|
}
|
|
func (s *FsUserStockModel) FindOne(ctx context.Context, id int64, userId int64, lock ...bool) (resp *FsUserStock, err error) {
|
|
db := s.db.WithContext(ctx).Model(&FsUserStock{}).Where("`id` = ? and `user_id` = ? and `status` = ?", id, userId, 1)
|
|
if len(lock) != 0 && lock[0] {
|
|
db = db.Set("gorm:query_option", "FOR UPDATE")
|
|
}
|
|
err = db.First(&resp).Error
|
|
return resp, err
|
|
}
|