contract_approval.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. package contract_approval
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_mobile_admin/models/tables/contract"
  5. "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
  6. "hongze/hongze_mobile_admin/utils"
  7. "time"
  8. )
  9. //合同审批单
  10. type ContractApproval struct {
  11. ContractApprovalId int `orm:"column(contract_approval_id);pk"`
  12. ApprovalType string `description:"审批单类型,枚举值,合同:contract;用印:seal;默认:contract"`
  13. ContractId int `orm:"column(contract_id)" description:"合同id"`
  14. Status string `description:"审批单状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
  15. ApplyContent string `description:"待审内容,长度255位"`
  16. ContractDetail string `description:"审批单详情,完整的合同json数据"`
  17. ApplyUserId int `description:"申请人id"`
  18. ApplyUserName string `description:"申请人名称"`
  19. ApproveRemark string `description:"审批人备注,可以是驳回理由,长度128位"`
  20. FlowId int `description:"审批流程id"`
  21. FlowVersion int `description:"审批流程版本"`
  22. CurrNodeId int `description:"当前审批节点id"`
  23. StartNodeId int `description:"开始审批节点id"`
  24. ModifyTime time.Time `description:"发起申请时间"`
  25. CreateTime time.Time `description:"最近一次审批单修改时间"`
  26. }
  27. //根据合同审批单id获取合同审批单信息
  28. func GetContractApprovalById(contractApprovalId int) (contractApprovalInfo *ContractApproval, err error) {
  29. o := orm.NewOrm()
  30. sql := `select * from contract_approval where contract_approval_id = ? `
  31. err = o.Raw(sql, contractApprovalId).QueryRow(&contractApprovalInfo)
  32. return
  33. }
  34. //根据合同id获取最近一次待审批的合同审批单信息
  35. func GetLastPendingContractApprovalByContractId(contractId int) (contractApprovalInfo *ContractApproval, err error) {
  36. o := orm.NewOrm()
  37. sql := `select * from contract_approval where status = "待审批" AND contract_id=? order by contract_approval_id desc`
  38. err = o.Raw(sql, contractId).QueryRow(&contractApprovalInfo)
  39. return
  40. }
  41. //根据合同id获取最近一次提交的合同审批单信息
  42. func GetLastContractApprovalByContractId(contractId int, approvalType string) (contractApprovalInfo *ContractApproval, err error) {
  43. o := orm.NewOrm()
  44. sql := `select * from contract_approval where contract_id=? and approval_type =? order by contract_approval_id desc`
  45. err = o.Raw(sql, contractId, approvalType).QueryRow(&contractApprovalInfo)
  46. return
  47. }
  48. //根据合同id获取总共被驳回的次数
  49. func GetRejectContractCountByContractId(contractId int) (total int64, err error) {
  50. o := orm.NewOrm()
  51. sql := `select count(1) total from contract_approval where status = "已驳回" AND contract_id=? `
  52. err = o.Raw(sql, contractId).QueryRow(&total)
  53. return
  54. }
  55. //获取合同审批列表数据数量
  56. func GetContractApprovalListCount(childCondition, condition, joinCondition string, childPars, pars []interface{}) (count int, err error) {
  57. o := orm.NewOrm()
  58. sql := `
  59. SELECT
  60. 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,
  61. 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
  62. join
  63. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
  64. sql += childCondition
  65. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  66. JOIN contract c ON a.contract_id = c.contract_id
  67. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
  68. WHERE c.is_delete = 0 AND a.approval_type="contract" `
  69. sql += condition
  70. sql += ` group by contract_id `
  71. sql = `select count(*) count from (` + sql + `) g`
  72. err = o.Raw(sql, childPars, pars).QueryRow(&count)
  73. return
  74. }
  75. //合同审批列表
  76. type ContractApprovalList struct {
  77. ContractApprovalId int `description:"审批单id"`
  78. ContractId int `description:"合同id"`
  79. ContractApprovalRecordId int `description:"审批流id"`
  80. ContractCode string `description:"合同编号"`
  81. Status string `description:"合同状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
  82. ApproveStatus string `json:"-" description:"审批单状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
  83. ApplyContent string `description:"待审内容"`
  84. ContractDetail string `json:"-" description:"提交审批时的合同信息;合同快照"`
  85. ContractInfo contract.ContractDetail `json:"-" description:"提交审批时的合同信息;合同快照"`
  86. ApproveRemark string `description:"审核备注"`
  87. ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"`
  88. ContractType string `description:"合同类型"`
  89. Price float64 `description:"合同金额"`
  90. CompanyName string `description:"客户名称"`
  91. CreditCode string `description:"社会统一信用代码,长度64位"`
  92. FileUrl string `description:"合同下载地址"`
  93. SellerId int `description:"销售id"`
  94. ProductId int `description:"产品id,1:ficc;2:权益"`
  95. SellerName string `description:"销售名称"`
  96. StartDate time.Time `description:"合同开始日期"`
  97. EndDate time.Time `description:"合同结束日期"`
  98. CreateTime time.Time `description:"发起审批的时间"`
  99. ModifyTime time.Time `description:"最后一次修改的时间"`
  100. ApproveTime time.Time `description:"审批时间"`
  101. InvalidTime time.Time `description:"作废时间"`
  102. CheckBackFileTime time.Time `description:"合同签回时间"`
  103. RescindTime time.Time `description:"解约时间"`
  104. StartDateStr string `description:"合同开始日期(字符串类型)"`
  105. EndDateStr string `description:"合同结束日期(字符串类型)"`
  106. CreateTimeStr string `description:"发起审批的时间(字符串类型)"`
  107. ModifyTimeStr string `description:"最后一次修改的时间(字符串类型)"`
  108. ApproveTimeStr string `description:"审批时间(字符串类型)"`
  109. InvalidTimeStr string `description:"作废时间(字符串类型)"`
  110. CheckBackFileTimeStr string `description:"合同签回时间(字符串类型)"`
  111. RescindTimeStr string `description:"解约时间(字符串类型)"`
  112. }
  113. // GetContractApprovalList 获取合同审批列表数据
  114. func GetContractApprovalList(childCondition, condition, joinCondition string, childPars, pars []interface{}, startSize, pageSize int) (list []*ContractApprovalList, err error) {
  115. o := orm.NewOrm()
  116. sql := `
  117. SELECT
  118. 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,
  119. 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
  120. join
  121. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
  122. sql += childCondition
  123. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  124. JOIN contract c ON a.contract_id = c.contract_id
  125. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
  126. WHERE c.is_delete = 0 AND a.approval_type="contract" `
  127. sql += condition
  128. sql += ` group by contract_id order by modify_time desc LIMIT ?,? `
  129. _, err = o.Raw(sql, childPars, pars, startSize, pageSize).QueryRows(&list)
  130. return
  131. }
  132. // GetContractApprovalListCountV2 获取审批后的数据(待审批、处理中的)
  133. func GetContractApprovalListCountV2(childCondition, condition string, childPars, pars []interface{}) (count int, err error) {
  134. o := orm.NewOrm()
  135. sql := `
  136. SELECT
  137. 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,
  138. 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
  139. join
  140. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
  141. sql += childCondition
  142. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  143. JOIN contract c ON a.contract_id = c.contract_id
  144. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
  145. WHERE c.is_delete = 0 AND a.approval_type="contract" `
  146. sql += condition
  147. sql += ` group by contract_id `
  148. sql = `select count(*) count from (` + sql + `) g`
  149. err = o.Raw(sql, childPars, pars).QueryRow(&count)
  150. return
  151. }
  152. // GetContractApprovalListV2 获取审批后的数据(待审批、处理中的)
  153. func GetContractApprovalListV2(childCondition, condition, orderBy string, childPars, pars []interface{}, startSize, pageSize int) (list []*ContractApprovalList, err error) {
  154. o := orm.NewOrm()
  155. sql := `
  156. SELECT
  157. 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,
  158. 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
  159. join
  160. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
  161. sql += childCondition
  162. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  163. JOIN contract c ON a.contract_id = c.contract_id
  164. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
  165. WHERE c.is_delete = 0 AND a.approval_type="contract" `
  166. sql += condition
  167. sql += ` group by contract_id order by ` + orderBy + ` LIMIT ?,? `
  168. _, err = o.Raw(sql, childPars, pars, startSize, pageSize).QueryRows(&list)
  169. return
  170. }
  171. //获取客户名称
  172. func GetCompanyNameListV2(childCondition, condition string, childPars, pars []interface{}) (list []*contract.CompanyNameList, err error) {
  173. o := orm.NewOrm()
  174. sql := `
  175. SELECT c.company_name from contract_approval a
  176. join
  177. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
  178. sql += childCondition
  179. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  180. JOIN contract c ON a.contract_id = c.contract_id
  181. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
  182. WHERE c.is_delete = 0 AND a.approval_type="contract" `
  183. sql += condition
  184. sql += ` group by company_name order by a.modify_time desc`
  185. _, err = o.Raw(sql, childPars, pars).QueryRows(&list)
  186. return
  187. }
  188. // GetContractListCountV2 合同客户统计数(包含待提交的)
  189. func GetContractListCountV2(childCondition, condition, joinCondition, unionCondition string, childPars, pars, unionPars []interface{}) (count int, err error) {
  190. o := orm.NewOrm()
  191. sql := `
  192. SELECT
  193. 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,
  194. 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
  195. join
  196. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
  197. sql += childCondition
  198. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  199. JOIN contract c ON a.contract_id = c.contract_id
  200. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
  201. WHERE c.is_delete = 0 AND a.approval_type="contract" `
  202. sql += condition
  203. sql += ` union (SELECT
  204. contract_approval_id, 0 as contract_approval_record_id,c.contract_id,"" as apply_content,"" as approve_remark,
  205. c.create_time,c.modify_time,"待提交" as approve_status,c.status,c.product_id,
  206. 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
  207. from contract c left join contract_approval b on c.contract_id=b.contract_id
  208. where is_delete=0 and b.contract_approval_id is null ` + unionCondition + `)`
  209. sql = `select count(*) count from (select * from (` + sql + `) g group by contract_id ) f`
  210. err = o.Raw(sql, childPars, pars, unionPars).QueryRow(&count)
  211. return
  212. }
  213. // GetContractListV2 获取合同审批列表数据(包含待提交的)
  214. func GetContractListV2(childCondition, condition, joinCondition, unionCondition string, childPars, pars, unionPars []interface{}, startSize, pageSize int) (list []*ContractApprovalList, err error) {
  215. o := orm.NewOrm()
  216. sql := `
  217. SELECT
  218. 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,
  219. 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
  220. join
  221. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="contract" `
  222. sql += childCondition
  223. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  224. JOIN contract c ON a.contract_id = c.contract_id
  225. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
  226. WHERE c.is_delete = 0 AND a.approval_type="contract" `
  227. sql += condition
  228. sql += ` union (SELECT
  229. contract_approval_id, 0 as contract_approval_record_id,c.contract_id,"" as apply_content,"" as approve_remark,
  230. c.create_time,c.modify_time,"待提交" as approve_status,c.status,c.product_id,
  231. 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
  232. from contract c left join contract_approval b on c.contract_id=b.contract_id
  233. where is_delete=0 and b.contract_approval_id is null ` + unionCondition + `)`
  234. sql = `select * from (` + sql + `) g group by contract_id order by modify_time desc LIMIT ?,?`
  235. _, err = o.Raw(sql, childPars, pars, unionPars, startSize, pageSize).QueryRows(&list)
  236. return
  237. }
  238. type CompanyNameList struct {
  239. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  240. }
  241. //根据合同id获取合同审批单信息
  242. func (ContractApproval) CheckPendingByContractId(contractId int, approvalType string) (has bool, err error) {
  243. o := orm.NewOrm()
  244. var contractApprovalInfo ContractApproval
  245. sql := `select * from contract_approval where contract_id = ? AND approval_type =? AND status="待审批" limit 1`
  246. err = o.Raw(sql, contractId, approvalType).QueryRow(&contractApprovalInfo)
  247. if err != nil {
  248. if err.Error() == utils.ErrNoRow() {
  249. //如果不存在待审批记录,那么报错信息置空,存在与否直接返回false
  250. err = nil
  251. return
  252. }
  253. //系统异常,返回错误信息
  254. return
  255. }
  256. //存在待审批的审批单
  257. has = true
  258. return
  259. }
  260. //发起审批(工作流)
  261. func (ContractApproval) Apply(contractApprovalInfo *ContractApproval, contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord) (err error) {
  262. o := orm.NewOrm()
  263. tx, err := o.Begin()
  264. if err != nil {
  265. return
  266. }
  267. defer func() {
  268. if err != nil {
  269. _ = tx.Rollback()
  270. } else {
  271. _ = tx.Commit()
  272. }
  273. }()
  274. //审批单记录
  275. ContractApprovalId, err := tx.Insert(contractApprovalInfo)
  276. if err != nil {
  277. return
  278. }
  279. //审批单id
  280. contractApprovalInfo.ContractApprovalId = int(ContractApprovalId)
  281. for _, contractApprovalRecord := range contractApprovalRecordList {
  282. //审批流记录
  283. contractApprovalRecord.ContractApprovalId = contractApprovalInfo.ContractApprovalId
  284. contractApprovalRecordId, tmpErr := tx.Insert(contractApprovalRecord)
  285. if tmpErr != nil {
  286. err = tmpErr
  287. return
  288. }
  289. //审批流id
  290. contractApprovalRecord.ContractApprovalRecordId = int(contractApprovalRecordId)
  291. }
  292. switch contractApprovalInfo.ApprovalType {
  293. case "contract":
  294. //修改合同状态为待审批
  295. sql := `UPDATE contract SET status="待审批",modify_time=NOW() WHERE contract_id = ?`
  296. _, err = tx.Raw(sql, contractApprovalInfo.ContractId).Exec()
  297. case "seal":
  298. //修改用印状态为待审批
  299. sql := `UPDATE seal SET status="待审批",modify_time=NOW() WHERE seal_id = ?`
  300. _, err = tx.Raw(sql, contractApprovalInfo.ContractId).Exec()
  301. }
  302. return
  303. }
  304. //撤回审核单
  305. func (ContractApproval) Cancel(contractApprovalInfo *ContractApproval, contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord) (err error) {
  306. o := orm.NewOrm()
  307. tx, err := o.Begin()
  308. if err != nil {
  309. return
  310. }
  311. defer func() {
  312. if err != nil {
  313. _ = tx.Rollback()
  314. } else {
  315. _ = tx.Commit()
  316. }
  317. }()
  318. //变更审批单记录
  319. contractApprovalInfo.Status = "已撤回"
  320. contractApprovalInfo.ModifyTime = time.Now()
  321. _, err = tx.Update(contractApprovalInfo, "Status", "ModifyTime")
  322. if err != nil {
  323. return
  324. }
  325. //变更所有审批流记录
  326. for _, contractApprovalRecord := range contractApprovalRecordList {
  327. contractApprovalRecord.Status = "已撤回"
  328. contractApprovalRecord.ModifyTime = time.Now()
  329. _, tmpErr := tx.Update(contractApprovalRecord, "Status", "ModifyTime")
  330. if tmpErr != nil {
  331. err = tmpErr
  332. return
  333. }
  334. }
  335. switch contractApprovalInfo.ApprovalType {
  336. case "contract":
  337. //修改合同状态为已撤回
  338. sql := `UPDATE contract SET status="已撤回",modify_time=NOW() WHERE contract_id = ?`
  339. _, err = tx.Raw(sql, contractApprovalInfo.ContractId).Exec()
  340. case "seal":
  341. //修改用印状态为已撤回
  342. sql := `UPDATE seal SET status="已撤回",modify_time=NOW() WHERE seal_id = ?`
  343. _, err = tx.Raw(sql, contractApprovalInfo.ContractId).Exec()
  344. }
  345. return
  346. }
  347. //审核驳回
  348. func (ContractApproval) Reject(contractApprovalInfo *ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, approveUserId int, approveUserName, remark string) (err error) {
  349. o := orm.NewOrm()
  350. tx, err := o.Begin()
  351. if err != nil {
  352. return
  353. }
  354. defer func() {
  355. if err != nil {
  356. _ = tx.Rollback()
  357. } else {
  358. _ = tx.Commit()
  359. }
  360. }()
  361. //变更审批单记录
  362. contractApprovalInfo.Status = "已驳回"
  363. contractApprovalInfo.ApproveRemark = remark //审核失败理由
  364. contractApprovalInfo.ModifyTime = time.Now()
  365. //contractApprovalInfo.CurrNodeId = 0 //当前节点变更为0,代表当前审批流程已经结束了
  366. _, err = tx.Update(contractApprovalInfo, "Status", "ApproveRemark", "ModifyTime")
  367. if err != nil {
  368. return
  369. }
  370. //变更审批流记录
  371. contractApprovalRecord.Status = "已驳回"
  372. contractApprovalRecord.ApproveRemark = remark //审核失败理由
  373. contractApprovalRecord.ApproveUserId = approveUserId //审批人id
  374. contractApprovalRecord.ApproveUserName = approveUserName //审批人名称
  375. contractApprovalRecord.ApproveTime = time.Now() //审批人名称
  376. contractApprovalRecord.ModifyTime = time.Now()
  377. _, err = tx.Update(contractApprovalRecord)
  378. if err != nil {
  379. return
  380. }
  381. switch contractApprovalInfo.ApprovalType {
  382. case "contract":
  383. //修改合同状态为已驳回
  384. sql := `UPDATE contract SET status="已驳回",modify_content="",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE contract_id = ?`
  385. _, err = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
  386. case "seal":
  387. //修改用印状态为已驳回
  388. sql := `UPDATE seal SET status="已驳回",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE seal_id = ?`
  389. _, err = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
  390. }
  391. return
  392. }
  393. //下级审批人信息
  394. type NextApproval struct {
  395. ApproveRoleTypeCode string
  396. ApproveUserId int
  397. ApproveUserName string
  398. }
  399. //审核通过
  400. func (ContractApproval) Approved(contractApprovalInfo *ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, remark string) (err error) {
  401. o := orm.NewOrm()
  402. tx, err := o.Begin()
  403. if err != nil {
  404. return
  405. }
  406. defer func() {
  407. if err != nil {
  408. _ = tx.Rollback()
  409. } else {
  410. _ = tx.Commit()
  411. }
  412. }()
  413. //变更审批流记录
  414. contractApprovalRecord.Status = "已审批"
  415. contractApprovalRecord.ApproveRemark = remark //审核失败理由
  416. contractApprovalRecord.ApproveTime = time.Now() //审批时间
  417. contractApprovalRecord.ModifyTime = time.Now()
  418. _, err = tx.Update(contractApprovalRecord, "Status", "ApproveRemark", "ApproveTime", "ModifyTime")
  419. if err != nil {
  420. return
  421. }
  422. //如果没有下级节点了,那么完成该审批单
  423. if contractApprovalRecord.NextNodeId <= 0 {
  424. //变更审批单记录
  425. contractApprovalInfo.Status = "已审批"
  426. contractApprovalInfo.ApproveRemark = remark //审核理由
  427. //contractApprovalInfo.CurrNodeId = contractApprovalRecord.NextNodeId //下级审批节点id
  428. contractApprovalInfo.ModifyTime = time.Now()
  429. _, tmpErr := tx.Update(contractApprovalInfo, "Status", "ApproveRemark", "ModifyTime")
  430. if tmpErr != nil {
  431. err = tmpErr
  432. return
  433. }
  434. switch contractApprovalInfo.ApprovalType {
  435. case "contract":
  436. //修改合同状态为已审批
  437. sql := `UPDATE contract SET status="已审批",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE contract_id = ?`
  438. _, tmpErr = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
  439. if tmpErr != nil {
  440. err = tmpErr
  441. return
  442. }
  443. case "seal":
  444. //修改用印状态为已审批
  445. sql := `UPDATE seal SET status="已审批",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE seal_id = ?`
  446. _, tmpErr = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
  447. if tmpErr != nil {
  448. err = tmpErr
  449. return
  450. }
  451. }
  452. } else {
  453. //变更审批单记录
  454. contractApprovalInfo.ApproveRemark = remark //审核理由
  455. contractApprovalInfo.CurrNodeId = contractApprovalRecord.NextNodeId //下级审批节点id
  456. contractApprovalInfo.ModifyTime = time.Now()
  457. _, tmpErr := tx.Update(contractApprovalInfo, "ApproveRemark", "CurrNodeId", "ModifyTime")
  458. if tmpErr != nil {
  459. err = tmpErr
  460. return
  461. }
  462. }
  463. return
  464. }
  465. //抄送
  466. func (ContractApproval) ApprovedByCc(contractApprovalInfo *ContractApproval, contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord, remark string, nextNodeId int) (err error) {
  467. o := orm.NewOrm()
  468. tx, err := o.Begin()
  469. if err != nil {
  470. return
  471. }
  472. defer func() {
  473. if err != nil {
  474. _ = tx.Rollback()
  475. } else {
  476. _ = tx.Commit()
  477. }
  478. }()
  479. for _, contractApprovalRecord := range contractApprovalRecordList {
  480. //变更审批流记录
  481. contractApprovalRecord.Status = "已审批"
  482. contractApprovalRecord.ApproveTime = time.Now() //审批时间
  483. contractApprovalRecord.ModifyTime = time.Now()
  484. _, err = tx.Update(contractApprovalRecord, "Status", "ApproveTime", "ModifyTime")
  485. if err != nil {
  486. return
  487. }
  488. }
  489. if err != nil {
  490. return
  491. }
  492. //如果没有下级节点了,那么完成该审批单
  493. if nextNodeId <= 0 {
  494. //变更审批单记录
  495. contractApprovalInfo.Status = "已审批"
  496. //contractApprovalInfo.CurrNodeId = nextNodeId //下级审批节点id
  497. contractApprovalInfo.ModifyTime = time.Now()
  498. _, tmpErr := tx.Update(contractApprovalInfo, "Status", "ModifyTime")
  499. if tmpErr != nil {
  500. err = tmpErr
  501. return
  502. }
  503. switch contractApprovalInfo.ApprovalType {
  504. case "contract":
  505. //修改合同状态为已审批
  506. sql := `UPDATE contract SET status="已审批",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE contract_id = ?`
  507. _, tmpErr = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
  508. if tmpErr != nil {
  509. err = tmpErr
  510. return
  511. }
  512. case "seal":
  513. //修改用印状态为已审批
  514. sql := `UPDATE seal SET status="已审批",approval_remark=?,approve_time=NOW(),modify_time=NOW() WHERE seal_id = ?`
  515. _, tmpErr = tx.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
  516. if tmpErr != nil {
  517. err = tmpErr
  518. return
  519. }
  520. }
  521. } else {
  522. //变更审批单记录
  523. contractApprovalInfo.CurrNodeId = nextNodeId //下级审批节点id
  524. contractApprovalInfo.ModifyTime = time.Now()
  525. _, tmpErr := tx.Update(contractApprovalInfo, "CurrNodeId", "ModifyTime")
  526. if tmpErr != nil {
  527. err = tmpErr
  528. return
  529. }
  530. }
  531. return
  532. }
  533. //根据合同审批单id集合获取合同审批单信息列表
  534. func GetContractApprovalByIds(contractApprovalIds string) (contractApprovalList []*ContractApproval, err error) {
  535. o := orm.NewOrm()
  536. sql := `select * from contract_approval where contract_approval_id in (` + contractApprovalIds + `) `
  537. _, err = o.Raw(sql).QueryRows(&contractApprovalList)
  538. return
  539. }
  540. //获取用印审批列表数据数量
  541. func GetSealApprovalListCount(childCondition, condition, joinCondition string, childPars, pars []interface{}) (count int, err error) {
  542. o := orm.NewOrm()
  543. 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,
  544. c.seal_type,c.company_name,c.user_id,c.user_name,c.file_url,c.code from contract_approval a
  545. join
  546. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="seal" `
  547. sql += childCondition
  548. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  549. JOIN seal c ON c.seal_id = a.contract_id
  550. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
  551. WHERE c.is_delete = 0 AND a.approval_type="seal" `
  552. sql += condition
  553. sql += ` group by contract_id `
  554. sql = `select count(*) count from (` + sql + `) g`
  555. err = o.Raw(sql, childPars, pars).QueryRow(&count)
  556. return
  557. }
  558. //用印审批列表
  559. type SealApprovalList struct {
  560. ContractApprovalId int `description:"审批单id"`
  561. ContractId int `description:"合同id"`
  562. ContractApprovalRecordId int `description:"审批流id"`
  563. ContractCode string `description:"合同编号"`
  564. Status string `description:"合同状态,枚举值:待审批','已审批','已驳回','已撤回','已签回',默认待审批"`
  565. ApproveStatus string `json:"-" description:"审批单状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
  566. ApplyContent string `description:"待审内容"`
  567. ApplyUserId int `description:"待审内容"`
  568. ApplyUserName string `description:"待审内容"`
  569. ContractDetail string `json:"-" description:"提交审批时的合同信息;用印快照"`
  570. ContractInfo contract.ContractDetail `json:"-" description:"提交审批时的合同信息;用印快照"`
  571. ApproveRemark string `description:"审核备注"`
  572. SealType string `description:"用印类型"`
  573. CompanyName string `description:"客户名称"`
  574. FileUrl string `description:"合同下载地址"`
  575. UserId int `description:"申请人id"`
  576. CurrNodeId int `description:"当前审批节点id"`
  577. StartNodeId int `description:"开始审批节点id"`
  578. UserName string `description:"申请人名称"`
  579. CreateTime time.Time `description:"发起审批的时间"`
  580. ModifyTime time.Time `description:"最后一次修改的时间"`
  581. ApproveTime time.Time `description:"审批时间"`
  582. InvalidTime time.Time `description:"作废时间"`
  583. CheckBackFileTime time.Time `description:"签回用印附件时间"`
  584. StartDateStr string `description:"合同开始日期(字符串类型)"`
  585. EndDateStr string `description:"合同结束日期(字符串类型)"`
  586. CreateTimeStr string `description:"发起审批的时间(字符串类型)"`
  587. ModifyTimeStr string `description:"最后一次修改的时间(字符串类型)"`
  588. ApproveTimeStr string `description:"审批时间(字符串类型)"`
  589. InvalidTimeStr string `description:"作废时间(字符串类型)"`
  590. }
  591. //获取合同审批列表数据
  592. func GetSealApprovalList(childCondition, condition, joinCondition string, childPars, pars []interface{}, startSize, pageSize int) (list []*SealApprovalList, err error) {
  593. o := orm.NewOrm()
  594. 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
  595. join
  596. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="seal" `
  597. sql += childCondition
  598. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  599. JOIN seal c ON c.seal_id = a.contract_id
  600. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id ` + joinCondition + `
  601. WHERE c.is_delete = 0 AND a.approval_type="seal" `
  602. sql += condition
  603. sql += ` group by contract_id order by modify_time desc LIMIT ?,? `
  604. _, err = o.Raw(sql, childPars, pars, startSize, pageSize).QueryRows(&list)
  605. return
  606. }
  607. //处理后的
  608. func GetSealApprovalListCountV2(childCondition, condition string, childPars, pars []interface{}) (count int, err error) {
  609. o := orm.NewOrm()
  610. 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,
  611. c.seal_type,c.company_name,c.user_id,c.user_name,c.file_url,c.code from contract_approval a
  612. join
  613. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="seal" `
  614. sql += childCondition
  615. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  616. JOIN seal c ON c.seal_id = a.contract_id
  617. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
  618. WHERE c.is_delete = 0 AND a.approval_type="seal" `
  619. sql += condition
  620. sql += ` group by contract_id `
  621. sql = `select count(*) count from (` + sql + `) g`
  622. err = o.Raw(sql, childPars, pars).QueryRow(&count)
  623. return
  624. }
  625. func GetSealApprovalListV2(childCondition, condition, orderBy string, childPars, pars []interface{}, startSize, pageSize int) (list []*SealApprovalList, err error) {
  626. o := orm.NewOrm()
  627. 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
  628. join
  629. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="seal" `
  630. sql += childCondition
  631. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  632. JOIN seal c ON c.seal_id = a.contract_id
  633. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
  634. WHERE c.is_delete = 0 AND a.approval_type="seal" `
  635. sql += condition
  636. sql += ` group by contract_id order by ` + orderBy + ` LIMIT ?,? `
  637. _, err = o.Raw(sql, childPars, pars, startSize, pageSize).QueryRows(&list)
  638. return
  639. }
  640. //审批列表
  641. type CompanyApprovalList struct {
  642. ContractApprovalId int `description:"审批单id"`
  643. ContractId int `description:"客户单id"`
  644. ContractApprovalRecordId int `description:"审批流id"`
  645. ContractCode string `description:"合同编号"`
  646. Status string `description:"审批单状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
  647. ApplyContent string `description:"待审内容"`
  648. ContractDetail string `json:"-" description:"提交审批时的客户审批信息;客户审批快照"`
  649. ContractInfo contract.ContractDetail `description:"提交审批时的合同信息;合同快照"`
  650. ApproveRemark string `description:"审核备注"`
  651. CompanyName string `description:"客户名称"`
  652. SellerId int `description:"销售id"`
  653. ApplyUserId int `description:"申请人id"`
  654. CurrNodeId int `description:"当前节点"`
  655. StartNodeId int `description:"开始节点"`
  656. ProductId int `description:"产品id,1:ficc;2:权益"`
  657. SellerName string `description:"销售名称"`
  658. StartDate time.Time `description:"合同开始日期"`
  659. EndDate time.Time `description:"合同结束日期"`
  660. CreateTime time.Time `description:"发起审批的时间"`
  661. ModifyTime time.Time `description:"最后一次修改的时间"`
  662. ApproveTime time.Time `description:"审批时间"`
  663. StartDateStr string `description:"合同开始日期(字符串类型)"`
  664. EndDateStr string `description:"合同结束日期(字符串类型)"`
  665. CreateTimeStr string `description:"发起审批的时间(字符串类型)"`
  666. ModifyTimeStr string `description:"最后一次修改的时间(字符串类型)"`
  667. ApproveTimeStr string `description:"审批时间(字符串类型)"`
  668. CompanyId int
  669. CreditCode string `description:"社会统一信用码"`
  670. CompanyType string `description:"客户类型"`
  671. IndustryName string `description:"行业名称"`
  672. ApprovalTime string `description:"提交申请时间"`
  673. CompanyProductStatus string `description:"客户状态"`
  674. ApproveContent string `description:"待审内容"`
  675. ApproveStatus string `description:"当前状态"`
  676. ApplyMethod int `description:"申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议"`
  677. ApplyReasons string `description:"申请理由"`
  678. FreezeStartDate string `description:"冻结开始日期"`
  679. FreezeEndDate string `description:"冻结结束日期"`
  680. FreezeExpireDays int `description:"冻结到期天数"`
  681. Address string `description:"地址"`
  682. Source string `description:"客户来源"`
  683. ExpireDay int `description:"服务到期天数"`
  684. CompanyApprovalId int `description:"审批记录ID"`
  685. ApprovalCount int `description:"已延期审批次数"`
  686. CompanyContractId int `description:"合同id"`
  687. DelayPermission string `description:"延期申请品种"`
  688. ApplyRealName string `description:"申请人姓名"`
  689. ContractSourceTag string `description:"合同来源标签:非标合同,标准合同"`
  690. }
  691. func GetCompanyApprovalCount(condition string, pars []interface{}) (count int, err error) {
  692. o := orm.NewOrm()
  693. sql := `
  694. SELECT
  695. 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
  696. from contract_approval a
  697. join ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="company" `
  698. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  699. INNER JOIN company_approval AS c ON a.contract_id=c.company_approval_id
  700. INNER JOIN company_product AS d ON d.company_id=c.company_id AND d.product_id=c.product_id
  701. INNER JOIN company AS e ON e.company_id=d.company_id
  702. JOIN contract_approval_record f on a.contract_approval_id=f.contract_approval_id
  703. WHERE 1=1 and a.approval_type="company" `
  704. sql += condition
  705. sql += ` group by contract_id `
  706. sql = `select count(1) from (` + sql + `) z `
  707. err = o.Raw(sql, pars).QueryRow(&count)
  708. return
  709. }
  710. func GetCompanyApprovalList(condition string, pars []interface{}, startSize, pageSize int) (list []*CompanyApprovalList, err error) {
  711. o := orm.NewOrm()
  712. sql := `
  713. SELECT
  714. 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
  715. from contract_approval a
  716. join ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="company" `
  717. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  718. INNER JOIN company_approval AS c ON a.contract_id=c.company_approval_id
  719. INNER JOIN company_product AS d ON d.company_id=c.company_id AND d.product_id=c.product_id
  720. INNER JOIN company AS e ON e.company_id=d.company_id
  721. JOIN contract_approval_record f on a.contract_approval_id=f.contract_approval_id
  722. WHERE 1=1 and a.approval_type="company" `
  723. sql += condition
  724. sql += ` group by contract_id order by modify_time desc LIMIT ?,? `
  725. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  726. return
  727. }