package forms

import (
	"errors"
	"time"
)

type ChangedLogsListReq struct {
	ListReq
	ChannelId  string  `json:"channel_id" form:"channel_id"`
	UserId     int64   `json:"user_id" form:"user_id"`
	Source     int32   `json:"source" form:"source"`
	Createtime []int64 `json:"createtime" form:"createtime[]"`
	ServerId   int32   `json:"server_id" form:"server_id"`
}

func (req *ChangedLogsListReq) Check() error {

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

	if len(req.Createtime) != 2 {
		return errors.New("必须选择一个有效的时间")
	}

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

		// 默认是毫秒
		req.Createtime[0] = req.Createtime[0] / 1000
		req.Createtime[1] = req.Createtime[1] / 1000

		if req.Createtime[1]-req.Createtime[0] > 86400*31 {
			return errors.New("时间选择区间不能超过1个月")
		}
	}

	return nil
}

type DiamondRankingReq struct {
	ListReq
	Type       int64   `json:"type" form:"type"`
	Createtime []int64 `json:"createtime" form:"createtime[]"`
	ServerId   int32   `json:"server_id" form:"server_id"`
}

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

	if len(req.Createtime) == 2 {
		req.Createtime[0] = req.Createtime[0] / 1000
		req.Createtime[1] = req.Createtime[1] / 1000
	}

	return nil
}

type ItemRankingReq struct {
	ListReq
	Source     int64   `json:"source" form:"source"`
	Createtime []int64 `json:"createtime" form:"createtime[]"`
	ServerId   int32   `json:"server_id" form:"server_id"`
}

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

	if len(req.Createtime) == 2 {
		req.Createtime[0] = req.Createtime[0] / 1000
		req.Createtime[1] = req.Createtime[1] / 1000
	}

	if req.Source <= 0 {
		return errors.New("必须选择一个有效的操作来源")
	}
	return nil
}

type ConsumptionStatisticsReq struct {
	ListReq
	ChannelId  string   `json:"channel_id" form:"channel_id"`
	Type       int64    `json:"type" form:"type"`
	Source     int32    `json:"source" form:"source"`
	Createtime []int64  `json:"createtime" form:"createtime[]"`
	Date       []string `json:"date" form:"date[]"`
	ServerId   int32    `json:"server_id" form:"server_id"`
}

func (req *ConsumptionStatisticsReq) Check() error {

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

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

		req.Date = make([]string, 2)

		// 默认是毫秒
		req.Date[0] = time.Unix(req.Createtime[0]/1000, 0).Format("2006-01-02")
		req.Date[1] = time.Unix(req.Createtime[1]/1000, 0).Format("2006-01-02")
	}

	if req.ServerId <= 0 {
		return errors.New("请选择一个有效的服务器")
	}

	return nil
}
