package forms

import (
	"errors"
	"github.com/spf13/cast"
	"strings"
	"time"
)

type OrderUserStatisticsReq struct {
	Playerid int64 `form:"playerid" json:"playerid" binding:"required"`
}

type OrderUserStatisticsRes struct {
	Playerid                int64   `json:"playerid"`
	OrderPlatform           int32   `json:"orderPlatform"`
	RechargeCount           int64   `json:"rechargeCount"`
	RechargeAmount          float64 `json:"rechargeAmount"`
	RechargeDiamondCount    int64   `json:"rechargeDiamondCount"`
	ConsumptionCoinCount    int64   `json:"consumptionCoinCount"`
	ConsumptionDiamondCount int64   `json:"consumptionDiamondCount"`
	FirstChargeAt           int64   `json:"firstChargeAt"`
	LastChargeAt            int64   `json:"lastChargeAt"`
}

type RechargeRankingReq struct {
	ListReq
	Platform   int32   `json:"platform" form:"platform"`
	CreatedAt  []int64 `json:"created_at" form:"created_at[]"`
	CreatedAt2 string  `json:"created_at2" form:"created_at"`
	Time       []time.Time
	ServerId   int32 `json:"server_id" form:"server_id"`
}

func (req *RechargeRankingReq) Check() error {
	// get导出时字段格式不一样
	if len(req.CreatedAt) == 0 && req.CreatedAt2 != "" {
		createdAt2 := strings.Split(req.CreatedAt2, ",")
		for _, v := range createdAt2 {
			req.CreatedAt = append(req.CreatedAt, cast.ToInt64(v))
		}
	}
	if req.CreatedAt != nil && len(req.CreatedAt) != 2 {
		return errors.New("时间必须选择一个区间或者留空")
	}

	if len(req.CreatedAt) == 2 {
		if req.CreatedAt[0] > req.CreatedAt[1] {
			return errors.New("时间选择的区间值不合理")
		}

		// 默认是毫秒
		req.Time = append(req.Time, time.Unix(req.CreatedAt[0]/1000, 0))
		req.Time = append(req.Time, time.Unix(req.CreatedAt[1]/1000, 0))
	}

	// 只显示前100
	req.Page = 1
	req.PerPage = 100

	return nil
}

type OrderDailyStatisticsListReq struct {
	ListReq
	ChannelId  string  `json:"channel_id" form:"channel_id"`
	CreatedAt  []int64 `json:"created_at" form:"created_at[]"`
	CreatedAt2 string  `json:"created_at2" form:"created_at"`
	Time       []time.Time
	IsExport   int32 `json:"is_export" form:"is_export"`
	ServerId   int32 `json:"server_id" form:"server_id"`
	Flag       int32 `json:"flag" form:"flag"`
}

func (req *OrderDailyStatisticsListReq) Check() error {
	// get导出时字段格式不一样
	if len(req.CreatedAt) == 0 && req.CreatedAt2 != "" {
		createdAt2 := strings.Split(req.CreatedAt2, ",")
		for _, v := range createdAt2 {
			req.CreatedAt = append(req.CreatedAt, cast.ToInt64(v))
		}
	}

	if req.CreatedAt != nil && len(req.CreatedAt) != 2 {
		return errors.New("时间必须选择一个区间或者留空")
	}

	if len(req.CreatedAt) == 2 {
		if req.CreatedAt[0] > req.CreatedAt[1] {
			return errors.New("时间选择的区间值不合理")
		}

		// 默认是毫秒
		req.Time = append(req.Time, time.Unix(req.CreatedAt[0]/1000, 0))
		req.Time = append(req.Time, time.Unix(req.CreatedAt[1]/1000, 0))
	}

	return nil
}

type OrderSevenStatisticsListReq struct {
	ListReq
	ChannelId  string  `json:"channel_id" form:"channel_id"`
	CreatedAt  []int64 `json:"created_at" form:"created_at[]"`
	CreatedAt2 string  `json:"created_at2" form:"created_at"`
	Time       []time.Time
	IsExport   int32 `json:"is_export" form:"is_export"`
	Platform   int32 `json:"platform" form:"platform"`
	ServerId   int32 `json:"server_id" form:"server_id"`
	Flag       int32 `json:"flag" form:"flag"`
}

func (req *OrderSevenStatisticsListReq) Check() error {
	// get导出时字段格式不一样
	if len(req.CreatedAt) == 0 && req.CreatedAt2 != "" {
		createdAt2 := strings.Split(req.CreatedAt2, ",")
		for _, v := range createdAt2 {
			req.CreatedAt = append(req.CreatedAt, cast.ToInt64(v))
		}
	}
	if req.CreatedAt != nil && len(req.CreatedAt) != 2 {
		return errors.New("时间必须选择一个区间或者留空")
	}

	if len(req.CreatedAt) == 2 {
		if req.CreatedAt[0] > req.CreatedAt[1] {
			return errors.New("时间选择的区间值不合理")
		}

		// 默认是毫秒
		req.Time = append(req.Time, time.Unix(req.CreatedAt[0]/1000, 0))
		req.Time = append(req.Time, time.Unix(req.CreatedAt[1]/1000, 0))
	}

	return nil
}

type OrderListReq struct {
	ListReq
	Platform   int32   `json:"platform" form:"platform"`
	PlayerID   int64   `json:"player_id" form:"player_id"`
	GoodsID    int32   `json:"goods_id" form:"goods_id"`
	OrderSn    string  `json:"order_sn" form:"order_sn"`
	OutTradeNo string  `json:"out_trade_no" form:"out_trade_no"`
	Status     int32   `json:"status" form:"status"`
	ChannelId  string  `json:"channel_id" form:"channel_id"`
	CreatedAt  []int64 `json:"created_at" form:"created_at[]"`
	CreatedAt2 string  `json:"created_at2" form:"created_at"`
	Time       []time.Time
	IsExport   int32 `json:"is_export" form:"is_export"`
	ServerId   int32 `json:"server_id" form:"server_id"`
	Flag       int32 `json:"flag" form:"flag"`
}

func (req *OrderListReq) Check() error {
	// get导出时字段格式不一样
	if len(req.CreatedAt) == 0 && req.CreatedAt2 != "" {
		createdAt2 := strings.Split(req.CreatedAt2, ",")
		for _, v := range createdAt2 {
			req.CreatedAt = append(req.CreatedAt, cast.ToInt64(v))
		}
	}
	if req.CreatedAt != nil && len(req.CreatedAt) != 2 {
		return errors.New("创建时间必须选择一个区间或者留空")
	}

	if len(req.CreatedAt) == 2 {
		if req.CreatedAt[0] > req.CreatedAt[1] {
			return errors.New("注册时间选择的区间值不合理")
		}
		// 默认是毫秒
		req.Time = append(req.Time, time.Unix(req.CreatedAt[0]/1000, 0))
		req.Time = append(req.Time, time.Unix(req.CreatedAt[1]/1000, 0))
	}

	return nil
}

type OrderListRes struct {
	ListRes
	SuccessfulOrder  int64   `json:"successful_order"`
	TransactionMoney float64 `json:"transaction_money"`
}

type AbnormalOrderListReq struct {
	ListReq
	PlayerID  int64   `json:"player_id" form:"player_id"`
	GoodsID   int32   `json:"goods_id" form:"goods_id"`
	OrderID   string  `json:"order_id" form:"order_id"`
	CreatedAt []int64 `json:"created_at" form:"created_at[]"`
	Time      []time.Time
	IsExport  int32 `json:"is_export" form:"is_export"`
	ServerId  int32 `json:"server_id" form:"server_id"`
}

func (req *AbnormalOrderListReq) Check() error {
	// get导出时字段格式不一样
	if req.CreatedAt != nil && len(req.CreatedAt) != 2 {
		return errors.New("创建时间必须选择一个区间或者留空")
	}

	if len(req.CreatedAt) == 2 {
		if req.CreatedAt[0] > req.CreatedAt[1] {
			return errors.New("注册时间选择的区间值不合理")
		}
		// 默认是毫秒
		req.Time = append(req.Time, time.Unix(req.CreatedAt[0]/1000, 0))
		req.Time = append(req.Time, time.Unix(req.CreatedAt[1]/1000, 0))
	}
	if req.ServerId <= 0 {
		return errors.New("未选择服务器")
	}
	return nil
}

type AbnormalOrderReissueReq struct {
	OrderId  string `json:"orderId" form:"orderId"`
	ServerId int    `json:"serverId" form:"serverId"`
}

func (req *AbnormalOrderReissueReq) Check() error {
	// get导出时字段格式不一样
	if req.OrderId == "" {
		return errors.New("订单ID不能为空")
	}
	if req.ServerId <= 0 {
		return errors.New("服务器ID不能为空")
	}
	return nil
}
