package contract

import "github.com/beego/beego/v2/client/orm"

// ContractRelation 合同关系表结构体
type ContractRelation struct {
	ContractId                int `description:"业务合同id"`
	PaymentOnBehalfContractId int `description:"代付合同id"`
}

type RelationContractList struct {
	ContractId                int     `description:"合同id"`
	CompanyName               string  `description:"甲方名称"`
	PaymentOnBehalfContractId int     `description:"代付合同id"`
	Price                     float64 `description:"付款金额"`
	Service                   []*ContractServiceAndDetail
	//ContractId int `description:"合同id"`
}

// GetContractRelationListByRelationContractIds 根据业务合同id集合获取对应的业务合同基础信息
func GetContractRelationListByRelationContractIds(contractIdStr string) (list []*RelationContractList, err error) {
	o := orm.NewOrm()
	sql := `select b.contract_id,a.company_name,b.payment_on_behalf_contract_id from contract a join contract_relation b on a.contract_id=b.payment_on_behalf_contract_id where a.status in ("已签回","已审批") AND a.is_delete = 0 AND b.contract_id in (` + contractIdStr + `)  order by a.contract_id asc`

	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetContractRelationListByPaymentOnBehalfContractIds 根据代付合同id集合获取对应的业务合同基础信息
func GetContractRelationListByPaymentOnBehalfContractIds(contractIdStr string) (list []*RelationContractList, err error) {
	o := orm.NewOrm()
	sql := `select  a.contract_id,a.company_name,b.payment_on_behalf_contract_id from contract a join contract_relation b on a.contract_id=b.contract_id where a.status in ("已签回","已审批") AND a.is_delete = 0 AND b.payment_on_behalf_contract_id in (` + contractIdStr + `) order by a.contract_id asc`

	_, err = o.Raw(sql).QueryRows(&list)
	return
}

// GetContractRelationListByRelationContractId 根据业务合同id获取对应的代付合同基础信息
func GetContractRelationListByRelationContractId(contractId int) (list []*RelationContractList, err error) {
	o := orm.NewOrm()
	sql := `select b.contract_id,a.company_name,b.payment_on_behalf_contract_id from contract a join contract_relation b on  a.contract_id=b.payment_on_behalf_contract_id where a.status in ("已签回","已审批") AND a.is_delete = 0 AND b.contract_id =?  order by a.contract_id asc`

	_, err = o.Raw(sql, contractId).QueryRows(&list)
	return
}

// GetContractRelationListByPaymentOnBehalfContractId 根据代付合同id获取对应的业务合同基础信息
func GetContractRelationListByPaymentOnBehalfContractId(contractId int) (list []*RelationContractList, err error) {
	o := orm.NewOrm()
	sql := `select a.contract_id,a.company_name,b.payment_on_behalf_contract_id from contract a join contract_relation b on a.contract_id=b.contract_id where a.status in ("已签回","已审批") AND a.is_delete = 0 AND b.payment_on_behalf_contract_id =? order by a.contract_id asc`

	_, err = o.Raw(sql, contractId).QueryRows(&list)
	return
}

// GetContractRelationListByContractId 根据业务合同id获取已经给他代付过的合同列表
func GetContractRelationListByContractId(contractId int) (list []*RelationContractList, err error) {
	o := orm.NewOrm()
	sql := `select  b.contract_id,a.company_name,b.payment_on_behalf_contract_id,a.price from contract a join contract_relation b on  a.contract_id=b.payment_on_behalf_contract_id where a.status in ("已签回","已审批","待审批") AND a.is_delete = 0 AND b.contract_id =? order by a.contract_id asc`

	_, err = o.Raw(sql, contractId).QueryRows(&list)
	return
}
