contract_approval.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. package contract_approval
  2. import (
  3. "hongze/hongze_mobile_admin/models/tables/contract"
  4. "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
  5. "hongze/hongze_mobile_admin/utils"
  6. "rdluck_tools/orm"
  7. "time"
  8. )
  9. //合同审批单
  10. type ContractApproval struct {
  11. ContractApprovalId int `orm:"column(contract_approval_id);pk"`
  12. ContractId int `orm:"column(contract_id)";description:"合同id"`
  13. Status string `description:"审批单状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
  14. ApplyContent string `description:"待审内容,长度255位"`
  15. ContractDetail string `description:"审批单详情,完整的合同json数据"`
  16. ApplyUserId int `description:"申请人id"`
  17. ApplyUserName string `description:"申请人名称"`
  18. ApproveRemark string `description:"审批人备注,可以是驳回理由,长度128位"`
  19. FlowId int `description:"审批流程id"`
  20. FlowVersion int `description:"审批流程版本"`
  21. CurrNodeId int `description:"当前审批节点id"`
  22. ModifyTime time.Time `description:"发起申请时间"`
  23. CreateTime time.Time `description:"最近一次审批单修改时间"`
  24. }
  25. //根据合同审批单id获取合同审批单信息
  26. func GetContractApprovalById(contractApprovalId int) (contractApprovalInfo *ContractApproval, err error) {
  27. o := orm.NewOrm()
  28. sql := `select * from contract_approval where contract_approval_id = ? `
  29. err = o.Raw(sql, contractApprovalId).QueryRow(&contractApprovalInfo)
  30. return
  31. }
  32. //根据合同id获取最近一次待审批的合同审批单信息
  33. func GetLastPendingContractApprovalByContractId(contractId int) (contractApprovalInfo *ContractApproval, err error) {
  34. o := orm.NewOrm()
  35. sql := `select * from contract_approval where status = "待审批" AND contract_id=? `
  36. err = o.Raw(sql, contractId).QueryRow(&contractApprovalInfo)
  37. return
  38. }
  39. //根据合同id获取总共被驳回的次数
  40. func GetRejectContractCountByContractId(contractId int) (total int64, err error) {
  41. o := orm.NewOrm()
  42. sql := `select count(1) total from contract_approval where status = "已驳回" AND contract_id=? `
  43. err = o.Raw(sql, contractId).QueryRow(&total)
  44. return
  45. }
  46. //获取审批列表数据数量
  47. func GetContractApprovalListCountOld(condition string, pars []interface{}) (count int, err error) {
  48. o := orm.NewOrm()
  49. sql := `
  50. SELECT a.* from contract_approval a
  51. JOIN contract c ON a.contract_id = c.contract_id
  52. WHERE
  53. c.is_delete = 0 `
  54. sql += condition
  55. sql += ` GROUP BY a.contract_id `
  56. sql = `select count(*) count from (` + sql + `) b`
  57. err = o.Raw(sql, pars).QueryRow(&count)
  58. return
  59. }
  60. func GetContractApprovalListCount(childCondition, condition string, childPars, pars []interface{}) (count int, err error) {
  61. o := orm.NewOrm()
  62. sql := `
  63. SELECT
  64. a.contract_approval_id,a.contract_id,a.apply_content,a.contract_detail,a.approve_remark,a.create_time,a.modify_time,a.status,c.status contract_status,c.product_id,
  65. 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
  66. join
  67. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 `
  68. sql += childCondition
  69. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  70. JOIN contract c ON a.contract_id = c.contract_id
  71. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
  72. WHERE c.is_delete = 0 `
  73. sql += condition
  74. sql += ` group by contract_id `
  75. sql = `select count(*) count from (` + sql + `) g`
  76. err = o.Raw(sql, pars).QueryRow(&count)
  77. return
  78. }
  79. //审批列表
  80. type ContractApprovalList struct {
  81. ContractApprovalId int `description:"审批单id"`
  82. ContractId int `description:"合同id"`
  83. ContractApprovalRecordId int `description:"审批流id"`
  84. ContractCode string `description:"合同编号"`
  85. Status string `description:"审批单状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
  86. ContractStatus string `json:"-";description:"合同状态,枚举值:待审批','已审批','已驳回','已撤回',默认待审批"`
  87. ApplyContent string `description:"待审内容"`
  88. ContractDetail string `json:"-";description:"提交审批时的合同信息;合同快照"`
  89. ContractInfo contract.ContractDetail `description:"提交审批时的合同信息;合同快照"`
  90. ApproveRemark string `description:"审核备注"`
  91. ContractType string `description:"合同类型"`
  92. Price float64 `description:"合同金额"`
  93. CompanyName string `description:"客户名称"`
  94. FileUrl string `description:"合同下载地址"`
  95. SellerId int `description:"销售id"`
  96. ProductId int `description:"产品id,1:ficc;2:权益"`
  97. SellerName string `description:"销售名称"`
  98. StartDate time.Time `description:"合同开始日期"`
  99. EndDate time.Time `description:"合同结束日期"`
  100. CreateTime time.Time `description:"发起审批的时间"`
  101. ModifyTime time.Time `description:"最后一次修改的时间"`
  102. ApproveTime time.Time `description:"审批时间"`
  103. CanApprovalButton bool `description:"是否具有审核操作权限,true才允许审核操作"`
  104. StartDateStr string `description:"合同开始日期(字符串类型)"`
  105. EndDateStr string `description:"合同结束日期(字符串类型)"`
  106. CreateTimeStr string `description:"发起审批的时间(字符串类型)"`
  107. ModifyTimeStr string `description:"最后一次修改的时间(字符串类型)"`
  108. ApproveTimeStr string `description:"审批时间(字符串类型)"`
  109. }
  110. //获取审批列表数据
  111. func GetContractApprovalList(childCondition, condition string, childPars, pars []interface{}, startSize, pageSize int) (list []*ContractApprovalList, err error) {
  112. o := orm.NewOrm()
  113. sql := `
  114. SELECT
  115. a.contract_approval_id,a.contract_id,a.approve_remark,a.create_time,a.modify_time,a.status,c.status contract_status,c.product_id,
  116. 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
  117. join
  118. ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 `
  119. sql += childCondition
  120. sql += ` GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
  121. JOIN contract c ON a.contract_id = c.contract_id
  122. JOIN contract_approval_record d on a.contract_approval_id=d.contract_approval_id
  123. WHERE c.is_delete = 0 `
  124. sql += condition
  125. sql += ` group by contract_id order by modify_time desc LIMIT ?,? `
  126. _, err = o.Raw(sql, childPars, pars, startSize, pageSize).QueryRows(&list)
  127. return
  128. }
  129. //根据合同id获取合同审批单信息
  130. func (ContractApproval) CheckPendingByContractId(contractId int) (has bool, err error) {
  131. o := orm.NewOrm()
  132. var contractApprovalInfo ContractApproval
  133. sql := `select * from contract_approval where contract_id = ? AND status="待审批" limit 1`
  134. err = o.Raw(sql, contractId).QueryRow(&contractApprovalInfo)
  135. if err != nil {
  136. if err.Error() == utils.ErrNoRow() {
  137. //如果不存在待审批记录,那么报错信息置空,存在与否直接返回false
  138. err = nil
  139. return
  140. }
  141. //系统异常,返回错误信息
  142. return
  143. }
  144. //存在待审批的审批单
  145. has = true
  146. return
  147. }
  148. //发起审批
  149. func (ContractApproval) Apply(contractApprovalInfo *ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord) (err error) {
  150. o := orm.NewOrm()
  151. o.Begin()
  152. defer func() {
  153. if err != nil {
  154. o.Rollback()
  155. } else {
  156. o.Commit()
  157. }
  158. }()
  159. //审批单记录
  160. ContractApprovalId, err := o.Insert(contractApprovalInfo)
  161. if err != nil {
  162. return
  163. }
  164. //审批单id
  165. contractApprovalInfo.ContractApprovalId = int(ContractApprovalId)
  166. //审批流记录
  167. contractApprovalRecord.ContractApprovalId = contractApprovalInfo.ContractApprovalId
  168. contractApprovalRecordId, err := o.Insert(contractApprovalRecord)
  169. if err != nil {
  170. return
  171. }
  172. //审批流id
  173. contractApprovalRecord.ContractApprovalRecordId = int(contractApprovalRecordId)
  174. //修改合同状态为待审批
  175. sql := `UPDATE contract SET status="待审批",modify_time=NOW() WHERE contract_id = ?`
  176. _, err = o.Raw(sql, contractApprovalInfo.ContractId).Exec()
  177. return
  178. }
  179. //发起审批(工作流)
  180. func (ContractApproval) Apply2(contractApprovalInfo *ContractApproval, contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord) (err error) {
  181. o := orm.NewOrm()
  182. o.Begin()
  183. defer func() {
  184. if err != nil {
  185. o.Rollback()
  186. } else {
  187. o.Commit()
  188. }
  189. }()
  190. //审批单记录
  191. ContractApprovalId, err := o.Insert(contractApprovalInfo)
  192. if err != nil {
  193. return
  194. }
  195. //审批单id
  196. contractApprovalInfo.ContractApprovalId = int(ContractApprovalId)
  197. for _, contractApprovalRecord := range contractApprovalRecordList {
  198. //审批流记录
  199. contractApprovalRecord.ContractApprovalId = contractApprovalInfo.ContractApprovalId
  200. contractApprovalRecordId, tmpErr := o.Insert(contractApprovalRecord)
  201. if tmpErr != nil {
  202. err = tmpErr
  203. return
  204. }
  205. //审批流id
  206. contractApprovalRecord.ContractApprovalRecordId = int(contractApprovalRecordId)
  207. }
  208. //修改合同状态为待审批
  209. sql := `UPDATE contract SET status="待审批",modify_time=NOW() WHERE contract_id = ?`
  210. _, err = o.Raw(sql, contractApprovalInfo.ContractId).Exec()
  211. return
  212. }
  213. //撤回审核单
  214. func (ContractApproval) Cancel(contractApprovalInfo *ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord) (err error) {
  215. o := orm.NewOrm()
  216. o.Begin()
  217. defer func() {
  218. if err != nil {
  219. o.Rollback()
  220. } else {
  221. o.Commit()
  222. }
  223. }()
  224. //变更审批单记录
  225. contractApprovalInfo.Status = "已撤回"
  226. contractApprovalInfo.ModifyTime = time.Now()
  227. _, err = o.Update(contractApprovalInfo)
  228. if err != nil {
  229. return
  230. }
  231. //变更审批流记录
  232. contractApprovalRecord.Status = "已撤回"
  233. contractApprovalRecord.ModifyTime = time.Now()
  234. _, err = o.Update(contractApprovalRecord)
  235. if err != nil {
  236. return
  237. }
  238. //修改合同状态为已撤回
  239. sql := `UPDATE contract SET status="已撤回",modify_time=NOW() WHERE contract_id = ?`
  240. _, err = o.Raw(sql, contractApprovalInfo.ContractId).Exec()
  241. return
  242. }
  243. func (ContractApproval) Cancel2(contractApprovalInfo *ContractApproval, contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord) (err error) {
  244. o := orm.NewOrm()
  245. o.Begin()
  246. defer func() {
  247. if err != nil {
  248. o.Rollback()
  249. } else {
  250. o.Commit()
  251. }
  252. }()
  253. //变更审批单记录
  254. contractApprovalInfo.Status = "已撤回"
  255. contractApprovalInfo.ModifyTime = time.Now()
  256. _, err = o.Update(contractApprovalInfo)
  257. if err != nil {
  258. return
  259. }
  260. //变更所有审批流记录
  261. for _, contractApprovalRecord := range contractApprovalRecordList {
  262. contractApprovalRecord.Status = "已撤回"
  263. contractApprovalRecord.ModifyTime = time.Now()
  264. _, tmpErr := o.Update(contractApprovalRecord)
  265. if tmpErr != nil {
  266. err = tmpErr
  267. return
  268. }
  269. }
  270. //修改合同状态为已撤回
  271. sql := `UPDATE contract SET status="已撤回",modify_time=NOW() WHERE contract_id = ?`
  272. _, err = o.Raw(sql, contractApprovalInfo.ContractId).Exec()
  273. return
  274. }
  275. //审核驳回
  276. func (ContractApproval) Reject(contractApprovalInfo *ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, approveUserId int, approveUserName, remark string) (err error) {
  277. o := orm.NewOrm()
  278. o.Begin()
  279. defer func() {
  280. if err != nil {
  281. o.Rollback()
  282. } else {
  283. o.Commit()
  284. }
  285. }()
  286. //变更审批单记录
  287. contractApprovalInfo.Status = "已驳回"
  288. contractApprovalInfo.ApproveRemark = remark //审核失败理由
  289. contractApprovalInfo.ModifyTime = time.Now()
  290. contractApprovalInfo.CurrNodeId = 0 //当前节点变更为0,代表当前审批流程已经结束了
  291. _, err = o.Update(contractApprovalInfo)
  292. if err != nil {
  293. return
  294. }
  295. //变更审批流记录
  296. contractApprovalRecord.Status = "已驳回"
  297. contractApprovalRecord.ApproveRemark = remark //审核失败理由
  298. contractApprovalRecord.ApproveUserId = approveUserId //审批人id
  299. contractApprovalRecord.ApproveUserName = approveUserName //审批人名称
  300. contractApprovalRecord.ApproveTime = time.Now() //审批人名称
  301. contractApprovalRecord.ModifyTime = time.Now()
  302. _, err = o.Update(contractApprovalRecord)
  303. if err != nil {
  304. return
  305. }
  306. //修改合同状态为已驳回
  307. sql := `UPDATE contract SET status="已驳回",modify_content="",approval_remark=?,modify_time=NOW() WHERE contract_id = ?`
  308. _, err = o.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
  309. return
  310. }
  311. //下级审批人信息
  312. type NextApproval struct {
  313. ApproveRoleTypeCode string
  314. ApproveUserId int
  315. ApproveUserName string
  316. }
  317. //审核通过
  318. func (ContractApproval) ApprovedOld(contractApprovalInfo *ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, approveUserId int, approveUserName, remark string, nextApproval NextApproval) (err error) {
  319. o := orm.NewOrm()
  320. o.Begin()
  321. defer func() {
  322. if err != nil {
  323. o.Rollback()
  324. } else {
  325. o.Commit()
  326. }
  327. }()
  328. //变更审批流记录
  329. contractApprovalRecord.Status = "已审批"
  330. contractApprovalRecord.ApproveRemark = remark //审核失败理由
  331. contractApprovalRecord.ApproveUserId = approveUserId //审批人id
  332. contractApprovalRecord.ApproveUserName = approveUserName //审批人名称
  333. contractApprovalRecord.ApproveTime = time.Now() //审批人名称
  334. contractApprovalRecord.ModifyTime = time.Now()
  335. _, err = o.Update(contractApprovalRecord)
  336. if err != nil {
  337. return
  338. }
  339. //判断是否存在下一级的审批人,如果不存在,那么就去变更审批单和合同的状态
  340. if nextApproval.ApproveRoleTypeCode == "" && nextApproval.ApproveUserId == 0 {
  341. //变更审批单记录
  342. contractApprovalInfo.Status = "已审批"
  343. contractApprovalInfo.ApproveRemark = remark //审核理由
  344. contractApprovalInfo.ModifyTime = time.Now()
  345. _, tmpErr := o.Update(contractApprovalInfo)
  346. if tmpErr != nil {
  347. err = tmpErr
  348. return
  349. }
  350. //修改合同状态为已审批
  351. sql := `UPDATE contract SET status="已审批",approval_remark=?,modify_time=NOW() WHERE contract_id = ?`
  352. _, tmpErr = o.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
  353. if tmpErr != nil {
  354. err = tmpErr
  355. return
  356. }
  357. } else {
  358. //如果还存在下一级审批人,那么进入下级审批流
  359. nextContractApprovalRecord := &contract_approval_record.ContractApprovalRecord{
  360. ContractApprovalId: contractApprovalInfo.ContractApprovalId,
  361. Status: "待审批",
  362. ApproveRoleTypeCode: nextApproval.ApproveRoleTypeCode,
  363. ApproveUserId: nextApproval.ApproveUserId,
  364. ApproveUserName: nextApproval.ApproveUserName,
  365. ModifyTime: time.Now(),
  366. CreateTime: time.Now(),
  367. }
  368. nextContractApprovalRecordId, tmpErr := o.Insert(nextContractApprovalRecord)
  369. if tmpErr != nil {
  370. err = tmpErr
  371. return
  372. }
  373. nextContractApprovalRecord.ContractApprovalRecordId = int(nextContractApprovalRecordId)
  374. }
  375. return
  376. }
  377. func (ContractApproval) Approved(contractApprovalInfo *ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, remark string) (err error) {
  378. o := orm.NewOrm()
  379. o.Begin()
  380. defer func() {
  381. if err != nil {
  382. o.Rollback()
  383. } else {
  384. o.Commit()
  385. }
  386. }()
  387. //变更审批流记录
  388. contractApprovalRecord.Status = "已审批"
  389. contractApprovalRecord.ApproveRemark = remark //审核失败理由
  390. contractApprovalRecord.ApproveTime = time.Now() //审批时间
  391. contractApprovalRecord.ModifyTime = time.Now()
  392. _, err = o.Update(contractApprovalRecord, "Status", "ApproveRemark", "ApproveTime", "ModifyTime")
  393. if err != nil {
  394. return
  395. }
  396. //如果没有下级节点了,那么完成该审批单
  397. if contractApprovalRecord.NextNodeId <= 0 {
  398. //变更审批单记录
  399. contractApprovalInfo.Status = "已审批"
  400. contractApprovalInfo.ApproveRemark = remark //审核理由
  401. contractApprovalInfo.CurrNodeId = contractApprovalRecord.NextNodeId //下级审批节点id
  402. contractApprovalInfo.ModifyTime = time.Now()
  403. _, tmpErr := o.Update(contractApprovalInfo, "Status", "ApproveRemark", "CurrNodeId", "ModifyTime")
  404. if tmpErr != nil {
  405. err = tmpErr
  406. return
  407. }
  408. //修改合同状态为已审批
  409. sql := `UPDATE contract SET status="已审批",approval_remark=?,modify_time=NOW() WHERE contract_id = ?`
  410. _, tmpErr = o.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
  411. if tmpErr != nil {
  412. err = tmpErr
  413. return
  414. }
  415. } else {
  416. //变更审批单记录
  417. contractApprovalInfo.ApproveRemark = remark //审核理由
  418. contractApprovalInfo.CurrNodeId = contractApprovalRecord.NextNodeId //下级审批节点id
  419. contractApprovalInfo.ModifyTime = time.Now()
  420. _, tmpErr := o.Update(contractApprovalInfo, "ApproveRemark", "CurrNodeId", "ModifyTime")
  421. if tmpErr != nil {
  422. err = tmpErr
  423. return
  424. }
  425. }
  426. return
  427. }
  428. //抄送
  429. func (ContractApproval) ApprovedByCc(contractApprovalInfo *ContractApproval, contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord, remark string, nextNodeId int) (err error) {
  430. o := orm.NewOrm()
  431. o.Begin()
  432. defer func() {
  433. if err != nil {
  434. o.Rollback()
  435. } else {
  436. o.Commit()
  437. }
  438. }()
  439. for _, contractApprovalRecord := range contractApprovalRecordList {
  440. //变更审批流记录
  441. contractApprovalRecord.Status = "已审批"
  442. contractApprovalRecord.ApproveTime = time.Now() //审批时间
  443. contractApprovalRecord.ModifyTime = time.Now()
  444. _, err = o.Update(contractApprovalRecord, "Status", "ApproveTime", "ModifyTime")
  445. if err != nil {
  446. return
  447. }
  448. }
  449. if err != nil {
  450. return
  451. }
  452. //如果没有下级节点了,那么完成该审批单
  453. if nextNodeId <= 0 {
  454. //变更审批单记录
  455. contractApprovalInfo.Status = "已审批"
  456. contractApprovalInfo.CurrNodeId = nextNodeId //下级审批节点id
  457. contractApprovalInfo.ModifyTime = time.Now()
  458. _, tmpErr := o.Update(contractApprovalInfo, "Status", "CurrNodeId", "ModifyTime")
  459. if tmpErr != nil {
  460. err = tmpErr
  461. return
  462. }
  463. //修改合同状态为已审批
  464. sql := `UPDATE contract SET status="已审批",approval_remark=?,modify_time=NOW() WHERE contract_id = ?`
  465. _, tmpErr = o.Raw(sql, remark, contractApprovalInfo.ContractId).Exec()
  466. if tmpErr != nil {
  467. err = tmpErr
  468. return
  469. }
  470. } else {
  471. //变更审批单记录
  472. contractApprovalInfo.CurrNodeId = nextNodeId //下级审批节点id
  473. contractApprovalInfo.ModifyTime = time.Now()
  474. _, tmpErr := o.Update(contractApprovalInfo, "CurrNodeId", "ModifyTime")
  475. if tmpErr != nil {
  476. err = tmpErr
  477. return
  478. }
  479. }
  480. return
  481. }
  482. //根据合同审批单id集合获取合同审批单信息列表
  483. func GetContractApprovalByIds(contractApprovalIds string) (contractApprovalList []*ContractApproval, err error) {
  484. o := orm.NewOrm()
  485. sql := `select * from contract_approval where contract_approval_id in (` + contractApprovalIds + `) `
  486. _, err = o.Raw(sql).QueryRows(&contractApprovalList)
  487. return
  488. }