diff --git a/pkg/utils/tools.go b/pkg/utils/tools.go index e796816..0acd02a 100644 --- a/pkg/utils/tools.go +++ b/pkg/utils/tools.go @@ -621,3 +621,16 @@ func UniqueStr(arr []string) []string { } return res } + +// Float 泛型约束,只允许 float32 / float64 +type Float interface { + ~float32 | ~float64 +} + +// RoundFloat 四舍五入保留n位小数,泛型版本 +func RoundFloat[T Float](val T) T { + shift := math.Pow10(NumberTwo) + f := float64(val) + res := math.Round(f*shift) / shift + return T(res) +} diff --git a/rpc/task/pb/task.pb.go b/rpc/task/pb/task.pb.go new file mode 100644 index 0000000..7785f5f --- /dev/null +++ b/rpc/task/pb/task.pb.go @@ -0,0 +1,418 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.36.11 +// protoc v4.25.8 +// source: task/task.proto + +package task + +import ( + _ "google.golang.org/genproto/googleapis/api/annotations" + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" + unsafe "unsafe" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Response struct { + state protoimpl.MessageState `protogen:"open.v1"` + Code int32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"` + Msg string `protobuf:"bytes,2,opt,name=msg,proto3" json:"msg,omitempty"` + Data string `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *Response) Reset() { + *x = Response{} + mi := &file_task_task_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *Response) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Response) ProtoMessage() {} + +func (x *Response) ProtoReflect() protoreflect.Message { + mi := &file_task_task_proto_msgTypes[0] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Response.ProtoReflect.Descriptor instead. +func (*Response) Descriptor() ([]byte, []int) { + return file_task_task_proto_rawDescGZIP(), []int{0} +} + +func (x *Response) GetCode() int32 { + if x != nil { + return x.Code + } + return 0 +} + +func (x *Response) GetMsg() string { + if x != nil { + return x.Msg + } + return "" +} + +func (x *Response) GetData() string { + if x != nil { + return x.Data + } + return "" +} + +type EmtpyRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *EmtpyRequest) Reset() { + *x = EmtpyRequest{} + mi := &file_task_task_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *EmtpyRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*EmtpyRequest) ProtoMessage() {} + +func (x *EmtpyRequest) ProtoReflect() protoreflect.Message { + mi := &file_task_task_proto_msgTypes[1] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use EmtpyRequest.ProtoReflect.Descriptor instead. +func (*EmtpyRequest) Descriptor() ([]byte, []int) { + return file_task_task_proto_rawDescGZIP(), []int{1} +} + +type EditRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Type int32 `protobuf:"varint,2,opt,name=type,proto3" json:"type,omitempty"` + Time int32 `protobuf:"varint,3,opt,name=time,proto3" json:"time,omitempty"` + Province int64 `protobuf:"varint,4,opt,name=province,proto3" json:"province,omitempty"` + ProvinceName string `protobuf:"bytes,5,opt,name=province_name,json=provinceName,proto3" json:"province_name,omitempty"` + Task string `protobuf:"bytes,6,opt,name=task,proto3" json:"task,omitempty"` + Award string `protobuf:"bytes,7,opt,name=award,proto3" json:"award,omitempty"` + ProductId int64 `protobuf:"varint,8,opt,name=product_id,json=productId,proto3" json:"product_id,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *EditRequest) Reset() { + *x = EditRequest{} + mi := &file_task_task_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *EditRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*EditRequest) ProtoMessage() {} + +func (x *EditRequest) ProtoReflect() protoreflect.Message { + mi := &file_task_task_proto_msgTypes[2] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use EditRequest.ProtoReflect.Descriptor instead. +func (*EditRequest) Descriptor() ([]byte, []int) { + return file_task_task_proto_rawDescGZIP(), []int{2} +} + +func (x *EditRequest) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *EditRequest) GetType() int32 { + if x != nil { + return x.Type + } + return 0 +} + +func (x *EditRequest) GetTime() int32 { + if x != nil { + return x.Time + } + return 0 +} + +func (x *EditRequest) GetProvince() int64 { + if x != nil { + return x.Province + } + return 0 +} + +func (x *EditRequest) GetProvinceName() string { + if x != nil { + return x.ProvinceName + } + return "" +} + +func (x *EditRequest) GetTask() string { + if x != nil { + return x.Task + } + return "" +} + +func (x *EditRequest) GetAward() string { + if x != nil { + return x.Award + } + return "" +} + +func (x *EditRequest) GetProductId() int64 { + if x != nil { + return x.ProductId + } + return 0 +} + +type IdRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *IdRequest) Reset() { + *x = IdRequest{} + mi := &file_task_task_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *IdRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*IdRequest) ProtoMessage() {} + +func (x *IdRequest) ProtoReflect() protoreflect.Message { + mi := &file_task_task_proto_msgTypes[3] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use IdRequest.ProtoReflect.Descriptor instead. +func (*IdRequest) Descriptor() ([]byte, []int) { + return file_task_task_proto_rawDescGZIP(), []int{3} +} + +func (x *IdRequest) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +type StatusRequest struct { + state protoimpl.MessageState `protogen:"open.v1"` + Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Status int32 `protobuf:"varint,2,opt,name=status,proto3" json:"status,omitempty"` + Reason string `protobuf:"bytes,3,opt,name=reason,proto3" json:"reason,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *StatusRequest) Reset() { + *x = StatusRequest{} + mi := &file_task_task_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *StatusRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*StatusRequest) ProtoMessage() {} + +func (x *StatusRequest) ProtoReflect() protoreflect.Message { + mi := &file_task_task_proto_msgTypes[4] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use StatusRequest.ProtoReflect.Descriptor instead. +func (*StatusRequest) Descriptor() ([]byte, []int) { + return file_task_task_proto_rawDescGZIP(), []int{4} +} + +func (x *StatusRequest) GetId() int64 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *StatusRequest) GetStatus() int32 { + if x != nil { + return x.Status + } + return 0 +} + +func (x *StatusRequest) GetReason() string { + if x != nil { + return x.Reason + } + return "" +} + +var File_task_task_proto protoreflect.FileDescriptor + +const file_task_task_proto_rawDesc = "" + + "\n" + + "\x0ftask/task.proto\x12\x04task\x1a\x1cgoogle/api/annotations.proto\"D\n" + + "\bResponse\x12\x12\n" + + "\x04code\x18\x01 \x01(\x05R\x04code\x12\x10\n" + + "\x03msg\x18\x02 \x01(\tR\x03msg\x12\x12\n" + + "\x04data\x18\x03 \x01(\tR\x04data\"\x0e\n" + + "\fEmtpyRequest\"\xcf\x01\n" + + "\vEditRequest\x12\x0e\n" + + "\x02id\x18\x01 \x01(\x03R\x02id\x12\x12\n" + + "\x04type\x18\x02 \x01(\x05R\x04type\x12\x12\n" + + "\x04time\x18\x03 \x01(\x05R\x04time\x12\x1a\n" + + "\bprovince\x18\x04 \x01(\x03R\bprovince\x12#\n" + + "\rprovince_name\x18\x05 \x01(\tR\fprovinceName\x12\x12\n" + + "\x04task\x18\x06 \x01(\tR\x04task\x12\x14\n" + + "\x05award\x18\a \x01(\tR\x05award\x12\x1d\n" + + "\n" + + "product_id\x18\b \x01(\x03R\tproductId\"\x1b\n" + + "\tIdRequest\x12\x0e\n" + + "\x02id\x18\x01 \x01(\x03R\x02id\"O\n" + + "\rStatusRequest\x12\x0e\n" + + "\x02id\x18\x01 \x01(\x03R\x02id\x12\x16\n" + + "\x06status\x18\x02 \x01(\x05R\x06status\x12\x16\n" + + "\x06reason\x18\x03 \x01(\tR\x06reason2\x88\x03\n" + + "\x04Task\x12L\n" + + "\x05Items\x12\x12.task.EmtpyRequest\x1a\x0e.task.Response\"\x1f\x82\xd3\xe4\x93\x02\x19:\x01*\x12\x14/admin/v3/task/items\x12G\n" + + "\x04Info\x12\x0f.task.IdRequest\x1a\x0e.task.Response\"\x1e\x82\xd3\xe4\x93\x02\x18:\x01*\x12\x13/admin/v3/task/info\x12M\n" + + "\x06Create\x12\x11.task.EditRequest\x1a\x0e.task.Response\" \x82\xd3\xe4\x93\x02\x1a:\x01*\"\x15/admin/v3/task/create\x12I\n" + + "\x04Edit\x12\x11.task.EditRequest\x1a\x0e.task.Response\"\x1e\x82\xd3\xe4\x93\x02\x18:\x01*\"\x13/admin/v3/task/edit\x12O\n" + + "\x06Status\x12\x13.task.StatusRequest\x1a\x0e.task.Response\" \x82\xd3\xe4\x93\x02\x1a:\x01*\"\x15/admin/v3/task/statusB\bZ\x06./taskb\x06proto3" + +var ( + file_task_task_proto_rawDescOnce sync.Once + file_task_task_proto_rawDescData []byte +) + +func file_task_task_proto_rawDescGZIP() []byte { + file_task_task_proto_rawDescOnce.Do(func() { + file_task_task_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_task_task_proto_rawDesc), len(file_task_task_proto_rawDesc))) + }) + return file_task_task_proto_rawDescData +} + +var file_task_task_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_task_task_proto_goTypes = []any{ + (*Response)(nil), // 0: task.Response + (*EmtpyRequest)(nil), // 1: task.EmtpyRequest + (*EditRequest)(nil), // 2: task.EditRequest + (*IdRequest)(nil), // 3: task.IdRequest + (*StatusRequest)(nil), // 4: task.StatusRequest +} +var file_task_task_proto_depIdxs = []int32{ + 1, // 0: task.Task.Items:input_type -> task.EmtpyRequest + 3, // 1: task.Task.Info:input_type -> task.IdRequest + 2, // 2: task.Task.Create:input_type -> task.EditRequest + 2, // 3: task.Task.Edit:input_type -> task.EditRequest + 4, // 4: task.Task.Status:input_type -> task.StatusRequest + 0, // 5: task.Task.Items:output_type -> task.Response + 0, // 6: task.Task.Info:output_type -> task.Response + 0, // 7: task.Task.Create:output_type -> task.Response + 0, // 8: task.Task.Edit:output_type -> task.Response + 0, // 9: task.Task.Status:output_type -> task.Response + 5, // [5:10] is the sub-list for method output_type + 0, // [0:5] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_task_task_proto_init() } +func file_task_task_proto_init() { + if File_task_task_proto != nil { + return + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: unsafe.Slice(unsafe.StringData(file_task_task_proto_rawDesc), len(file_task_task_proto_rawDesc)), + NumEnums: 0, + NumMessages: 5, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_task_task_proto_goTypes, + DependencyIndexes: file_task_task_proto_depIdxs, + MessageInfos: file_task_task_proto_msgTypes, + }.Build() + File_task_task_proto = out.File + file_task_task_proto_goTypes = nil + file_task_task_proto_depIdxs = nil +} diff --git a/rpc/task/pb/task_grpc.pb.go b/rpc/task/pb/task_grpc.pb.go new file mode 100644 index 0000000..04d1706 --- /dev/null +++ b/rpc/task/pb/task_grpc.pb.go @@ -0,0 +1,273 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.6.2 +// - protoc v4.25.8 +// source: task/task.proto + +package task + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + Task_Items_FullMethodName = "/task.Task/Items" + Task_Info_FullMethodName = "/task.Task/Info" + Task_Create_FullMethodName = "/task.Task/Create" + Task_Edit_FullMethodName = "/task.Task/Edit" + Task_Status_FullMethodName = "/task.Task/Status" +) + +// TaskClient is the client API for Task service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type TaskClient interface { + Items(ctx context.Context, in *EmtpyRequest, opts ...grpc.CallOption) (*Response, error) + Info(ctx context.Context, in *IdRequest, opts ...grpc.CallOption) (*Response, error) + Create(ctx context.Context, in *EditRequest, opts ...grpc.CallOption) (*Response, error) + Edit(ctx context.Context, in *EditRequest, opts ...grpc.CallOption) (*Response, error) + Status(ctx context.Context, in *StatusRequest, opts ...grpc.CallOption) (*Response, error) +} + +type taskClient struct { + cc grpc.ClientConnInterface +} + +func NewTaskClient(cc grpc.ClientConnInterface) TaskClient { + return &taskClient{cc} +} + +func (c *taskClient) Items(ctx context.Context, in *EmtpyRequest, opts ...grpc.CallOption) (*Response, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(Response) + err := c.cc.Invoke(ctx, Task_Items_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *taskClient) Info(ctx context.Context, in *IdRequest, opts ...grpc.CallOption) (*Response, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(Response) + err := c.cc.Invoke(ctx, Task_Info_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *taskClient) Create(ctx context.Context, in *EditRequest, opts ...grpc.CallOption) (*Response, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(Response) + err := c.cc.Invoke(ctx, Task_Create_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *taskClient) Edit(ctx context.Context, in *EditRequest, opts ...grpc.CallOption) (*Response, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(Response) + err := c.cc.Invoke(ctx, Task_Edit_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *taskClient) Status(ctx context.Context, in *StatusRequest, opts ...grpc.CallOption) (*Response, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(Response) + err := c.cc.Invoke(ctx, Task_Status_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// TaskServer is the server API for Task service. +// All implementations must embed UnimplementedTaskServer +// for forward compatibility. +type TaskServer interface { + Items(context.Context, *EmtpyRequest) (*Response, error) + Info(context.Context, *IdRequest) (*Response, error) + Create(context.Context, *EditRequest) (*Response, error) + Edit(context.Context, *EditRequest) (*Response, error) + Status(context.Context, *StatusRequest) (*Response, error) + mustEmbedUnimplementedTaskServer() +} + +// UnimplementedTaskServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedTaskServer struct{} + +func (UnimplementedTaskServer) Items(context.Context, *EmtpyRequest) (*Response, error) { + return nil, status.Error(codes.Unimplemented, "method Items not implemented") +} +func (UnimplementedTaskServer) Info(context.Context, *IdRequest) (*Response, error) { + return nil, status.Error(codes.Unimplemented, "method Info not implemented") +} +func (UnimplementedTaskServer) Create(context.Context, *EditRequest) (*Response, error) { + return nil, status.Error(codes.Unimplemented, "method Create not implemented") +} +func (UnimplementedTaskServer) Edit(context.Context, *EditRequest) (*Response, error) { + return nil, status.Error(codes.Unimplemented, "method Edit not implemented") +} +func (UnimplementedTaskServer) Status(context.Context, *StatusRequest) (*Response, error) { + return nil, status.Error(codes.Unimplemented, "method Status not implemented") +} +func (UnimplementedTaskServer) mustEmbedUnimplementedTaskServer() {} +func (UnimplementedTaskServer) testEmbeddedByValue() {} + +// UnsafeTaskServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to TaskServer will +// result in compilation errors. +type UnsafeTaskServer interface { + mustEmbedUnimplementedTaskServer() +} + +func RegisterTaskServer(s grpc.ServiceRegistrar, srv TaskServer) { + // If the following call panics, it indicates UnimplementedTaskServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&Task_ServiceDesc, srv) +} + +func _Task_Items_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(EmtpyRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(TaskServer).Items(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Task_Items_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(TaskServer).Items(ctx, req.(*EmtpyRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Task_Info_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(IdRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(TaskServer).Info(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Task_Info_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(TaskServer).Info(ctx, req.(*IdRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Task_Create_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(EditRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(TaskServer).Create(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Task_Create_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(TaskServer).Create(ctx, req.(*EditRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Task_Edit_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(EditRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(TaskServer).Edit(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Task_Edit_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(TaskServer).Edit(ctx, req.(*EditRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Task_Status_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(StatusRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(TaskServer).Status(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Task_Status_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(TaskServer).Status(ctx, req.(*StatusRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// Task_ServiceDesc is the grpc.ServiceDesc for Task service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Task_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "task.Task", + HandlerType: (*TaskServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Items", + Handler: _Task_Items_Handler, + }, + { + MethodName: "Info", + Handler: _Task_Info_Handler, + }, + { + MethodName: "Create", + Handler: _Task_Create_Handler, + }, + { + MethodName: "Edit", + Handler: _Task_Edit_Handler, + }, + { + MethodName: "Status", + Handler: _Task_Status_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "task/task.proto", +} diff --git a/rpc/task/task.pb b/rpc/task/task.pb new file mode 100644 index 0000000..56dff6b Binary files /dev/null and b/rpc/task/task.pb differ diff --git a/rpc/task/task.proto b/rpc/task/task.proto new file mode 100644 index 0000000..157bec1 --- /dev/null +++ b/rpc/task/task.proto @@ -0,0 +1,69 @@ +syntax = "proto3"; + +package task; +option go_package="./task"; + +import "google/api/annotations.proto"; + +message Response { + int32 code = 1; + string msg = 2; + string data = 3; +} + +message EmtpyRequest { +} + +message EditRequest { + int64 id = 1; + int32 type =2; + int32 time =3; + int64 province =4; + string province_name =5; + string task =6; + string award =7; + int64 product_id =8; +} + +message IdRequest { + int64 id = 1; +} + +message StatusRequest { + int64 id = 1; + int32 status = 2; + string reason = 3; +} + +service Task { + rpc Items(EmtpyRequest) returns(Response){ + option (google.api.http) = { + get: "/admin/v3/task/items" + body: "*" + }; + }; + rpc Info(IdRequest) returns(Response){ + option (google.api.http) = { + get: "/admin/v3/task/info" + body: "*" + }; + }; + rpc Create(EditRequest) returns(Response){ + option (google.api.http) = { + post: "/admin/v3/task/create" + body: "*" + }; + }; + rpc Edit(EditRequest) returns(Response){ + option (google.api.http) = { + post: "/admin/v3/task/edit" + body: "*" + }; + }; + rpc Status(StatusRequest) returns(Response){ + option (google.api.http) = { + post: "/admin/v3/task/status" + body: "*" + }; + }; +} diff --git a/services/order/internal/dao/cart.go b/services/order/internal/dao/cart.go index f3ee22a..12bd3cd 100644 --- a/services/order/internal/dao/cart.go +++ b/services/order/internal/dao/cart.go @@ -25,3 +25,14 @@ type ProductInfo struct { Images string `json:"images" gorm:"images"` Stock int `json:"stock" gorm:"stock"` } + +type CartRet struct { + ProductId int64 `json:"product_id" gorm:"product_id"` + ProductName string `json:"product_name" gorm:"product_name"` + Images []string `json:"images" gorm:"images"` + Number int `json:"number" gorm:"number"` + Price float64 `json:"price" gorm:"price"` + StorePrice float64 `json:"store_price" gorm:"store_price"` + SalePrice float64 `json:"sale_price" gorm:"sale_price"` + Stock uint32 `json:"stock" gorm:"stock"` +} diff --git a/services/order/internal/logic/webCartLogic.go b/services/order/internal/logic/webCartLogic.go index 0bdacd3..29bfe3d 100644 --- a/services/order/internal/logic/webCartLogic.go +++ b/services/order/internal/logic/webCartLogic.go @@ -4,6 +4,7 @@ import ( "context" "lone-services/pkg/modelbase" "lone-services/pkg/utils" + product "lone-services/rpc/product/pb" "lone-services/services/order/internal/dao" "lone-services/services/order/internal/model" "lone-services/services/order/validator" @@ -63,6 +64,55 @@ func (l *WebCartLogic) WebCart(in *order.CartRequest) (*order.Response, error) { l.Logger.Error(err) return l.fail(utils.Fail) } + var ret []dao.CartRet + if len(info.Content) < utils.NumberOne { + return l.ok(ret) + } - return l.ok(info) + var data [][]int + ok := utils.JsonStringToStruct(info.Content, &data) + if !ok { + return l.ok(ret) + } + + var ids []int64 + idsName := make(map[int64]int) + for _, v := range data { + ids = append(ids, int64(v[0])) + idsName[int64(v[0])] = v[1] + } + + cli, err := svc.GetRpcClient(l.svcCtx.ProductSvcName) + if err != nil { + logx.Errorf("get rpc client err: %v", err) + return l.fail(utils.ErrorInternalServer) + } + productClient := product.NewProductClient(cli.Conn()) + productItems, err := productClient.ItemsByIds(context.Background(), &product.ItemsByIdsReq{Ids: ids}) + if err != nil { + logx.Errorf("get products err: %v", err) + return l.fail(utils.ErrorInternalServer) + } + + logx.Errorf("get products: %v", productItems.Items) + logx.Errorf("get ids: %v", idsName) + + for _, productItem := range productItems.Items { + num := 0 + if v, ok := idsName[productItem.Id]; ok { + num = v + } + ret = append(ret, dao.CartRet{ + ProductId: productItem.Id, + ProductName: productItem.Name, + Price: utils.RoundFloat(productItem.Price), + SalePrice: utils.RoundFloat(productItem.SalePrice), + StorePrice: utils.RoundFloat(productItem.StorePrice), + Stock: productItem.Number, + Images: productItem.Images, + Number: num, + }) + } + + return l.ok(ret) }