stage_audit.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2019/2/27
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. module.exports = app => {
  11. class StageAudit extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'stage_audit';
  21. }
  22. /**
  23. * 获取 审核人信息
  24. *
  25. * @param {Number} stageId - 期id
  26. * @param {Number} auditorId - 审核人id
  27. * @param {Number} times - 第几次审批
  28. * @returns {Promise<*>}
  29. */
  30. async getAuditor(stageId, auditorId, times = 1) {
  31. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  32. 'FROM ?? AS la, ?? AS pa ' +
  33. 'WHERE la.`sid` = ? and la.`aid` = ? and la.`times` = ?' +
  34. ' and la.`aid` = pa.`id`';
  35. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditorId, times];
  36. return await this.db.queryOne(sql, sqlParam);
  37. }
  38. /**
  39. * 获取 审核列表信息
  40. *
  41. * @param {Number} stageId - 期id
  42. * @param {Number} times - 第几次审批
  43. * @returns {Promise<*>}
  44. */
  45. async getAuditors(stageId, times = 1) {
  46. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  47. 'FROM ?? AS la, ?? AS pa ' +
  48. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id`';
  49. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  50. return await this.db.query(sql, sqlParam);
  51. }
  52. /**
  53. * 获取 当前审核人
  54. *
  55. * @param {Number} stageId - 期id
  56. * @param {Number} times - 第几次审批
  57. * @returns {Promise<*>}
  58. */
  59. async getCurAuditor(stageId, times = 1) {
  60. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  61. 'FROM ?? AS la, ?? AS pa ' +
  62. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
  63. ' and la.`aid` = pa.`id`';
  64. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
  65. return await this.db.queryOne(sql, sqlParam);
  66. }
  67. /**
  68. * 获取 最新审核顺序
  69. *
  70. * @param {Number} stageId - 期id
  71. * @param {Number} times - 第几次审批
  72. * @returns {Promise<number>}
  73. */
  74. async getNewOrder(stageId, times = 1) {
  75. const sql = 'SELECT Max(??) As max_order FROM ?? Where `sid` = ? and `times` = ?';
  76. const sqlParam = ['order', this.tableName, stageId, times];
  77. const result = await this.db.queryOne(sql, sqlParam);
  78. return result && result.max_order ? result.max_order + 1 : 1;
  79. }
  80. /**
  81. * 新增审核人
  82. *
  83. * @param {Number} stageId - 期id
  84. * @param {Number} auditorId - 审核人id
  85. * @param {Number} times - 第几次审批
  86. * @returns {Promise<number>}
  87. */
  88. async addAuditor(stageId, auditorId, times = 1) {
  89. const newOrder = await this.getNewOrder(stageId, times);
  90. const data = {
  91. tid: this.ctx.tender.id,
  92. sid: stageId,
  93. aid: auditorId,
  94. times: times,
  95. order: newOrder,
  96. status: auditConst.status.uncheck,
  97. };
  98. const result = await this.db.insert(this.tableName, data);
  99. return result.effectRows = 1;
  100. }
  101. /**
  102. * 移除审核人时,同步其后审核人order
  103. * @param transaction - 事务
  104. * @param {Number} stageId - 标段id
  105. * @param {Number} auditorId - 审核人id
  106. * @param {Number} times - 第几次审批
  107. * @returns {Promise<*>}
  108. * @private
  109. */
  110. async _syncOrderByDelete(transaction, stageId, order, times) {
  111. this.initSqlBuilder();
  112. this.sqlBuilder.setAndWhere('sid', {
  113. value: stageId,
  114. operate: '='
  115. });
  116. this.sqlBuilder.setAndWhere('order', {
  117. value: order,
  118. operate: '>=',
  119. });
  120. this.sqlBuilder.setAndWhere('times', {
  121. value: times,
  122. operate: '=',
  123. });
  124. this.sqlBuilder.setUpdateData('order', {
  125. value: 1,
  126. selfOperate: '-',
  127. });
  128. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  129. const data = await transaction.query(sql, sqlParam);
  130. return data;
  131. }
  132. /**
  133. * 移除审核人
  134. *
  135. * @param {Number} stageId - 期id
  136. * @param {Number} auditorId - 审核人id
  137. * @param {Number} times - 第几次审批
  138. * @returns {Promise<boolean>}
  139. */
  140. async deleteAuditor(stageId, auditorId, times = 1) {
  141. const transaction = await this.db.beginTransaction();
  142. try {
  143. const condition = {sid: stageId, aid: auditorId, times: times};
  144. const auditor = await this.getDataByCondition(condition);
  145. if (!auditor) {
  146. throw '该审核人不存在';
  147. }
  148. await this._syncOrderByDelete(transaction, stageId, auditor.order, times);
  149. await transaction.delete(this.tableName, condition);
  150. await transaction.commit();
  151. } catch(err) {
  152. await transaction.rollback();
  153. throw err;
  154. }
  155. return true;
  156. }
  157. /**
  158. * 开始审批
  159. *
  160. * @param {Number} stageId - 期id
  161. * @param {Number} times - 第几次审批
  162. * @returns {Promise<boolean>}
  163. */
  164. async start(stageId, times = 1) {
  165. const audit = await this.getDataByCondition({sid: stageId, times: times, order: 1});
  166. if (!audit) {
  167. throw '审核人信息错误';
  168. }
  169. const transaction = await this.db.beginTransaction();
  170. try {
  171. await transaction.update(this.tableName, {id: audit.id, status: auditConst.status.checking, begin_time: new Date()});
  172. // 计算原报最终数据
  173. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  174. // 复制一份下一审核人数据
  175. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  176. // 更新期数据
  177. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  178. await transaction.update(this.ctx.service.stage.tableName, {
  179. id: stageId, status: auditConst.status.checking,
  180. contract_tp: tpData.contract_tp,
  181. qc_tp: tpData.qc_tp
  182. });
  183. // todo 更新标段tender状态 ?
  184. await transaction.commit();
  185. } catch(err) {
  186. await transaction.rollback();
  187. throw err;
  188. }
  189. return true;
  190. }
  191. /**
  192. * 审批
  193. * @param {Number} stageId - 标段id
  194. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  195. * @param {Number} times - 第几次审批
  196. * @returns {Promise<void>}
  197. */
  198. async check(stageId, checkData, times = 1) {
  199. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  200. throw '提交数据错误';
  201. }
  202. const transaction = await this.db.beginTransaction();
  203. try {
  204. // 整理当前流程审核人状态更新
  205. const time = new Date();
  206. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  207. if (!audit) {
  208. throw '审核数据错误';
  209. }
  210. // 更新当前审核流程
  211. await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  212. if (checkData.checkType === auditConst.status.checked) { // 审批通过
  213. const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  214. // 无下一审核人表示,审核结束
  215. if (nextAudit) {
  216. // 计算该审批人最终数据
  217. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  218. // 复制一份下一审核人数据
  219. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  220. // 流程至下一审批人
  221. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  222. // 同步 期信息
  223. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  224. await transaction.update(this.ctx.service.stage.tableName, {
  225. id: stageId, status: auditConst.status.checking,
  226. contract_tp: tpData.contract_tp,
  227. qc_tp: tpData.qc_tp,
  228. });
  229. } else {
  230. // 本期结束
  231. // 生成截止本期数据 final数据
  232. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  233. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  234. // 计算并合同支付最终数据
  235. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  236. // 同步 期信息
  237. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  238. await transaction.update(this.ctx.service.stage.tableName, {
  239. id: stageId, status: checkData.checkType,
  240. contract_tp: tpData.contract_tp,
  241. qc_tp: tpData.qc_tp,
  242. });
  243. }
  244. } else if (checkData.checkType === auditConst.status.checkNo) { // 审批退回 原报, times+1
  245. // 同步 期信息
  246. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  247. await transaction.update(this.ctx.service.stage.tableName, {
  248. id: stageId, status: checkData.checkType,
  249. contract_tp: tpData.contract_tp,
  250. qc_tp: tpData.qc_tp,
  251. times: times + 1,
  252. });
  253. // 拷贝新一次审核流程列表
  254. // const auditors = await this.getAllDataByCondition({
  255. // where: {sid: stageId, times: times},
  256. // columns: ['tid', 'sid', 'aid', 'order']
  257. // });
  258. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  259. const sqlParam = [this.tableName, stageId, times];
  260. const auditors = await this.db.query(sql, sqlParam);
  261. for (const a of auditors) {
  262. a.times = times + 1;
  263. a.status = auditConst.status.uncheck;
  264. }
  265. await transaction.insert(this.tableName, auditors);
  266. // 计算该审批人最终数据
  267. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  268. // 复制一份最新数据给原报
  269. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  270. } else if (checkData.checkType === auditConst.status.checkNoPre) { // 审批退回 上一审批人
  271. // 同步 期信息
  272. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  273. await transaction.update(this.ctx.service.stage.tableName, {
  274. id: stageId, status: checkData.checkType,
  275. contract_tp: tpData.contract_tp,
  276. qc_tp: tpData.qc_tp,
  277. });
  278. // 将当前审批人 与 上一审批人再次添加至流程,顺移其后审批人流程顺序
  279. if (audit.order > 1) {
  280. // 顺移气候审核人流程顺序
  281. this.initSqlBuilder();
  282. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  283. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  284. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  285. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  286. const data = await transaction.query(sql, sqlParam);
  287. // 上一审批人,当前审批人 再次添加至流程
  288. const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  289. const newAuditors = [];
  290. newAuditors.push({
  291. tid: preAuditor.tid, sid: preAuditor.sid, aid: preAuditor.aid,
  292. times: preAuditor.times, order: preAuditor.order + 2, status: auditConst.status.checking,
  293. begin_time: time,
  294. });
  295. newAuditors.push({
  296. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  297. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck
  298. });
  299. await transaction.insert(this.tableName, newAuditors);
  300. // 计算该审批人最终数据
  301. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  302. // 复制一份最新数据给原报
  303. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  304. } else {
  305. throw '审核数据错误';
  306. }
  307. } else {
  308. throw '无效审批操作';
  309. }
  310. await transaction.commit();
  311. } catch (err) {
  312. await transaction.rollback();
  313. throw err;
  314. }
  315. }
  316. /**
  317. * 获取审核人需要审核的期列表
  318. *
  319. * @param auditorId
  320. * @returns {Promise<*>}
  321. */
  322. async getAuditStage(auditorId) {
  323. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  324. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  325. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  326. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  327. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  328. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id`';
  329. const sqlParam = [this.tableName, this.ctx.service.stage.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  330. return await this.db.query(sql, sqlParam);
  331. }
  332. /**
  333. * 获取 某时间后 审批进度 更新的期
  334. * @param {Number} pid - 查询标段
  335. * @param {Number} uid - 查询人
  336. * @param {Date} time - 查询时间
  337. * @returns {Promise<*>}
  338. */
  339. async getNoticeStage(pid, uid, time) {
  340. const sql = 'SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  341. ' s.`order` As `s_order`, s.`status` As `s_status`, ' +
  342. ' sa.`aid`, sa.`times`, sa.`order`, sa.`end_time`, sa.`tid`, sa.`sid`, sa.`status`, ' +
  343. ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  344. ' FROM ?? As t' +
  345. ' LEFT JOIN ?? As s On t.`id` = s.`tid`' +
  346. ' LEFT JOIN ?? As sa ON s.`id` = sa.`sid`' +
  347. ' LEFT JOIN ?? As pa ON sa.`aid` = pa.`id`' +
  348. ' WHERE sa.`aid` <> ? and sa.`end_time` > ? and t.`project_id` = ?' +
  349. ' GROUP By t.`id`' +
  350. ' ORDER By sa.`end_time`';
  351. const sqlParam = [this.ctx.service.tender.tableName, this.ctx.service.stage.tableName, this.tableName,
  352. this.ctx.service.projectAccount.tableName, uid, time, pid];
  353. return await this.db.query(sql, sqlParam);
  354. }
  355. /**
  356. * 获取审核人流程列表
  357. *
  358. * @param auditorId
  359. * @returns {Promise<*>}
  360. */
  361. async getAuditGroupByList(stageId, times) {
  362. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  363. 'FROM ?? AS la, ?? AS pa ' +
  364. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid`';
  365. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  366. return await this.db.query(sql, sqlParam);
  367. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  368. // const sqlParam = [this.tableName, stageId, times];
  369. // return await this.db.query(sql, sqlParam);
  370. }
  371. /**
  372. * 复制上一期的审批人列表给最新一期
  373. *
  374. * @param transaction - 新增一期的事务
  375. * @param {Object} preStage - 上一期
  376. * @param {Object} newStage - 最新一期
  377. * @returns {Promise<*>}
  378. */
  379. async copyPreStageAuditors(transaction, preStage, newStage) {
  380. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  381. const newAuditors = [];
  382. for (const a of auditors) {
  383. const na = {
  384. tid: preStage.tid,
  385. sid: newStage.id,
  386. aid: a.aid,
  387. times: newStage.times,
  388. order: newAuditors.length + 1,
  389. status: auditConst.status.uncheck
  390. };
  391. newAuditors.push(na);
  392. }
  393. const result = await transaction.insert(this.tableName, newAuditors);
  394. return result.effectRows = auditors.length;
  395. }
  396. /**
  397. * 移除审核人
  398. *
  399. * @param {Number} stageId - 期id
  400. * @param {Number} status - 期状态
  401. * @param {Number} status - 期次数
  402. * @return {Promise<boolean>}
  403. */
  404. async getAuditorByStatus(stageId, status, times = 1) {
  405. let auditor = null;
  406. let sql = '';
  407. let sqlParam = '';
  408. switch (status) {
  409. case auditConst.status.checking :
  410. case auditConst.status.checked :
  411. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  412. 'FROM ?? AS la, ?? AS pa ' +
  413. 'WHERE la.`sid` = ? and (la.`status` = ? or la.`status` = ?) and la.`aid` = pa.`id` order by la.`times` desc';
  414. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, auditConst.status.checked];
  415. auditor = await this.db.queryOne(sql, sqlParam);
  416. break;
  417. case auditConst.status.checkNo :
  418. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  419. 'FROM ?? AS la, ?? AS pa ' +
  420. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc';
  421. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  422. auditor = await this.db.queryOne(sql, sqlParam);
  423. break;
  424. case auditConst.status.checkNoPre :
  425. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  426. 'FROM ?? AS la, ?? AS pa ' +
  427. 'WHERE la.`sid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc';
  428. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNoPre];
  429. auditor = await this.db.queryOne(sql, sqlParam);
  430. break;
  431. case auditConst.status.uncheck :
  432. default:break;
  433. }
  434. return auditor;
  435. }
  436. }
  437. return StageAudit;
  438. };