Files
2026-08-27 16:54:51 +08:00

75 lines
1.8 KiB
Go

package svc
import (
"context"
"fmt"
"lone-services/pkg/utils"
product "lone-services/rpc/product/pb"
"math/rand"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/zeromicro/go-zero/core/logx"
)
func GetOrderSn(orderType string, adminId int64) string {
now := time.Now().Format("20060102150405")
r := rand.New(rand.NewSource(time.Now().UnixNano()))
rnd := r.Intn(90000000) + 10000000
sn := fmt.Sprintf("O%s%s%d%d", now, orderType, adminId, rnd)
return sn
}
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)
}
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
}
return products, utils.Ok
}
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[int64]int64, len(list))
for _, item := range list {
if len(item) >= 2 {
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
}