Add Filter Sales Metric
This commit is contained in:
parent
4e4d9d3a90
commit
eff502b24a
@ -126,6 +126,7 @@ type OrderSearch struct {
|
|||||||
Offset int
|
Offset int
|
||||||
StartDate string
|
StartDate string
|
||||||
EndDate string
|
EndDate string
|
||||||
|
Period string
|
||||||
}
|
}
|
||||||
|
|
||||||
type HistoryOrderList []*HistoryOrderDB
|
type HistoryOrderList []*HistoryOrderDB
|
||||||
@ -180,7 +181,9 @@ func (e *TicketSoldDB) ToTicketSold() *TicketSold {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ProductDailySales struct {
|
type ProductDailySales struct {
|
||||||
Day time.Time
|
Day time.Time
|
||||||
ProductID int64
|
SiteID int64
|
||||||
TotalQuantity int
|
SiteName string
|
||||||
|
PaymentType string
|
||||||
|
Total float64
|
||||||
}
|
}
|
||||||
|
|||||||
@ -282,9 +282,11 @@ func (h *Handler) toDailySales(resp []entity.ProductDailySales) []response.Produ
|
|||||||
var dailySales []response.ProductDailySales
|
var dailySales []response.ProductDailySales
|
||||||
for _, b := range resp {
|
for _, b := range resp {
|
||||||
dailySales = append(dailySales, response.ProductDailySales{
|
dailySales = append(dailySales, response.ProductDailySales{
|
||||||
Day: b.Day,
|
Day: b.Day,
|
||||||
ProductID: b.ProductID,
|
SiteID: b.SiteID,
|
||||||
TotalQuantity: b.TotalQuantity,
|
Total: b.Total,
|
||||||
|
SiteName: b.SiteName,
|
||||||
|
PaymentType: b.PaymentType,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return dailySales
|
return dailySales
|
||||||
|
|||||||
@ -19,6 +19,7 @@ type OrderParam struct {
|
|||||||
Status string `form:"status" json:"status"`
|
Status string `form:"status" json:"status"`
|
||||||
Limit int `form:"limit" json:"limit" example:"10"`
|
Limit int `form:"limit" json:"limit" example:"10"`
|
||||||
Offset int `form:"offset" json:"offset" example:"0"`
|
Offset int `form:"offset" json:"offset" example:"0"`
|
||||||
|
Period string `form:"period" json:"period" example:"1d,7d,1m"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OrderParam) ToOrderEntity(ctx mycontext.Context) entity.OrderSearch {
|
func (o *OrderParam) ToOrderEntity(ctx mycontext.Context) entity.OrderSearch {
|
||||||
@ -32,6 +33,7 @@ func (o *OrderParam) ToOrderEntity(ctx mycontext.Context) entity.OrderSearch {
|
|||||||
StartDate: o.StartDate,
|
StartDate: o.StartDate,
|
||||||
EndDate: o.EndDate,
|
EndDate: o.EndDate,
|
||||||
Status: o.Status,
|
Status: o.Status,
|
||||||
|
Period: o.Period,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -113,7 +113,9 @@ type CreateOrderItemResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ProductDailySales struct {
|
type ProductDailySales struct {
|
||||||
Day time.Time
|
Day time.Time `json:"day"`
|
||||||
ProductID int64
|
SiteID int64 `json:"site_id"`
|
||||||
TotalQuantity int
|
SiteName string `json:"site_name"`
|
||||||
|
PaymentType string `json:"payment_type"`
|
||||||
|
Total float64 `json:"total"`
|
||||||
}
|
}
|
||||||
|
|||||||
@ -203,25 +203,47 @@ func (r *OrderRepository) SumAmount(ctx mycontext.Context, req entity.OrderSearc
|
|||||||
func (r *OrderRepository) GetDailySalesMetrics(ctx context.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error) {
|
func (r *OrderRepository) GetDailySalesMetrics(ctx context.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error) {
|
||||||
var sales []entity.ProductDailySales
|
var sales []entity.ProductDailySales
|
||||||
|
|
||||||
|
var dateTrunc, periodFilter string
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
switch req.Period {
|
||||||
|
case "1d":
|
||||||
|
dateTrunc = "hour"
|
||||||
|
periodFilter = now.Add(-24 * time.Hour).Format("2006-01-02 15:04:05")
|
||||||
|
case "7d":
|
||||||
|
dateTrunc = "day"
|
||||||
|
periodFilter = now.Add(-7 * 24 * time.Hour).Format("2006-01-02 15:04:05")
|
||||||
|
case "1m":
|
||||||
|
dateTrunc = "day"
|
||||||
|
periodFilter = now.AddDate(0, -1, 0).Format("2006-01-02 15:04:05")
|
||||||
|
case "1y":
|
||||||
|
dateTrunc = "week"
|
||||||
|
periodFilter = now.AddDate(-1, 0, 0).Format("2006-01-02 15:04:05")
|
||||||
|
default:
|
||||||
|
dateTrunc = "day"
|
||||||
|
periodFilter = now.AddDate(0, -1, 0).Format("2006-01-02 15:04:05") // Default to last month
|
||||||
|
}
|
||||||
|
|
||||||
// Build the query with GORM
|
// Build the query with GORM
|
||||||
query := r.db.WithContext(ctx).
|
query := r.db.WithContext(ctx).
|
||||||
Table("orders o").
|
Table("orders o").
|
||||||
Select(`DATE_TRUNC('day', o.created_at) AS day, oi.item_id, SUM(oi.qty) AS total_quantity`).
|
Select(`DATE_TRUNC(?, o.created_at) AS day, s.id AS site_id, s.name AS site_name, o.payment_type, SUM(oi.qty * oi.price) AS total_quantity`, dateTrunc).
|
||||||
Joins("LEFT JOIN order_items oi ON o.id = oi.order_id").
|
Joins("JOIN order_items oi ON o.id = oi.order_id").
|
||||||
|
Joins("JOIN sites s ON o.site_id = s.id").
|
||||||
Where("o.status = ?", "PAID").
|
Where("o.status = ?", "PAID").
|
||||||
Where("o.created_at >= ?", time.Now().AddDate(0, 0, -30)). // Last 30 days
|
Where("o.created_at >= ?", periodFilter)
|
||||||
Group("day, oi.item_id").
|
|
||||||
Order("day")
|
|
||||||
|
|
||||||
// Apply filters based on the presence of PartnerID and SiteID
|
|
||||||
if req.PartnerID != nil {
|
if req.PartnerID != nil {
|
||||||
query = query.Where("o.partner_id = ?", *req.PartnerID)
|
query = query.Where("o.partner_id = ?", *req.PartnerID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.SiteID != nil {
|
if req.SiteID != nil {
|
||||||
query = query.Where("o.site_id = ?", *req.SiteID)
|
query = query.Where("o.site_id = ?", *req.SiteID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute the query and scan the results
|
query = query.Group("day, s.id, s.name, o.payment_type").
|
||||||
|
Order("day")
|
||||||
|
|
||||||
if err := query.Find(&sales).Error; err != nil {
|
if err := query.Find(&sales).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user