package logic import ( "context" "fmt" "muyu-apiserver/pkg/tenantctx" "muyu-apiserver/rpc/purchase/internal/svc" "muyu-apiserver/rpc/purchase/pb" "github.com/zeromicro/go-zero/core/logx" ) type GetPurchaseStatsSummaryLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewGetPurchaseStatsSummaryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPurchaseStatsSummaryLogic { return &GetPurchaseStatsSummaryLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)} } func (l *GetPurchaseStatsSummaryLogic) GetPurchaseStatsSummary(in *pb.PurchaseStatsSummaryReq) (*pb.PurchaseStatsSummaryResp, error) { tenantId := tenantctx.ExtractTenantId(l.ctx) // Use existing FindList to aggregate; page/pageSize 0 means fetch all orders, _, err := l.svcCtx.OrderModel.FindList(l.ctx, tenantId, 1, 10000, "", -1, in.StartDate, in.EndDate) if err != nil { return nil, err } var totalPurchase, totalPaid float64 var orderCount int64 for _, o := range orders { if o.Status == 2 { continue // skip cancelled } totalPurchase += o.ContractAmount totalPaid += o.PaidAmount orderCount++ } receipts, total, err := l.svcCtx.ReceiptModel.FindList(l.ctx, tenantId, 1, 1, "", -1, in.StartDate, in.EndDate) _ = receipts receiptCount := int64(0) if err == nil { receiptCount = total } return &pb.PurchaseStatsSummaryResp{ TotalPurchaseAmount: fmt.Sprintf("%.2f", totalPurchase), TotalPaidAmount: fmt.Sprintf("%.2f", totalPaid), TotalUnpaidAmount: fmt.Sprintf("%.2f", totalPurchase-totalPaid), OrderCount: orderCount, ReceiptCount: receiptCount, }, nil }