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.
213 lines
5.3 KiB
213 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"
|
||
|
)
|
||
|
|
||
|
// UserShareHkdOrder
|
||
|
// @Description:
|
||
|
type UserShareHkdOrder struct {
|
||
|
ud UserShareHkdOrderRepo
|
||
|
|
||
|
log *log.Helper
|
||
|
}
|
||
|
|
||
|
// UserShareHkdOrderRepo
|
||
|
// @Description: 订单操作(订单列表|下单|设置止损止盈|撤单|平仓|一键平仓)
|
||
|
type UserShareHkdOrderRepo interface {
|
||
|
CheckToken(ctx context.Context) (int, error)
|
||
|
GetBotUserIsRealByUserId(ctx context.Context, userId int) bool
|
||
|
GetBotStockHkdTradeList(ctx context.Context, pageSize, pageCount, userId, status int64) ([]*models.BotStockHkdTrade, int64, error)
|
||
|
PlacingStockHkdOrders(ctx context.Context, userId int64, order structure.ShareOrder) (string, error)
|
||
|
UpdateBotStockHkdStopByOrderId(ctx context.Context, order structure.StopOrder) (bool, error)
|
||
|
UpdateBotStockHkdCancelByOrderId(ctx context.Context, orderId string, typeStatus int64) (bool, error)
|
||
|
UpdateBotStockHkdClosingByOrderId(ctx context.Context, orderId string, typeStatus int64) (bool, error)
|
||
|
UpdateBotStockHkdAllClosingByOrderId(ctx context.Context, userId int64) error
|
||
|
}
|
||
|
|
||
|
// NewUserShareHkdOrderRepo
|
||
|
//
|
||
|
// @Description:
|
||
|
// @param uo
|
||
|
// @param logger
|
||
|
// @return *UserShareHkdOrder
|
||
|
func NewUserShareHkdOrderRepo(uo UserShareHkdOrderRepo, logger log.Logger) *UserShareHkdOrder {
|
||
|
return &UserShareHkdOrder{ud: uo, log: log.NewHelper(logger)}
|
||
|
}
|
||
|
|
||
|
// GetUserIdByToken
|
||
|
//
|
||
|
// @Description: 通过token获取用户Id
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @return int64
|
||
|
// @return error
|
||
|
func (uo *UserShareHkdOrder) 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
|
||
|
}
|
||
|
|
||
|
// BotStockHkdTradeList
|
||
|
//
|
||
|
// @Description: 订单列表查询
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param pageSize
|
||
|
// @param pageCount
|
||
|
// @param status
|
||
|
// @return []*models.BotStockHkdTrade
|
||
|
// @return int64
|
||
|
// @return error
|
||
|
func (uo *UserShareHkdOrder) BotStockHkdTradeList(ctx context.Context, pageSize, pageCount, status int64) ([]*models.BotStockHkdTrade, int64, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return nil, 0, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
stockList, totalCount, err := uo.ud.GetBotStockHkdTradeList(ctx, pageSize, pageCount-1, userId, status)
|
||
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
|
return stockList, totalCount, nil
|
||
|
}
|
||
|
|
||
|
// ShareHkdPlaceOrder
|
||
|
//
|
||
|
// @Description: 下单
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param order
|
||
|
// @return string
|
||
|
// @return error
|
||
|
func (uo *UserShareHkdOrder) ShareHkdPlaceOrder(ctx context.Context, order structure.ShareOrder) (string, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return flags.SetNull, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
if !uo.ud.GetBotUserIsRealByUserId(ctx, int(userId)) {
|
||
|
return flags.SetNull, flags.ErrIsReal
|
||
|
}
|
||
|
|
||
|
// add BotStockHkdTrade
|
||
|
orderId, err := uo.ud.PlacingStockHkdOrders(ctx, userId, order)
|
||
|
if err != nil {
|
||
|
return flags.SetNull, err
|
||
|
}
|
||
|
|
||
|
return orderId, nil
|
||
|
}
|
||
|
|
||
|
// ShareHkdUpdateOrder
|
||
|
//
|
||
|
// @Description: 设置止盈止损
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param order
|
||
|
// @return bool
|
||
|
// @return error
|
||
|
func (uo *UserShareHkdOrder) ShareHkdUpdateOrder(ctx context.Context, order structure.StopOrder) (bool, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return false, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
if !uo.ud.GetBotUserIsRealByUserId(ctx, int(userId)) {
|
||
|
return false, flags.ErrIsReal
|
||
|
}
|
||
|
|
||
|
check, err := uo.ud.UpdateBotStockHkdStopByOrderId(ctx, order)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
return check, nil
|
||
|
}
|
||
|
|
||
|
// ShareHkdCancel
|
||
|
//
|
||
|
// @Description: 撤单
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param orderId
|
||
|
// @return bool
|
||
|
// @return error
|
||
|
func (uo *UserShareHkdOrder) ShareHkdCancel(ctx context.Context, orderId string) (bool, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return false, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
if !uo.ud.GetBotUserIsRealByUserId(ctx, int(userId)) {
|
||
|
return false, flags.ErrIsReal
|
||
|
}
|
||
|
|
||
|
check, err := uo.ud.UpdateBotStockHkdCancelByOrderId(ctx, orderId, 0)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
return check, nil
|
||
|
}
|
||
|
|
||
|
// ShareHkdPosition
|
||
|
//
|
||
|
// @Description: 平仓
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @param orderId
|
||
|
// @return bool
|
||
|
// @return error
|
||
|
func (uo *UserShareHkdOrder) ShareHkdPosition(ctx context.Context, orderId string) (bool, error) {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return false, flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
if !uo.ud.GetBotUserIsRealByUserId(ctx, int(userId)) {
|
||
|
return false, flags.ErrIsReal
|
||
|
}
|
||
|
|
||
|
check, err := uo.ud.UpdateBotStockHkdClosingByOrderId(ctx, orderId, 0)
|
||
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
return check, nil
|
||
|
}
|
||
|
|
||
|
// ShareHkdAllPosition
|
||
|
//
|
||
|
// @Description: 一键平仓
|
||
|
// @receiver uo
|
||
|
// @param ctx
|
||
|
// @return error
|
||
|
func (uo *UserShareHkdOrder) ShareHkdAllPosition(ctx context.Context) error {
|
||
|
userId, token, err := uo.GetUserIdByToken(ctx)
|
||
|
if !token || err != nil || userId == 0 {
|
||
|
return flags.ErrTokenMessage
|
||
|
}
|
||
|
|
||
|
if !uo.ud.GetBotUserIsRealByUserId(ctx, int(userId)) {
|
||
|
return flags.ErrIsReal
|
||
|
}
|
||
|
|
||
|
if err = uo.ud.UpdateBotStockHkdAllClosingByOrderId(ctx, userId); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|