70 lines
1.8 KiB
Go
Raw Permalink Normal View History

package service
import (
"context"
"github.com/muyuqingfeng/iloom/purchaser-service/internal/model"
"github.com/muyuqingfeng/iloom/purchaser-service/internal/repository"
)
type DashboardStats struct {
TotalPlans int `json:"total_plans"`
ProducingPlans int `json:"producing_plans"`
CompletedPlans int `json:"completed_plans"`
TotalProducts int `json:"total_products"`
TotalStock float64 `json:"total_stock"`
UnpaidAmount float64 `json:"unpaid_amount"`
RecentPlans []model.ProductionPlan `json:"recent_plans"`
}
type DashboardService struct {
planRepo *repository.PlanRepo
productRepo *repository.ProductRepo
apRepo *repository.APRepo
}
func NewDashboardService(planRepo *repository.PlanRepo, productRepo *repository.ProductRepo, apRepo *repository.APRepo) *DashboardService {
return &DashboardService{
planRepo: planRepo,
productRepo: productRepo,
apRepo: apRepo,
}
}
func (s *DashboardService) Stats(ctx context.Context, companyID string) (*DashboardStats, error) {
total, producing, completed, err := s.planRepo.CountByStatus(ctx, companyID)
if err != nil {
return nil, err
}
productCount, err := s.productRepo.Count(ctx, companyID)
if err != nil {
return nil, err
}
totalStock, err := s.productRepo.TotalStock(ctx, companyID)
if err != nil {
return nil, err
}
unpaid, err := s.apRepo.TotalUnpaid(ctx, companyID)
if err != nil {
return nil, err
}
recentPlans, err := s.planRepo.Recent(ctx, companyID, 5)
if err != nil {
return nil, err
}
return &DashboardStats{
TotalPlans: total,
ProducingPlans: producing,
CompletedPlans: completed,
TotalProducts: productCount,
TotalStock: totalStock,
UnpaidAmount: unpaid,
RecentPlans: recentPlans,
}, nil
}