package fms

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

// ContractService 合同服务套餐
type ContractService struct {
	ContractServiceId  int       `gorm:"primaryKey;column:contract_service_id" json:"contract_service_id"`
	ContractRegisterId int       `gorm:"column:contract_register_id" json:"contract_register_id" description:"登记ID"`
	ProductId          int       `gorm:"column:product_id" json:"product_id" description:"产品ID"`
	ServiceTemplateId  int       `gorm:"column:service_template_id" json:"service_template_id" description:"合同服务模板ID"`
	Title              string    `gorm:"column:title" json:"title" description:"套餐名称"`
	Value              string    `gorm:"column:value" json:"value" description:"套餐的值"`
	TableValue         string    `gorm:"column:table_value" json:"table_value" description:"表格数据，用于word生成时的json数据"`
	HasDetail          string    `gorm:"column:has_detail" json:"has_detail" description:"是否有详情: 无; 有"`
	ChartPermissionId  int       `gorm:"column:chart_permission_id" json:"chart_permission_id" description:"权限ID"`
	ChartPermissionIds string    `gorm:"column:chart_permission_ids" json:"chart_permission_ids" description:"权限IDs-小套餐"`
	CreateTime         time.Time `gorm:"autoCreateTime;column:create_time" json:"create_time" description:"创建时间"`
	ModifyTime         time.Time `gorm:"autoUpdateTime:milli;column:modify_time" json:"modify_time" description:"最后更新时间"`
}

// GetContractRegisterIdsByTempId 套餐模板ID获取关联的合同登记ID
func GetContractRegisterIdsByTempId(tempIds []string) (registerIds []int, err error) {
	o := orm.NewOrmUsingDB("fms")
	sql := `SELECT DISTINCT contract_register_id FROM contract_service WHERE service_template_id in (` + utils.GetOrmInReplace(len(tempIds)) + `)`
	_, err = o.Raw(sql, tempIds).QueryRows(&registerIds)
	return
}

// GetContractRegisterServicesByRegisterIds 合同登记IDs获取套餐拼接字符串
func GetContractRegisterServicesByRegisterIds(registerIds []int) (list []*ContractService, err error) {
	o := orm.NewOrmUsingDB("fms")
	sql := `SELECT * FROM contract_service WHERE contract_register_id IN (` + utils.GetOrmInReplace(len(registerIds)) + `)`
	_, err = o.Raw(sql, registerIds).QueryRows(&list)
	return
}
