Add Daily sales
This commit is contained in:
@@ -199,3 +199,32 @@ func (r *OrderRepository) SumAmount(ctx mycontext.Context, req entity.OrderSearc
|
||||
|
||||
return amount, nil
|
||||
}
|
||||
|
||||
func (r *OrderRepository) GetDailySalesMetrics(ctx context.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error) {
|
||||
var sales []entity.ProductDailySales
|
||||
|
||||
// Build the query with GORM
|
||||
query := r.db.WithContext(ctx).
|
||||
Table("orders o").
|
||||
Select(`DATE_TRUNC('day', o.created_at) AS day, oi.item_id, SUM(oi.qty) AS total_quantity`).
|
||||
Joins("LEFT JOIN order_items oi ON o.id = oi.order_id").
|
||||
Where("o.status = ?", "PAID").
|
||||
Where("o.created_at >= ?", time.Now().AddDate(0, 0, -30)). // Last 30 days
|
||||
Group("day, oi.item_id").
|
||||
Order("day")
|
||||
|
||||
// Apply filters based on the presence of PartnerID and SiteID
|
||||
if req.PartnerID != nil {
|
||||
query = query.Where("o.partner_id = ?", *req.PartnerID)
|
||||
}
|
||||
if req.SiteID != nil {
|
||||
query = query.Where("o.site_id = ?", *req.SiteID)
|
||||
}
|
||||
|
||||
// Execute the query and scan the results
|
||||
if err := query.Find(&sales).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return sales, nil
|
||||
}
|
||||
|
||||
@@ -144,6 +144,7 @@ type Order interface {
|
||||
GetAllHystoryOrders(ctx context.Context, req entity.OrderSearch) (entity.HistoryOrderList, int, error)
|
||||
SumAmount(ctx mycontext.Context, req entity.OrderSearch) (*entity.OrderDB, error)
|
||||
CountSoldOfTicket(ctx mycontext.Context, req entity.OrderSearch) (*entity.TicketSoldDB, error)
|
||||
GetDailySalesMetrics(ctx context.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error)
|
||||
}
|
||||
|
||||
type OSSRepository interface {
|
||||
|
||||
Reference in New Issue
Block a user