From a556a18d1a293480f0b17998b5e403d15482d2b2 Mon Sep 17 00:00:00 2001 From: wuchao <1272174216@qq.com> Date: Mon, 22 Sep 2025 22:17:23 +0800 Subject: [PATCH] =?UTF-8?q?```=20feat(zltx=5Fproduct):=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=95=86=E5=93=81=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E5=B9=B3=E5=8F=B0=E5=95=86=E5=93=81?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 `ZltxProductData` 结构体字段 `AuthProductInfo` 类型为 []int- 新增 `ZltxProductDataById` 结构体用于支持按 ID 查询商品数据 -优化商品数据解析逻辑,增加对单个商品数据的解析处理- 修改平台商品列表执行方法返回类型为 []map[string]any- 改进错误处理逻辑,使用 continue 而非直接返回 nil - 测试用例中更新 channel 类型为 entitys.ResponseData- 新增 `Test_ProductLog` 测试函数 ``` --- internal/biz/router_test.go | 12 +++- internal/tools/zltx_product.go | 102 ++++++++++++++++++++------------- 2 files changed, 73 insertions(+), 41 deletions(-) diff --git a/internal/biz/router_test.go b/internal/biz/router_test.go index 7e056a5..c97abae 100644 --- a/internal/biz/router_test.go +++ b/internal/biz/router_test.go @@ -33,7 +33,7 @@ type configData struct { func Test_Order(t *testing.T) { routerBiz := in() - ch := make(chan []byte, 5) + ch := make(chan entitys.ResponseData, 5) defer close(ch) err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"order_number":"822895927188791297"}`}, &model.AiTask{Config: `{"tool": "zltxOrderDetail", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`}) select { @@ -47,12 +47,20 @@ func Test_Order(t *testing.T) { func Test_OrderLog(t *testing.T) { routerBiz := in() - ch := make(chan []byte, 5) + ch := make(chan entitys.ResponseData, 5) defer close(ch) err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"order_number":"822979421673758721","serial_number":"822979421979938817"}`}, &model.AiTask{Config: `{"tool": "zltxOrderDirectLog", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`}) t.Log(err) } +func Test_ProductLog(t *testing.T) { + routerBiz := in() + ch := make(chan entitys.ResponseData, 5) + defer close(ch) + err := routerBiz.handleTask(ch, nil, &entitys.Match{Index: "order_diagnosis", Parameters: `{"id":"142","serial_number":"822979421979938817"}`}, &model.AiTask{Config: `{"tool": "zltxProduct", "param": {"type": "object", "optional": [], "required": ["order_number"], "properties": {"order_number": {"type": "string", "description": "订单编号/流水号"}}}}`}) + t.Log(err) +} + func in() *AiRouterBiz { modDir, err := getModuleDir() diff --git a/internal/tools/zltx_product.go b/internal/tools/zltx_product.go index 5f6b266..7f19e40 100644 --- a/internal/tools/zltx_product.go +++ b/internal/tools/zltx_product.go @@ -69,25 +69,31 @@ type ZltxProductResponse struct { Error string `json:"error"` } +type ZltxProductDataById struct { + Code int `json:"code"` + Data ZltxProductData `json:"data"` + Error string `json:"error"` +} + type ZltxProductData struct { - ID int `json:"id"` - OursProductCategoryID int `json:"ours_product_category_id"` - OfficialProductID int `json:"official_product_id"` - Tag string `json:"tag"` - Name string `json:"name"` - Type int `json:"type"` - Discount string `json:"discount"` - Preview string `json:"preview"` - Describe string `json:"describe"` - Price string `json:"price"` - Status int `json:"status"` - CreateTime string `json:"create_time"` - UpdateTime string `json:"update_time"` - Extend string `json:"extend"` - Wight int `json:"wight"` - Property int `json:"property"` - AuthProductInfo []string `json:"auth_product_info"` - AuthProductIds string `json:"auth_product_ids"` + ID int `json:"id"` + OursProductCategoryID int `json:"ours_product_category_id"` + OfficialProductID int `json:"official_product_id"` + Tag string `json:"tag"` + Name string `json:"name"` + Type int `json:"type"` + Discount string `json:"discount"` + Preview string `json:"preview"` + Describe string `json:"describe"` + Price string `json:"price"` + Status int `json:"status"` + CreateTime string `json:"create_time"` + UpdateTime string `json:"update_time"` + Extend string `json:"extend"` + Wight int `json:"wight"` + Property int `json:"property"` + AuthProductInfo []int `json:"auth_product_info"` + AuthProductIds string `json:"auth_product_ids"` Category struct { ID int `json:"id"` Name string `json:"name"` @@ -156,45 +162,52 @@ func (z ZltxProductTool) getZltxProduct(channel chan entitys.ResponseData, c *we } var resp ZltxProductResponse if err := json.Unmarshal(res.Content, &resp); err != nil { - //尝试解析为ZltxProductResponse - var data ZltxProductData - err = json.Unmarshal(res.Content, &data) - if err != nil { - return fmt.Errorf("解析商品数据失败:%w", err) - } - resp.Data.List = []ZltxProductData{data} - resp.Data.DataCount = 1 + return fmt.Errorf("解析商品数据失败:%w", err) } if resp.Code != 200 { return fmt.Errorf("商品查询失败:%s", resp.Error) } + if resp.Data.List == nil || len(resp.Data.List) == 0 { + var respData ZltxProductDataById + if err := json.Unmarshal(res.Content, &respData); err != nil { + return fmt.Errorf("解析商品数据失败:%w", err) + } + resp.Data.List = []ZltxProductData{respData.Data} + resp.Data.DataCount = 1 + } //调用 平台商品列表 if resp.Data.List != nil && len(resp.Data.List) > 0 { - for _, product := range resp.Data.List { - //调用 平台商品列表 - if product.AuthProductIds != "" { - product.PlatformProductList = z.ExecutePlatformProductList(auth, product.AuthProductIds) + for i := range resp.Data.List { + // 调用 平台商品列表 + if resp.Data.List[i].AuthProductIds != "" { + platformProductList := z.ExecutePlatformProductList(auth, resp.Data.List[i].AuthProductIds) + resp.Data.List[i].PlatformProductList = platformProductList } } } + marshal, err := json.Marshal(resp) + if err != nil { + return err + } channel <- entitys.ResponseData{ Done: false, - Content: res.Text, + Content: string(marshal), Type: entitys.ResponseJson, } return nil } -func (z ZltxProductTool) ExecutePlatformProductList(auth string, authProductIds string) []ZltxProductPlatformProductList { +func (z ZltxProductTool) ExecutePlatformProductList(auth string, authProductIds string) []map[string]any { if authProductIds == "" { return nil } + //authProductIds 以逗号分割 authProductIdList := strings.Split(authProductIds, ",") - var result []ZltxProductPlatformProductList + var result []map[string]any + for _, authProductId := range authProductIdList { - //调用 平台商品列表 - var platformProductList ZltxProductPlatformProductList + var platformProductResponse map[string]any req := l_request.Request{ //https://gateway.dev.cdlsxd.cn/zltx_api/admin/platformProduct/{product_id}?id={product_id} Url: fmt.Sprintf("%s/%s?id=%s", z.config.AddURL, authProductId, authProductId), @@ -203,15 +216,26 @@ func (z ZltxProductTool) ExecutePlatformProductList(auth string, authProductIds }, Method: "GET", } + res, err := req.Send() if err != nil { - return nil + // 可以考虑记录日志而不是直接跳过 + continue } - if err := json.Unmarshal(res.Content, &platformProductList); err != nil { - return nil + + if err := json.Unmarshal(res.Content, &platformProductResponse); err != nil { + // 可以考虑记录日志而不是直接跳过 + continue + } + + // 只提取 data 部分 + if data, ok := platformProductResponse["data"]; ok { + if dataMap, ok := data.(map[string]any); ok { + result = append(result, dataMap) + } } - result = append(result, platformProductList) } + return result }