package service

import (
	"boss/internal/common"
	"boss/internal/models/order"
	"math"
	"time"
)

type OrderSummary struct {
}

func (s *OrderSummary) GetTodaySummary() (todaySummary order.Summary) {
	todaySummary = order.Summary{}

	now := time.Now()
	//	获取今日零点
	todayZero := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
	orderList, err := order.GetOrderByTime(todayZero.String(), now.String())

	if err != nil {
		return
	}

	amount := 0.0
	paidNum := 0
	paidAmount := 0.0
	platformIncome := 0.0

	for _, info := range orderList {
		amount += info.OrderAmount

		// 统计成功订单
		if info.Status == common.OrderStatusSuccess {
			paidNum += 1
			paidAmount += info.OrderAmount
		}

	}

	todaySummary = order.Summary{
		TotalNum:       len(orderList),
		TotalAmount:    math.Round(amount*100) / 100,
		PaidNum:        paidNum,
		PaidAmount:     math.Round(paidAmount*100) / 100,
		PlatformIncome: platformIncome,
	}

	return
}
