feat: add endpoint and logic for retrieving top exported components, including SQL queries, models, and service integration

This commit is contained in:
Tran Anh Tuan
2026-05-14 11:02:09 +07:00
parent 96bc22942b
commit cee0186225
12 changed files with 431 additions and 4 deletions

View File

@@ -232,6 +232,71 @@ func (q *Queries) GetTodayInvoiceCounts(ctx context.Context) ([]GetTodayInvoiceC
return items, nil
}
const getTopExportedComponents = `-- name: GetTopExportedComponents :many
SELECT c.id, c.name, c.unit, ct.name AS component_type_name,
COALESCE(SUM(st.quantity), 0)::bigint AS total_exported
FROM stock_transactions st
JOIN components c ON c.id = st.component_id
LEFT JOIN component_types ct ON c.component_type_id = ct.id
JOIN containers con ON st.container_id = con.id
JOIN shelves s ON con.shelf_id = s.id
JOIN cabinets cab ON s.cabinet_id = cab.id
JOIN rooms r ON cab.room_id = r.id
WHERE st.transaction_type = 'export'
AND st.created_at >= $1::timestamptz
AND st.created_at < $2::timestamptz
AND ($3::bigint IS NULL OR r.warehouse_id = $3::bigint)
GROUP BY c.id, c.name, c.unit, ct.name
ORDER BY total_exported DESC
LIMIT $4::int
`
type GetTopExportedComponentsParams struct {
StartDate time.Time `db:"start_date" json:"startDate"`
EndDate time.Time `db:"end_date" json:"endDate"`
WarehouseID pgtype.Int8 `db:"warehouse_id" json:"warehouseId"`
LimitCount int32 `db:"limit_count" json:"limitCount"`
}
type GetTopExportedComponentsRow struct {
ID int64 `db:"id" json:"id"`
Name string `db:"name" json:"name"`
Unit string `db:"unit" json:"unit"`
ComponentTypeName pgtype.Text `db:"component_type_name" json:"componentTypeName"`
TotalExported int64 `db:"total_exported" json:"totalExported"`
}
func (q *Queries) GetTopExportedComponents(ctx context.Context, arg GetTopExportedComponentsParams) ([]GetTopExportedComponentsRow, error) {
rows, err := q.db.Query(ctx, getTopExportedComponents,
arg.StartDate,
arg.EndDate,
arg.WarehouseID,
arg.LimitCount,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetTopExportedComponentsRow
for rows.Next() {
var i GetTopExportedComponentsRow
if err := rows.Scan(
&i.ID,
&i.Name,
&i.Unit,
&i.ComponentTypeName,
&i.TotalExported,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getTotalComponentStats = `-- name: GetTotalComponentStats :one
SELECT COUNT(DISTINCT ci.component_id) AS total_types, COALESCE(SUM(ci.quantity), 0)::bigint AS total_quantity
FROM component_items ci

View File

@@ -70,6 +70,7 @@ type Querier interface {
GetShelveByID(ctx context.Context, id int64) (Shelf, error)
GetStockAlerts(ctx context.Context) ([]GetStockAlertsRow, error)
GetTodayInvoiceCounts(ctx context.Context) ([]GetTodayInvoiceCountsRow, error)
GetTopExportedComponents(ctx context.Context, arg GetTopExportedComponentsParams) ([]GetTopExportedComponentsRow, error)
GetTotalComponentStats(ctx context.Context, warehouseID pgtype.Int8) (GetTotalComponentStatsRow, error)
GetTransactionChartData(ctx context.Context, arg GetTransactionChartDataParams) ([]GetTransactionChartDataRow, error)
GetUserByEmail(ctx context.Context, email string) (User, error)