package dto

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

	"github.com/shopspring/decimal"
)

type MemberRenwalConfigGetPageReq struct {
	dto.Pagination `search:"-"`
	PackageName    string `form:"packageName"  search:"type:contains;column:package_name;table:member_renwal_config" comment:"套餐名称"`
	Status         string `form:"status"  search:"type:exact;column:status;table:member_renwal_config" comment:"套餐状态（字典 rewal_status）"`
	MemberRenwalConfigOrder
}

type MemberRenwalConfigOrder struct {
	Id            string `form:"idOrder"  search:"type:order;column:id;table:member_renwal_config"`
	Sort          string `form:"sortOrder" search:"type:order;column:sort;table:member_renwal_config"`
	PackageName   string `form:"packageNameOrder"  search:"type:order;column:package_name;table:member_renwal_config"`
	DurationDay   string `form:"durationDayOrder"  search:"type:order;column:duration_day;table:member_renwal_config"`
	OriginalPrice string `form:"originalPriceOrder"  search:"type:order;column:original_price;table:member_renwal_config"`
	DiscountPrice string `form:"discountPriceOrder"  search:"type:order;column:discount_price;table:member_renwal_config"`
	IsDefault     string `form:"isDefaultOrder"  search:"type:order;column:is_default;table:member_renwal_config"`
	Status        string `form:"statusOrder"  search:"type:order;column:status;table:member_renwal_config"`
	CreatedAt     string `form:"createdAtOrder"  search:"type:order;column:created_at;table:member_renwal_config"`
	UpdatedAt     string `form:"updatedAtOrder"  search:"type:order;column:updated_at;table:member_renwal_config"`
	DeletedAt     string `form:"deletedAtOrder"  search:"type:order;column:deleted_at;table:member_renwal_config"`
	CreateBy      string `form:"createByOrder"  search:"type:order;column:create_by;table:member_renwal_config"`
	UpdateBy      string `form:"updateByOrder"  search:"type:order;column:update_by;table:member_renwal_config"`
}

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

type MemberRenwalConfigInsertReq struct {
	Id            int              `json:"-" comment:"主键"` // 主键
	PackageName   string           `json:"packageName" comment:"套餐名称"`
	PackageNameEn string           `json:"packageNameEn" comment:"套餐名称英文"`
	DurationDay   int              `json:"durationDay" comment:"续期时间(天)"`
	OriginalPrice decimal.Decimal  `json:"originalPrice" comment:"原始单价"`
	DiscountPrice *decimal.Decimal `json:"discountPrice" comment:"折扣价格 -1为未设置"`
	IsDefault     int              `json:"isDefault" comment:"是否默认套餐 0-无 1-是 2-否"`
	Remark        string           `json:"remark" comment:"备注"`
	Status        string           `json:"status" comment:"套餐状态（字典 rewal_status）"`
	IsVisible     int              `json:"isVisible" comment:"是否可见 2-不可见 1-可见"`
	Sort          int              `json:"sort" comment:"排序"`
	common.ControlBy
}

// 校验
func (s *MemberRenwalConfigInsertReq) Valid() error {
	if s.PackageName == "" {
		return errors.New("套餐名称不能为空")
	}

	if s.PackageNameEn == "" {
		return errors.New("套餐名称英文不能为空")
	}

	if s.DurationDay <= 0 {
		return errors.New("续期时间(天)不能为空")
	}

	return nil
}

func (s *MemberRenwalConfigInsertReq) Generate(model *models.MemberRenwalConfig) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.PackageName = s.PackageName
	model.PackageNameEn = s.PackageNameEn
	model.DurationDay = s.DurationDay
	model.OriginalPrice = s.OriginalPrice

	if s.DiscountPrice == nil {
		model.DiscountPrice = decimal.NewFromInt(-1)
	} else {
		model.DiscountPrice = *s.DiscountPrice
	}
	model.IsDefault = s.IsDefault
	model.Remark = s.Remark
	model.Status = s.Status
	model.IsVisible = s.IsVisible
	model.Sort = s.Sort
	model.CreateBy = s.CreateBy // 添加这而，需要记录是被谁创建的
}

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

type MemberRenwalConfigUpdateReq struct {
	Id            int              `uri:"id" comment:"主键"` // 主键
	PackageName   string           `json:"packageName" comment:"套餐名称"`
	PackageNameEn string           `json:"packageNameEn" comment:"套餐名称英文"`
	DurationDay   int              `json:"durationDay" comment:"续期时间(天)"`
	OriginalPrice decimal.Decimal  `json:"originalPrice" comment:"原始单价"`
	DiscountPrice *decimal.Decimal `json:"discountPrice" comment:"折扣价格 -1为未设置"`
	IsDefault     int              `json:"isDefault" comment:"是否默认套餐 0-无 1-是 2-否"`
	Remark        string           `json:"remark" comment:"备注"`
	Status        string           `json:"status" comment:"套餐状态（字典 rewal_status）"`
	IsVisible     int              `json:"isVisible" comment:"是否可见 2-不可见 1-可见"`
	Sort          int              `json:"sort" comment:"排序"`
	common.ControlBy
}

// 校验
func (s *MemberRenwalConfigUpdateReq) Valid() error {
	if s.PackageName == "" {
		return errors.New("套餐名称不能为空")
	}

	if s.PackageNameEn == "" {
		return errors.New("套餐名称英文不能为空")
	}

	if s.DurationDay <= 0 {
		return errors.New("续期时间(天)不能为空")
	}

	return nil
}

func (s *MemberRenwalConfigUpdateReq) Generate(model *models.MemberRenwalConfig) {
	if s.Id == 0 {
		model.Model = common.Model{Id: s.Id}
	}
	model.PackageName = s.PackageName
	model.PackageNameEn = s.PackageNameEn
	model.DurationDay = s.DurationDay
	model.OriginalPrice = s.OriginalPrice

	if s.DiscountPrice == nil {
		model.DiscountPrice = decimal.NewFromInt(-1)
	} else {
		model.DiscountPrice = *s.DiscountPrice
	}
	model.IsDefault = s.IsDefault
	model.Status = s.Status
	model.Remark = s.Remark
	model.IsVisible = s.IsVisible
	model.Sort = s.Sort
	model.UpdateBy = s.UpdateBy // 添加这而，需要记录是被谁更新的
}

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

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

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

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

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

type MemberRenwalConfigAppResp struct {
	Id            int             `json:"id" comment:"主键"` // 主键
	PackageName   string          `json:"packageName" comment:"套餐名称"`
	DurationDay   int             `json:"durationDay" comment:"续期时间(天)"`
	OriginalPrice decimal.Decimal `json:"originalPrice" comment:"原始单价"`
	DiscountPrice decimal.Decimal `json:"discountPrice" comment:"折扣价格 -1为未设置"`
	Remark        string          `json:"remark" comment:"备注"`
}
