advance_audit.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 audit_id, 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} tid - 标段id
  62. * @param {Number} vid - 预付款id
  63. * @param {Number} audit_id - 审核人id
  64. * @param {Number} times - 第几次审批
  65. * @return {Boolean} 是否插入成功
  66. */
  67. async addAuditor(tid, vid, audit_id, times = 1) {
  68. const newOrder = await this.getNewOrder(vid, times);
  69. const record = {
  70. tid,
  71. vid,
  72. audit_id,
  73. times,
  74. order: newOrder,
  75. status: auditConst.status.uncheck,
  76. };
  77. const result = await this.db.insert(this.tableName, record);
  78. return result && result.affectedRows === 1;
  79. }
  80. /**
  81. * 移除审核人
  82. * @param {Number} vid - 预付款id
  83. * @param {Number} audit_id - 审核人id
  84. * @param {Number} times - 第几次审批
  85. * @return {Promise<boolean>}
  86. */
  87. async deleteAuditor(vid, audit_id, times = 1) {
  88. const transaction = await this.db.beginTransaction();
  89. try {
  90. const condition = { vid, audit_id, times };
  91. const auditor = await this.getDataByCondition(condition);
  92. if (!auditor) {
  93. throw '该审核人不存在';
  94. }
  95. await this._syncOrderByDelete(transaction, vid, auditor.order, times);
  96. await transaction.delete(this.tableName, condition);
  97. await transaction.commit();
  98. } catch (err) {
  99. await transaction.rollback();
  100. throw err;
  101. }
  102. return true;
  103. }
  104. /**
  105. * 移除审核人时,同步其后审核人order
  106. * @param {Function} transaction - 事务
  107. * @param {Number} vid - 预付款id
  108. * @param {Number} order - 审核顺序
  109. * @param {Number} times - 第几次审批
  110. * @return {Promise<*>} 查询结果集
  111. * @private
  112. */
  113. async _syncOrderByDelete(transaction, vid, order, times) {
  114. this.initSqlBuilder();
  115. this.sqlBuilder.setAndWhere('vid', {
  116. value: this.db.escape(vid),
  117. operate: '=',
  118. });
  119. this.sqlBuilder.setAndWhere('order', {
  120. value: order,
  121. operate: '>=',
  122. });
  123. this.sqlBuilder.setAndWhere('times', {
  124. value: times,
  125. operate: '=',
  126. });
  127. this.sqlBuilder.setUpdateData('order', {
  128. value: 1,
  129. selfOperate: '-',
  130. });
  131. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  132. const data = await transaction.query(sql, sqlParam);
  133. return data;
  134. }
  135. /**
  136. * 获取当前审核人
  137. * @param {Number} vid - 预付款id
  138. * @param {Number} times - 第几次审批
  139. * @return {Promise<Object>} 查询结果集
  140. */
  141. async getCurAuditor(vid, times = 1) {
  142. const sql =
  143. '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` ' +
  144. 'FROM ?? AS la, ?? AS pa ' +
  145. 'WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ?' +
  146. ' and la.`audit_id` = pa.`id`';
  147. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checking, times];
  148. return await this.db.queryOne(sql, sqlParam);
  149. }
  150. /**
  151. * 获取审核列表信息
  152. * @param {Number} vid - 预付款id
  153. * @param {Number} times - 第几次审批
  154. * @return {Promise<Array>} 查询结果集
  155. */
  156. async getAuditors(vid, times = 1) {
  157. const sql =
  158. '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` ' +
  159. '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 ' +
  160. 'WHERE la.`vid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` and g.`audit_id` = la.`audit_id` order by la.`order`';
  161. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, vid, times, vid, times];
  162. const result = await this.db.query(sql, sqlParam);
  163. const sql2 = 'SELECT COUNT(a.`audit_id`) as num FROM (SELECT `audit_id` FROM ?? WHERE `vid` = ? AND `times` = ? GROUP BY `audit_id`) as a';
  164. const sqlParam2 = [this.tableName, vid, times];
  165. const count = await this.db.queryOne(sql2, sqlParam2);
  166. for (const i in result) {
  167. result[i].max_sort = count.num;
  168. }
  169. return result;
  170. }
  171. /**
  172. * 获取审核人信息
  173. * @param {Number} vid - 预付款id
  174. * @param {Number} audit_id - 审核人id
  175. * @param {Number} times - 第几次审批
  176. * @return {Promise<*>} 查询结果
  177. */
  178. async getAuditor(vid, audit_id, times = 1) {
  179. const sql =
  180. '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` ' +
  181. 'FROM ?? AS la, ?? AS pa ' +
  182. 'WHERE la.`vid` = ? and la.`audit_id` = ? and la.`times` = ?' +
  183. ' and la.`audit_id` = pa.`id`';
  184. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, audit_id, times];
  185. return await this.db.queryOne(sql, sqlParam);
  186. }
  187. /**
  188. * 开始审批
  189. * @param {Number} vid - 预付款id
  190. * @param {Number} times - 第几次审批
  191. * @param {Object} data - 载荷
  192. */
  193. async start(vid, times = 1, data) {
  194. const audit = await this.getDataByCondition({ vid, times, order: 1 });
  195. if (!audit) {
  196. throw '请先选择审批人,再上报数据';
  197. }
  198. const transaction = await this.db.beginTransaction();
  199. try {
  200. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, create_time: new Date() });
  201. await transaction.update(this.ctx.service.advance.tableName, {
  202. id: audit.vid,
  203. ...data,
  204. });
  205. await transaction.commit();
  206. } catch (err) {
  207. await transaction.rollback();
  208. throw err;
  209. }
  210. return true;
  211. }
  212. async _checked(pid, advanceId, checkData, times) {
  213. const time = new Date();
  214. // 整理当前流程审核人状态更新
  215. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  216. if (!audit) {
  217. throw '审核数据错误';
  218. }
  219. // 获取审核人列表
  220. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC';
  221. const sqlParam = [this.tableName, advanceId, times];
  222. const auditors = await this.db.query(sql, sqlParam);
  223. const nextAudit = await this.getDataByCondition({ vid: advanceId, times, order: audit.order + 1 });
  224. const transaction = await this.db.beginTransaction();
  225. try {
  226. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  227. // 获取推送必要信息
  228. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id);
  229. // 添加推送
  230. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checked, content: noticeContent }];
  231. auditors.forEach(audit => {
  232. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checked, content: noticeContent });
  233. });
  234. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  235. // 无下一审核人表示,审核结束
  236. if (nextAudit) {
  237. // 流程至下一审批人
  238. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, create_time: time });
  239. // 同步期信息
  240. await transaction.update(this.ctx.service.advance.tableName, {
  241. id: advanceId,
  242. status: auditConst.status.checking,
  243. });
  244. } else {
  245. await transaction.update(this.ctx.service.advance.tableName, {
  246. id: advanceId,
  247. status: checkData.checkType,
  248. });
  249. }
  250. await transaction.commit();
  251. } catch (err) {
  252. await transaction.rollback();
  253. throw err;
  254. }
  255. }
  256. async _checkNo(pid, advanceId, checkData, times) {
  257. const time = new Date();
  258. // 整理当前流程审核人状态更新
  259. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  260. if (!audit) {
  261. throw '审核数据错误';
  262. }
  263. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC';
  264. const sqlParam = [this.tableName, advanceId, times];
  265. const auditors = await this.db.query(sql, sqlParam);
  266. let order = 1;
  267. for (const a of auditors) {
  268. a.times = times + 1;
  269. a.order = order;
  270. a.status = auditConst.status.uncheck;
  271. order++;
  272. }
  273. const transaction = await this.db.beginTransaction();
  274. try {
  275. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  276. // 添加到消息推送表
  277. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id);
  278. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNo, content: noticeContent }];
  279. auditors.forEach(audit => {
  280. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNo, content: noticeContent });
  281. });
  282. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  283. // 同步期信息
  284. await transaction.update(this.ctx.service.advance.tableName, {
  285. id: advanceId,
  286. status: checkData.checkType,
  287. times: times + 1,
  288. });
  289. // 拷贝新一次审核流程列表
  290. await transaction.insert(this.tableName, auditors);
  291. await transaction.commit();
  292. } catch (err) {
  293. await transaction.rollback();
  294. throw err;
  295. }
  296. }
  297. async _checkNoPre(pid, advanceId, checkData, times) {
  298. const time = new Date();
  299. // 整理当前流程审核人状态更新
  300. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  301. if (!audit || audit.order <= 1) {
  302. throw '审核数据错误';
  303. }
  304. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  305. const auditors2 = await this.getAuditGroupByList(advanceId, times);
  306. const auditorIndex = await auditors2.findIndex(function(item) {
  307. return item.audit_id === audit.audit_id;
  308. });
  309. const preAuditor = auditors2[auditorIndex - 1];
  310. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id);
  311. const transaction = await this.db.beginTransaction();
  312. try {
  313. // 添加到消息推送表
  314. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNoPre, content: noticeContent }];
  315. auditors2.forEach(audit => {
  316. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNoPre, content: noticeContent });
  317. });
  318. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  319. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  320. // 顺移气候审核人流程顺序
  321. this.initSqlBuilder();
  322. this.sqlBuilder.setAndWhere('vid', { value: advanceId, operate: '=' });
  323. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  324. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  325. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  326. await transaction.query(sql, sqlParam);
  327. const newAuditors = [];
  328. newAuditors.push({
  329. tid: audit.tid,
  330. vid: audit.vid,
  331. audit_id: preAuditor.audit_id,
  332. times: audit.times,
  333. order: audit.order + 1,
  334. status: auditConst.status.checking,
  335. create_time: time,
  336. });
  337. newAuditors.push({
  338. tid: audit.tid,
  339. vid: audit.vid,
  340. audit_id: audit.audit_id,
  341. times: audit.times,
  342. order: audit.order + 2,
  343. status: auditConst.status.uncheck,
  344. });
  345. await transaction.insert(this.tableName, newAuditors);
  346. await transaction.commit();
  347. } catch (error) {
  348. await transaction.rollback();
  349. throw error;
  350. }
  351. }
  352. /**
  353. * 审批
  354. * @param {Number} advanceId - 预付款id
  355. * @param {auditConst.status.checked|auditConst.status.checkNo} checkData - 审批结果
  356. * @param {Number} times - 第几次审批
  357. * @return {Promise<void>}
  358. */
  359. async check(advanceId, checkData, times = 1) {
  360. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  361. throw '提交数据错误';
  362. }
  363. const pid = this.ctx.session.sessionProject.id;
  364. switch (checkData.checkType) {
  365. case auditConst.status.checked:
  366. await this._checked(pid, advanceId, checkData, times);
  367. break;
  368. case auditConst.status.checkNo:
  369. await this._checkNo(pid, advanceId, checkData, times);
  370. break;
  371. case auditConst.status.checkNoPre:
  372. await this._checkNoPre(pid, advanceId, checkData, times);
  373. break;
  374. default:
  375. throw '无效审批操作';
  376. }
  377. }
  378. /**
  379. * 用于添加推送所需的content内容
  380. * @param {Number} pid 项目id
  381. * @param {Number} tid 台账id
  382. * @param {Number} vid 预付款id
  383. * @param {Number} uid 审批人id
  384. */
  385. async getNoticeContent(pid, tid, vid, uid) {
  386. const noticeSql =
  387. 'SELECT * FROM (SELECT ' +
  388. ' t.`id` As `tid`, ad.`vid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  389. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  390. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  391. ' LEFT JOIN ?? As ad ON m.`id` = ad.`vid`' +
  392. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  393. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  394. const noticeSqlParam = [
  395. this.ctx.service.tender.tableName,
  396. tid,
  397. this.ctx.service.advance.tableName,
  398. vid,
  399. this.tableName,
  400. this.ctx.service.projectAccount.tableName,
  401. uid,
  402. pid,
  403. ];
  404. const content = await this.db.query(noticeSql, noticeSqlParam);
  405. return content.length ? JSON.stringify(content[0]) : '';
  406. }
  407. /**
  408. * 通过状态获取审核人信息
  409. * @param {Number} vid - 预付款id
  410. * @param {Number} status - 期状态
  411. * @param {Number} times - 审批次数
  412. * @return {Object} auditor 审核人信息
  413. */
  414. async getAuditorByStatus(vid, status, times = 1) {
  415. let auditor = null;
  416. let sql = '';
  417. let sqlParam = '';
  418. switch (status) {
  419. case auditConst.status.checking:
  420. case auditConst.status.checked:
  421. case auditConst.status.checkNoPre:
  422. sql =
  423. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  424. 'FROM ?? AS la, ?? AS pa ' +
  425. 'WHERE la.`vid` = ? and la.`status` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`order` desc';
  426. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, status];
  427. auditor = await this.db.queryOne(sql, sqlParam);
  428. break;
  429. case auditConst.status.checkNo:
  430. sql =
  431. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  432. 'FROM ?? AS la, ?? AS pa ' +
  433. 'WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`order` desc';
  434. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checkNo, parseInt(times) - 1];
  435. auditor = await this.db.queryOne(sql, sqlParam);
  436. break;
  437. case auditConst.status.uncheck:
  438. default:
  439. break;
  440. }
  441. return auditor;
  442. }
  443. /**
  444. * 获取所有审核人
  445. * @param {Number} tenderId 标段id
  446. */
  447. async getAllAuditors(tenderId) {
  448. const sql = 'SELECT au.audit_id, au.tid FROM ' + this.tableName + ' au' +
  449. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On au.tid = t.id' +
  450. ' WHERE t.id = ?' +
  451. ' GROUP BY au.audit_id';
  452. const sqlParam = [tenderId];
  453. return this.db.query(sql, sqlParam);
  454. }
  455. }
  456. return AdvanceAudit;
  457. };