Основные изменения: 1. CSV экспорт и веб-интерфейс: - Компоненты теперь сортируются по иерархии категорий (display_order) - Категории отображаются в правильном порядке: BB, CPU, MEM, GPU и т.д. - Компоненты без категории отображаются в конце 2. Раздел PCI в конфигураторе: - Разделен на секции: GPU/DPU, NIC/HCA, HBA - Улучшена навигация и выбор компонентов 3. Сохранение "своей цены": - Добавлено поле custom_price в модель Configuration - Создана миграция 002_add_custom_price.sql - "Своя цена" сохраняется при сохранении конфигурации - При загрузке конфигурации восстанавливается сохраненная цена 4. Автосохранение: - Конфигурация автоматически сохраняется через 1 секунду после изменений - Debounce предотвращает избыточные запросы - Автосохранение работает для всех изменений (компоненты, количество, цена) 5. Дополнительно: - Добавлен cmd/importer для импорта метаданных из таблицы lot - Создан скрипт apply_migration.sh для применения миграций - Оптимизирована работа с категориями в ExportService Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
122 lines
3.5 KiB
Go
122 lines
3.5 KiB
Go
package handlers
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
"time"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"git.mchus.pro/mchus/quoteforge/internal/middleware"
|
||
"git.mchus.pro/mchus/quoteforge/internal/services"
|
||
)
|
||
|
||
type ExportHandler struct {
|
||
exportService *services.ExportService
|
||
configService *services.ConfigurationService
|
||
componentService *services.ComponentService
|
||
}
|
||
|
||
func NewExportHandler(
|
||
exportService *services.ExportService,
|
||
configService *services.ConfigurationService,
|
||
componentService *services.ComponentService,
|
||
) *ExportHandler {
|
||
return &ExportHandler{
|
||
exportService: exportService,
|
||
configService: configService,
|
||
componentService: componentService,
|
||
}
|
||
}
|
||
|
||
type ExportRequest struct {
|
||
Name string `json:"name" binding:"required"`
|
||
Items []struct {
|
||
LotName string `json:"lot_name" binding:"required"`
|
||
Quantity int `json:"quantity" binding:"required,min=1"`
|
||
UnitPrice float64 `json:"unit_price"`
|
||
} `json:"items" binding:"required,min=1"`
|
||
Notes string `json:"notes"`
|
||
}
|
||
|
||
func (h *ExportHandler) ExportCSV(c *gin.Context) {
|
||
var req ExportRequest
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
|
||
data := h.buildExportData(&req)
|
||
|
||
csvData, err := h.exportService.ToCSV(data)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
|
||
filename := fmt.Sprintf("%s %s SPEC.csv", time.Now().Format("2006-01-02"), req.Name)
|
||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
|
||
c.Data(http.StatusOK, "text/csv; charset=utf-8", csvData)
|
||
}
|
||
|
||
func (h *ExportHandler) buildExportData(req *ExportRequest) *services.ExportData {
|
||
items := make([]services.ExportItem, len(req.Items))
|
||
var total float64
|
||
|
||
for i, item := range req.Items {
|
||
itemTotal := item.UnitPrice * float64(item.Quantity)
|
||
|
||
// Получаем информацию о компоненте для заполнения категории и описания
|
||
componentView, err := h.componentService.GetByLotName(item.LotName)
|
||
if err != nil {
|
||
// Если не удалось получить информацию о компоненте, используем только основные данные
|
||
items[i] = services.ExportItem{
|
||
LotName: item.LotName,
|
||
Quantity: item.Quantity,
|
||
UnitPrice: item.UnitPrice,
|
||
TotalPrice: itemTotal,
|
||
}
|
||
} else {
|
||
items[i] = services.ExportItem{
|
||
LotName: item.LotName,
|
||
Description: componentView.Description,
|
||
Category: componentView.Category,
|
||
Quantity: item.Quantity,
|
||
UnitPrice: item.UnitPrice,
|
||
TotalPrice: itemTotal,
|
||
}
|
||
}
|
||
total += itemTotal
|
||
}
|
||
|
||
return &services.ExportData{
|
||
Name: req.Name,
|
||
Items: items,
|
||
Total: total,
|
||
Notes: req.Notes,
|
||
CreatedAt: time.Now(),
|
||
}
|
||
}
|
||
|
||
func (h *ExportHandler) ExportConfigCSV(c *gin.Context) {
|
||
userID := middleware.GetUserID(c)
|
||
uuid := c.Param("uuid")
|
||
|
||
config, err := h.configService.GetByUUID(uuid, userID)
|
||
if err != nil {
|
||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
|
||
data := h.exportService.ConfigToExportData(config, h.componentService)
|
||
|
||
csvData, err := h.exportService.ToCSV(data)
|
||
if err != nil {
|
||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||
return
|
||
}
|
||
|
||
filename := fmt.Sprintf("%s %s SPEC.csv", config.CreatedAt.Format("2006-01-02"), config.Name)
|
||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
|
||
c.Data(http.StatusOK, "text/csv; charset=utf-8", csvData)
|
||
}
|