package service

import (
	"time"
	"youngee_b_api/app/dao"
	"youngee_b_api/app/vo"
)

type EnterpriseService struct{}

func (e EnterpriseService) GetEnterpriseTakegoodsInfo(enterpriseId string, dateRange string) vo.ReWorkspaceTakegoods {
	var result vo.ReWorkspaceTakegoods
	//result.Pay = 1234.5
	//result.PayList = []float64{4, 6, 9}
	// 处理时间范围
	switch dateRange {
	case "7days":
		dates := getLastNDays(7)
		result = calcTakegoodsInfo(dates, enterpriseId)
		break
	case "30days":
		dates := getLastNDays(30)
		result = calcTakegoodsInfo(dates, enterpriseId)
		break
	case "90days":
		dates := getLastNDays(90)
		result = calcTakegoodsInfo(dates, enterpriseId)
		break
	case "monthly":
		dates := getCurrentMonthDates()
		result = calcTakegoodsInfo(dates, enterpriseId)
		break
	}

	return result
}

func getLastNDays(n int) []time.Time {
	var dates []time.Time
	today := time.Now()
	for i := 0; i < n; i++ {
		date := today.AddDate(0, 0, -i)
		dates = append(dates, date)
	}
	return dates
}

func getCurrentMonthDates() []time.Time {
	var dates []time.Time
	today := time.Now()
	year, month, _ := today.Date()
	location := today.Location()

	firstOfMonth := time.Date(year, month, 1, 0, 0, 0, 0, location)
	nextMonth := firstOfMonth.AddDate(0, 1, 0)

	for current := firstOfMonth; current.Before(nextMonth); current = current.AddDate(0, 0, 1) {
		dates = append(dates, current)
	}
	return dates
}

func calcTakegoodsInfo(dates []time.Time, enterpriseId string) vo.ReWorkspaceTakegoods {
	var pay, finish, commission, commissionRate float64
	var order, person int64
	var payList, finishList, commissionList, commissionRateList []float64
	var orderList, personList []int64

	for _, date := range dates {
		enterprises, _ := (&dao.SelectionInfoDAO{}).GetSelectionInfoListOfDay(enterpriseId, date)
		if enterprises != nil {
			var currentPay float64
			var currentFinish float64
			var currentCommission float64
			var currentCommissionRate float64
			var currentOrder int64
			var currentPerson int64
			for _, enterprise := range enterprises {
				// 带货数据
				currentPay += enterprise.EstimatedCost
				currentFinish += enterprise.SettlementAmount
				currentCommission += enterprise.EstimatedCost * enterprise.CommissionRate
				currentOrder += enterprise.SampleNum - enterprise.RemainNum
				// 出单数量
				currentPerson, _ = (&dao.SelectionTaskInfoDao{}).CountBySelectionId(enterprise.SelectionID)
				currentCommissionRate = enterprise.SettlementAmount / float64(currentPerson)
			}
			// 带货数据
			pay += currentPay
			payList = append(payList, currentPay)
			finish += currentFinish
			finishList = append(finishList, currentFinish)
			commission += currentCommission
			commissionList = append(commissionList, currentCommission)
			order += currentOrder
			orderList = append(orderList, currentOrder)
			// 出单数量
			person += currentPerson
			personList = append(personList, person)
			commissionRate += currentCommissionRate
			commissionRateList = append(commissionRateList, currentCommissionRate)
		}
	}
	res := vo.ReWorkspaceTakegoods{
		Pay:                pay,
		PayList:            payList,
		Finish:             finish,
		FinishList:         finishList,
		Commission:         commission,
		CommissionList:     commissionList,
		Order:              order,
		OrderList:          orderList,
		Person:             person,
		PersonList:         personList,
		CommissionRate:     commissionRate,
		CommissionRateList: commissionRateList,
	}
	return res
}
