package tools import ( "ai_scheduler/internal/config" "ai_scheduler/internal/entitys" "context" "encoding/json" "fmt" "sort" "gitea.cdlsxd.cn/self-tools/l_request" ) type ZltxOrderAfterSaleTool struct { config config.ToolConfig } func (z ZltxOrderAfterSaleTool) Name() string { return "zltxOrderAfterSale" } func (z ZltxOrderAfterSaleTool) Description() string { return "查询直连天下订单售后信息" } func (z ZltxOrderAfterSaleTool) 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 ZltxOrderAfterSaleRequest struct { Number string `json:"number"` } func (z ZltxOrderAfterSaleTool) Execute(ctx context.Context, requireData *entitys.RequireData) error { var req ZltxOrderAfterSaleRequest if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil { return err } if req.Number == "" { return fmt.Errorf("number is required") } return z.getZltxOrderAfterSale(req.Number, requireData) } type ZltxOrderAfterSaleResponse struct { Code int `json:"code"` Data struct { List []*ZltxOrderAfterSaleData `json:"list"` } `json:"data"` Error string `json:"error"` } type ZltxOrderAfterSaleData struct { // 处理方式 价款/扣款 HandleMethod string `json:"handle_method"` // 售后金额 AfterSaleAmount float64 `json:"after_sale_amount"` // 售后原因 AfterSaleReason string `json:"after_sale_reason"` // 原单信息 OriginalOrderInfo *OrderInfo } type OrderInfo struct { SerialNumber string `json:"serial_number"` // 订单流水号 SupplierName string `json:"supplier_name"` // 供应商名称 SigningBody string `json:"signing_body"` // 签约主体 ProductName string `json:"product_name"` // 商品名称 UpstreamPrice float64 `json:"upstream_price"` // 上游价格 RechargeAccount string `json:"recharge_account"` // 充值账号 RechargeStatus string `json:"recharge_status"` // 充值状态 } func (z ZltxOrderAfterSaleTool) getZltxOrderAfterSale(number string, requireData *entitys.RequireData) error { //查询订单详情 url := fmt.Sprintf("%s%s", z.config.BaseURL, number) req := l_request.Request{ Url: url, Headers: map[string]string{ "Authorization": fmt.Sprintf("Bearer %s", requireData.Auth), }, Method: "GET", } res, err := req.Send() var resData ZltxOrderAfterSaleResponse if err != nil { return err } if err := json.Unmarshal(res.Content, &resData); err != nil { return err } if resData.Code != 200 { return fmt.Errorf("为获取到数据,请检查权限: %s", string(res.Content)) } //按照日期排序 sort.Slice(resData.Data.RecentThreeDays, func(i, j int) bool { return resData.Data.RecentThreeDays[i].Date < resData.Data.RecentThreeDays[j].Date }) sort.Slice(resData.Data.RecentOneMonth, func(i, j int) bool { return resData.Data.RecentOneMonth[i].Date < resData.Data.RecentOneMonth[j].Date }) jsonByte, err := json.Marshal(resData) if err != nil { return err } entitys.ResJson(requireData.Ch, z.Name(), string(jsonByte)) return nil } func NewZltxOrderAfterSaleTool(config config.ToolConfig) *ZltxOrderAfterSaleTool { return &ZltxOrderAfterSaleTool{ config: config, } }