ai_scheduler/internal/domain/tools/hyt/supplier_search/client.go

62 lines
1.3 KiB
Go

package supplier_search
import (
"ai_scheduler/internal/pkg/l_request"
"context"
"encoding/json"
"errors"
"fmt"
)
func Call(ctx context.Context, name string) (int, error) {
if name == "" {
return 0, errors.New("supplier name is empty")
}
reqBody := SearchRequest{
Page: 1,
Limit: 1,
Search: SearchCondition{
Name: name,
},
}
apiReq := make(map[string]interface{})
bytes, _ := json.Marshal(reqBody)
_ = json.Unmarshal(bytes, &apiReq)
req := l_request.Request{
Method: "Post",
Url: "https://gateway.dev.cdlsxd.cn/goods-admin/api/v1/supplier/list",
Json: apiReq,
Headers: map[string]string{
"User-Agent": "Apifox/1.0.0 (https://apifox.com)",
"Content-Type": "application/json",
},
}
res, err := req.Send()
if err != nil {
return 0, err
}
if res.StatusCode != 200 {
return 0, fmt.Errorf("supplier search failed with status code: %d", res.StatusCode)
}
var resData SearchResponse
if err := json.Unmarshal([]byte(res.Text), &resData); err != nil {
return 0, fmt.Errorf("failed to parse supplier search response: %w", err)
}
if resData.Code != 200 {
return 0, fmt.Errorf("supplier search business error: %s", resData.Msg)
}
if len(resData.Data.List) == 0 {
return 0, fmt.Errorf("supplier not found: %s", name)
}
return resData.Data.List[0].ID, nil
}