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, "导出工具未初始化") } // 设置文件名和响应头 fileName := fmt.Sprintf("coach_outlet_%s.xlsx", providerId) c.Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName)) c.Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") // 直接将Excel数据写入响应 if err := s.export.ExportCheapProduct(providerId, c); err != nil { logrus.Errorf("导出Coach Outlet商品数据失败: %v", err) return fiber.NewError(fiber.StatusInternalServerError, fmt.Sprintf("导出失败: %v", err)) } return nil }