advance_audit.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. 'use strict';
  2. const auditConst = require('../const/audit').advance;
  3. const shenpiConst = require('../const/shenpi');
  4. const pushType = require('../const/audit').pushType;
  5. module.exports = app => {
  6. class AdvanceAudit extends app.BaseService {
  7. constructor(ctx) {
  8. super(ctx);
  9. this.tableName = 'advance_audit';
  10. }
  11. /**
  12. * 获取审核人流程列表
  13. * @param {Number} vid 预付款记录id
  14. * @param {Number} times 审核次数
  15. * @return {Promise<Array>} 查询结果集
  16. */
  17. async getAuditGroupByList(vid, times = 1) {
  18. const sql =
  19. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  20. 'FROM ?? AS la, ?? AS pa ' +
  21. 'WHERE la.`vid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` GROUP BY la.`audit_id` ORDER BY la.`order`';
  22. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, times];
  23. return await this.db.query(sql, sqlParam);
  24. }
  25. /**
  26. * 获取审核人流程列表(包括原报)
  27. * @param {Number} vid 预付款记录id
  28. * @param {Number} times 审核次数
  29. * @return {Promise<Array>} 查询结果集(包括原报)
  30. */
  31. async getAuditorsWithOwner(vid, times = 1) {
  32. const result = await this.getAuditGroupByList(vid, times);
  33. const sql =
  34. 'SELECT pa.`id` As audit_id, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As vid, 0 As `order`' +
  35. ' FROM ' +
  36. this.ctx.service.advance.tableName +
  37. ' As s' +
  38. ' LEFT JOIN ' +
  39. this.ctx.service.projectAccount.tableName +
  40. ' As pa' +
  41. ' ON s.uid = pa.id' +
  42. ' WHERE s.id = ?';
  43. const sqlParam = [times, vid, vid];
  44. const user = await this.db.queryOne(sql, sqlParam);
  45. result.unshift(user);
  46. return result;
  47. }
  48. /**
  49. * 获取最新审核顺序
  50. * @param {Number} vid - 预付款id
  51. * @param {Number} times - 第几次审批
  52. * @return {Number} 审核顺序
  53. */
  54. async getNewOrder(vid, times = 1) {
  55. const sql = 'SELECT Max(??) As max_order FROM ?? Where `vid` = ? and `times` = ?';
  56. const sqlParam = ['order', this.tableName, vid, times];
  57. const result = await this.db.queryOne(sql, sqlParam);
  58. return result && result.max_order ? result.max_order + 1 : 1;
  59. }
  60. /**
  61. * 新增审核人
  62. * @param {Number} tid - 标段id
  63. * @param {Number} vid - 预付款id
  64. * @param {Number} audit_id - 审核人id
  65. * @param {Number} times - 第几次审批
  66. * @return {Boolean} 是否插入成功
  67. */
  68. async addAuditor(tid, vid, audit_id, times = 1, is_gdzs = 0) {
  69. const transaction = await this.db.beginTransaction();
  70. try {
  71. let newOrder = await this.getNewOrder(vid, times);
  72. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  73. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  74. if (is_gdzs) await this._syncOrderByDelete(transaction, vid, newOrder, times, '+');
  75. const record = {
  76. tid,
  77. vid,
  78. audit_id,
  79. times,
  80. order: newOrder,
  81. status: auditConst.status.uncheck,
  82. };
  83. const result = await transaction.insert(this.tableName, record);
  84. await transaction.commit();
  85. return result && result.affectedRows === 1;
  86. } catch (err) {
  87. await transaction.rollback();
  88. throw err;
  89. }
  90. return false;
  91. }
  92. /**
  93. * 移除审核人
  94. * @param {Number} vid - 预付款id
  95. * @param {Number} audit_id - 审核人id
  96. * @param {Number} times - 第几次审批
  97. * @return {Promise<boolean>}
  98. */
  99. async deleteAuditor(vid, audit_id, times = 1) {
  100. const transaction = await this.db.beginTransaction();
  101. try {
  102. const condition = { vid, audit_id, times };
  103. const auditor = await this.getDataByCondition(condition);
  104. if (!auditor) {
  105. throw '该审核人不存在';
  106. }
  107. await this._syncOrderByDelete(transaction, vid, auditor.order, times);
  108. await transaction.delete(this.tableName, condition);
  109. await transaction.commit();
  110. } catch (err) {
  111. await transaction.rollback();
  112. throw err;
  113. }
  114. return true;
  115. }
  116. /**
  117. * 移除审核人时,同步其后审核人order
  118. * @param {Function} transaction - 事务
  119. * @param {Number} vid - 预付款id
  120. * @param {Number} order - 审核顺序
  121. * @param {Number} times - 第几次审批
  122. * @return {Promise<*>} 查询结果集
  123. * @private
  124. */
  125. async _syncOrderByDelete(transaction, vid, order, times, selfOperate = '-') {
  126. this.initSqlBuilder();
  127. this.sqlBuilder.setAndWhere('vid', {
  128. value: this.db.escape(vid),
  129. operate: '=',
  130. });
  131. this.sqlBuilder.setAndWhere('order', {
  132. value: order,
  133. operate: '>=',
  134. });
  135. this.sqlBuilder.setAndWhere('times', {
  136. value: times,
  137. operate: '=',
  138. });
  139. this.sqlBuilder.setUpdateData('order', {
  140. value: 1,
  141. selfOperate: selfOperate,
  142. });
  143. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  144. const data = await transaction.query(sql, sqlParam);
  145. return data;
  146. }
  147. /**
  148. * 获取当前审核人
  149. * @param {Number} vid - 预付款id
  150. * @param {Number} times - 第几次审批
  151. * @return {Promise<Object>} 查询结果集
  152. */
  153. async getCurAuditor(vid, times = 1) {
  154. const sql =
  155. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`create_time`, la.`end_time` ' +
  156. 'FROM ?? AS la, ?? AS pa ' +
  157. 'WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ?' +
  158. ' and la.`audit_id` = pa.`id`';
  159. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checking, times];
  160. return await this.db.queryOne(sql, sqlParam);
  161. }
  162. /**
  163. * 获取审核列表信息
  164. * @param {Number} vid - 预付款id
  165. * @param {Number} times - 第几次审批
  166. * @return {Promise<Array>} 查询结果集
  167. */
  168. async getAuditors(vid, times = 1) {
  169. const sql =
  170. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`create_time`, la.`end_time`, g.`sort` ' +
  171. '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 ' +
  172. 'WHERE la.`vid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` and g.`audit_id` = la.`audit_id` order by la.`order`';
  173. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, vid, times, vid, times];
  174. const result = await this.db.query(sql, sqlParam);
  175. const sql2 = 'SELECT COUNT(a.`audit_id`) as num FROM (SELECT `audit_id` FROM ?? WHERE `vid` = ? AND `times` = ? GROUP BY `audit_id`) as a';
  176. const sqlParam2 = [this.tableName, vid, times];
  177. const count = await this.db.queryOne(sql2, sqlParam2);
  178. for (const i in result) {
  179. result[i].max_sort = count.num;
  180. }
  181. return result;
  182. }
  183. /**
  184. * 获取审核人信息
  185. * @param {Number} vid - 预付款id
  186. * @param {Number} audit_id - 审核人id
  187. * @param {Number} times - 第几次审批
  188. * @return {Promise<*>} 查询结果
  189. */
  190. async getAuditor(vid, audit_id, times = 1) {
  191. const sql =
  192. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`create_time`, la.`end_time` ' +
  193. 'FROM ?? AS la, ?? AS pa ' +
  194. 'WHERE la.`vid` = ? and la.`audit_id` = ? and la.`times` = ?' +
  195. ' and la.`audit_id` = pa.`id`';
  196. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, audit_id, times];
  197. return await this.db.queryOne(sql, sqlParam);
  198. }
  199. /**
  200. * 开始审批
  201. * @param {Number} vid - 预付款id
  202. * @param {Number} times - 第几次审批
  203. * @param {Object} data - 载荷
  204. */
  205. async start(vid, times = 1, data) {
  206. const audit = await this.getDataByCondition({ vid, times, order: 1 });
  207. if (!audit) {
  208. if(this.ctx.tender.info.shenpi.advance === shenpiConst.sp_status.gdspl) {
  209. throw '请联系管理员添加审批人';
  210. } else {
  211. throw '请先选择审批人,再上报数据';
  212. }
  213. }
  214. const transaction = await this.db.beginTransaction();
  215. try {
  216. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, create_time: new Date() });
  217. await transaction.update(this.ctx.service.advance.tableName, {
  218. id: audit.vid,
  219. ...data,
  220. });
  221. await transaction.commit();
  222. } catch (err) {
  223. await transaction.rollback();
  224. throw err;
  225. }
  226. return true;
  227. }
  228. async _checked(pid, advanceId, checkData, times) {
  229. const time = new Date();
  230. // 整理当前流程审核人状态更新
  231. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  232. if (!audit) {
  233. throw '审核数据错误';
  234. }
  235. // 获取审核人列表
  236. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC';
  237. const sqlParam = [this.tableName, advanceId, times];
  238. const auditors = await this.db.query(sql, sqlParam);
  239. const nextAudit = await this.getDataByCondition({ vid: advanceId, times, order: audit.order + 1 });
  240. const transaction = await this.db.beginTransaction();
  241. try {
  242. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  243. // 获取推送必要信息
  244. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id);
  245. // 添加推送
  246. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checked, content: noticeContent }];
  247. auditors.forEach(audit => {
  248. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checked, content: noticeContent });
  249. });
  250. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  251. // 无下一审核人表示,审核结束
  252. if (nextAudit) {
  253. // 流程至下一审批人
  254. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, create_time: time });
  255. // 同步期信息
  256. await transaction.update(this.ctx.service.advance.tableName, {
  257. id: advanceId,
  258. status: auditConst.status.checking,
  259. });
  260. } else {
  261. await transaction.update(this.ctx.service.advance.tableName, {
  262. id: advanceId,
  263. status: checkData.checkType,
  264. end_time: time,
  265. });
  266. }
  267. await transaction.commit();
  268. } catch (err) {
  269. await transaction.rollback();
  270. throw err;
  271. }
  272. }
  273. async _checkNo(pid, advanceId, checkData, times) {
  274. const time = new Date();
  275. // 整理当前流程审核人状态更新
  276. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  277. if (!audit) {
  278. throw '审核数据错误';
  279. }
  280. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC';
  281. const sqlParam = [this.tableName, advanceId, times];
  282. const auditors = await this.db.query(sql, sqlParam);
  283. let order = 1;
  284. for (const a of auditors) {
  285. a.times = times + 1;
  286. a.order = order;
  287. a.status = auditConst.status.uncheck;
  288. order++;
  289. }
  290. const transaction = await this.db.beginTransaction();
  291. try {
  292. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  293. // 添加到消息推送表
  294. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id);
  295. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNo, content: noticeContent }];
  296. auditors.forEach(audit => {
  297. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNo, content: noticeContent });
  298. });
  299. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  300. // 同步期信息
  301. await transaction.update(this.ctx.service.advance.tableName, {
  302. id: advanceId,
  303. status: checkData.checkType,
  304. times: times + 1,
  305. });
  306. // 拷贝新一次审核流程列表
  307. await transaction.insert(this.tableName, auditors);
  308. await transaction.commit();
  309. } catch (err) {
  310. await transaction.rollback();
  311. throw err;
  312. }
  313. }
  314. async _checkNoPre(pid, advanceId, checkData, times) {
  315. const time = new Date();
  316. // 整理当前流程审核人状态更新
  317. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  318. if (!audit || audit.order <= 1) {
  319. throw '审核数据错误';
  320. }
  321. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  322. const auditors2 = await this.getAuditGroupByList(advanceId, times);
  323. const auditorIndex = await auditors2.findIndex(function(item) {
  324. return item.audit_id === audit.audit_id;
  325. });
  326. const preAuditor = auditors2[auditorIndex - 1];
  327. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id);
  328. const transaction = await this.db.beginTransaction();
  329. try {
  330. // 添加到消息推送表
  331. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNoPre, content: noticeContent }];
  332. auditors2.forEach(audit => {
  333. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNoPre, content: noticeContent });
  334. });
  335. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  336. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  337. // 顺移气候审核人流程顺序
  338. this.initSqlBuilder();
  339. this.sqlBuilder.setAndWhere('vid', { value: advanceId, operate: '=' });
  340. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  341. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  342. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  343. await transaction.query(sql, sqlParam);
  344. const newAuditors = [];
  345. newAuditors.push({
  346. tid: audit.tid,
  347. vid: audit.vid,
  348. audit_id: preAuditor.audit_id,
  349. times: audit.times,
  350. order: audit.order + 1,
  351. status: auditConst.status.checking,
  352. create_time: time,
  353. });
  354. newAuditors.push({
  355. tid: audit.tid,
  356. vid: audit.vid,
  357. audit_id: audit.audit_id,
  358. times: audit.times,
  359. order: audit.order + 2,
  360. status: auditConst.status.uncheck,
  361. });
  362. await transaction.insert(this.tableName, newAuditors);
  363. await transaction.commit();
  364. } catch (error) {
  365. await transaction.rollback();
  366. throw error;
  367. }
  368. }
  369. /**
  370. * 审批
  371. * @param {Number} advanceId - 预付款id
  372. * @param {auditConst.status.checked|auditConst.status.checkNo} checkData - 审批结果
  373. * @param {Number} times - 第几次审批
  374. * @return {Promise<void>}
  375. */
  376. async check(advanceId, checkData, times = 1) {
  377. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  378. throw '提交数据错误';
  379. }
  380. const pid = this.ctx.session.sessionProject.id;
  381. switch (checkData.checkType) {
  382. case auditConst.status.checked:
  383. await this._checked(pid, advanceId, checkData, times);
  384. break;
  385. case auditConst.status.checkNo:
  386. await this._checkNo(pid, advanceId, checkData, times);
  387. break;
  388. case auditConst.status.checkNoPre:
  389. await this._checkNoPre(pid, advanceId, checkData, times);
  390. break;
  391. default:
  392. throw '无效审批操作';
  393. }
  394. }
  395. /**
  396. * 用于添加推送所需的content内容
  397. * @param {Number} pid 项目id
  398. * @param {Number} tid 台账id
  399. * @param {Number} vid 预付款id
  400. * @param {Number} uid 审批人id
  401. */
  402. async getNoticeContent(pid, tid, vid, uid) {
  403. const noticeSql =
  404. 'SELECT * FROM (SELECT ' +
  405. ' t.`id` As `tid`, ad.`vid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  406. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  407. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  408. ' LEFT JOIN ?? As ad ON m.`id` = ad.`vid`' +
  409. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  410. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  411. const noticeSqlParam = [
  412. this.ctx.service.tender.tableName,
  413. tid,
  414. this.ctx.service.advance.tableName,
  415. vid,
  416. this.tableName,
  417. this.ctx.service.projectAccount.tableName,
  418. uid,
  419. pid,
  420. ];
  421. const content = await this.db.query(noticeSql, noticeSqlParam);
  422. return content.length ? JSON.stringify(content[0]) : '';
  423. }
  424. /**
  425. * 通过状态获取审核人信息
  426. * @param {Number} vid - 预付款id
  427. * @param {Number} status - 期状态
  428. * @param {Number} times - 审批次数
  429. * @return {Object} auditor 审核人信息
  430. */
  431. async getAuditorByStatus(vid, status, times = 1) {
  432. let auditor = null;
  433. let sql = '';
  434. let sqlParam = '';
  435. switch (status) {
  436. case auditConst.status.checking:
  437. case auditConst.status.checked:
  438. case auditConst.status.checkNoPre:
  439. sql =
  440. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  441. 'FROM ?? AS la, ?? AS pa ' +
  442. 'WHERE la.`vid` = ? and la.`status` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`order` desc';
  443. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, status];
  444. auditor = await this.db.queryOne(sql, sqlParam);
  445. break;
  446. case auditConst.status.checkNo:
  447. sql =
  448. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  449. 'FROM ?? AS la, ?? AS pa ' +
  450. 'WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`order` desc';
  451. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checkNo, parseInt(times) - 1];
  452. auditor = await this.db.queryOne(sql, sqlParam);
  453. break;
  454. case auditConst.status.uncheck:
  455. default:
  456. break;
  457. }
  458. return auditor;
  459. }
  460. /**
  461. * 获取所有审核人
  462. * @param {Number} tenderId 标段id
  463. */
  464. async getAllAuditors(tenderId) {
  465. const sql = 'SELECT au.audit_id, au.tid FROM ' + this.tableName + ' au' +
  466. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On au.tid = t.id' +
  467. ' WHERE t.id = ?' +
  468. ' GROUP BY au.audit_id';
  469. const sqlParam = [tenderId];
  470. return this.db.query(sql, sqlParam);
  471. }
  472. /**
  473. * 获取待处理审核列表
  474. * @param {Number} auditorId 审核人id
  475. */
  476. async getAuditAdvance(auditorId) {
  477. const sql = 'SELECT ma.`audit_id`, ma.`times`, ma.`order`, ma.`create_time`, ma.`end_time`, ma.`tid`, ma.`vid`,' +
  478. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  479. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  480. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  481. ' WHERE ((ma.`audit_id` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  482. ' and ma.`vid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`create_time` DESC';
  483. const sqlParam = [this.tableName, this.ctx.service.advance.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  484. return await this.db.query(sql, sqlParam);
  485. }
  486. async updateNewAuditList(advance, newIdList) {
  487. const transaction = await this.db.beginTransaction();
  488. try {
  489. // 先删除旧的审批流,再添加新的
  490. await transaction.delete(this.tableName, { vid: advance.id, times: advance.times });
  491. const newAuditors = [];
  492. let order = 1;
  493. for (const aid of newIdList) {
  494. newAuditors.push({
  495. tid: advance.tid, vid: advance.id, audit_id: aid,
  496. times: advance.times, order, status: auditConst.status.uncheck,
  497. });
  498. order++;
  499. }
  500. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  501. await transaction.commit();
  502. } catch (err) {
  503. await transaction.rollback();
  504. throw err;
  505. }
  506. }
  507. async updateLastAudit(advance, auditList, lastId) {
  508. const transaction = await this.db.beginTransaction();
  509. try {
  510. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  511. const idList = this._.map(auditList, 'audit_id');
  512. let order = idList.length + 1;
  513. if (idList.indexOf(lastId) !== -1) {
  514. await transaction.delete(this.tableName, { vid: advance.id, times: advance.times, audit_id: lastId });
  515. const audit = this._.find(auditList, { 'audit_id': lastId });
  516. // 顺移之后审核人流程顺序
  517. await this._syncOrderByDelete(transaction, advance.id, audit.order, advance.times);
  518. order = order - 1;
  519. }
  520. // 添加终审
  521. const newAuditor = {
  522. tid: advance.tid, vid: advance.id, audit_id: lastId,
  523. times: advance.times, order, status: auditConst.status.uncheck,
  524. };
  525. await transaction.insert(this.tableName, newAuditor);
  526. await transaction.commit();
  527. } catch (err) {
  528. await transaction.rollback();
  529. throw err;
  530. }
  531. }
  532. }
  533. return AdvanceAudit;
  534. };