advance_audit.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. end_time: time,
  249. });
  250. }
  251. await transaction.commit();
  252. } catch (err) {
  253. await transaction.rollback();
  254. throw err;
  255. }
  256. }
  257. async _checkNo(pid, advanceId, checkData, times) {
  258. const time = new Date();
  259. // 整理当前流程审核人状态更新
  260. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  261. if (!audit) {
  262. throw '审核数据错误';
  263. }
  264. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC';
  265. const sqlParam = [this.tableName, advanceId, times];
  266. const auditors = await this.db.query(sql, sqlParam);
  267. let order = 1;
  268. for (const a of auditors) {
  269. a.times = times + 1;
  270. a.order = order;
  271. a.status = auditConst.status.uncheck;
  272. order++;
  273. }
  274. const transaction = await this.db.beginTransaction();
  275. try {
  276. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  277. // 添加到消息推送表
  278. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id);
  279. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNo, content: noticeContent }];
  280. auditors.forEach(audit => {
  281. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNo, content: noticeContent });
  282. });
  283. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  284. // 同步期信息
  285. await transaction.update(this.ctx.service.advance.tableName, {
  286. id: advanceId,
  287. status: checkData.checkType,
  288. times: times + 1,
  289. });
  290. // 拷贝新一次审核流程列表
  291. await transaction.insert(this.tableName, auditors);
  292. await transaction.commit();
  293. } catch (err) {
  294. await transaction.rollback();
  295. throw err;
  296. }
  297. }
  298. async _checkNoPre(pid, advanceId, checkData, times) {
  299. const time = new Date();
  300. // 整理当前流程审核人状态更新
  301. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  302. if (!audit || audit.order <= 1) {
  303. throw '审核数据错误';
  304. }
  305. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  306. const auditors2 = await this.getAuditGroupByList(advanceId, times);
  307. const auditorIndex = await auditors2.findIndex(function(item) {
  308. return item.audit_id === audit.audit_id;
  309. });
  310. const preAuditor = auditors2[auditorIndex - 1];
  311. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id);
  312. const transaction = await this.db.beginTransaction();
  313. try {
  314. // 添加到消息推送表
  315. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNoPre, content: noticeContent }];
  316. auditors2.forEach(audit => {
  317. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNoPre, content: noticeContent });
  318. });
  319. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  320. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  321. // 顺移气候审核人流程顺序
  322. this.initSqlBuilder();
  323. this.sqlBuilder.setAndWhere('vid', { value: advanceId, operate: '=' });
  324. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  325. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  326. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  327. await transaction.query(sql, sqlParam);
  328. const newAuditors = [];
  329. newAuditors.push({
  330. tid: audit.tid,
  331. vid: audit.vid,
  332. audit_id: preAuditor.audit_id,
  333. times: audit.times,
  334. order: audit.order + 1,
  335. status: auditConst.status.checking,
  336. create_time: time,
  337. });
  338. newAuditors.push({
  339. tid: audit.tid,
  340. vid: audit.vid,
  341. audit_id: audit.audit_id,
  342. times: audit.times,
  343. order: audit.order + 2,
  344. status: auditConst.status.uncheck,
  345. });
  346. await transaction.insert(this.tableName, newAuditors);
  347. await transaction.commit();
  348. } catch (error) {
  349. await transaction.rollback();
  350. throw error;
  351. }
  352. }
  353. /**
  354. * 审批
  355. * @param {Number} advanceId - 预付款id
  356. * @param {auditConst.status.checked|auditConst.status.checkNo} checkData - 审批结果
  357. * @param {Number} times - 第几次审批
  358. * @return {Promise<void>}
  359. */
  360. async check(advanceId, checkData, times = 1) {
  361. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  362. throw '提交数据错误';
  363. }
  364. const pid = this.ctx.session.sessionProject.id;
  365. switch (checkData.checkType) {
  366. case auditConst.status.checked:
  367. await this._checked(pid, advanceId, checkData, times);
  368. break;
  369. case auditConst.status.checkNo:
  370. await this._checkNo(pid, advanceId, checkData, times);
  371. break;
  372. case auditConst.status.checkNoPre:
  373. await this._checkNoPre(pid, advanceId, checkData, times);
  374. break;
  375. default:
  376. throw '无效审批操作';
  377. }
  378. }
  379. /**
  380. * 用于添加推送所需的content内容
  381. * @param {Number} pid 项目id
  382. * @param {Number} tid 台账id
  383. * @param {Number} vid 预付款id
  384. * @param {Number} uid 审批人id
  385. */
  386. async getNoticeContent(pid, tid, vid, uid) {
  387. const noticeSql =
  388. 'SELECT * FROM (SELECT ' +
  389. ' t.`id` As `tid`, ad.`vid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  390. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  391. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  392. ' LEFT JOIN ?? As ad ON m.`id` = ad.`vid`' +
  393. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  394. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  395. const noticeSqlParam = [
  396. this.ctx.service.tender.tableName,
  397. tid,
  398. this.ctx.service.advance.tableName,
  399. vid,
  400. this.tableName,
  401. this.ctx.service.projectAccount.tableName,
  402. uid,
  403. pid,
  404. ];
  405. const content = await this.db.query(noticeSql, noticeSqlParam);
  406. return content.length ? JSON.stringify(content[0]) : '';
  407. }
  408. /**
  409. * 通过状态获取审核人信息
  410. * @param {Number} vid - 预付款id
  411. * @param {Number} status - 期状态
  412. * @param {Number} times - 审批次数
  413. * @return {Object} auditor 审核人信息
  414. */
  415. async getAuditorByStatus(vid, status, times = 1) {
  416. let auditor = null;
  417. let sql = '';
  418. let sqlParam = '';
  419. switch (status) {
  420. case auditConst.status.checking:
  421. case auditConst.status.checked:
  422. case auditConst.status.checkNoPre:
  423. sql =
  424. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  425. 'FROM ?? AS la, ?? AS pa ' +
  426. 'WHERE la.`vid` = ? and la.`status` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`order` desc';
  427. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, status];
  428. auditor = await this.db.queryOne(sql, sqlParam);
  429. break;
  430. case auditConst.status.checkNo:
  431. sql =
  432. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  433. 'FROM ?? AS la, ?? AS pa ' +
  434. 'WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`order` desc';
  435. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checkNo, parseInt(times) - 1];
  436. auditor = await this.db.queryOne(sql, sqlParam);
  437. break;
  438. case auditConst.status.uncheck:
  439. default:
  440. break;
  441. }
  442. return auditor;
  443. }
  444. /**
  445. * 获取所有审核人
  446. * @param {Number} tenderId 标段id
  447. */
  448. async getAllAuditors(tenderId) {
  449. const sql = 'SELECT au.audit_id, au.tid FROM ' + this.tableName + ' au' +
  450. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On au.tid = t.id' +
  451. ' WHERE t.id = ?' +
  452. ' GROUP BY au.audit_id';
  453. const sqlParam = [tenderId];
  454. return this.db.query(sql, sqlParam);
  455. }
  456. /**
  457. * 获取待处理审核列表
  458. * @param {Number} auditorId 审核人id
  459. */
  460. async getAuditAdvance(auditorId) {
  461. const sql = 'SELECT ma.`audit_id`, ma.`times`, ma.`order`, ma.`create_time`, ma.`end_time`, ma.`tid`, ma.`vid`,' +
  462. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  463. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  464. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  465. ' WHERE ((ma.`audit_id` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  466. ' and ma.`vid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`create_time` DESC';
  467. 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];
  468. return await this.db.query(sql, sqlParam);
  469. }
  470. }
  471. return AdvanceAudit;
  472. };