123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782 |
- package contract_approval
- import (
- "github.com/beego/beego/v2/client/orm"
- "hongze/hongze_mobile_admin/models/tables/contract"
- "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
- "hongze/hongze_mobile_admin/utils"
- "time"
- )
- //合同审批单
- type ContractApproval struct {
- ContractApprovalId int `orm:"column(contract_approval_id);pk"`
- ApprovalType string `description:"审批单类型,枚举值,合同:contract;用印:seal;默认:contract"`
- ContractId int `orm:"column(contract_id)" description:"合同id"`
- Status string `description:"审批单状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
- ApplyContent string `description:"待审内容,长度255位"`
- ContractDetail string `description:"审批单详情,完整的合同json数据"`
- ApplyUserId int `description:"申请人id"`
- ApplyUserName string `description:"申请人名称"`
- ApproveRemark string `description:"审批人备注,可以是驳回理由,长度128位"`
- FlowId int `description:"审批流程id"`
- FlowVersion int `description:"审批流程版本"`
- CurrNodeId int `description:"当前审批节点id"`
- StartNodeId int `description:"开始审批节点id"`
- ModifyTime time.Time `description:"发起申请时间"`
- CreateTime time.Time `description:"最近一次审批单修改时间"`
- }
- //根据合同审批单id获取合同审批单信息
- func GetContractApprovalById(contractApprovalId int) (contractApprovalInfo *ContractApproval, err error) {
- o := orm.NewOrm()
- sql := `select * from contract_approval where contract_approval_id = ? `
- err = o.Raw(sql, contractApprovalId).QueryRow(&contractApprovalInfo)
- return
- }
- //根据合同id获取最近一次待审批的合同审批单信息
- func GetLastPendingContractApprovalByContractId(contractId int) (contractApprovalInfo *ContractApproval, err error) {
- o := orm.NewOrm()
- sql := `select * from contract_approval where status = "待审批" AND contract_id=? order by contract_approval_id desc`
- err = o.Raw(sql, contractId).QueryRow(&contractApprovalInfo)
- return
- }
- //根据合同id获取最近一次提交的合同审批单信息
- func GetLastContractApprovalByContractId(contractId int, approvalType string) (contractApprovalInfo *ContractApproval, err error) {
- o := orm.NewOrm()
- sql := `select * from contract_approval where contract_id=? and approval_type =? order by contract_approval_id desc`
- err = o.Raw(sql, contractId, approvalType).QueryRow(&contractApprovalInfo)
- return
- }
- //根据合同id获取总共被驳回的次数
- func GetRejectContractCountByContractId(contractId int) (total int64, err error) {
- o := orm.NewOrm()
- sql := `select count(1) total from contract_approval where status = "已驳回" AND contract_id=? `
- err = o.Raw(sql, contractId).QueryRow(&total)
- return
- }
- //获取合同审批列表数据数量
- func GetContractApprovalListCount(childCondition, condition, joinCondition string, childPars, pars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- sql := `
- SELECT
- a.contract_approval_id,a.contract_id,a.apply_content,a.contract_detail,a.approve_remark,a.create_time,a.modify_time,a.status approve_status,c.status,c.product_id,
- c.contract_type,c.start_date,c.end_date,c.price,c.company_name,c.seller_id,c.seller_name,c.file_url,c.contract_code from contract_approval a
- join
- ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
- sql += childCondition
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- JOIN contract c ON a.contract_id = c.contract_id
- JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
- WHERE c.is_delete = 0 AND a.approval_type="contract" `
- sql += condition
- sql += ` group by contract_id `
- sql = `select count(*) count from (` + sql + `) g`
- err = o.Raw(sql, childPars, pars).QueryRow(&count)
- return
- }
- //合同审批列表
- type ContractApprovalList struct {
- ContractApprovalId int `description:"审批单id"`
- ContractId int `description:"合同id"`
- ContractApprovalRecordId int `description:"审批流id"`
- ContractCode string `description:"合同编号"`
- Status string `description:"合同状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
- ApproveStatus string `json:"-" description:"审批单状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
- ApplyContent string `description:"待审内容"`
- ContractDetail string `json:"-" description:"提交审批时的合同信息;合同快照"`
- ContractInfo contract.ContractDetail `json:"-" description:"提交审批时的合同信息;合同快照"`
- ApproveRemark string `description:"审核备注"`
- ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"`
- ContractType string `description:"合同类型"`
- Price float64 `description:"合同金额"`
- CompanyName string `description:"客户名称"`
- CreditCode string `description:"社会统一信用代码,长度64位"`
- FileUrl string `description:"合同下载地址"`
- SellerId int `description:"销售id"`
- ProductId int `description:"产品id,1:ficc;2:权益"`
- SellerName string `description:"销售名称"`
- StartDate time.Time `description:"合同开始日期"`
- EndDate time.Time `description:"合同结束日期"`
- CreateTime time.Time `description:"发起审批的时间"`
- ModifyTime time.Time `description:"最后一次修改的时间"`
- ApproveTime time.Time `description:"审批时间"`
- InvalidTime time.Time `description:"作废时间"`
- CheckBackFileTime time.Time `description:"合同签回时间"`
- RescindTime time.Time `description:"解约时间"`
- StartDateStr string `description:"合同开始日期(字符串类型)"`
- EndDateStr string `description:"合同结束日期(字符串类型)"`
- CreateTimeStr string `description:"发起审批的时间(字符串类型)"`
- ModifyTimeStr string `description:"最后一次修改的时间(字符串类型)"`
- ApproveTimeStr string `description:"审批时间(字符串类型)"`
- InvalidTimeStr string `description:"作废时间(字符串类型)"`
- CheckBackFileTimeStr string `description:"合同签回时间(字符串类型)"`
- RescindTimeStr string `description:"解约时间(字符串类型)"`
- }
- // GetContractApprovalList 获取合同审批列表数据
- func GetContractApprovalList(childCondition, condition, joinCondition string, childPars, pars []interface{}, startSize, pageSize int) (list []*ContractApprovalList, err error) {
- o := orm.NewOrm()
- sql := `
- SELECT
- a.contract_approval_id,d.contract_approval_record_id,a.contract_id,a.apply_content,a.approve_remark,a.create_time,a.modify_time,a.status approve_status,c.status,c.product_id,
- c.contract_type,c.start_date,c.end_date,c.price,c.company_name,c.credit_code,c.seller_id,c.seller_name,c.file_url,c.contract_code,c.approve_time,c.invalid_time,c.check_back_file_time,c.rescind_time,c.contract_business_type from contract_approval a
- join
- ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
- sql += childCondition
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- JOIN contract c ON a.contract_id = c.contract_id
- JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
- WHERE c.is_delete = 0 AND a.approval_type="contract" `
- sql += condition
- sql += ` group by contract_id order by modify_time desc LIMIT ?,? `
- _, err = o.Raw(sql, childPars, pars, startSize, pageSize).QueryRows(&list)
- return
- }
- // GetContractApprovalListCountV2 获取审批后的数据(待审批、处理中的)
- func GetContractApprovalListCountV2(childCondition, condition string, childPars, pars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- sql := `
- SELECT
- a.contract_approval_id,a.contract_id,a.apply_content,a.contract_detail,a.approve_remark,a.create_time,a.modify_time,a.status approve_status,c.status,c.product_id,
- c.contract_type,c.start_date,c.end_date,c.price,c.company_name,c.seller_id,c.seller_name,c.file_url,c.contract_code from contract_approval a
- join
- ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
- sql += childCondition
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- JOIN contract c ON a.contract_id = c.contract_id
- JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
- WHERE c.is_delete = 0 AND a.approval_type="contract" `
- sql += condition
- sql += ` group by contract_id `
- sql = `select count(*) count from (` + sql + `) g`
- err = o.Raw(sql, childPars, pars).QueryRow(&count)
- return
- }
- // GetContractApprovalListV2 获取审批后的数据(待审批、处理中的)
- func GetContractApprovalListV2(childCondition, condition, orderBy string, childPars, pars []interface{}, startSize, pageSize int) (list []*ContractApprovalList, err error) {
- o := orm.NewOrm()
- sql := `
- SELECT
- a.contract_approval_id,d.contract_approval_record_id,a.contract_id,a.apply_content,a.approve_remark,a.create_time,a.modify_time,a.status approve_status,c.status,c.product_id,
- c.contract_type,c.start_date,c.end_date,c.price,c.company_name,c.credit_code,c.seller_id,c.seller_name,c.file_url,c.contract_code,c.approve_time,c.invalid_time,c.check_back_file_time,c.rescind_time,c.contract_business_type from contract_approval a
- join
- ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
- sql += childCondition
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- JOIN contract c ON a.contract_id = c.contract_id
- JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
- WHERE c.is_delete = 0 AND a.approval_type="contract" `
- sql += condition
- sql += ` group by contract_id order by ` + orderBy + ` LIMIT ?,? `
- _, err = o.Raw(sql, childPars, pars, startSize, pageSize).QueryRows(&list)
- return
- }
- //获取客户名称
- func GetCompanyNameListV2(childCondition, condition string, childPars, pars []interface{}) (list []*contract.CompanyNameList, err error) {
- o := orm.NewOrm()
- sql := `
- SELECT c.company_name from contract_approval a
- join
- ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
- sql += childCondition
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- JOIN contract c ON a.contract_id = c.contract_id
- JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
- WHERE c.is_delete = 0 AND a.approval_type="contract" `
- sql += condition
- sql += ` group by company_name order by a.modify_time desc`
- _, err = o.Raw(sql, childPars, pars).QueryRows(&list)
- return
- }
- // GetContractListCountV2 合同客户统计数(包含待提交的)
- func GetContractListCountV2(childCondition, condition, joinCondition, unionCondition string, childPars, pars, unionPars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- sql := `
- SELECT
- a.contract_approval_id,d.contract_approval_record_id,a.contract_id,a.apply_content,a.approve_remark,a.create_time,a.modify_time,a.status approve_status,c.status,c.product_id,
- c.contract_type,c.start_date,c.end_date,c.price,c.company_name,c.seller_id,c.seller_name,c.file_url,c.contract_code,c.approve_time,c.invalid_time,c.check_back_file_time,c.rescind_time from contract_approval a
- join
- ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
- sql += childCondition
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- JOIN contract c ON a.contract_id = c.contract_id
- JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
- WHERE c.is_delete = 0 AND a.approval_type="contract" `
- sql += condition
- sql += ` union (SELECT
- contract_approval_id, 0 as contract_approval_record_id,c.contract_id,"" as apply_content,"" as approve_remark,
- c.create_time,c.modify_time,"待提交" as approve_status,c.status,c.product_id,
- c.contract_type,c.start_date,c.end_date,c.price,c.company_name,c.seller_id,c.seller_name,c.file_url,c.contract_code,c.approve_time,c.invalid_time,c.check_back_file_time,c.rescind_time
- from contract c left join contract_approval b on c.contract_id=b.contract_id
- where is_delete=0 and b.contract_approval_id is null ` + unionCondition + `)`
- sql = `select count(*) count from (select * from (` + sql + `) g group by contract_id ) f`
- err = o.Raw(sql, childPars, pars, unionPars).QueryRow(&count)
- return
- }
- // GetContractListV2 获取合同审批列表数据(包含待提交的)
- func GetContractListV2(childCondition, condition, joinCondition, unionCondition string, childPars, pars, unionPars []interface{}, startSize, pageSize int) (list []*ContractApprovalList, err error) {
- o := orm.NewOrm()
- sql := `
- SELECT
- a.contract_approval_id,d.contract_approval_record_id,a.contract_id,a.apply_content,a.approve_remark,a.create_time,a.modify_time,a.status approve_status,c.status,c.product_id,
- c.contract_type,c.start_date,c.end_date,c.price,c.company_name,c.seller_id,c.seller_name,c.file_url,c.contract_code,c.approve_time,c.invalid_time,c.check_back_file_time,c.rescind_time,c.contract_business_type from contract_approval a
- join
- ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
- sql += childCondition
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- JOIN contract c ON a.contract_id = c.contract_id
- JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
- WHERE c.is_delete = 0 AND a.approval_type="contract" `
- sql += condition
- sql += ` union (SELECT
- contract_approval_id, 0 as contract_approval_record_id,c.contract_id,"" as apply_content,"" as approve_remark,
- c.create_time,c.modify_time,"待提交" as approve_status,c.status,c.product_id,
- c.contract_type,c.start_date,c.end_date,c.price,c.company_name,c.seller_id,c.seller_name,c.file_url,c.contract_code,c.approve_time,c.invalid_time,c.check_back_file_time,c.rescind_time
- from contract c left join contract_approval b on c.contract_id=b.contract_id
- where is_delete=0 and b.contract_approval_id is null ` + unionCondition + `)`
- sql = `select * from (` + sql + `) g group by contract_id order by modify_time desc LIMIT ?,?`
- _, err = o.Raw(sql, childPars, pars, unionPars, startSize, pageSize).QueryRows(&list)
- return
- }
- type CompanyNameList struct {
- CompanyName string `description:"客户名称,甲方名称,长度32位"`
- }
- //根据合同id获取合同审批单信息
- func (ContractApproval) CheckPendingByContractId(contractId int, approvalType string) (has bool, err error) {
- o := orm.NewOrm()
- var contractApprovalInfo ContractApproval
- sql := `select * from contract_approval where contract_id = ? AND approval_type =? AND status="待审批" limit 1`
- err = o.Raw(sql, contractId, approvalType).QueryRow(&contractApprovalInfo)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- //如果不存在待审批记录,那么报错信息置空,存在与否直接返回false
- err = nil
- return
- }
- //系统异常,返回错误信息
- return
- }
- //存在待审批的审批单
- has = true
- return
- }
- //发起审批(工作流)
- func (ContractApproval) Apply(contractApprovalInfo *ContractApproval, contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- //审批单记录
- ContractApprovalId, err := tx.Insert(contractApprovalInfo)
- if err != nil {
- return
- }
- //审批单id
- contractApprovalInfo.ContractApprovalId = int(ContractApprovalId)
- for _, contractApprovalRecord := range contractApprovalRecordList {
- //审批流记录
- contractApprovalRecord.ContractApprovalId = contractApprovalInfo.ContractApprovalId
- contractApprovalRecordId, tmpErr := tx.Insert(contractApprovalRecord)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //审批流id
- contractApprovalRecord.ContractApprovalRecordId = int(contractApprovalRecordId)
- }
- switch contractApprovalInfo.ApprovalType {
- case "contract":
- //修改合同状态为待审批
- sql := `UPDATE contract SET status="待审批",modify_time=NOW() WHERE contract_id = ?`
- _, err = tx.Raw(sql, contractApprovalInfo.ContractId).Exec()
- case "seal":
- //修改用印状态为待审批
- sql := `UPDATE seal SET status="待审批",modify_time=NOW() WHERE seal_id = ?`
- _, err = tx.Raw(sql, contractApprovalInfo.ContractId).Exec()
- }
- return
- }
- //撤回审核单
- func (ContractApproval) Cancel(contractApprovalInfo *ContractApproval, contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- //变更审批单记录
- contractApprovalInfo.Status = "已撤回"
- contractApprovalInfo.ModifyTime = time.Now()
- _, err = tx.Update(contractApprovalInfo, "Status", "ModifyTime")
- if err != nil {
- return
- }
- //变更所有审批流记录
- for _, contractApprovalRecord := range contractApprovalRecordList {
- contractApprovalRecord.Status = "已撤回"
- contractApprovalRecord.ModifyTime = time.Now()
- _, tmpErr := tx.Update(contractApprovalRecord, "Status", "ModifyTime")
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- switch contractApprovalInfo.ApprovalType {
- case "contract":
- //修改合同状态为已撤回
- sql := `UPDATE contract SET status="已撤回",modify_time=NOW() WHERE contract_id = ?`
- _, err = tx.Raw(sql, contractApprovalInfo.ContractId).Exec()
- case "seal":
- //修改用印状态为已撤回
- sql := `UPDATE seal SET status="已撤回",modify_time=NOW() WHERE seal_id = ?`
- _, err = tx.Raw(sql, contractApprovalInfo.ContractId).Exec()
- }
- return
- }
- //审核驳回
- func (ContractApproval) Reject(contractApprovalInfo *ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, approveUserId int, approveUserName, remark string) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- //变更审批单记录
- contractApprovalInfo.Status = "已驳回"
- contractApprovalInfo.ApproveRemark = remark //审核失败理由
- contractApprovalInfo.ModifyTime = time.Now()
- //contractApprovalInfo.CurrNodeId = 0 //当前节点变更为0,代表当前审批流程已经结束了
- _, err = tx.Update(contractApprovalInfo, "Status", "ApproveRemark", "ModifyTime")
- if err != nil {
- return
- }
- //变更审批流记录
- contractApprovalRecord.Status = "已驳回"
- contractApprovalRecord.ApproveRemark = remark //审核失败理由
- contractApprovalRecord.ApproveUserId = approveUserId //审批人id
- contractApprovalRecord.ApproveUserName = approveUserName //审批人名称
- contractApprovalRecord.ApproveTime = time.Now() //审批人名称
- contractApprovalRecord.ModifyTime = time.Now()
- _, err = tx.Update(contractApprovalRecord)
- if err != nil {
- return
- }
- switch contractApprovalInfo.ApprovalType {
- case "contract":
- //修改合同状态为已驳回
- sql := `UPDATE contract SET status="已驳回",modify_content="",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE contract_id = ?`
- _, err = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
- case "seal":
- //修改用印状态为已驳回
- sql := `UPDATE seal SET status="已驳回",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE seal_id = ?`
- _, err = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
- }
- return
- }
- //下级审批人信息
- type NextApproval struct {
- ApproveRoleTypeCode string
- ApproveUserId int
- ApproveUserName string
- }
- //审核通过
- func (ContractApproval) Approved(contractApprovalInfo *ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, remark string) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- //变更审批流记录
- contractApprovalRecord.Status = "已审批"
- contractApprovalRecord.ApproveRemark = remark //审核失败理由
- contractApprovalRecord.ApproveTime = time.Now() //审批时间
- contractApprovalRecord.ModifyTime = time.Now()
- _, err = tx.Update(contractApprovalRecord, "Status", "ApproveRemark", "ApproveTime", "ModifyTime")
- if err != nil {
- return
- }
- //如果没有下级节点了,那么完成该审批单
- if contractApprovalRecord.NextNodeId <= 0 {
- //变更审批单记录
- contractApprovalInfo.Status = "已审批"
- contractApprovalInfo.ApproveRemark = remark //审核理由
- //contractApprovalInfo.CurrNodeId = contractApprovalRecord.NextNodeId //下级审批节点id
- contractApprovalInfo.ModifyTime = time.Now()
- _, tmpErr := tx.Update(contractApprovalInfo, "Status", "ApproveRemark", "ModifyTime")
- if tmpErr != nil {
- err = tmpErr
- return
- }
- switch contractApprovalInfo.ApprovalType {
- case "contract":
- //修改合同状态为已审批
- sql := `UPDATE contract SET status="已审批",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE contract_id = ?`
- _, tmpErr = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
- if tmpErr != nil {
- err = tmpErr
- return
- }
- case "seal":
- //修改用印状态为已审批
- sql := `UPDATE seal SET status="已审批",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE seal_id = ?`
- _, tmpErr = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- } else {
- //变更审批单记录
- contractApprovalInfo.ApproveRemark = remark //审核理由
- contractApprovalInfo.CurrNodeId = contractApprovalRecord.NextNodeId //下级审批节点id
- contractApprovalInfo.ModifyTime = time.Now()
- _, tmpErr := tx.Update(contractApprovalInfo, "ApproveRemark", "CurrNodeId", "ModifyTime")
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- return
- }
- //抄送
- func (ContractApproval) ApprovedByCc(contractApprovalInfo *ContractApproval, contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord, remark string, nextNodeId int) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- for _, contractApprovalRecord := range contractApprovalRecordList {
- //变更审批流记录
- contractApprovalRecord.Status = "已审批"
- contractApprovalRecord.ApproveTime = time.Now() //审批时间
- contractApprovalRecord.ModifyTime = time.Now()
- _, err = tx.Update(contractApprovalRecord, "Status", "ApproveTime", "ModifyTime")
- if err != nil {
- return
- }
- }
- if err != nil {
- return
- }
- //如果没有下级节点了,那么完成该审批单
- if nextNodeId <= 0 {
- //变更审批单记录
- contractApprovalInfo.Status = "已审批"
- //contractApprovalInfo.CurrNodeId = nextNodeId //下级审批节点id
- contractApprovalInfo.ModifyTime = time.Now()
- _, tmpErr := tx.Update(contractApprovalInfo, "Status", "ModifyTime")
- if tmpErr != nil {
- err = tmpErr
- return
- }
- switch contractApprovalInfo.ApprovalType {
- case "contract":
- //修改合同状态为已审批
- sql := `UPDATE contract SET status="已审批",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE contract_id = ?`
- _, tmpErr = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
- if tmpErr != nil {
- err = tmpErr
- return
- }
- case "seal":
- //修改用印状态为已审批
- sql := `UPDATE seal SET status="已审批",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE seal_id = ?`
- _, tmpErr = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- } else {
- //变更审批单记录
- contractApprovalInfo.CurrNodeId = nextNodeId //下级审批节点id
- contractApprovalInfo.ModifyTime = time.Now()
- _, tmpErr := tx.Update(contractApprovalInfo, "CurrNodeId", "ModifyTime")
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- return
- }
- //根据合同审批单id集合获取合同审批单信息列表
- func GetContractApprovalByIds(contractApprovalIds string) (contractApprovalList []*ContractApproval, err error) {
- o := orm.NewOrm()
- sql := `select * from contract_approval where contract_approval_id in (` + contractApprovalIds + `) `
- _, err = o.Raw(sql).QueryRows(&contractApprovalList)
- return
- }
- //获取用印审批列表数据数量
- func GetSealApprovalListCount(childCondition, condition, joinCondition string, childPars, pars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- sql := `SELECT a.contract_approval_id,a.contract_id,a.apply_content,a.approve_remark,a.create_time,a.modify_time,a.status,c.status seal_status,
- c.seal_type,c.company_name,c.user_id,c.user_name,c.file_url,c.code from contract_approval a
- join
- ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="seal" `
- sql += childCondition
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- JOIN seal c ON c.seal_id = a.contract_id
- JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
- WHERE c.is_delete = 0 AND a.approval_type="seal" `
- sql += condition
- sql += ` group by contract_id `
- sql = `select count(*) count from (` + sql + `) g`
- err = o.Raw(sql, childPars, pars).QueryRow(&count)
- return
- }
- //用印审批列表
- type SealApprovalList struct {
- ContractApprovalId int `description:"审批单id"`
- ContractId int `description:"合同id"`
- ContractApprovalRecordId int `description:"审批流id"`
- ContractCode string `description:"合同编号"`
- Status string `description:"合同状态,枚举值:待审批','已审批','已驳回','已撤回','已签回',默认待审批"`
- ApproveStatus string `json:"-" description:"审批单状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
- ApplyContent string `description:"待审内容"`
- ApplyUserId int `description:"待审内容"`
- ApplyUserName string `description:"待审内容"`
- ContractDetail string `json:"-" description:"提交审批时的合同信息;用印快照"`
- ContractInfo contract.ContractDetail `json:"-" description:"提交审批时的合同信息;用印快照"`
- ApproveRemark string `description:"审核备注"`
- SealType string `description:"用印类型"`
- CompanyName string `description:"客户名称"`
- FileUrl string `description:"合同下载地址"`
- UserId int `description:"申请人id"`
- CurrNodeId int `description:"当前审批节点id"`
- StartNodeId int `description:"开始审批节点id"`
- UserName string `description:"申请人名称"`
- CreateTime time.Time `description:"发起审批的时间"`
- ModifyTime time.Time `description:"最后一次修改的时间"`
- ApproveTime time.Time `description:"审批时间"`
- InvalidTime time.Time `description:"作废时间"`
- CheckBackFileTime time.Time `description:"签回用印附件时间"`
- StartDateStr string `description:"合同开始日期(字符串类型)"`
- EndDateStr string `description:"合同结束日期(字符串类型)"`
- CreateTimeStr string `description:"发起审批的时间(字符串类型)"`
- ModifyTimeStr string `description:"最后一次修改的时间(字符串类型)"`
- ApproveTimeStr string `description:"审批时间(字符串类型)"`
- InvalidTimeStr string `description:"作废时间(字符串类型)"`
- }
- //获取合同审批列表数据
- func GetSealApprovalList(childCondition, condition, joinCondition string, childPars, pars []interface{}, startSize, pageSize int) (list []*SealApprovalList, err error) {
- o := orm.NewOrm()
- sql := `SELECT a.contract_approval_id,a.contract_id,a.apply_content,a.approve_remark,a.apply_user_id,a.apply_user_name,a.curr_node_id,a.start_node_id,a.create_time,a.modify_time,a.status approval_status,c.status,c.seal_type,c.company_name,c.user_id,c.user_name,c.file_url,c.approve_time,c.invalid_time,c.code,c.check_back_file_time from contract_approval a
- join
- ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="seal" `
- sql += childCondition
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- JOIN seal c ON c.seal_id = a.contract_id
- JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
- WHERE c.is_delete = 0 AND a.approval_type="seal" `
- sql += condition
- sql += ` group by contract_id order by modify_time desc LIMIT ?,? `
- _, err = o.Raw(sql, childPars, pars, startSize, pageSize).QueryRows(&list)
- return
- }
- //处理后的
- func GetSealApprovalListCountV2(childCondition, condition string, childPars, pars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- sql := `SELECT a.contract_approval_id,a.contract_id,a.apply_content,a.approve_remark,a.create_time,a.modify_time,a.status,c.status seal_status,
- c.seal_type,c.company_name,c.user_id,c.user_name,c.file_url,c.code from contract_approval a
- join
- ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="seal" `
- sql += childCondition
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- JOIN seal c ON c.seal_id = a.contract_id
- JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
- WHERE c.is_delete = 0 AND a.approval_type="seal" `
- sql += condition
- sql += ` group by contract_id `
- sql = `select count(*) count from (` + sql + `) g`
- err = o.Raw(sql, childPars, pars).QueryRow(&count)
- return
- }
- func GetSealApprovalListV2(childCondition, condition, orderBy string, childPars, pars []interface{}, startSize, pageSize int) (list []*SealApprovalList, err error) {
- o := orm.NewOrm()
- sql := `SELECT a.contract_approval_id,a.contract_id,a.apply_content,a.approve_remark,a.apply_user_id,a.apply_user_name,a.curr_node_id,a.start_node_id,a.create_time,a.modify_time,a.status approval_status,c.status,c.seal_type,c.company_name,c.user_id,c.user_name,c.file_url,c.approve_time,c.invalid_time,c.code,c.check_back_file_time from contract_approval a
- join
- ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="seal" `
- sql += childCondition
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- JOIN seal c ON c.seal_id = a.contract_id
- JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
- WHERE c.is_delete = 0 AND a.approval_type="seal" `
- sql += condition
- sql += ` group by contract_id order by ` + orderBy + ` LIMIT ?,? `
- _, err = o.Raw(sql, childPars, pars, startSize, pageSize).QueryRows(&list)
- return
- }
- //审批列表
- type CompanyApprovalList struct {
- ContractApprovalId int `description:"审批单id"`
- ContractId int `description:"客户单id"`
- ContractApprovalRecordId int `description:"审批流id"`
- ContractCode string `description:"合同编号"`
- Status string `description:"审批单状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
- ApplyContent string `description:"待审内容"`
- ContractDetail string `json:"-" description:"提交审批时的客户审批信息;客户审批快照"`
- ContractInfo contract.ContractDetail `description:"提交审批时的合同信息;合同快照"`
- ApproveRemark string `description:"审核备注"`
- CompanyName string `description:"客户名称"`
- SellerId int `description:"销售id"`
- ApplyUserId int `description:"申请人id"`
- CurrNodeId int `description:"当前节点"`
- StartNodeId int `description:"开始节点"`
- ProductId int `description:"产品id,1:ficc;2:权益"`
- SellerName string `description:"销售名称"`
- StartDate time.Time `description:"合同开始日期"`
- EndDate time.Time `description:"合同结束日期"`
- CreateTime time.Time `description:"发起审批的时间"`
- ModifyTime time.Time `description:"最后一次修改的时间"`
- ApproveTime time.Time `description:"审批时间"`
- StartDateStr string `description:"合同开始日期(字符串类型)"`
- EndDateStr string `description:"合同结束日期(字符串类型)"`
- CreateTimeStr string `description:"发起审批的时间(字符串类型)"`
- ModifyTimeStr string `description:"最后一次修改的时间(字符串类型)"`
- ApproveTimeStr string `description:"审批时间(字符串类型)"`
- CompanyId int
- CreditCode string `description:"社会统一信用码"`
- CompanyType string `description:"客户类型"`
- IndustryName string `description:"行业名称"`
- ApprovalTime string `description:"提交申请时间"`
- CompanyProductStatus string `description:"客户状态"`
- ApproveContent string `description:"待审内容"`
- ApproveStatus string `description:"当前状态"`
- ApplyMethod int `description:"申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议"`
- ApplyReasons string `description:"申请理由"`
- FreezeStartDate string `description:"冻结开始日期"`
- FreezeEndDate string `description:"冻结结束日期"`
- FreezeExpireDays int `description:"冻结到期天数"`
- Address string `description:"地址"`
- Source string `description:"客户来源"`
- ExpireDay int `description:"服务到期天数"`
- CompanyApprovalId int `description:"审批记录ID"`
- ApprovalCount int `description:"已延期审批次数"`
- CompanyContractId int `description:"合同id"`
- DelayPermission string `description:"延期申请品种"`
- ApplyRealName string `description:"申请人姓名"`
- ContractSourceTag string `description:"合同来源标签:非标合同,标准合同"`
- }
- func GetCompanyApprovalCount(condition string, pars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- sql := `
- SELECT
- a.contract_approval_id,a.contract_id,a.apply_content,a.contract_detail,a.create_time,a.modify_time,a.status,a.curr_node_id,a.start_node_id,a.apply_user_id,c.approve_time,c.company_approval_id,c.company_id,c.product_id,c.apply_real_name,d.company_type,d.industry_name,d.seller_name,d.status company_product_status,c.approve_status,c.apply_method,c.apply_reasons,d.freeze_start_date,d.freeze_end_date,e.address,d.source,c.approve_remark,d.start_date,d.end_date,c.company_contract_id,e.company_name,e.credit_code
- from contract_approval a
- join ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="company" `
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- INNER JOIN company_approval AS c ON a.contract_id=c.company_approval_id
- INNER JOIN company_product AS d ON d.company_id=c.company_id AND d.product_id=c.product_id
- INNER JOIN company AS e ON e.company_id=d.company_id
- JOIN contract_approval_record f on a.contract_approval_id=f.contract_approval_id
- WHERE 1=1 and a.approval_type="company" `
- sql += condition
- sql += ` group by contract_id `
- sql = `select count(1) from (` + sql + `) z `
- err = o.Raw(sql, pars).QueryRow(&count)
- return
- }
- func GetCompanyApprovalList(condition string, pars []interface{}, startSize, pageSize int) (list []*CompanyApprovalList, err error) {
- o := orm.NewOrm()
- sql := `
- SELECT
- a.contract_approval_id,a.contract_id,a.apply_content,a.contract_detail,a.create_time,a.modify_time,a.status,a.curr_node_id,a.start_node_id,a.apply_user_id,c.approve_time,c.company_approval_id,c.company_id,c.product_id,c.apply_real_name,d.company_type,d.industry_name,d.seller_name,d.status company_product_status,c.approve_status,c.apply_method,c.apply_reasons,d.freeze_start_date,d.freeze_end_date,e.address,d.source,c.approve_remark,d.start_date,d.end_date,c.company_contract_id,e.company_name,e.credit_code
- from contract_approval a
- join ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="company" `
- sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- INNER JOIN company_approval AS c ON a.contract_id=c.company_approval_id
- INNER JOIN company_product AS d ON d.company_id=c.company_id AND d.product_id=c.product_id
- INNER JOIN company AS e ON e.company_id=d.company_id
- JOIN contract_approval_record f on a.contract_approval_id=f.contract_approval_id
- WHERE 1=1 and a.approval_type="company" `
- sql += condition
- sql += ` group by contract_id order by modify_time desc LIMIT ?,? `
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
- return
- }
|