You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
3.7 KiB
151 lines
3.7 KiB
2 months ago
|
package biz
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/log"
|
||
|
|
||
|
"matchmaking-system/internal/biz/structure"
|
||
|
"matchmaking-system/internal/pkg/flags"
|
||
|
|
||
|
models "matchmaking-system/internal/pkg/model"
|
||
|
)
|
||
|
|
||
|
// UserSpotsOrder
|
||
|
// @Description:
|
||
|
type UserSpotsOrder struct {
|
||
|
ud UserSpotsOrderRepo
|
||
|
|
||
|
log *log.Helper
|
||
|
}
|
||
|
|
||
|
// UserContractOrderRepo
|
||
|
// @Description: 操作(订单列表|下单|撤单)
|
||
|
type UserSpotsOrderRepo interface {
|
||
|
CheckToken(ctx context.Context) (int, error)
|
||
|
GetBotDigitalTradeList(pageSize, pageCount, userId, status int64) ([]*models.BotDigitalTrade, int64, error)
|
||
|
GetBotDigitalTradeByOrderId(orderId string) ([]models.BotDigitalTrade, error)
|
||
|
CreateBotDigitalTrade(ctx context.Context, userId int64, order structure.SpotsOrder) (string, error)
|
||
|
CreateOneClickRedemption(ctx context.Context, userId int64, order structure.SpotsOrder) (string, error)
|
||
|
UpdateBotDigitalCancelByOrderId(ctx context.Context, orderId string) (bool, error)
|
||
|
}
|
||
|
|
||
|
// NewUserSpotsOrderRepo
|
||
|
//
|
||
|
// @Description:
|
||
|
// @param uo
|
||
|
// @param logger
|
||
|
// @return *UserSecondOrder
|
||
|
func NewUserSpotsOrderRepo(uo UserSpotsOrderRepo, logger log.Logger) *UserSpotsOrder {
|
||
|
return &UserSpotsOrder{ud: uo, log: log.NewHelper(logger)}
|
||
|
}
|
||
|
|
||
|
// GetUserIdByToken
|
||
|
//
|
||
|
// @Description: 通过token获取用户Id
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @return int64
|
||
|
// @return error
|
||
|
func (uo *UserSpotsOrder) GetUserIdByToken(ctx context.Context) (int64, bool, error) {
|
||
|
userId, err := uo.ud.CheckToken(ctx)
|
||
|
if err != nil {
|
||
|
return 0, false, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
return int64(userId), true, nil
|
||
|
}
|
||
|
|
||
|
// BotDigitalTradeList
|
||
|
//
|
||
|
// @Description: 订单列表查询
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param pageSize
|
||
|
// @param pageCount
|
||
|
// @param status
|
||
|
// @return []*models.BotDigitalTrade
|
||
|
// @return int64
|
||
|
// @return error
|
||
|
func (uo *UserSpotsOrder) BotDigitalTradeList(ctx context.Context, pageSize, pageCount, status int64) ([]*models.BotDigitalTrade, int64, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if userId == 0 || err != nil || !token {
|
||
|
return nil, 0, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
digitalList, totalCount, err := uo.ud.GetBotDigitalTradeList(pageSize, pageCount-1, userId, status)
|
||
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
|
return digitalList, totalCount, nil
|
||
|
}
|
||
|
|
||
|
// SpotsPlaceOrder
|
||
|
//
|
||
|
// @Description: 委托订单
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param order
|
||
|
// @return string
|
||
|
// @return error
|
||
|
func (uo *UserSpotsOrder) SpotsPlaceOrder(ctx context.Context, order structure.SpotsOrder) (string, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return flags.SetNull, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
// create BotDigitalTrade
|
||
|
orderId, err := uo.ud.CreateBotDigitalTrade(ctx, userId, order)
|
||
|
if err != nil {
|
||
|
return flags.SetNull, err
|
||
|
}
|
||
|
|
||
|
return orderId, nil
|
||
|
}
|
||
|
|
||
|
// SpotsCancel
|
||
|
//
|
||
|
// @Description: 撤单
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param orderId
|
||
|
// @return bool
|
||
|
// @return error
|
||
|
func (uo *UserSpotsOrder) SpotsCancel(ctx context.Context, orderId string) (bool, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return false, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
check, err := uo.ud.UpdateBotDigitalCancelByOrderId(ctx, orderId)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
return check, nil
|
||
|
}
|
||
|
|
||
|
// SpotsOneClickRedemption
|
||
|
//
|
||
|
// @Description: 一键兑换
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param order
|
||
|
// @return string
|
||
|
// @return error
|
||
|
func (uo *UserSpotsOrder) SpotsOneClickRedemption(ctx context.Context, order structure.SpotsOrder) (string, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return flags.SetNull, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
// create BotDigitalTrade
|
||
|
orderId, err := uo.ud.CreateOneClickRedemption(ctx, userId, order)
|
||
|
if err != nil {
|
||
|
return flags.SetNull, err
|
||
|
}
|
||
|
|
||
|
return orderId, nil
|
||
|
}
|