124 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			124 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
package tools
 | 
						|
 | 
						|
import (
 | 
						|
	"ai_scheduler/internal/config"
 | 
						|
	"ai_scheduler/internal/entitys"
 | 
						|
	"context"
 | 
						|
	"encoding/json"
 | 
						|
	"fmt"
 | 
						|
	"sort"
 | 
						|
 | 
						|
	"gitea.cdlsxd.cn/self-tools/l_request"
 | 
						|
)
 | 
						|
 | 
						|
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(ctx context.Context, requireData *entitys.RequireData) error {
 | 
						|
	var req ZltxOrderStatisticsRequest
 | 
						|
	if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
 | 
						|
		return err
 | 
						|
	}
 | 
						|
	if req.Number == "" {
 | 
						|
		return fmt.Errorf("number is required")
 | 
						|
	}
 | 
						|
	return z.getZltxOrderStatistics(req.Number, requireData)
 | 
						|
}
 | 
						|
 | 
						|
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(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 ZltxOrderStatisticsResponse
 | 
						|
	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
 | 
						|
	}
 | 
						|
	requireData.Ch <- entitys.Response{
 | 
						|
		Index:   z.Name(),
 | 
						|
		Content: string(jsonByte),
 | 
						|
		Type:    entitys.ResponseJson,
 | 
						|
	}
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
func NewZltxOrderStatisticsTool(config config.ToolConfig) *ZltxOrderStatisticsTool {
 | 
						|
	return &ZltxOrderStatisticsTool{
 | 
						|
		config: config,
 | 
						|
	}
 | 
						|
}
 |