advance_audit.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. 'use strict'
  2. const auditConst = require('../const/audit').advance
  3. const pushType = require('../const/audit').pushType
  4. module.exports = app => {
  5. class AdvanceAudit extends app.BaseService {
  6. constructor(ctx) {
  7. super(ctx)
  8. this.tableName = 'advance_audit'
  9. }
  10. /**
  11. * 获取审核人流程列表
  12. * @param {Number} vid 预付款记录id
  13. * @param {Number} times 审核次数
  14. * @return {Promise<Array>} 查询结果集
  15. */
  16. async getAuditGroupByList(vid, times = 1) {
  17. const sql =
  18. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  19. 'FROM ?? AS la, ?? AS pa ' +
  20. 'WHERE la.`vid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` GROUP BY la.`audit_id` ORDER BY la.`order`'
  21. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, times]
  22. return await this.db.query(sql, sqlParam)
  23. }
  24. /**
  25. * 获取审核人流程列表(包括原报)
  26. * @param {Number} vid 预付款记录id
  27. * @param {Number} times 审核次数
  28. * @return {Promise<Array>} 查询结果集(包括原报)
  29. */
  30. async getAuditorsWithOwner(vid, times = 1) {
  31. const result = await this.getAuditGroupByList(vid, times)
  32. const sql =
  33. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As vid, 0 As `order`' +
  34. ' FROM ' +
  35. this.ctx.service.advance.tableName +
  36. ' As s' +
  37. ' LEFT JOIN ' +
  38. this.ctx.service.projectAccount.tableName +
  39. ' As pa' +
  40. ' ON s.uid = pa.id' +
  41. ' WHERE s.id = ?'
  42. const sqlParam = [times, vid, vid]
  43. const user = await this.db.queryOne(sql, sqlParam)
  44. result.unshift(user)
  45. return result
  46. }
  47. /**
  48. * 获取最新审核顺序
  49. * @param {Number} vid - 预付款id
  50. * @param {Number} times - 第几次审批
  51. * @return {Number} 审核顺序
  52. */
  53. async getNewOrder(vid, times = 1) {
  54. const sql = 'SELECT Max(??) As max_order FROM ?? Where `vid` = ? and `times` = ?'
  55. const sqlParam = ['order', this.tableName, vid, times]
  56. const result = await this.db.queryOne(sql, sqlParam)
  57. return result && result.max_order ? result.max_order + 1 : 1
  58. }
  59. /**
  60. * 新增审核人
  61. * @param {Number} vid - 预付款id
  62. * @param {Number} audit_id - 审核人id
  63. * @param {Number} times - 第几次审批
  64. * @return {Boolean} 是否插入成功
  65. */
  66. async addAuditor(vid, audit_id, times = 1) {
  67. const newOrder = await this.getNewOrder(vid, times)
  68. const record = {
  69. vid,
  70. audit_id,
  71. times,
  72. order: newOrder,
  73. status: auditConst.status.uncheck
  74. }
  75. const result = await this.db.insert(this.tableName, record)
  76. return result && result.affectedRows === 1
  77. }
  78. /**
  79. * 移除审核人
  80. * @param {Number} vid - 预付款id
  81. * @param {Number} audit_id - 审核人id
  82. * @param {Number} times - 第几次审批
  83. * @return {Promise<boolean>}
  84. */
  85. async deleteAuditor(vid, audit_id, times = 1) {
  86. const transaction = await this.db.beginTransaction()
  87. try {
  88. const condition = { vid, audit_id, times }
  89. const auditor = await this.getDataByCondition(condition)
  90. if (!auditor) {
  91. throw '该审核人不存在'
  92. }
  93. await this._syncOrderByDelete(transaction, vid, auditor.order, times)
  94. await transaction.delete(this.tableName, condition)
  95. await transaction.commit()
  96. } catch (err) {
  97. await transaction.rollback()
  98. throw err
  99. }
  100. return true
  101. }
  102. /**
  103. * 移除审核人时,同步其后审核人order
  104. * @param {Function} transaction - 事务
  105. * @param {Number} vid - 预付款id
  106. * @param {Number} order - 审核顺序
  107. * @param {Number} times - 第几次审批
  108. * @return {Promise<*>} 查询结果集
  109. * @private
  110. */
  111. async _syncOrderByDelete(transaction, vid, order, times) {
  112. this.initSqlBuilder()
  113. this.sqlBuilder.setAndWhere('vid', {
  114. value: this.db.escape(vid),
  115. operate: '='
  116. })
  117. this.sqlBuilder.setAndWhere('order', {
  118. value: order,
  119. operate: '>='
  120. })
  121. this.sqlBuilder.setAndWhere('times', {
  122. value: times,
  123. operate: '='
  124. })
  125. this.sqlBuilder.setUpdateData('order', {
  126. value: 1,
  127. selfOperate: '-'
  128. })
  129. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update')
  130. const data = await transaction.query(sql, sqlParam)
  131. return data
  132. }
  133. /**
  134. * 获取当前审核人
  135. * @param {Number} vid - 预付款id
  136. * @param {Number} times - 第几次审批
  137. * @return {Promise<Object>} 查询结果集
  138. */
  139. async getCurAuditor(vid, times = 1) {
  140. const sql =
  141. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  142. 'FROM ?? AS la, ?? AS pa ' +
  143. 'WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ?' +
  144. ' and la.`audit_id` = pa.`id`'
  145. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checking, times]
  146. return await this.db.queryOne(sql, sqlParam)
  147. }
  148. /**
  149. * 获取审核列表信息
  150. * @param {Number} vid - 预付款id
  151. * @param {Number} times - 第几次审批
  152. * @return {Promise<Array>} 查询结果集
  153. */
  154. async getAuditors(vid, times = 1) {
  155. const sql =
  156. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, g.`sort` ' +
  157. 'FROM ?? AS la, ?? AS pa, (SELECT `audit_id`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `vid` = ? AND `times` = ? GROUP BY `audit_id`) as g ' +
  158. 'WHERE la.`vid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` and g.`audit_id` = la.`audit_id` order by la.`order`'
  159. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, vid, times, vid, times]
  160. const result = await this.db.query(sql, sqlParam)
  161. const sql2 = 'SELECT COUNT(a.`audit_id`) as num FROM (SELECT `audit_id` FROM ?? WHERE `vid` = ? AND `times` = ? GROUP BY `audit_id`) as a'
  162. const sqlParam2 = [this.tableName, vid, times]
  163. const count = await this.db.queryOne(sql2, sqlParam2)
  164. for (const i in result) {
  165. result[i].max_sort = count.num
  166. }
  167. return result
  168. }
  169. /**
  170. * 获取审核人信息
  171. * @param {Number} vid - 预付款id
  172. * @param {Number} audit_id - 审核人id
  173. * @param {Number} times - 第几次审批
  174. * @return {Promise<*>} 查询结果
  175. */
  176. async getAuditor(vid, audit_id, times = 1) {
  177. const sql =
  178. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  179. 'FROM ?? AS la, ?? AS pa ' +
  180. 'WHERE la.`vid` = ? and la.`audit_id` = ? and la.`times` = ?' +
  181. ' and la.`audit_id` = pa.`id`'
  182. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, audit_id, times]
  183. return await this.db.queryOne(sql, sqlParam)
  184. }
  185. /**
  186. * 开始审批
  187. * @param {Number} vid - 预付款id
  188. * @param {Number} times - 第几次审批
  189. * @param {Object} data - 载荷
  190. */
  191. async start(vid, times = 1, data) {
  192. const audit = await this.getDataByCondition({ vid, times, order: 1 })
  193. if (!audit) {
  194. throw '请先选择审批人,再上报数据'
  195. }
  196. const transaction = await this.db.beginTransaction()
  197. try {
  198. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date() })
  199. await transaction.update(this.ctx.service.advance.tableName, {
  200. id: audit.vid,
  201. ...data
  202. })
  203. await transaction.commit()
  204. } catch (err) {
  205. await transaction.rollback()
  206. throw err
  207. }
  208. return true
  209. }
  210. async _checked(pid, advanceId, checkData, times) {
  211. const time = new Date()
  212. // 整理当前流程审核人状态更新
  213. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking })
  214. if (!audit) {
  215. throw '审核数据错误'
  216. }
  217. // 获取审核人列表
  218. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC'
  219. const sqlParam = [this.tableName, advanceId, times]
  220. const auditors = await this.db.query(sql, sqlParam)
  221. const nextAudit = await this.getDataByCondition({ vid: advanceId, times, order: audit.order + 1 })
  222. const transaction = await this.db.beginTransaction()
  223. try {
  224. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time })
  225. // 获取推送必要信息
  226. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id)
  227. // 添加推送
  228. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checked, content: noticeContent }]
  229. auditors.forEach(audit => {
  230. records.push({ pid, type: pushType.advance, uid: audit.aid, status: auditConst.status.checked, content: noticeContent })
  231. })
  232. await transaction.insert(this.ctx.service.notice.tableName, records)
  233. // 无下一审核人表示,审核结束
  234. if (nextAudit) {
  235. // 流程至下一审批人
  236. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time })
  237. // 同步期信息
  238. await transaction.update(this.ctx.service.advance.tableName, {
  239. id: advanceId,
  240. status: auditConst.status.checking
  241. })
  242. } else {
  243. await transaction.update(this.ctx.service.advance.tableName, {
  244. id: advanceId,
  245. status: checkData.checkType
  246. })
  247. }
  248. await transaction.commit()
  249. } catch (err) {
  250. await transaction.rollback()
  251. throw err
  252. }
  253. }
  254. async _checkNo(pid, advanceId, checkData, times) {
  255. const time = new Date()
  256. // 整理当前流程审核人状态更新
  257. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking })
  258. if (!audit) {
  259. throw '审核数据错误'
  260. }
  261. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC'
  262. const sqlParam = [this.tableName, advanceId, times]
  263. const auditors = await this.db.query(sql, sqlParam)
  264. let order = 1
  265. for (const a of auditors) {
  266. a.times = times + 1
  267. a.order = order
  268. a.status = auditConst.status.uncheck
  269. order++
  270. }
  271. const transaction = await this.db.beginTransaction()
  272. try {
  273. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time })
  274. // 添加到消息推送表
  275. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.aid)
  276. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNo, content: noticeContent }]
  277. auditors.forEach(audit => {
  278. records.push({ pid, type: pushType.advance, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent })
  279. })
  280. await transaction.insert(this.ctx.service.notice.tableName, records)
  281. // 同步期信息
  282. await transaction.update(this.ctx.service.advance.tableName, {
  283. id: advanceId,
  284. status: checkData.checkType,
  285. times: times + 1
  286. })
  287. // 拷贝新一次审核流程列表
  288. await transaction.insert(this.tableName, auditors)
  289. await transaction.commit()
  290. } catch (err) {
  291. await transaction.rollback()
  292. throw err
  293. }
  294. }
  295. async _checkNoPre(pid, advanceId, checkData, times) {
  296. const time = new Date()
  297. // 整理当前流程审核人状态更新
  298. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking })
  299. if (!audit || audit.order <= 1) {
  300. throw '审核数据错误'
  301. }
  302. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  303. const auditors2 = await this.getAuditGroupByList(advanceId, times)
  304. const auditorIndex = await auditors2.findIndex(function (item) {
  305. return item.audit_id === audit.audit_id
  306. })
  307. const preAuditor = auditors2[auditorIndex - 1]
  308. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.aid)
  309. const transaction = await this.db.beginTransaction()
  310. try {
  311. // 添加到消息推送表
  312. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNoPre, content: noticeContent }]
  313. auditors2.forEach(audit => {
  314. records.push({ pid, type: pushType.advance, uid: audit.aid, status: auditConst.status.checkNoPre, content: noticeContent })
  315. })
  316. await transaction.insert(this.ctx.service.notice.tableName, records)
  317. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time })
  318. // 顺移气候审核人流程顺序
  319. this.initSqlBuilder()
  320. this.sqlBuilder.setAndWhere('vid', { value: advanceId, operate: '=' })
  321. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' })
  322. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' })
  323. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update')
  324. await transaction.query(sql, sqlParam)
  325. const newAuditors = []
  326. newAuditors.push({
  327. tid: audit.tid,
  328. vid: audit.vid,
  329. audit_id: preAuditor.audit_id,
  330. times: audit.times,
  331. order: audit.order + 1,
  332. status: auditConst.status.checking,
  333. begin_time: time
  334. })
  335. newAuditors.push({
  336. tid: audit.tid,
  337. vid: audit.vid,
  338. audit_id: audit.audit_id,
  339. times: audit.times,
  340. order: audit.order + 2,
  341. status: auditConst.status.uncheck
  342. })
  343. await transaction.insert(this.tableName, newAuditors)
  344. await transaction.commit()
  345. } catch (error) {
  346. await transaction.rollback()
  347. throw error
  348. }
  349. }
  350. /**
  351. * 审批
  352. * @param {Number} advanceId - 预付款id
  353. * @param {auditConst.status.checked|auditConst.status.checkNo} checkData - 审批结果
  354. * @param {Number} times - 第几次审批
  355. * @return {Promise<void>}
  356. */
  357. async check(advanceId, checkData, times = 1) {
  358. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  359. throw '提交数据错误'
  360. }
  361. const pid = this.ctx.session.sessionProject.id
  362. switch (checkData.checkType) {
  363. case auditConst.status.checked:
  364. await this._checked(pid, advanceId, checkData, times)
  365. break
  366. case auditConst.status.checkNo:
  367. await this._checkNo(pid, advanceId, checkData, times)
  368. break
  369. case auditConst.status.checkNoPre:
  370. await this._checkNoPre(pid, advanceId, checkData, times)
  371. break
  372. default:
  373. throw '无效审批操作'
  374. }
  375. }
  376. /**
  377. * 用于添加推送所需的content内容
  378. * @param {Number} pid 项目id
  379. * @param {Number} tid 台账id
  380. * @param {Number} vid 预付款id
  381. * @param {Number} uid 审批人id
  382. */
  383. async getAdvanceContent(pid, tid, vid, uid) {
  384. const noticeSql =
  385. 'SELECT * FROM (SELECT ' +
  386. ' t.`id` As `tid`, ad.`vid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  387. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  388. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  389. ' LEFT JOIN ?? As ad ON m.`id` = ad.`vid`' +
  390. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  391. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`'
  392. const noticeSqlParam = [
  393. this.ctx.service.tender.tableName,
  394. tid,
  395. this.ctx.service.advance.tableName,
  396. vid,
  397. this.tableName,
  398. this.ctx.service.projectAccount.tableName,
  399. uid,
  400. pid
  401. ]
  402. const content = await this.db.query(noticeSql, noticeSqlParam)
  403. return content.length ? JSON.stringify(content[0]) : ''
  404. }
  405. }
  406. return AdvanceAudit
  407. }