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.
211 lines
5.3 KiB
211 lines
5.3 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"
|
||
|
)
|
||
|
|
||
|
// UserMoneyOrder
|
||
|
// @Description:
|
||
|
type UserMoneyOrder struct {
|
||
|
ud UserMoneyOrderRepo
|
||
|
|
||
|
log *log.Helper
|
||
|
}
|
||
|
|
||
|
// UserMoneyOrderRepo
|
||
|
// @Description: 操作订单(订单列表|下单|设置止损止盈|撤单|平仓|一键平仓)
|
||
|
type UserMoneyOrderRepo interface {
|
||
|
CheckToken(ctx context.Context) (int, error)
|
||
|
GetBotMoneyTradeList(ctx context.Context, pageSize, pageCount, userId, status, marketType int64) ([]*models.BotMoneyTrade, int64, error)
|
||
|
CreateBotMoneyTrade(ctx context.Context, userId int64, order structure.MoneyOrder) (string, error)
|
||
|
UpdateBotMoneyStopByOrderId(ctx context.Context, order structure.StopOrder) (bool, error)
|
||
|
UpdateBotMoneyCancelByOrderId(ctx context.Context, orderId string) (bool, error)
|
||
|
UpdateBotMoneyClosingByOrderId(ctx context.Context, orderId string) (bool, error)
|
||
|
UpdateBotMoneyClosingAllByOrderId(ctx context.Context, userId int64) error
|
||
|
MoneyCreateOneClickRedemption(ctx context.Context, userId int64, order structure.MoneyOrder) (string, error)
|
||
|
}
|
||
|
|
||
|
// NewUserMoneyOrderRepo
|
||
|
//
|
||
|
// @Description:
|
||
|
// @param uo
|
||
|
// @param logger
|
||
|
// @return *UserSecondOrder
|
||
|
func NewUserMoneyOrderRepo(uo UserMoneyOrderRepo, logger log.Logger) *UserMoneyOrder {
|
||
|
return &UserMoneyOrder{ud: uo, log: log.NewHelper(logger)}
|
||
|
}
|
||
|
|
||
|
// GetUserIdByToken
|
||
|
//
|
||
|
// @Description: 通过token获取用户Id
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @return int64
|
||
|
// @return error
|
||
|
func (uo *UserMoneyOrder) 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
|
||
|
}
|
||
|
|
||
|
// BotMoneyTradeList
|
||
|
//
|
||
|
// @Description: 订单列表查询
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param pageSize
|
||
|
// @param pageCount
|
||
|
// @param status
|
||
|
// @return []*models.BotMoneyTrade
|
||
|
// @return int64
|
||
|
// @return error
|
||
|
func (uo *UserMoneyOrder) BotMoneyTradeList(ctx context.Context, pageSize, pageCount, status, marketType int64) ([]*models.BotMoneyTrade, int64, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if userId == 0 || err != nil || !token {
|
||
|
return nil, 0, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
contractList, totalCount, err := uo.ud.GetBotMoneyTradeList(ctx, pageSize, pageCount-1, userId, status, marketType)
|
||
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
|
return contractList, totalCount, nil
|
||
|
}
|
||
|
|
||
|
// MoneyPlaceOrder
|
||
|
//
|
||
|
// @Description: 委托订单
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param order
|
||
|
// @return string
|
||
|
// @return error
|
||
|
func (uo *UserMoneyOrder) MoneyPlaceOrder(ctx context.Context, order structure.MoneyOrder) (string, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return flags.SetNull, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
orderId, err := uo.ud.CreateBotMoneyTrade(ctx, userId, order)
|
||
|
if err != nil {
|
||
|
return flags.SetNull, err
|
||
|
}
|
||
|
|
||
|
return orderId, nil
|
||
|
}
|
||
|
|
||
|
// MoneyUpdatePlaceOrder
|
||
|
//
|
||
|
// @Description: 设置止盈止损
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param order
|
||
|
// @return bool
|
||
|
// @return error
|
||
|
func (uo *UserMoneyOrder) MoneyUpdatePlaceOrder(ctx context.Context, order structure.StopOrder) (bool, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return false, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
check, err := uo.ud.UpdateBotMoneyStopByOrderId(ctx, order)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
return check, nil
|
||
|
}
|
||
|
|
||
|
// MoneyCancel
|
||
|
//
|
||
|
// @Description: 撤单
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param orderId
|
||
|
// @return bool
|
||
|
// @return error
|
||
|
func (uo *UserMoneyOrder) MoneyCancel(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.UpdateBotMoneyCancelByOrderId(ctx, orderId)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
return check, nil
|
||
|
}
|
||
|
|
||
|
// MoneyPosition
|
||
|
//
|
||
|
// @Description: 平仓
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param orderId
|
||
|
// @return bool
|
||
|
// @return error
|
||
|
func (uo *UserMoneyOrder) MoneyPosition(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.UpdateBotMoneyClosingByOrderId(ctx, orderId)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
return check, nil
|
||
|
}
|
||
|
|
||
|
// MoneyAllPosition
|
||
|
//
|
||
|
// @Description: 一键平仓
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @return error
|
||
|
func (uo *UserMoneyOrder) MoneyAllPosition(ctx context.Context) error {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
if err = uo.ud.UpdateBotMoneyClosingAllByOrderId(ctx, userId); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// MoneyOneClickRedemption
|
||
|
//
|
||
|
// @Description: 一键兑换
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param order
|
||
|
// @return string
|
||
|
// @return error
|
||
|
func (uo *UserMoneyOrder) MoneyOneClickRedemption(ctx context.Context, order structure.MoneyOrder) (string, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return flags.SetNull, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
orderId, err := uo.ud.MoneyCreateOneClickRedemption(ctx, userId, order)
|
||
|
if err != nil {
|
||
|
return flags.SetNull, err
|
||
|
}
|
||
|
|
||
|
return orderId, nil
|
||
|
}
|