Files
smart-search-back/internal/service/supplier.go
vallyenfail d959dcca96 add service
2026-01-17 17:39:33 +03:00

76 lines
1.9 KiB
Go

package service
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/xuri/excelize/v2"
"smart-search-back/internal/repository"
)
type supplierService struct {
supplierRepo repository.SupplierRepository
}
func NewSupplierService(supplierRepo repository.SupplierRepository) SupplierService {
return &supplierService{
supplierRepo: supplierRepo,
}
}
func (s *supplierService) ExportExcel(ctx context.Context, requestID uuid.UUID) ([]byte, error) {
suppliers, err := s.supplierRepo.GetByRequestID(ctx, requestID)
if err != nil {
return nil, err
}
f := excelize.NewFile()
defer f.Close()
sheetName := "Suppliers"
index, err := f.NewSheet(sheetName)
if err != nil {
return nil, err
}
headers := []string{"Company ID", "Email", "Phone", "Company Name", "URL"}
for i, header := range headers {
cell := fmt.Sprintf("%c1", 'A'+i)
f.SetCellValue(sheetName, cell, header)
}
style, err := f.NewStyle(&excelize.Style{
Font: &excelize.Font{Bold: true},
Fill: excelize.Fill{Type: "pattern", Color: []string{"#E0E0E0"}, Pattern: 1},
})
if err == nil {
f.SetCellStyle(sheetName, "A1", fmt.Sprintf("%c1", 'A'+len(headers)-1), style)
}
for i, supplier := range suppliers {
row := i + 2
f.SetCellValue(sheetName, fmt.Sprintf("A%d", row), supplier.ID)
f.SetCellValue(sheetName, fmt.Sprintf("B%d", row), supplier.Email)
f.SetCellValue(sheetName, fmt.Sprintf("C%d", row), supplier.Phone)
f.SetCellValue(sheetName, fmt.Sprintf("D%d", row), supplier.Name)
f.SetCellValue(sheetName, fmt.Sprintf("E%d", row), supplier.URL)
}
f.SetColWidth(sheetName, "A", "A", 12)
f.SetColWidth(sheetName, "B", "B", 30)
f.SetColWidth(sheetName, "C", "C", 20)
f.SetColWidth(sheetName, "D", "D", 40)
f.SetColWidth(sheetName, "E", "E", 40)
f.SetActiveSheet(index)
f.DeleteSheet("Sheet1")
buffer, err := f.WriteToBuffer()
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}