package contract

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

//合同审批单
type ContractApprovalRecord struct {
	ContractApprovalRecordId int       `orm:"column(contract_approval_record_id);pk"`
	ContractApprovalId       int       `orm:"column(contract_approval_id)" description:"审批单id"`
	Status                   string    `description:"合同状态，枚举值：待审批','已审批','已驳回','已撤回'，默认待审批"`
	ApproveRemark            string    `description:"审批人备注，可以是驳回理由，长度128位"`
	ApproveRoleTypeCode      string    `description:"审批人角色类型，长度36位"`
	ApproveUserId            int       `description:"审批人id"`
	ApproveUserName          string    `description:"审批人名称"`
	NodeId                   int       `description:"当前审批节点id"`
	NodeType                 string    `description:"节点类型，审批人：check；抄送人：cc（Carbon Copy）"`
	PrevNodeId               int       `description:"上级节点id"`
	NextNodeId               int       `description:"下级节点id"`
	AuditType                int       `description:"多人审批时，1：或签（一名审批人同意即可），2：会签（需所有审批人同意）"`
	ApproveTime              time.Time `description:"审批时间"`
	ModifyTime               time.Time `description:"发起申请时间"`
	CreateTime               time.Time `description:"最近一次审批单修改时间"`
}

//根据合同审批记录id获取合同流程记录信息
func GetContractApprovalRecordById(contractApprovalRecordId int) (contractApprovalRecordInfo *ContractApprovalRecord, err error) {
	o := orm.NewOrm()
	sql := `select * from contract_approval_record where contract_approval_record_id = ? `
	err = o.Raw(sql, contractApprovalRecordId).QueryRow(&contractApprovalRecordInfo)
	return
}

//根据审批单id获取当前待审批的合同审批流信息
func GetCurrContractApprovalRecordByContractId(contractApprovalId int) (contractApprovalRecordInfo *ContractApprovalRecord, err error) {
	o := orm.NewOrm()
	sql := `select a.* from contract_approval_record a 
 left join contract_approval b on a.contract_approval_id=b.contract_approval_id and a.node_id=b.curr_node_id
 where a.status = "待审批" 
  AND a.contract_approval_id=? order by a.node_id asc`
	err = o.Raw(sql, contractApprovalId).QueryRow(&contractApprovalRecordInfo)
	return
}

//根据审批单id和节点id获取当前待审批的合同审批流信息列表
func GetPendingContractApprovalRecordListByContractId(contractApprovalId, nodeId int) (contractApprovalRecordList []*ContractApprovalRecord, err error) {
	o := orm.NewOrm()
	sql := `select a.* from contract_approval_record a 
 left join contract_approval b on a.contract_approval_id=b.contract_approval_id 
 where a.status = "待审批" 
  AND a.contract_approval_id=? AND node_id  >= ? order by a.node_id asc`
	_, err = o.Raw(sql, contractApprovalId, nodeId).QueryRows(&contractApprovalRecordList)
	return
}

//根据审批单id和节点id获取当前节点的合同审批流信息列表
func GetContractApprovalRecordListByContractIdAndNode(contractApprovalId, nodeId int) (contractApprovalRecordList []*ContractApprovalRecord, err error) {
	o := orm.NewOrm()
	sql := `select a.* from contract_approval_record a 
 join contract_approval b on a.contract_approval_id=b.contract_approval_id
 where 1=1 
  AND a.contract_approval_id=? AND node_id  = ? order by a.node_id asc`
	_, err = o.Raw(sql, contractApprovalId, nodeId).QueryRows(&contractApprovalRecordList)
	return
}

//根据审批单id和节点id获取当前节点的合同审批流信息列表
func GetContractApprovalRecordListByContractApprovalId(contractApprovalId int) (contractApprovalRecordList []*ContractApprovalRecord, err error) {
	o := orm.NewOrm()
	sql := `select a.* from contract_approval_record a 
  join contract_approval b on a.contract_approval_id=b.contract_approval_id
  AND a.contract_approval_id=? AND node_type  = "cc" order by a.node_id asc`
	_, err = o.Raw(sql, contractApprovalId).QueryRows(&contractApprovalRecordList)
	return
}

//根据审批单id获取最近一次待审批的合同审批流信息
func GetLastPendingContractApprovalRecordByContractId(contractApprovalId int) (contractApprovalRecordInfo *ContractApprovalRecord, err error) {
	o := orm.NewOrm()
	sql := `select * from contract_approval_record where status = "待审批" 
  AND contract_approval_id=? order by contract_approval_record_id desc`
	err = o.Raw(sql, contractApprovalId).QueryRow(&contractApprovalRecordInfo)
	return
}

//根据审批单id批量获取最新的审批流列表数据
func GetContractApprovalRecordListOld(contractApprovalIds string) (list []*ContractApprovalRecord, err error) {
	o := orm.NewOrm()
	sql := `
SELECT
a.* from contract_approval_record a
	join 
	( SELECT max( contract_approval_record_id ) max_id,contract_approval_id FROM contract_approval_record where contract_approval_id in (` + contractApprovalIds + `) GROUP BY contract_approval_id `
	sql += ` ) b on a.contract_approval_record_id=b.max_id
WHERE a.contract_approval_id in (` + contractApprovalIds + `) `
	sql += ` order by contract_approval_record_id desc  `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}
func GetContractApprovalRecordList(contractApprovalIds string, approveUserId int) (list []*ContractApprovalRecord, err error) {
	o := orm.NewOrm()
	sql := `
SELECT
a.* from contract_approval_record a
	join contract_approval b on a.contract_approval_id = b.contract_approval_id and a.node_id=b.curr_node_id
WHERE a.contract_approval_id in (` + contractApprovalIds + `) AND (approve_user_id = ? or approve_user_id=0)`
	sql += ` order by contract_approval_record_id desc  `
	_, err = o.Raw(sql, approveUserId).QueryRows(&list)
	return
}

// GetContractApprovalRecordListByContractApprovalIds 根据审批单IDs获取审批节点列表
func GetContractApprovalRecordListByContractApprovalIds(contractApprovalIds string) (contractApprovalRecordList []*ContractApprovalRecord, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM contract_approval_record WHERE contract_approval_id IN (` + contractApprovalIds + `)`
	_, err = o.Raw(sql).QueryRows(&contractApprovalRecordList)
	return
}

// GetApprovalRecordListByApprovalId 获取指定ID审批单下的审批节点列表
func GetApprovalRecordListByApprovalId(contractApprovalId int) (contractApprovalRecordList []*ContractApprovalRecord, err error) {
	o := orm.NewOrm()
	sql := `select * from contract_approval_record where contract_approval_id=? order by node_id,contract_approval_record_id asc`
	_, err = o.Raw(sql, contractApprovalId).QueryRows(&contractApprovalRecordList)
	return
}

// GetSelfContractApprovalRecordList  根据审批单id批量获取自己的审批流列表数据
func GetSelfContractApprovalRecordList(contractApprovalIds string, approveUserId int) (list []*ContractApprovalRecord, err error) {
	o := orm.NewOrm()
	sql := `
SELECT
a.* from contract_approval_record a
	join contract_approval b on a.contract_approval_id = b.contract_approval_id 
WHERE a.contract_approval_id in (` + contractApprovalIds + `) AND (approve_user_id = ? or approve_user_id=?) 
and a.node_id<=b.curr_node_id`
	sql += ` order by contract_approval_record_id desc  `
	_, err = o.Raw(sql, approveUserId, approveUserId).QueryRows(&list)
	return
}
