86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"wm-backend/internal/mapper"
|
|
"wm-backend/internal/models"
|
|
db "wm-backend/sqlc_gen"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func GetDashboardSummary(ctx context.Context, queries *db.Queries, warehouseID pgtype.Int8) (models.DashboardSummary, error) {
|
|
totalStats, err := queries.GetTotalComponentStats(ctx, warehouseID)
|
|
if err != nil {
|
|
return models.DashboardSummary{}, err
|
|
}
|
|
|
|
pendingInvoices, err := queries.CountPendingInvoices(ctx)
|
|
if err != nil {
|
|
return models.DashboardSummary{}, err
|
|
}
|
|
|
|
lowStockCount, err := queries.CountLowStockComponents(ctx)
|
|
if err != nil {
|
|
return models.DashboardSummary{}, err
|
|
}
|
|
|
|
abnormalRows, err := queries.GetAbnormalItemCounts(ctx, warehouseID)
|
|
if err != nil {
|
|
return models.DashboardSummary{}, err
|
|
}
|
|
|
|
todayInvoiceRows, err := queries.GetTodayInvoiceCounts(ctx)
|
|
if err != nil {
|
|
return models.DashboardSummary{}, err
|
|
}
|
|
|
|
containerStats, err := queries.GetContainerStats(ctx, warehouseID)
|
|
if err != nil {
|
|
return models.DashboardSummary{}, err
|
|
}
|
|
|
|
abnormalAlerts := make([]models.AbnormalAlert, 0, len(abnormalRows))
|
|
for _, r := range abnormalRows {
|
|
abnormalAlerts = append(abnormalAlerts, mapper.ToDomainAbnormalAlert(r))
|
|
}
|
|
|
|
todayInvoices := make([]models.TodayInvoiceCount, 0, len(todayInvoiceRows))
|
|
for _, r := range todayInvoiceRows {
|
|
todayInvoices = append(todayInvoices, mapper.ToDomainTodayInvoiceCount(r))
|
|
}
|
|
|
|
return models.DashboardSummary{
|
|
TotalComponents: mapper.ToDomainTotalComponentStats(totalStats),
|
|
PendingInvoices: pendingInvoices,
|
|
LowStockComponents: lowStockCount,
|
|
AbnormalAlerts: abnormalAlerts,
|
|
TodayInvoices: todayInvoices,
|
|
EmptyContainers: mapper.ToDomainContainerStats(containerStats),
|
|
}, nil
|
|
}
|
|
|
|
func GetStockAlerts(ctx context.Context, queries *db.Queries) ([]models.StockAlert, error) {
|
|
results, err := queries.GetStockAlerts(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]models.StockAlert, 0, len(results))
|
|
for _, r := range results {
|
|
items = append(items, mapper.ToDomainStockAlert(r))
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func GetAnomalyItems(ctx context.Context, queries *db.Queries, warehouseID pgtype.Int8) ([]models.AnomalyItem, error) {
|
|
results, err := queries.GetAnomalyItems(ctx, warehouseID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]models.AnomalyItem, 0, len(results))
|
|
for _, r := range results {
|
|
items = append(items, mapper.ToDomainAnomalyItem(r))
|
|
}
|
|
return items, nil
|
|
}
|