-
Notifications
You must be signed in to change notification settings - Fork 16
/
create_order_logic.go
163 lines (153 loc) · 4.94 KB
/
create_order_logic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package logic
import (
"context"
"database/sql"
"fmt"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/dtm-labs/client/dtmgrpc"
"github.com/luxun9527/gex/app/order/rpc/internal/dao/model"
"github.com/luxun9527/gex/common/errs"
enum "github.com/luxun9527/gex/common/proto/enum"
matchMq "github.com/luxun9527/gex/common/proto/mq/match"
commonWs "github.com/luxun9527/gex/common/proto/ws"
gpush "github.com/luxun9527/gpush/proto"
logger "github.com/luxun9527/zaplog"
"github.com/spf13/cast"
"github.com/yitter/idgenerator-go/idgen"
"google.golang.org/protobuf/proto"
"gorm.io/gorm"
"time"
"github.com/luxun9527/gex/app/order/rpc/internal/svc"
"github.com/luxun9527/gex/app/order/rpc/pb"
commonUtils "github.com/luxun9527/gex/common/utils"
"github.com/zeromicro/go-zero/core/logx"
)
type CreateOrderLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
return &CreateOrderLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 创建订单
func (l *CreateOrderLogic) CreateOrder(in *pb.CreateOrderReq) (*pb.OrderEmpty, error) {
if in.OrderType == enum.OrderType_MO {
if in.Side == enum.Side_Buy {
in.Price = "0"
in.Qty = "0"
} else {
in.Price = "0"
in.Amount = "0"
}
}
//订单Id的规则
//市价单MO
//限价单LO
//买1 卖 2
orderId := "mo"
if in.OrderType == enum.OrderType_LO {
orderId = "lo"
}
orderId = fmt.Sprintf("%v%v%v", orderId, int32(in.Side), idgen.NextId())
or := &model.EntrustOrder{
ID: idgen.NextId(),
OrderID: orderId,
UserID: in.UserId,
SymbolID: in.SymbolId,
SymbolName: in.SymbolName,
Qty: in.Qty,
Price: in.Price,
Side: int32(in.Side),
Amount: in.Amount,
Status: int32(enum.OrderStatus_NewCreated),
OrderType: int32(in.OrderType),
FilledQty: "0",
UnFilledQty: in.Qty,
FilledAvgPrice: "0",
FilledAmount: "0",
UnFilledAmount: in.Amount,
CreatedAt: time.Now().Unix(),
}
barrier, err := dtmgrpc.BarrierFromGrpc(l.ctx)
if err != nil {
logx.Errorw("CreateOrder BarrierFromGrpc db failed", logger.ErrorField(err))
return nil, errs.CastToDtmError(errs.DtmErr)
}
entrustOrder := l.svcCtx.Query.EntrustOrder
db, err := entrustOrder.WithContext(l.ctx).UnderlyingDB().DB()
if err != nil {
logx.Errorw("CreateOrder get UnderlyingDB failed", logger.ErrorField(err))
return nil, errs.CastToDtmError(errs.ExecSqlFailed)
}
toSQL := entrustOrder.WithContext(l.ctx).UnderlyingDB().ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Create(or)
})
//测试执行失败的情况
if err := barrier.CallWithDB(db, func(tx *sql.Tx) error {
if _, err := tx.Exec(toSQL); err != nil {
return err
}
return nil
}); err != nil {
logx.Errorw("CreateOrder CallWithDB failed", logger.ErrorField(err))
return nil, errs.CastToDtmError(errs.Internal)
}
//构建消息发送
msg := &matchMq.MatchReq{Operate: &matchMq.MatchReq_NewOrder{
NewOrder: &matchMq.NewOrderOperate{
OrderId: or.OrderID,
SequenceId: or.ID,
Uid: or.UserID,
Side: in.Side,
Price: in.Price,
Qty: in.Qty,
Amount: in.Amount,
OrderType: in.OrderType,
},
}}
data, _ := proto.Marshal(msg)
if _, err := l.svcCtx.MatchProducer.Send(l.ctx, &pulsar.ProducerMessage{
Payload: data,
}); err != nil {
return nil, errs.CastToDtmError(errs.PulsarErr)
}
//发送ws数据
wsOrder := &commonWs.Order{
Id: cast.ToString(or.ID),
OrderId: cast.ToString(or.OrderID),
SymbolName: or.SymbolName,
Price: commonUtils.PrecCut(or.Price, l.svcCtx.Config.SymbolInfo.QuoteCoinPrec.Load()),
Qty: commonUtils.PrecCut(or.Qty, l.svcCtx.Config.SymbolInfo.BaseCoinPrec.Load()),
Amount: commonUtils.PrecCut(or.Amount, l.svcCtx.Config.SymbolInfo.QuoteCoinPrec.Load()),
Side: int8(or.Side),
Status: int8(enum.OrderStatus_NewCreated),
OrderType: int8(or.OrderType),
FilledAmount: commonUtils.PrecCut(or.FilledAmount, l.svcCtx.Config.SymbolInfo.QuoteCoinPrec.Load()),
FilledQty: commonUtils.PrecCut(or.FilledQty, l.svcCtx.Config.SymbolInfo.BaseCoinPrec.Load()),
FilledAvgPrice: commonUtils.PrecCut(or.FilledAvgPrice, l.svcCtx.Config.SymbolInfo.QuoteCoinPrec.Load()),
Uid: cast.ToString(or.UserID),
CreatedAt: or.CreatedAt,
}
l.pushWsData(wsOrder)
return &pb.OrderEmpty{}, nil
}
func (l *CreateOrderLogic) pushWsData(order *commonWs.Order) {
msg := commonWs.Message[commonWs.Order]{
Topic: commonWs.OrderPrefix.WithParam(l.svcCtx.Config.SymbolInfo.SymbolName),
Payload: *order,
}
logx.Infow("push ws data", logx.Field("msg", msg))
_, err := l.svcCtx.WsClient.PushData(context.Background(), &gpush.Data{
Uid: order.Uid,
Topic: msg.Topic,
Data: msg.ToBytes(),
})
if err != nil {
logx.Errorw("push ws data failed", logger.ErrorField(err))
}
}