128 lines
3.6 KiB
Go
128 lines
3.6 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"time"
|
|
"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
|
|
}
|
|
|
|
func GetTransactionChartData(ctx context.Context, queries *db.Queries, startDate, endDate time.Time, warehouseID pgtype.Int8) (models.TransactionChartData, error) {
|
|
results, err := queries.GetTransactionChartData(ctx, db.GetTransactionChartDataParams{
|
|
StartDate: startDate,
|
|
EndDate: endDate,
|
|
WarehouseID: warehouseID,
|
|
})
|
|
if err != nil {
|
|
return models.TransactionChartData{}, err
|
|
}
|
|
|
|
dateMap := make(map[string]*models.TransactionChartItem)
|
|
for _, r := range results {
|
|
row := mapper.ToDomainTransactionChartRow(r)
|
|
dateStr := row.Date.Format("2006-01-02")
|
|
if _, exists := dateMap[dateStr]; !exists {
|
|
dateMap[dateStr] = &models.TransactionChartItem{
|
|
Date: dateStr,
|
|
ImportQuantity: 0,
|
|
ExportQuantity: 0,
|
|
}
|
|
}
|
|
switch row.TransactionType {
|
|
case "import":
|
|
dateMap[dateStr].ImportQuantity = row.TotalQuantity
|
|
case "export":
|
|
dateMap[dateStr].ExportQuantity = row.TotalQuantity
|
|
}
|
|
}
|
|
|
|
items := make([]models.TransactionChartItem, 0, len(dateMap))
|
|
for _, item := range dateMap {
|
|
items = append(items, *item)
|
|
}
|
|
sort.Slice(items, func(i, j int) bool {
|
|
return items[i].Date < items[j].Date
|
|
})
|
|
|
|
return models.TransactionChartData{Items: items}, nil
|
|
}
|