package contract

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

//合同操作记录单
type ContractOperationRecord struct {
	Id               int       `orm:"column(id);pk"`
	ContractId       int       `orm:"column(contract_id)";description:"合同id"`
	Operation        string    `description:"操作类型，add：新增，edit：编辑，apply：发起审批，cancel_apply：撤回审批，reject：拒绝审批，approval：审批操作，invalid：作废;长度36位"`
	OpUserId         int       `description:"操作人id"`
	OpUserName       string    `description:"操作人名称"`
	Remark           string    `description:"备注，长度255位"`
	ApprovalRecordId int       `orm:"column(approval_record_id)";description:"审批流id"`
	CreateTime       time.Time `description:"日志添加时间"`
}

//根据合同id获取整个合同的操作日志
func GetContractOperationRListByContractId(contractId int) (list []*ContractOperationRecord, err error) {
	o := orm.NewOrm()
	sql := `select * from contract_operation_record where contract_id = ? order by id desc `
	_, err = o.Raw(sql, contractId).QueryRows(&list)
	return
}

//添加合同操作日志
func AddContractOperationRecord(contractId, opUserId, approvalRecordId int, operation, opUserName, remark string) (err error) {
	o := orm.NewOrm()
	contractOperationRecord := &ContractOperationRecord{
		ContractId:       contractId,
		Operation:        operation,
		OpUserId:         opUserId,
		OpUserName:       opUserName,
		Remark:           remark,
		ApprovalRecordId: approvalRecordId,
		CreateTime:       time.Now(),
	}
	_, err = o.Insert(contractOperationRecord)
	return
}
