package contract

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

//合同的服务内容
type ContractService struct {
	ContractServiceId int       `orm:"column(contract_service_id);pk"`
	ContractId        int       `description:"合同id"`
	ProductId         int       `description:"产品id，1：ficc；2：权益"`
	ServiceTemplateId int       `description:"合同服务模板id"`
	Title             string    `description:"套餐名称"`
	Value             string    `description:"套餐的值"`
	TableValue        string    `description:"表格数据，用于word生成时的json数据"`
	HasDetail         string    `description:"是否有详情，枚举值：是、否；默认：否"`
	ChartPermissionId int       `description:"权限id"`
	CreateTime        time.Time `description:"合同添加时间"`
}

//合同的服务内容
type ContractServiceAndDetail struct {
	ContractServiceId int    `orm:"column(contract_service_id);pk"`
	ContractId        int    `description:"合同id"`
	ProductId         int    `description:"产品id，1：ficc；2：权益"`
	ServiceTemplateId int    `description:"合同服务模板id"`
	Title             string `description:"套餐标题"`
	Value             string `description:"套餐的值"`
	HasDetail         string `description:"是否有详情，枚举值：是、否；默认：否"`
	TableValue        string `description:"表格数据，用于word生成时的json数据"`
	ChartPermissionId int    `description:"权限id"`
	DetailList        []*ContractServiceDetail
}

// GetContractServiceAndDetailList 根据id获取合同列表数据
func GetContractServiceAndDetailList(contractId int) (list []*ContractServiceAndDetail, err error) {
	o := orm.NewOrm()
	sql := "select * from contract_service where contract_id = ?  "
	_, err = o.Raw(sql, contractId).QueryRows(&list)
	return
}

// GetContractServiceAndDetailListByIds 根据id集合获取合同列表数据
func GetContractServiceAndDetailListByIds(contractIds string) (list []*ContractServiceAndDetail, err error) {
	o := orm.NewOrm()
	sql := `select * from contract_service where contract_id in (` + contractIds + `) `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetContractServiceListByIds 根据合同IDs获取合同服务列表
func GetContractServiceListByIds(contractIds string) (list []*ContractService, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM contract_service WHERE contract_id IN (` + contractIds + `) `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// MultiCreateContractService 批量新增合同套餐
func MultiCreateContractService(list []*ContractService) (err error) {
	listLen := len(list)
	if listLen == 0 {
		return
	}
	o := orm.NewOrm()
	_, err = o.InsertMulti(listLen, list)
	return
}