package service

import (
	"math"
	"merchant/consts"
	"merchant/models/account"
	"merchant/models/order"
	"time"
)

func QueryTotalSummary(params map[string]string) account.Summary {
	orderInfoList := order.GetOrderByMap(params, -1, 0)

	bankOrderIdList := make([]string, 0)

	for _, info := range orderInfoList {
		bankOrderIdList = append(bankOrderIdList, info.BankOrderId)
	}

	orderProfitList := order.GetOrderProfitListByBankOrderIdList(bankOrderIdList)

	supplierAll := 0.0
	platformAll := 0.0
	agentAll := 0.0
	allAmount := 0.0
	totalNum := 0
	succeedNum := 0
	succeedPaidAmount := 0.0

	for _, v := range orderProfitList {
		totalNum += 1
		allAmount += v.ShowAmount //订单总金额

		if v.Status != "success" {
			continue
		}

		succeedNum += 1
		succeedPaidAmount += v.FactAmount
		supplierAll += v.SupplierProfit //上游利润
		platformAll += v.PlatformProfit //平台利润
		agentAll += v.AgentProfit       //代理利润
	}

	succeedRate := 0.0

	if totalNum != 0 {
		succeedRate = math.Round(float64(succeedNum)/float64(totalNum)*100*100) / 100
	} else {
		succeedRate = 0
	}

	return account.Summary{
		TotalNum:       totalNum,
		TotalAmount:    math.Round(allAmount*100) / 100,
		PaidNum:        succeedNum,
		PaidAmount:     math.Round(succeedPaidAmount*100) / 100,
		PlatformIncome: math.Round(platformAll*100) / 100,
		SucceedRate:    succeedRate,
	}
}

func QuerySummaryByOrderInfo(infoInfoList []order.OrderInfo) account.Summary {
	orderProfitInfoList := make([]order.OrderProfitInfo, 0)

	for _, info := range infoInfoList {
		orderProfitInfoList = append(orderProfitInfoList, order.GetOrderProfitByBankOrderId(info.BankOrderId))
	}

	totalNum := 0
	todayAllAmount := 0.0
	todaySupplierAll := 0.0
	todayPlatformAll := 0.0
	todayAgentAll := 0.0
	TodaySuccessNum := 0

	for _, info := range orderProfitInfoList {
		totalNum += 1
		todayAllAmount += info.FactAmount

		if info.Status != consts.SUCCESS {
			continue
		}

		todaySupplierAll += info.SupplierProfit
		todayPlatformAll += info.PlatformProfit
		todayAgentAll += info.AgentProfit
		TodaySuccessNum += 1
	}

	return account.Summary{
		TotalNum:       totalNum,
		TotalAmount:    math.Round(todayAllAmount*100) / 100,
		PaidNum:        TodaySuccessNum,
		PlatformIncome: math.Round(todayPlatformAll*100) / 100,
		AgencyIncome:   todayAgentAll,
	}
}

func QueryTodaySummary(in map[string]string) account.Summary {
	now := time.Now()
	todayAtMidnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
	tomorrowAtMidNight := time.Date(now.Year(), now.Month(), now.Day(), 59, 59, 59, 0, now.Location())
	st := todayAtMidnight.Format("2006-01-02 15:04:05")
	end := tomorrowAtMidNight.Format("2006-01-02 15:04:05")
	paramsProfit := in

	if startTimeStr, exists := paramsProfit["create_time__gte"]; exists {
		startTime, err := time.Parse("2006-01-02 15:04:05", startTimeStr)
		if err != nil {
			paramsProfit["create_time__gte"] = st
		} else {
			if tomorrowAtMidNight.Before(startTime) || todayAtMidnight.After(startTime) {
				paramsProfit["create_time__gte"] = st
			}
		}
	} else {
		paramsProfit["create_time__gte"] = st
	}

	if endTimeStr, exists := paramsProfit["create_time__lte"]; exists {
		endTime, err := time.Parse("2006-01-02 15:04:05", endTimeStr)
		if err != nil {
			paramsProfit["create_time__gte"] = st
		} else {
			if tomorrowAtMidNight.Before(endTime) || todayAtMidnight.After(endTime) {
				paramsProfit["create_time__gte"] = st
			}
		}
	} else {
		paramsProfit["create_time__lte"] = end
	}

	orderInfoList := order.GetOrderByMap(paramsProfit, -1, 0)

	bankOrderIDLst := make([]string, 0)
	for _, info := range orderInfoList {
		bankOrderIDLst = append(bankOrderIDLst, info.BankOrderId)
	}
	dataInfo := order.GetOrderProfitListByBankOrderIdList(bankOrderIDLst)

	totalNum := 0
	todayAllAmount := 0.0
	todaySupplierAll := 0.0
	todayPlatformAll := 0.0
	todayAgentAll := 0.0
	todaySuccessNum := 0
	todayPaidAmount := 0.0

	for _, info := range dataInfo {
		totalNum += 1
		todayAllAmount += info.FactAmount

		if info.Status != consts.SUCCESS {
			continue
		}

		todaySupplierAll += info.SupplierProfit
		todayPlatformAll += info.PlatformProfit
		todayAgentAll += info.AgentProfit
		todaySuccessNum += 1
		todayPaidAmount += info.FactAmount
	}

	succeedRate := 0.0

	if totalNum != 0 {
		succeedRate = math.Round(float64(todaySuccessNum)/float64(totalNum)*100*100) / 100
	} else {
		succeedRate = 0
	}

	return account.Summary{
		TotalNum:       totalNum,
		TotalAmount:    todayAllAmount,
		PaidNum:        todaySuccessNum,
		PaidAmount:     todayPaidAmount,
		PlatformIncome: math.Round(todayPlatformAll*100) / 100,
		SucceedRate:    succeedRate,
	}
}
