address add bystoreid api

This commit is contained in:
2026-08-27 16:54:51 +08:00
parent fa412d46ca
commit 433a17140c
15 changed files with 596 additions and 145 deletions
+36 -23
View File
@@ -1,14 +1,15 @@
package svc
import (
"context"
"fmt"
"lone-services/pkg/utils"
"lone-services/services/order/internal/dao"
product "lone-services/rpc/product/pb"
"math/rand"
"strconv"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/zeromicro/go-zero/core/logx"
)
func GetOrderSn(orderType string, adminId int64) string {
@@ -20,42 +21,54 @@ func GetOrderSn(orderType string, adminId int64) string {
return sn
}
func GetCartData(info map[string]int) ([]dao.ProductInfo, utils.Status) {
keys := make([]string, utils.NumberZero, len(info))
func GetCartData(name string, info map[int64]int64) (*product.ItemsByIdsData, utils.Status) {
keys := make([]int64, utils.NumberZero, len(info))
for k := range info {
keys = append(keys, k)
}
//TODO keys 查询产品
var products []dao.ProductInfo
cli, err := GetRpcClient(name)
if err != nil {
logx.Errorf("get rpc client err: %v", err)
return nil, utils.Fail
}
productClient := product.NewProductClient(cli.Conn())
products, err := productClient.ItemsByIds(context.Background(), &product.ItemsByIdsReq{Ids: keys})
if err != nil {
logx.Errorf("get products err: %v", err)
return nil, utils.Fail
}
products = append(products, dao.ProductInfo{
Id: 1, Name: "Product 1",
Price: 100,
Number: 5,
Images: "image1.jpg",
Stock: 10})
products = append(products, dao.ProductInfo{
Id: 2, Name: "Product 2",
Price: 100.33,
Number: 2,
Images: "image1.jpg",
Stock: 1033})
return products, utils.Ok
}
func GetCartInfo(info string) (map[string]int, utils.Status) {
var list [][]int
func GetCartInfo(info string) (map[int64]int64, utils.Status) {
var list [][]int64
err := jsoniter.Unmarshal([]byte(info), &list)
if err != nil {
return nil, utils.ErrorJsonDataError
}
m := make(map[string]int, len(list))
m := make(map[int64]int64, len(list))
for _, item := range list {
if len(item) >= 2 {
m[strconv.Itoa(item[0])] = item[1]
m[item[0]] = item[1]
}
}
return m, utils.Ok
}
func GetOneData(name string, id int64) (*product.Item, utils.Status) {
cli, err := GetRpcClient(name)
if err != nil {
logx.Errorf("get rpc client err: %v", err)
return nil, utils.Fail
}
productClient := product.NewProductClient(cli.Conn())
productInfo, err := productClient.InfoById(context.Background(), &product.InfoByIdReq{Id: id})
if err != nil {
logx.Errorf("get products err: %v", err)
return nil, utils.Fail
}
return productInfo, utils.Ok
}
@@ -78,22 +78,22 @@ type ServiceContext struct {
DB *gorm.DB
Prefix string
ExpressSvcName string //服务名
ProductSvcName string //服务名
}
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
//启动仅读取配置,不建立rpc连接 多个服务就多个
expressSvc := utils.GetConfigString("services.express")
productSvc := utils.GetConfigString("services.product")
if expressSvc == utils.StringEmpty {
logx.Error("config services.express empty")
if productSvc == utils.StringEmpty {
logx.Error("config services.product empty")
}
svcCtx := &ServiceContext{
Config: c,
DB: db,
Prefix: utils.GetConfigString("mysql.prefix"),
ExpressSvcName: expressSvc,
ProductSvcName: productSvc,
}
return svcCtx