package fms

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// ContractServiceTemplate 合同服务模板表
type ContractServiceTemplate struct {
	ServiceTemplateId int       `gorm:"primaryKey;column:service_template_id" json:"service_template_id"`
	ProductId         int       `gorm:"column:product_id" json:"product_id" description:"产品id"`
	Pid               int       `gorm:"column:pid" json:"pid" description:"父级id"`
	Title             string    `gorm:"column:title" json:"title" description:"套餐名称"`
	Value             string    `gorm:"column:value" json:"value" description:"对应的数据"`
	SelectType        string    `gorm:"column:select_type" json:"select_type" description:"选择器类型，单选还是多选，radio；单选：checkbox"`
	SelectName        string    `gorm:"column:select_name" json:"select_name" description:"选择器名称"`
	TableValue        string    `gorm:"column:table_value" json:"table_value" description:"表格数据，用于word生成时的json数据"`
	Remark            string    `gorm:"column:remark" json:"remark" description:"表头备注"`
	ChartPermissionId int       `gorm:"column:chart_permission_id" json:"chart_permission_id" description:"权限id"`
	CreateTime        time.Time `gorm:"column:create_time" json:"create_time" description:"创建时间"`
	ModifyTime        time.Time `gorm:"column:modify_time" json:"modify_time" description:"修改时间"`
	IsDelete          int       `gorm:"column:is_delete" json:"-" description:"删除状态: 0-正常; 1-删除"`
}

type ContractServiceTemplateItem struct {
	ServiceTemplateId int    `json:"service_template_id" description:"套餐模板ID"`
	ProductId         int    `json:"product_id" description:"产品ID"`
	Pid               int    `json:"pid" description:"父级ID"`
	ParentTitle       string `json:"parent_title" description:"父级套餐名称"`
	Title             string `json:"title" description:"套餐名称"`
	Value             string `json:"value" description:"对应的数据"`
	SelectType        string `json:"select_type" description:"选择器类型：radio-单选; checkbox-多选"`
	SelectName        string `json:"select_name" description:"选择器名称"`
	ChartPermissionId int    `json:"chart_permission_id" description:"权限ID"`
}


// ContractServiceListReq CRM系统合同套餐-列表请求体
type ContractServiceListReq struct {
	ProductId string `json:"product_id" form:"product_id" description:"产品:空值-全部； 1-FICC; 2-权益"`
}

type ContractServiceTemplateFullName struct {
	ContractServiceTemplateItem
	ParentTitle string
}

type ContractServiceTemplateFormat struct {
	FormatTitle        string
	ServiceTemplateId  int
	ServiceTemplatePid int
	ServiceProductId   int
}

type ContractServiceTemplateFormatList []ContractServiceTemplateFormat

func (c ContractServiceTemplateFormatList) Len() int {
	return len(c)
}

func (c ContractServiceTemplateFormatList) Less(i, j int) bool {
	return c[i].ServiceTemplateId < c[j].ServiceTemplateId
}

func (c ContractServiceTemplateFormatList) Swap(i, j int) {
	c[i], c[j] = c[j], c[i]
}

func GetContractServiceTemplateFullName() (items []*ContractServiceTemplateFullName, err error) {
	o := orm.NewOrmUsingDB("fms")
	sql := `SELECT t1.*, t2.title as parent_title FROM contract_service_template as t1
		left join contract_service_template as t2 on t1.pid = t2.service_template_id
		WHERE t1.is_delete = 0  ORDER BY t1.service_template_id ASC `
	_,err = o.Raw(sql).QueryRows(&items)

	return
}

func GetContractServiceTemplateSecond() (items []*ContractServiceTemplateItem, err error) {
	o := orm.NewOrmUsingDB("fms")
	sql := `SELECT * FROM contract_service_template
		WHERE pid = 0 AND is_delete = 0  ORDER BY service_template_id ASC `
	_,err = o.Raw(sql).QueryRows(&items)

	return
}

type ContractServiceTemplateMapItems struct {
	ContractServiceTemplateItem
	Children []*ContractServiceTemplateMapItems `json:"children"`
	Detail   []*ContractServiceDetail           `json:"detail"`
}

// ContractServiceDetail 合同服务内容详情/表单数据
type ContractServiceDetail struct {
	Id                 int       `gorm:"primaryKey;column:id" json:"id"`
	ContractServiceId  int       `gorm:"column:contract_service_id" json:"contract_service_id" description:"合同服务id"`
	ContractRegisterId int       `gorm:"column:contract_register_id" json:"contract_register_id" description:"合同id"`
	ServiceTemplateId  int       `gorm:"column:service_template_id" json:"service_template_id" description:"服务模板id"`
	Col1               string    `gorm:"column:col_1" json:"col_1" description:"第1列数据"`
	Col2               string    `gorm:"column:col_2" json:"col_2" description:"第2列数据"`
	Col3               string    `gorm:"column:col_3" json:"col_3" description:"第3列数据"`
	Col4               string    `gorm:"column:col_4" json:"col_4" description:"第4列数据"`
	Col5               string    `gorm:"column:col_5" json:"col_5" description:"第5列数据"`
	Col6               string    `gorm:"column:col_6" json:"col_6" description:"第6列数据"`
	Col7               string    `gorm:"column:col_7" json:"col_7" description:"第7列数据"`
	CreateTime         time.Time `gorm:"column:create_time" json:"create_time" description:"创建时间"`
}


type ContractServiceTemplateList struct {
	Title    string                             `json:"title"`
	Children []*ContractServiceTemplateMapItems `json:"children"`
}


func GetContractServiceTemplateMapByParentId(parentId int) (items []*ContractServiceTemplateItem, err error) {
	o := orm.NewOrmUsingDB("fms")
	sql := `SELECT * FROM contract_service_template 
		WHERE pid = ? AND is_delete = 0   `
	_,err = o.Raw(sql, parentId).QueryRows(&items)

	return
}
