fix: use snowflake for all order and state log ID generation
This commit is contained in:
@@ -83,7 +83,6 @@ func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderReq) (resp *types.C
|
||||
return nil, err
|
||||
}
|
||||
|
||||
orderID := time.Now().UnixNano()
|
||||
consumerName := strconv.FormatInt(consumerID, 10)
|
||||
playerName := strconv.FormatInt(playerID, 10)
|
||||
shopName := ""
|
||||
@@ -94,7 +93,6 @@ func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderReq) (resp *types.C
|
||||
status := "pending_payment"
|
||||
|
||||
addReq := &orderservice.AddOrdersReq{
|
||||
Id: orderID,
|
||||
ConsumerId: consumerID,
|
||||
ConsumerName: consumerName,
|
||||
PlayerId: playerID,
|
||||
@@ -113,11 +111,12 @@ func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderReq) (resp *types.C
|
||||
addReq.Note = &req.Note
|
||||
}
|
||||
|
||||
if _, err = l.svcCtx.OrderRpc.AddOrders(l.ctx, addReq); err != nil {
|
||||
addResp, err := l.svcCtx.OrderRpc.AddOrders(l.ctx, addReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
created, err := l.svcCtx.OrderRpc.GetOrdersById(l.ctx, &orderservice.GetOrdersByIdReq{Id: orderID})
|
||||
created, err := l.svcCtx.OrderRpc.GetOrdersById(l.ctx, &orderservice.GetOrdersByIdReq{Id: addResp.Id})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -97,7 +97,6 @@ func transitionOrderStatus(ctx context.Context, svcCtx *svc.ServiceContext, orde
|
||||
actorRole = "user"
|
||||
}
|
||||
_, err = svcCtx.OrderRpc.AddOrderStateLogs(ctx, &orderservice.AddOrderStateLogsReq{
|
||||
Id: time.Now().UnixNano(),
|
||||
OrderId: orderID,
|
||||
FromStatus: &fromStatus,
|
||||
ToStatus: toStatus,
|
||||
|
||||
@@ -40,13 +40,11 @@ func (l *ReorderLogic) Reorder(req *types.PathId) (resp *types.CreateOrderResp,
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
newID := time.Now().UnixNano()
|
||||
now := time.Now().Unix()
|
||||
status := "pending_payment"
|
||||
searchText := oldOrder.GetSearchText()
|
||||
|
||||
addReq := &orderservice.AddOrdersReq{
|
||||
Id: newID,
|
||||
ConsumerId: oldOrder.GetConsumerId(),
|
||||
ConsumerName: oldOrder.GetConsumerName(),
|
||||
PlayerId: oldOrder.GetPlayerId(),
|
||||
@@ -68,11 +66,12 @@ func (l *ReorderLogic) Reorder(req *types.PathId) (resp *types.CreateOrderResp,
|
||||
addReq.Note = oldOrder.Note
|
||||
}
|
||||
|
||||
if _, err = l.svcCtx.OrderRpc.AddOrders(l.ctx, addReq); err != nil {
|
||||
addResp, err := l.svcCtx.OrderRpc.AddOrders(l.ctx, addReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
created, err := l.svcCtx.OrderRpc.GetOrdersById(l.ctx, &orderservice.GetOrdersByIdReq{Id: newID})
|
||||
created, err := l.svcCtx.OrderRpc.GetOrdersById(l.ctx, &orderservice.GetOrdersByIdReq{Id: addResp.Id})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"juwan-backend/app/order/rpc/internal/svc"
|
||||
"juwan-backend/app/order/rpc/pb"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -31,8 +32,13 @@ func (l *AddOrderStateLogsLogic) AddOrderStateLogs(in *pb.AddOrderStateLogsReq)
|
||||
return nil, errors.New("order state log is required")
|
||||
}
|
||||
|
||||
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
builder := l.svcCtx.OrderModelsRW.OrderStateLogs.Create().
|
||||
SetID(in.Id).
|
||||
SetID(idResp.Id).
|
||||
SetOrderID(in.OrderId).
|
||||
SetToStatus(in.ToStatus).
|
||||
SetAction(in.Action).
|
||||
|
||||
@@ -30,14 +30,11 @@ func NewAddOrdersLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddOrde
|
||||
|
||||
// -----------------------orders-----------------------
|
||||
func (l *AddOrdersLogic) AddOrders(in *pb.AddOrdersReq) (*pb.AddOrdersResp, error) {
|
||||
orderID := in.Id
|
||||
if orderID <= 0 {
|
||||
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
orderID = idResp.Id
|
||||
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
orderID := idResp.Id
|
||||
|
||||
totalPrice, err := parseDecimal(in.GetTotalPrice())
|
||||
if err != nil || totalPrice.Cmp(decimal.Zero) <= 0 {
|
||||
@@ -101,5 +98,5 @@ func (l *AddOrdersLogic) AddOrders(in *pb.AddOrdersReq) (*pb.AddOrdersResp, erro
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.AddOrdersResp{}, nil
|
||||
return &pb.AddOrdersResp{Id: orderID}, nil
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.9.2
|
||||
// goctl 1.10.1
|
||||
// Source: order.proto
|
||||
|
||||
package server
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.9.2
|
||||
// goctl 1.10.1
|
||||
// Source: order.proto
|
||||
|
||||
package orderservice
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.11
|
||||
// protoc v5.29.6
|
||||
// protoc v6.33.5
|
||||
// source: order.proto
|
||||
|
||||
package pb
|
||||
@@ -416,6 +416,7 @@ func (x *AddOrdersReq) GetUpdatedAt() int64 {
|
||||
|
||||
type AddOrdersResp struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` //id
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -450,6 +451,13 @@ func (*AddOrdersResp) Descriptor() ([]byte, []int) {
|
||||
return file_order_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *AddOrdersResp) GetId() int64 {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type UpdateOrdersReq struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` //id
|
||||
@@ -1932,8 +1940,9 @@ const file_order_proto_rawDesc = "" +
|
||||
"\f_completedAtB\x0e\n" +
|
||||
"\f_cancelledAtB\f\n" +
|
||||
"\n" +
|
||||
"_updatedAt\"\x0f\n" +
|
||||
"\rAddOrdersResp\"\xd3\a\n" +
|
||||
"_updatedAt\"\x1f\n" +
|
||||
"\rAddOrdersResp\x12\x0e\n" +
|
||||
"\x02id\x18\x01 \x01(\x03R\x02id\"\xd3\a\n" +
|
||||
"\x0fUpdateOrdersReq\x12\x0e\n" +
|
||||
"\x02id\x18\x01 \x01(\x03R\x02id\x12#\n" +
|
||||
"\n" +
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.6.1
|
||||
// - protoc v5.29.6
|
||||
// - protoc v6.33.5
|
||||
// source: order.proto
|
||||
|
||||
package pb
|
||||
|
||||
Reference in New Issue
Block a user