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.
190 lines
4.8 KiB
190 lines
4.8 KiB
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"
|
|
)
|
|
|
|
// UserContractOrder
|
|
// @Description:
|
|
type UserContractOrder struct {
|
|
ud UserContractOrderRepo
|
|
|
|
log *log.Helper
|
|
}
|
|
|
|
// UserContractOrderRepo
|
|
// @Description: 操作订单(订单列表|下单|设置止损止盈|撤单|平仓|一键平仓)
|
|
type UserContractOrderRepo interface {
|
|
CheckToken(ctx context.Context) (int, error)
|
|
GetBotContractTradeList(ctx context.Context, pageSize, pageCount, userId, status int64) ([]*models.BotContractTrade, int64, error)
|
|
CreateBotContractTrade(ctx context.Context, userId int64, order structure.ContractOrder) (string, error)
|
|
UpdateBotContractStopByOrderId(ctx context.Context, order structure.StopOrder) (bool, error)
|
|
UpdateBotContractCancelByOrderId(ctx context.Context, orderId string) (bool, error)
|
|
UpdateBotContractClosingByOrderId(ctx context.Context, orderId string) (bool, error)
|
|
UpdateBotContractClosingAllByOrderId(ctx context.Context, userId int64) error
|
|
}
|
|
|
|
// NewUserContractOrderRepo
|
|
//
|
|
// @Description:
|
|
// @param uo
|
|
// @param logger
|
|
// @return *UserSecondOrder
|
|
func NewUserContractOrderRepo(uo UserContractOrderRepo, logger log.Logger) *UserContractOrder {
|
|
return &UserContractOrder{ud: uo, log: log.NewHelper(logger)}
|
|
}
|
|
|
|
// GetUserIdByToken
|
|
//
|
|
// @Description: 通过token获取用户Id
|
|
// @receiver uo
|
|
// @param ctx
|
|
// @return int64
|
|
// @return error
|
|
func (uo *UserContractOrder) 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
|
|
}
|
|
|
|
// BotContractTradeList
|
|
//
|
|
// @Description: 订单列表查询
|
|
// @receiver uo
|
|
// @param ctx
|
|
// @param pageSize
|
|
// @param pageCount
|
|
// @param status
|
|
// @return []*models.BotContractTrade
|
|
// @return int64
|
|
// @return error
|
|
func (uo *UserContractOrder) BotContractTradeList(ctx context.Context, pageSize, pageCount, status int64) ([]*models.BotContractTrade, int64, error) {
|
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
|
if userId == 0 || err != nil || !token {
|
|
return nil, 0, flags.ErrTokenMessage
|
|
}
|
|
|
|
contractList, totalCount, err := uo.ud.GetBotContractTradeList(ctx, pageSize, pageCount-1, userId, status)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return contractList, totalCount, nil
|
|
}
|
|
|
|
// ContractPlaceOrder
|
|
//
|
|
// @Description: 委托订单
|
|
// @receiver uo
|
|
// @param ctx
|
|
// @param order
|
|
// @return string
|
|
// @return error
|
|
func (uo *UserContractOrder) ContractPlaceOrder(ctx context.Context, order structure.ContractOrder) (string, error) {
|
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
|
if !token || err != nil || userId == 0 {
|
|
return flags.SetNull, flags.ErrTokenMessage
|
|
}
|
|
|
|
orderId, err := uo.ud.CreateBotContractTrade(ctx, userId, order)
|
|
if err != nil {
|
|
return flags.SetNull, err
|
|
}
|
|
|
|
return orderId, nil
|
|
}
|
|
|
|
// ContractUpdatePlaceOrder
|
|
//
|
|
// @Description: 设置止盈止损
|
|
// @receiver uo
|
|
// @param ctx
|
|
// @param order
|
|
// @return bool
|
|
// @return error
|
|
func (uo *UserContractOrder) ContractUpdatePlaceOrder(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.UpdateBotContractStopByOrderId(ctx, order)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return check, nil
|
|
}
|
|
|
|
// ContractCancel
|
|
//
|
|
// @Description: 撤单
|
|
// @receiver uo
|
|
// @param ctx
|
|
// @param orderId
|
|
// @return bool
|
|
// @return error
|
|
func (uo *UserContractOrder) ContractCancel(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.UpdateBotContractCancelByOrderId(ctx, orderId)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return check, nil
|
|
}
|
|
|
|
// ContractPosition
|
|
//
|
|
// @Description: 平仓
|
|
// @receiver uo
|
|
// @param ctx
|
|
// @param orderId
|
|
// @return bool
|
|
// @return error
|
|
func (uo *UserContractOrder) ContractPosition(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.UpdateBotContractClosingByOrderId(ctx, orderId)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return check, nil
|
|
}
|
|
|
|
// ContractAllPosition
|
|
//
|
|
// @Description: 一键平仓
|
|
// @receiver uo
|
|
// @param ctx
|
|
// @return error
|
|
func (uo *UserContractOrder) ContractAllPosition(ctx context.Context) error {
|
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
|
if !token || err != nil || userId == 0 {
|
|
return flags.ErrTokenMessage
|
|
}
|
|
|
|
if err = uo.ud.UpdateBotContractClosingAllByOrderId(ctx, userId); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|