62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
"github.com/muyuqingfeng/iloom/textile-service/internal/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
type DashboardStats struct {
|
||
|
|
TotalPlans int `json:"total_plans"`
|
||
|
|
ActivePlans int `json:"active_plans"`
|
||
|
|
CompletedPlans int `json:"completed_plans"`
|
||
|
|
YarnStockCount int `json:"yarn_stock_count"`
|
||
|
|
LowStockCount int `json:"low_stock_count"`
|
||
|
|
PendingPayments int `json:"pending_payments"`
|
||
|
|
}
|
||
|
|
|
||
|
|
type DashboardService struct {
|
||
|
|
planRepo *repository.PlanRepo
|
||
|
|
yarnRepo *repository.YarnRepo
|
||
|
|
paymentRepo *repository.PaymentRepo
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewDashboardService(planRepo *repository.PlanRepo, yarnRepo *repository.YarnRepo, paymentRepo *repository.PaymentRepo) *DashboardService {
|
||
|
|
return &DashboardService{
|
||
|
|
planRepo: planRepo,
|
||
|
|
yarnRepo: yarnRepo,
|
||
|
|
paymentRepo: paymentRepo,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *DashboardService) Stats(ctx context.Context, companyID string) (*DashboardStats, error) {
|
||
|
|
total, active, completed, err := s.planRepo.CountByFactory(ctx, companyID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
yarnCount, err := s.yarnRepo.Count(ctx, companyID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
lowStockCount, err := s.yarnRepo.CountLowStock(ctx, companyID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
pendingPayments, err := s.paymentRepo.CountPending(ctx, companyID)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return &DashboardStats{
|
||
|
|
TotalPlans: total,
|
||
|
|
ActivePlans: active,
|
||
|
|
CompletedPlans: completed,
|
||
|
|
YarnStockCount: yarnCount,
|
||
|
|
LowStockCount: lowStockCount,
|
||
|
|
PendingPayments: pendingPayments,
|
||
|
|
}, nil
|
||
|
|
}
|