package dto

import (
	"errors"
	"go-admin/app/admin/models"
	"go-admin/common/dto"
	common "go-admin/common/models"
	"strconv"
	"strings"
)

type LineApiUserGroupGetPageReq struct {
	dto.Pagination `search:"-"`
	GroupName      string `form:"groupName"  search:"type:contains;column:group_name;table:line_api_user_group" comment:"分组名称"`
	LineApiUserGroupOrder
}

type LineApiUserGroupOrder struct {
	Id        string `form:"idOrder"  search:"type:order;column:id;table:line_api_user_group"`
	GroupName string `form:"groupNameOrder"  search:"type:order;column:group_name;table:line_api_user_group"`
	UserId    string `form:"userIdOrder"  search:"type:order;column:user_id;table:line_api_user_group"`
	Status    string `form:"statusOrder"  search:"type:order;column:status;table:line_api_user_group"`
	CreatedAt string `form:"createdAtOrder"  search:"type:order;column:created_at;table:line_api_user_group"`
	UpdatedAt string `form:"updatedAtOrder"  search:"type:order;column:updated_at;table:line_api_user_group"`
	DeletedAt string `form:"deletedAtOrder"  search:"type:order;column:deleted_at;table:line_api_user_group"`
	CreateBy  string `form:"createByOrder"  search:"type:order;column:create_by;table:line_api_user_group"`
	UpdateBy  string `form:"updateByOrder"  search:"type:order;column:update_by;table:line_api_user_group"`
}

func (m *LineApiUserGroupGetPageReq) GetNeedSearch() interface{} {
	return *m
}

type LineApiUserGroupInsertReq struct {
	Id           int    `json:"-" comment:"组件"` // 组件
	ExchangeType string `json:"exchangeType" comment:"交易类型"`

	GroupName string `json:"groupName" comment:"分组名称"`
	UserId    string `json:"userId" comment:"用户ids 逗号分割"`
	UserIds   []int  `json:"userIds" comment:"用户ids"`
	Status    int    `json:"status" comment:"状态 1-启用 2-禁用"`
	common.ControlBy
}

func (s *LineApiUserGroupInsertReq) Valid() error {
	if s.ExchangeType == "" {
		return errors.New("交易类型不能为空")
	}

	if s.GroupName == "" {
		return errors.New("分组名称不能为空")
	}

	if len(s.UserIds) == 0 {
		return errors.New("用户不能为空")
	}

	return nil
}

func (s *LineApiUserGroupInsertReq) Generate(model *models.LineApiUserGroup) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	strIds := make([]string, len(s.UserIds))
	for i, id := range s.UserIds {
		strIds[i] = strconv.Itoa(id)
	}
	model.GroupName = s.GroupName
	model.ExchangeType = s.ExchangeType
	model.UserId = strings.Join(strIds, ",")
	model.Status = s.Status
	model.CreateBy = s.CreateBy // 添加这而，需要记录是被谁创建的
}

func (s *LineApiUserGroupInsertReq) GetId() interface{} {
	return s.Id
}

type LineApiUserGroupUpdateReq struct {
	Id           int    `uri:"id" comment:"组件"` // 组件
	ExchangeType string `json:"exchangeType" comment:"交易类型"`
	GroupName    string `json:"groupName" comment:"分组名称"`
	UserId       string `json:"userId" comment:"用户ids 逗号分割"`
	UserIds      []int  `json:"userIds" comment:"用户ids"`
	Status       int    `json:"status" comment:"状态 1-启用 2-禁用"`
	common.ControlBy
}

func (s *LineApiUserGroupUpdateReq) Valid() error {
	if s.ExchangeType == "" {
		return errors.New("交易类型不能为空")
	}

	if s.GroupName == "" {
		return errors.New("分组名称不能为空")
	}

	if len(s.UserIds) == 0 {
		return errors.New("用户不能为空")
	}

	return nil
}
func (s *LineApiUserGroupUpdateReq) Generate(model *models.LineApiUserGroup) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	strIds := make([]string, len(s.UserIds))
	for i, id := range s.UserIds {
		strIds[i] = strconv.Itoa(id)
	}
	model.GroupName = s.GroupName
	model.ExchangeType = s.ExchangeType
	model.UserId = strings.Join(strIds, ",")
	model.Status = s.Status
	model.UpdateBy = s.UpdateBy // 添加这而，需要记录是被谁更新的
}

func (s *LineApiUserGroupUpdateReq) GetId() interface{} {
	return s.Id
}

// LineApiUserGroupGetReq 功能获取请求参数
type LineApiUserGroupGetReq struct {
	Id int `uri:"id"`
}

func (s *LineApiUserGroupGetReq) GetId() interface{} {
	return s.Id
}

// LineApiUserGroupDeleteReq 功能删除请求参数
type LineApiUserGroupDeleteReq struct {
	Ids []int `json:"ids"`
}

func (s *LineApiUserGroupDeleteReq) GetId() interface{} {
	return s.Ids
}

type LineApiUserGroupGetOptionsReq struct {
	ExchangeType string `form:"exchangeType"`
}

type LineApiUserGroupOptions struct {
	Id       int    `json:"id"`
	Label    string `json:"label"`
	Value    []int  `json:"value"`
	Disabled bool   `json:"disabled"`
}
