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