2023-06-19 10:27:31 +00:00
|
|
|
package gmodel
|
2023-06-27 06:25:25 +00:00
|
|
|
|
2023-06-28 04:25:40 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
2023-06-27 06:25:25 +00:00
|
|
|
|
|
|
|
// TODO: 使用model的属性做你想做的
|
2023-06-27 09:04:58 +00:00
|
|
|
type GetStockListReq struct {
|
|
|
|
UserId int64
|
|
|
|
Ids []int64
|
|
|
|
Status *int64
|
|
|
|
Page int
|
|
|
|
Limit int
|
|
|
|
}
|
2023-06-27 06:25:25 +00:00
|
|
|
|
2023-06-27 09:04:58 +00:00
|
|
|
func (s *FsUserStockModel) GetStockList(ctx context.Context, req GetStockListReq) (resp []FsUserStock, total int64, err error) {
|
2023-06-28 06:55:13 +00:00
|
|
|
db := s.db.WithContext(ctx).Model(&FsUserStock{})
|
2023-06-27 09:04:58 +00:00
|
|
|
if req.UserId > 0 {
|
|
|
|
db = db.Where("`user_id` = ?", req.UserId)
|
2023-06-27 06:25:25 +00:00
|
|
|
}
|
2023-06-27 09:04:58 +00:00
|
|
|
if len(req.Ids) > 0 {
|
|
|
|
db = db.Where("`id` in (?)", req.Ids)
|
2023-06-27 06:25:25 +00:00
|
|
|
}
|
2023-06-27 09:04:58 +00:00
|
|
|
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
|
2023-06-27 06:25:25 +00:00
|
|
|
}
|
|
|
|
func (s *FsUserStockModel) FindOne(ctx context.Context, id int64, userId int64, lock ...bool) (resp *FsUserStock, err error) {
|
2023-06-27 09:04:58 +00:00
|
|
|
db := s.db.WithContext(ctx).Model(&FsUserStock{}).Where("`id` = ? and `user_id` = ? and `status` = ?", id, userId, 1)
|
2023-06-27 06:25:25 +00:00
|
|
|
if len(lock) != 0 && lock[0] {
|
|
|
|
db = db.Set("gorm:query_option", "FOR UPDATE")
|
|
|
|
}
|
|
|
|
err = db.First(&resp).Error
|
|
|
|
return resp, err
|
|
|
|
}
|