Add Daily sales
This commit is contained in:
parent
d50cefbc7f
commit
4e4d9d3a90
@ -178,3 +178,9 @@ func (e *TicketSoldDB) ToTicketSold() *TicketSold {
|
||||
Count: e.Count,
|
||||
}
|
||||
}
|
||||
|
||||
type ProductDailySales struct {
|
||||
Day time.Time
|
||||
ProductID int64
|
||||
TotalQuantity int
|
||||
}
|
||||
|
||||
@ -25,6 +25,7 @@ func (h *Handler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
||||
route.GET("/history", jwt, h.GetAllHistoryOrders)
|
||||
route.GET("/ticket-sold", jwt, h.CountSoldOfTicket)
|
||||
route.GET("/sum-amount", jwt, h.SumAmount)
|
||||
route.GET("/daily-sales", jwt, h.GetDailySalesTicket)
|
||||
}
|
||||
|
||||
func NewHandler(service services.Order) *Handler {
|
||||
@ -240,6 +241,29 @@ func (h *Handler) CountSoldOfTicket(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) GetDailySalesTicket(c *gin.Context) {
|
||||
var req request.OrderParam
|
||||
if err := c.ShouldBindQuery(&req); err != nil {
|
||||
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := request.GetMyContext(c)
|
||||
|
||||
resp, err := h.service.GetDailySales(ctx, req.ToOrderEntity(ctx))
|
||||
|
||||
if err != nil {
|
||||
response.ErrorWrapper(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response.BaseResponse{
|
||||
Success: true,
|
||||
Status: http.StatusOK,
|
||||
Data: h.toDailySales(resp),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) toHistoryOrderList(resp []*entity.HistoryOrder, total int64, req request.OrderParam) response.HistoryOrderList {
|
||||
var orders []response.HistoryOrder
|
||||
for _, b := range resp {
|
||||
@ -253,3 +277,15 @@ func (h *Handler) toHistoryOrderList(resp []*entity.HistoryOrder, total int64, r
|
||||
Offset: req.Offset,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) toDailySales(resp []entity.ProductDailySales) []response.ProductDailySales {
|
||||
var dailySales []response.ProductDailySales
|
||||
for _, b := range resp {
|
||||
dailySales = append(dailySales, response.ProductDailySales{
|
||||
Day: b.Day,
|
||||
ProductID: b.ProductID,
|
||||
TotalQuantity: b.TotalQuantity,
|
||||
})
|
||||
}
|
||||
return dailySales
|
||||
}
|
||||
|
||||
@ -111,3 +111,9 @@ type CreateOrderItemResponse struct {
|
||||
Price float64 `json:"price"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type ProductDailySales struct {
|
||||
Day time.Time
|
||||
ProductID int64
|
||||
TotalQuantity int
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -324,6 +324,17 @@ func (s OrderService) CountSoldOfTicket(ctx mycontext.Context, req entity.OrderS
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s OrderService) GetDailySales(ctx mycontext.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error) {
|
||||
dailySales, err := s.repo.GetDailySalesMetrics(ctx, req)
|
||||
|
||||
if err != nil {
|
||||
logger.ContextLogger(ctx).Error("error when get all history orders", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dailySales, nil
|
||||
}
|
||||
|
||||
func (s OrderService) SumAmount(ctx mycontext.Context, req entity.OrderSearch) (*entity.Order, error) {
|
||||
amount, err := s.repo.SumAmount(ctx, req)
|
||||
|
||||
|
||||
@ -114,6 +114,7 @@ type Order interface {
|
||||
GetAllHistoryOrders(ctx mycontext.Context, req entity.OrderSearch) ([]*entity.HistoryOrder, int, error)
|
||||
CountSoldOfTicket(ctx mycontext.Context, req entity.OrderSearch) (*entity.TicketSold, error)
|
||||
SumAmount(ctx mycontext.Context, req entity.OrderSearch) (*entity.Order, error)
|
||||
GetDailySales(ctx mycontext.Context, req entity.OrderSearch) ([]entity.ProductDailySales, error)
|
||||
}
|
||||
|
||||
type OSSService interface {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user