package card_t_mall_order

import (
	"context"
	"fmt"
	"kami/internal/dao"
	"kami/internal/model"
	"kami/utility/config"
	"math"

	"github.com/gogf/gf/v2/os/gtime"
)

// GetOrderSummary 获取订单统计
func (h *sRechargeTMallGameOrder) GetOrderSummary(ctx context.Context, input *model.TMallSummaryListRecordInput) (total int, record []model.TMallSummaryListRecordOutput, err error) {
	total = 0
	record = make([]model.TMallSummaryListRecordOutput, 0)
	m := dao.V1RechargeTMallOrder.Ctx(ctx).DB(config.GetDatabaseV1()).
		FieldSum(fmt.Sprintf("CASE WHEN `%s` = 'finished' THEN 1 ELSE 0 END", dao.V1RechargeTMallOrder.Columns().Status), "succeedCount").
		FieldSum(fmt.Sprintf("CASE WHEN `%s` = 'finished' THEN %s ELSE 0 END", dao.V1RechargeTMallOrder.Columns().Status, dao.V1SysUserPaymentRecords.Columns().Amount), "succeedAmount").
		FieldCount(dao.V1RechargeTMallOrder.Columns().OrderNo, "count").
		FieldSum(dao.V1RechargeTMallOrder.Columns().Amount, "amount").
		Fields(fmt.Sprintf("DATE(`%s`.`%s`) AS `date`", dao.V1RechargeTMallOrder.Table(), dao.V1RechargeTMallOrder.Columns().CreatedAt)).
		Group(
			fmt.Sprintf("DATE(`%s`.`%s`)", dao.V1RechargeTMallOrder.Table(), dao.V1RechargeTMallOrder.Columns().CreatedAt),
			dao.V1RechargeTMallOrder.Columns().ChannelName,
		)
	if input.ChannelName != "" {
		m = m.Where(dao.V1RechargeTMallOrder.Columns().ChannelName, input.ChannelName)
	}
	if input.CallBackType != "" {
		//m = m.Fields(dao.V1RechargeTMallOrder.Columns().CallbackType)
		m = m.Where(dao.V1RechargeTMallOrder.Columns().CallbackType, input.CallBackType)
		//m = m.Group(dao.V1RechargeTMallOrder.Columns().CallbackType)
	}
	err = m.OrderDesc("date").
		Page(input.Current, input.PageSize).
		ScanAndCount(&record, &total, false)
	for i, output := range record {
		record[i].Rate = math.Round(float64(output.SucceedCount)/float64(output.Count)*100) / 100
		if math.IsNaN(output.Rate) {
			record[i].Rate = 0
		}
	}
	return
}

func (h *sRechargeTMallGameOrder) GetOrderStats(ctx context.Context, input *model.TMallGameStatsInput) (output *model.TMallGameStatsOutput, err error) {
	output = &model.TMallGameStatsOutput{}
	m := dao.V1RechargeTMallOrder.Ctx(ctx).DB(config.GetDatabaseV1()).
		FieldSum(fmt.Sprintf("CASE WHEN %s = 'finished' THEN %s ELSE 0 END", dao.V1RechargeTMallOrder.Columns().Status, dao.V1RechargeTMallOrder.Columns().Amount), "totalSucceedAmount").
		FieldSum(fmt.Sprintf("CASE WHEN %s = 'finished' THEN 1 ELSE 0 END", dao.V1RechargeTMallOrder.Columns().Status), "totalSucceedCount").
		FieldCount(dao.V1RechargeTMallOrder.Columns().OrderNo, "totalCount").
		FieldSum(dao.V1RechargeTMallOrder.Columns().Amount, "totalAmount")
	if input.ChannelName != "" {
		m = m.Where(dao.V1RechargeTMallOrder.Columns().ChannelName, input.ChannelName)
	}
	err = m.OrderDesc(dao.V1RechargeTMallOrder.Columns().CreatedAt).Scan(&output)
	m = dao.V1RechargeTMallOrder.Ctx(ctx).DB(config.GetDatabaseV1()).
		FieldSum(fmt.Sprintf("CASE WHEN %s = 'finished' THEN %s ELSE 0 END", dao.V1RechargeTMallOrder.Columns().Status, dao.V1RechargeTMallOrder.Columns().Amount), "todaySucceedAmount").
		FieldSum(fmt.Sprintf("CASE WHEN %s = 'finished' THEN 1 ELSE 0 END", dao.V1RechargeTMallOrder.Columns().Status), "todaySucceedCount").
		FieldCount(dao.V1RechargeTMallOrder.Columns().OrderNo, "todayCount").
		FieldSum(dao.V1RechargeTMallOrder.Columns().Amount, "todayAmount")
	if input.ChannelName != "" {
		m = m.Where(dao.V1RechargeTMallOrder.Columns().ChannelName, input.ChannelName)
	}
	err = m.Wheref("DATE(`%s`)=%s", dao.V1RechargeTMallOrder.Columns().CreatedAt, gtime.Date()).Scan(&output)
	output.TotalRate = math.Round(float64(output.TotalSucceedCount)/float64(output.TotalCount)*100) / 100
	output.TodayRate = math.Round(float64(output.TodaySucceedCount)/float64(output.TodayCount)*100) / 100
	if math.IsNaN(output.TotalRate) {
		output.TotalRate = 0
	}
	if math.IsNaN(output.TodayRate) {
		output.TodayRate = 0
	}
	return
}
