feat: 增加多账号并发

This commit is contained in:
fuzhongyun 2025-11-26 16:25:07 +08:00
parent 5264f78fb0
commit 2a312dc205
2 changed files with 189 additions and 73 deletions

View File

@ -7,6 +7,7 @@ import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
)
@ -36,7 +37,7 @@ func (t *OrderAfterSaleResellerTool) Definition() entitys.ToolDefinition {
type OrderAfterSaleResellerRequest struct {
OrderNumber []string `json:"orderNumber"` // 订单号
Account string `json:"account"` // 充值账号
Account []string `json:"account"` // 充值账号
SerialCreateTime string `json:"serialCreateTime"` // 流水创建时间
AfterType int `json:"afterType"` // 处理方式 1.退款 2.扣款
AfterSalesPrice float64 `json:"afterSalesPrice"` // 售后金额
@ -107,7 +108,7 @@ func (t *OrderAfterSaleResellerTool) Execute(ctx context.Context, requireData *e
if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
return err
}
if len(req.OrderNumber) == 0 && req.Account == "" {
if len(req.OrderNumber) == 0 && len(req.Account) == 0 {
return fmt.Errorf("订单号 和 充值账号 不能同时为空")
}
@ -125,41 +126,105 @@ func (t *OrderAfterSaleResellerTool) checkOrderAfterSaleReseller(toolReq OrderAf
serialStartTime := serialCreateTime.Unix() - 10*60
serialEndTime := serialCreateTime.Unix() + 10*60
// 账号数量超过10直接截断
if len(toolReq.Account) > 10 {
entitys.ResLog(requireData.Ch, t.Name(), "账号数量超过10已被截断")
toolReq.Account = toolReq.Account[:10]
}
headers := map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
}
// 最终输出
var orderList []*OrderAfterSaleResellerData
// 多订单号
if len(toolReq.OrderNumber) > 0 {
body := map[string]any{
"orderNumber": toolReq.OrderNumber, // 订单号
}
orderList, err = t.getAfterSaleResellerList(headers, body, toolReq)
if err != nil {
return err
}
} else if len(toolReq.Account) > 0 {
// 多充值账号并发
orderListChan := make(chan []*OrderAfterSaleResellerData, len(toolReq.Account))
waitGroup := sync.WaitGroup{}
// 并发请求
for _, account := range toolReq.Account {
waitGroup.Add(1)
go func(account string) {
defer waitGroup.Done()
body := map[string]any{
"account": account, // 充值账号
"serialStartTime": serialStartTime, // 流水创建时间
"serialEndTime": serialEndTime, // 流水结束时间
}
orderListIn, errIn := t.getAfterSaleResellerList(headers, body, toolReq)
if errIn != nil {
return
}
orderListChan <- orderListIn
}(account)
}
waitGroup.Wait()
close(orderListChan)
// 合并结果
for orderListIn := range orderListChan {
orderList = append(orderList, orderListIn...)
}
} else {
return fmt.Errorf("订单号 和 充值账号 不能同时为空")
}
toolResp := OrderAfterSaleResellerResponse{
Code: 0,
Msg: "Success",
Data: orderList,
}
var jsonByte []byte
jsonByte, err = json.Marshal(toolResp)
if err != nil {
return err
}
entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
return nil
}
func (t *OrderAfterSaleResellerTool) getAfterSaleResellerList(headers map[string]string, body map[string]any, originInput OrderAfterSaleResellerRequest) ([]*OrderAfterSaleResellerData, error) {
req := l_request.Request{
Url: t.config.BaseURL,
Headers: map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
},
Method: "POST",
Json: map[string]any{
"orderNumber": toolReq.OrderNumber, // 流水号
"account": toolReq.Account, // 充值账号
"serialStartTime": serialStartTime, // 流水创建时间
"serialEndTime": serialEndTime, // 流水结束时间
},
Url: t.config.BaseURL,
Headers: headers,
Method: "POST",
Json: body,
}
res, err := req.Send()
if err != nil {
return err
return nil, err
}
// 解析响应
var resp OrderAfterSaleResellerApiResponse
if err = json.Unmarshal(res.Content, &resp); err != nil {
return err
return nil, err
}
if resp.Code != 0 {
return fmt.Errorf("after sale supplier failed: %s", resp.Msg)
return nil, fmt.Errorf("after sale reseller failed: %s", resp.Msg)
}
toolResp := OrderAfterSaleResellerResponse{
Code: resp.Code,
Msg: resp.Msg,
Data: make([]*OrderAfterSaleResellerData, 0, len(resp.Data.Data)),
}
orderList := make([]*OrderAfterSaleResellerData, 0, len(resp.Data.Data))
// 转换数据
for _, item := range resp.Data.Data {
toolResp.Data = append(toolResp.Data, &OrderAfterSaleResellerData{
orderList = append(orderList, &OrderAfterSaleResellerData{
OrderType: item.OrderType,
OrderNumber: item.OrderNumber,
OrderAmount: item.OrderAmount,
@ -172,30 +237,23 @@ func (t *OrderAfterSaleResellerTool) checkOrderAfterSaleReseller(toolReq OrderAf
OurProductTitle: item.OurProductTitle,
Account: item.Account,
Platforms: item.Platforms,
AfterType: toolReq.AfterType,
Remark: toolReq.AfterSalesReason,
AfterAmount: toolReq.AfterSalesPrice,
ResponsibleType: toolReq.ResponsibleType,
ResponsiblePerson: toolReq.ResponsiblePerson,
AfterType: originInput.AfterType,
Remark: originInput.AfterSalesReason,
AfterAmount: originInput.AfterSalesPrice,
ResponsibleType: originInput.ResponsibleType,
ResponsiblePerson: originInput.ResponsiblePerson,
})
}
// 追加扩展数据
for _, item := range toolResp.Data {
for _, item := range orderList {
if extItem, ok := resp.Data.ExtData[item.OrderNumber]; ok {
item.IsExistsAfterSale = extItem.IsExistsAfterSale
item.CreateTime = extItem.SerialCreateTime
}
}
jsonByte, err := json.Marshal(toolResp)
if err != nil {
return err
}
entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
return nil
return orderList, nil
}
func (t *OrderAfterSaleResellerTool) checkOrderAfterSaleResellerMock(req OrderAfterSaleResellerRequest, requireData *entitys.RequireData) error {

View File

@ -7,6 +7,7 @@ import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
)
@ -36,7 +37,7 @@ func (t *OrderAfterSaleSupplierTool) Definition() entitys.ToolDefinition {
type OrderAfterSaleSupplierRequest struct {
SerialNumber []string `json:"serialNumber"` // 流水号
Account string `json:"account"` // 充值账号
Account []string `json:"account"` // 充值账号
SerialCreateTime string `json:"serialCreateTime"` // 流水创建时间
AfterSalesReason string `json:"afterSalesReason"` // 售后原因
AfterSalesPrice string `json:"afterSalesPrice"` // 售后金额
@ -103,7 +104,7 @@ func (t *OrderAfterSaleSupplierTool) Execute(ctx context.Context, requireData *e
if err := json.Unmarshal([]byte(requireData.Match.Parameters), &req); err != nil {
return err
}
if len(req.SerialNumber) == 0 && req.Account == "" {
if len(req.SerialNumber) == 0 && len(req.Account) == 0 {
return fmt.Errorf("充值流水号 和 充值账号 不能同时为空")
}
@ -121,41 +122,105 @@ func (t *OrderAfterSaleSupplierTool) checkOrderAfterSaleSupplier(toolReq OrderAf
serialStartTime := serialCreateTime.Unix() - 10*60
serialEndTime := serialCreateTime.Unix() + 10*60
req := l_request.Request{
Url: t.config.BaseURL,
Headers: map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
},
Method: "POST",
Json: map[string]any{
"serialNumber": toolReq.SerialNumber, // 流水号
"account": toolReq.Account, // 充值账号
"serialStartTime": serialStartTime, // 流水创建时间
"serialEndTime": serialEndTime, // 流水结束时间
},
// 账号数量超过10直接截断
if len(toolReq.Account) > 10 {
entitys.ResLog(requireData.Ch, t.Name(), "账号数量超过10已被截断")
toolReq.Account = toolReq.Account[:10]
}
res, err := req.Send()
if err != nil {
return err
headers := map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", requireData.Auth),
}
// 解析响应
var resp OrderAfterSaleSupplierApiResponse
if err := json.Unmarshal(res.Content, &resp); err != nil {
return err
}
if resp.Code != 0 {
return fmt.Errorf("after sale supplier failed: %s", resp.Msg)
// 最终输出
var orderList []*OrderAfterSaleSupplierData
// 多流水号
if len(toolReq.SerialNumber) > 0 {
body := map[string]any{
"serialNumber": toolReq.SerialNumber, // 流水号
}
orderList, err = t.getAfterSaleSupplierList(headers, body)
if err != nil {
return err
}
} else if len(toolReq.Account) > 0 {
// 多充值账号并发
orderListChan := make(chan []*OrderAfterSaleSupplierData, len(toolReq.Account))
waitGroup := sync.WaitGroup{}
// 并发请求
for _, account := range toolReq.Account {
waitGroup.Add(1)
go func(account string) {
defer waitGroup.Done()
body := map[string]any{
"account": account, // 充值账号
"serialStartTime": serialStartTime, // 流水创建时间
"serialEndTime": serialEndTime, // 流水结束时间
}
orderListIn, errIn := t.getAfterSaleSupplierList(headers, body)
if errIn != nil {
return
}
orderListChan <- orderListIn
}(account)
}
// 等待所有请求完成
waitGroup.Wait()
close(orderListChan)
for orderListIn := range orderListChan {
orderList = append(orderList, orderListIn...)
}
} else {
return fmt.Errorf("充值流水号 和 充值账号 不能同时为空")
}
toolResp := OrderAfterSaleSupplierResponse{
Code: resp.Code,
Msg: resp.Msg,
Data: make([]*OrderAfterSaleSupplierData, 0, len(resp.Data.Data)),
Code: 0,
Msg: "Success",
Data: orderList,
}
var jsonByte []byte
jsonByte, err = json.Marshal(toolResp)
if err != nil {
return err
}
entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
return nil
}
func (t *OrderAfterSaleSupplierTool) getAfterSaleSupplierList(headers map[string]string, body map[string]any) ([]*OrderAfterSaleSupplierData, error) {
req := l_request.Request{
Url: t.config.BaseURL,
Headers: headers,
Method: "POST",
Json: body,
}
res, err := req.Send()
if err != nil {
return nil, err
}
// 解析响应
var resp OrderAfterSaleSupplierApiResponse
if err = json.Unmarshal(res.Content, &resp); err != nil {
return nil, err
}
if resp.Code != 0 {
return nil, fmt.Errorf("after sale supplier failed: %s", resp.Msg)
}
orderList := make([]*OrderAfterSaleSupplierData, 0, len(resp.Data.Data))
// 转换数据
for _, item := range resp.Data.Data {
toolResp.Data = append(toolResp.Data, &OrderAfterSaleSupplierData{
orderList = append(orderList, &OrderAfterSaleSupplierData{
SerialNumber: item.SerialNumber,
PlatformName: item.PlatformName,
SignCompany: item.SignCompany,
@ -173,21 +238,14 @@ func (t *OrderAfterSaleSupplierTool) checkOrderAfterSaleSupplier(toolReq OrderAf
}
// 追加扩展数据
for _, item := range toolResp.Data {
for _, item := range orderList {
if extItem, ok := resp.Data.ExtData[item.SerialNumber]; ok {
item.IsExistsAfterSale = extItem.IsExistsAfterSale
item.ExecuteTime = extItem.SerialCreateTime
}
}
jsonByte, err := json.Marshal(toolResp)
if err != nil {
return err
}
entitys.ResLog(requireData.Ch, t.Name(), "售后订单信息拉取完成")
entitys.ResJson(requireData.Ch, t.Name(), string(jsonByte))
return nil
return orderList, nil
}
// mock待删除