67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/muyuqingfeng/iloom/washing-service/internal/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
type DashboardStats struct {
|
||
|
|
TotalPlans int `json:"total_plans"`
|
||
|
|
PendingPlans int `json:"pending_plans"`
|
||
|
|
CompletedPlans int `json:"completed_plans"`
|
||
|
|
FinishedProducts int `json:"finished_products"`
|
||
|
|
TotalStockMeters float64 `json:"total_stock_meters"`
|
||
|
|
PendingPayments int `json:"pending_payments"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type DashboardService struct {
|
||
|
|
planRepo *repository.PlanRepo
|
||
|
|
fpRepo *repository.FinishedProductRepo
|
||
|
|
payRepo *repository.PaymentRepo
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewDashboardService(
|
||
|
|
pr *repository.PlanRepo,
|
||
|
|
fpr *repository.FinishedProductRepo,
|
||
|
|
payr *repository.PaymentRepo,
|
||
|
|
) *DashboardService {
|
||
|
|
return &DashboardService{
|
||
|
|
planRepo: pr,
|
||
|
|
fpRepo: fpr,
|
||
|
|
payRepo: payr,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *DashboardService) Stats(ctx context.Context, companyID string) (*DashboardStats, error) {
|
||
|
|
total, pending, completed, err := s.planRepo.CountByStatus(ctx, companyID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("count plans: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
fpCount, err := s.fpRepo.Count(ctx, companyID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("count finished products: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
stockMeters, err := s.fpRepo.TotalStock(ctx, companyID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("total stock: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
pendingPay, err := s.payRepo.CountPending(ctx, companyID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("count pending payments: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return &DashboardStats{
|
||
|
|
TotalPlans: total,
|
||
|
|
PendingPlans: pending,
|
||
|
|
CompletedPlans: completed,
|
||
|
|
FinishedProducts: fpCount,
|
||
|
|
TotalStockMeters: stockMeters,
|
||
|
|
PendingPayments: pendingPay,
|
||
|
|
}, nil
|
||
|
|
}
|