article/service/tools_svc.go
timerzz 00e4f26d71
All checks were successful
Build image / build (push) Successful in 1m24s
update 修复文件问题
2025-03-30 21:20:13 +08:00

72 lines
2.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"fmt"
coach_outlet "gitea.timerzz.com/kedaya_haitao/article/tools/coach-outlet"
"gitea.timerzz.com/kedaya_haitao/common/structs/storage"
dw_sdk "gitea.timerzz.com/kedaya_haitao/dw-sdk"
"github.com/gofiber/fiber/v3"
"github.com/sirupsen/logrus"
)
type Tools struct {
storage *storage.Storage
dw *dw_sdk.Client
export *coach_outlet.Exporter
}
// NewTools 创建工具服务实例
func NewTools(storage *storage.Storage, dw *dw_sdk.Client) *Tools {
exporter, err := coach_outlet.NewExporter(storage, dw)
if err != nil {
logrus.Errorf("初始化Coach Outlet导出工具失败: %v", err)
}
return &Tools{
storage: storage,
dw: dw,
export: exporter,
}
}
// Registry 注册路由
func (s *Tools) Registry(r fiber.Router) {
api := r.Group("/api/v2")
// 添加新的Coach Outlet工具接口
tools := api.Group("/tools")
tools.Get("/excel/coach-outlet/:provider_id", s.CoachOutletExcel)
}
// CoachOutletExcel 导出Coach Outlet商品数据到Excel
func (s *Tools) CoachOutletExcel(c fiber.Ctx) error {
// 获取供应商ID参数
providerId := c.Params("provider_id")
if providerId == "" {
providerId = coach_outlet.CACoachOutlet // 默认使用加拿大Coach Outlet
}
// 验证供应商ID是否有效
if providerId != coach_outlet.CACoachOutlet && providerId != coach_outlet.USCoachOutlet {
return fiber.NewError(fiber.StatusBadRequest,
fmt.Sprintf("无效的供应商ID必须是 %s 或 %s", coach_outlet.CACoachOutlet, coach_outlet.USCoachOutlet))
}
// 检查导出工具是否初始化成功
if s.export == nil {
return fiber.NewError(fiber.StatusInternalServerError, "导出工具未初始化")
}
// 直接将Excel数据写入响应
fileName, err := s.export.ExportCheapProduct(providerId)
if err != nil {
logrus.Errorf("导出Coach Outlet商品数据失败: %v", err)
return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("导出失败: %v", err))
}
// 设置文件名和响应头
c.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
c.Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
return c.SendFile(fileName)
}