package tools import ( "ai_scheduler/internal/config" "ai_scheduler/internal/entitys" "encoding/json" "fmt" "gitea.cdlsxd.cn/self-tools/l_request" "github.com/gofiber/websocket/v2" ) type ZltxOrderStatisticsTool struct { config config.ToolConfig } func (z ZltxOrderStatisticsTool) Name() string { return "zltxOrderStatistics" } func (z ZltxOrderStatisticsTool) Description() string { return "通过账号获取订单统计信息" } func (z ZltxOrderStatisticsTool) Definition() entitys.ToolDefinition { return entitys.ToolDefinition{ Type: "function", Function: entitys.FunctionDef{ Name: z.Name(), Description: z.Description(), Parameters: map[string]interface{}{ "type": "object", "properties": map[string]interface{}{ "number": map[string]interface{}{ "type": "string", "description": "账号或分销商号", }, }, "required": []string{"number"}, }, }, } } type ZltxOrderStatisticsRequest struct { Number string `json:"number"` } func (z ZltxOrderStatisticsTool) Execute(channel chan entitys.ResponseData, c *websocket.Conn, args json.RawMessage) error { var req ZltxOrderStatisticsRequest if err := json.Unmarshal(args, &req); err != nil { return err } if req.Number == "" { return fmt.Errorf("number is required") } return z.getZltxOrderStatistics(channel, c, req.Number) } type ZltxOrderStatisticsResponse struct { Code int `json:"code"` Data struct { RecentThreeDays []ZltxOrderStatisticsData `json:"recentThreeDays"` RecentOneMonth []ZltxOrderStatisticsData `json:"recentOneMonth"` } `json:"data"` Error string `json:"error"` } type ZltxOrderStatisticsData struct { Date string `json:"date"` Number string `json:"number"` Success int `json:"success"` Fail int `json:"fail"` Total int `json:"total"` } func (z ZltxOrderStatisticsTool) getZltxOrderStatistics(channel chan entitys.ResponseData, c *websocket.Conn, number string) error { //查询订单详情 var auth string if c != nil { auth = c.Headers("X-Authorization", "") } if len(auth) == 0 { auth = z.config.APIKey } url := fmt.Sprintf("%s%s", z.config.BaseURL, number) req := l_request.Request{ Url: url, Headers: map[string]string{ "Authorization": fmt.Sprintf("Bearer %s", auth), }, Method: "GET", } res, err := req.Send() var resData ZltxOrderStatisticsResponse if err != nil { return err } if err := json.Unmarshal(res.Content, &resData); err != nil { return err } if resData.Code != 200 { return fmt.Errorf("zltx order statistics error: %s", resData.Error) } if c != nil { _ = c.WriteMessage(websocket.TextMessage, res.Content) return nil } else { channel <- entitys.ResponseData{ Done: false, Content: res.Text, Type: entitys.ResponseJson, } } return nil } func NewZltxOrderStatisticsTool(config config.ToolConfig) *ZltxOrderStatisticsTool { return &ZltxOrderStatisticsTool{ config: config, } }