package forms

import (
	"errors"
	"github.com/jinzhu/now"
	"strings"
	"time"
)

type ChatLogPlayerListReq struct {
	ListReq
	Time   []int64 `json:"time" form:"time[]"`
	AccId  int64   `json:"accId" form:"accId"`
	Name   string  `json:"name" form:"playerName"`
	Text   string  `json:"text" form:"text"`
	OpenId string  `json:"openId" form:"openId"`
}

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

	if len(req.Time) == 2 {
		if req.Time[0] > req.Time[1] {
			return errors.New("生成时间选择的区间值不合理")
		}
		// 默认是毫秒
		req.Time[0] = req.Time[0] / 1000
		req.Time[1] = req.Time[1] / 1000
	}
	//如果为空 默认今天
	if len(req.Time) == 0 {
		req.Time = append(req.Time, now.With(time.Now()).BeginningOfDay().Unix(), now.With(time.Now()).EndOfDay().Unix())
	}

	if req.OpenId != "" {
		if !strings.Contains(req.OpenId, "@") {
			req.OpenId = strings.Join([]string{req.OpenId, "@wx"}, "")
		}
	}
	return nil
}

type ChatLogInfoReq struct {
	ListReq
	OpenId string  `json:"openId" form:"openId"`
	Time   []int64 `json:"time" form:"time[]"`
}

func (req *ChatLogInfoReq) Check() error {
	if req.OpenId == "" {
		return errors.New("参数为空")
	}
	if req.Time != nil && len(req.Time) != 2 {
		return errors.New("生成时间必须选择一个区间或者留空")
	}

	if len(req.Time) == 2 {
		if req.Time[0] > req.Time[1] {
			return errors.New("生成时间选择的区间值不合理")
		}
		// 默认是毫秒
		req.Time[0] = req.Time[0] / 1000
		req.Time[1] = req.Time[1] / 1000
	}
	//如果为空 默认今天
	if len(req.Time) == 0 {
		req.Time = append(req.Time, now.With(time.Now()).BeginningOfDay().Unix(), now.With(time.Now()).EndOfDay().Unix())
	}
	return nil
}
