feat(zltx_product): 更新商品数据结构并优化平台商品列表处理

- 修改 `ZltxProductData` 结构体字段 `AuthProductInfo` 类型为 []int- 新增 `ZltxProductDataById` 结构体用于支持按 ID 查询商品数据
-优化商品数据解析逻辑,增加对单个商品数据的解析处理- 修改平台商品列表执行方法返回类型为 []map[string]any- 改进错误处理逻辑,使用 continue 而非直接返回 nil
- 测试用例中更新 channel 类型为 entitys.ResponseData- 新增 `Test_ProductLog` 测试函数
```
This commit is contained in:
wuchao 2025-09-22 22:17:23 +08:00
parent f11be8c1c0
commit a556a18d1a
2 changed files with 73 additions and 41 deletions

View File

@ -33,7 +33,7 @@ type configData struct {
func Test_Order(t *testing.T) { func Test_Order(t *testing.T) {
routerBiz := in() routerBiz := in()
ch := make(chan []byte, 5) ch := make(chan entitys.ResponseData, 5)
defer close(ch) 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": "订单编号/流水号"}}}}`}) 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 { select {
@ -47,12 +47,20 @@ func Test_Order(t *testing.T) {
func Test_OrderLog(t *testing.T) { func Test_OrderLog(t *testing.T) {
routerBiz := in() routerBiz := in()
ch := make(chan []byte, 5) ch := make(chan entitys.ResponseData, 5)
defer close(ch) 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": "订单编号/流水号"}}}}`}) 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) 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 { func in() *AiRouterBiz {
modDir, err := getModuleDir() modDir, err := getModuleDir()

View File

@ -69,25 +69,31 @@ type ZltxProductResponse struct {
Error string `json:"error"` Error string `json:"error"`
} }
type ZltxProductDataById struct {
Code int `json:"code"`
Data ZltxProductData `json:"data"`
Error string `json:"error"`
}
type ZltxProductData struct { type ZltxProductData struct {
ID int `json:"id"` ID int `json:"id"`
OursProductCategoryID int `json:"ours_product_category_id"` OursProductCategoryID int `json:"ours_product_category_id"`
OfficialProductID int `json:"official_product_id"` OfficialProductID int `json:"official_product_id"`
Tag string `json:"tag"` Tag string `json:"tag"`
Name string `json:"name"` Name string `json:"name"`
Type int `json:"type"` Type int `json:"type"`
Discount string `json:"discount"` Discount string `json:"discount"`
Preview string `json:"preview"` Preview string `json:"preview"`
Describe string `json:"describe"` Describe string `json:"describe"`
Price string `json:"price"` Price string `json:"price"`
Status int `json:"status"` Status int `json:"status"`
CreateTime string `json:"create_time"` CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"` UpdateTime string `json:"update_time"`
Extend string `json:"extend"` Extend string `json:"extend"`
Wight int `json:"wight"` Wight int `json:"wight"`
Property int `json:"property"` Property int `json:"property"`
AuthProductInfo []string `json:"auth_product_info"` AuthProductInfo []int `json:"auth_product_info"`
AuthProductIds string `json:"auth_product_ids"` AuthProductIds string `json:"auth_product_ids"`
Category struct { Category struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
@ -156,45 +162,52 @@ func (z ZltxProductTool) getZltxProduct(channel chan entitys.ResponseData, c *we
} }
var resp ZltxProductResponse var resp ZltxProductResponse
if err := json.Unmarshal(res.Content, &resp); err != nil { if err := json.Unmarshal(res.Content, &resp); err != nil {
//尝试解析为ZltxProductResponse return fmt.Errorf("解析商品数据失败:%w", err)
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
} }
if resp.Code != 200 { if resp.Code != 200 {
return fmt.Errorf("商品查询失败:%s", resp.Error) 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 { if resp.Data.List != nil && len(resp.Data.List) > 0 {
for _, product := range resp.Data.List { for i := range resp.Data.List {
//调用 平台商品列表 // 调用 平台商品列表
if product.AuthProductIds != "" { if resp.Data.List[i].AuthProductIds != "" {
product.PlatformProductList = z.ExecutePlatformProductList(auth, product.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{ channel <- entitys.ResponseData{
Done: false, Done: false,
Content: res.Text, Content: string(marshal),
Type: entitys.ResponseJson, Type: entitys.ResponseJson,
} }
return nil return nil
} }
func (z ZltxProductTool) ExecutePlatformProductList(auth string, authProductIds string) []ZltxProductPlatformProductList { func (z ZltxProductTool) ExecutePlatformProductList(auth string, authProductIds string) []map[string]any {
if authProductIds == "" { if authProductIds == "" {
return nil return nil
} }
//authProductIds 以逗号分割 //authProductIds 以逗号分割
authProductIdList := strings.Split(authProductIds, ",") authProductIdList := strings.Split(authProductIds, ",")
var result []ZltxProductPlatformProductList var result []map[string]any
for _, authProductId := range authProductIdList { for _, authProductId := range authProductIdList {
//调用 平台商品列表 var platformProductResponse map[string]any
var platformProductList ZltxProductPlatformProductList
req := l_request.Request{ req := l_request.Request{
//https://gateway.dev.cdlsxd.cn/zltx_api/admin/platformProduct/{product_id}?id={product_id} //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), 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", Method: "GET",
} }
res, err := req.Send() res, err := req.Send()
if err != nil { 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 return result
} }