stage_audit.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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` order by la.`order`';
  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. async _checked(stageId, checkData, times) {
  192. const time = new Date();
  193. // 整理当前流程审核人状态更新
  194. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  195. if (!audit) {
  196. throw '审核数据错误';
  197. }
  198. const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  199. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  200. const transaction = await this.db.beginTransaction();
  201. try {
  202. await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  203. // 计算并合同支付最终数据
  204. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  205. // 无下一审核人表示,审核结束
  206. if (nextAudit) {
  207. // 复制一份下一审核人数据
  208. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  209. // 流程至下一审批人
  210. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  211. // 同步 期信息
  212. await transaction.update(this.ctx.service.stage.tableName, {
  213. id: stageId, status: auditConst.status.checking,
  214. contract_tp: tpData.contract_tp,
  215. qc_tp: tpData.qc_tp,
  216. });
  217. } else {
  218. // 本期结束
  219. // 生成截止本期数据 final数据
  220. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  221. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  222. // 同步 期信息
  223. await transaction.update(this.ctx.service.stage.tableName, {
  224. id: stageId, status: checkData.checkType,
  225. contract_tp: tpData.contract_tp,
  226. qc_tp: tpData.qc_tp,
  227. });
  228. }
  229. await transaction.commit();
  230. } catch (err) {
  231. await transaction.rollback();
  232. throw err;
  233. }
  234. }
  235. async _checkNo(stageId, checkData, times) {
  236. const time = new Date();
  237. // 整理当前流程审核人状态更新
  238. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  239. if (!audit) {
  240. throw '审核数据错误';
  241. }
  242. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  243. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  244. const sqlParam = [this.tableName, stageId, times];
  245. const auditors = await this.db.query(sql, sqlParam);
  246. let order = 1;
  247. for (const a of auditors) {
  248. a.times = times + 1;
  249. a.order = order;
  250. a.status = auditConst.status.uncheck;
  251. order++;
  252. }
  253. const transaction = await this.db.beginTransaction();
  254. try {
  255. await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  256. // 同步 期信息
  257. await transaction.update(this.ctx.service.stage.tableName, {
  258. id: stageId, status: checkData.checkType,
  259. contract_tp: tpData.contract_tp,
  260. qc_tp: tpData.qc_tp,
  261. times: times + 1,
  262. });
  263. // 拷贝新一次审核流程列表
  264. await transaction.insert(this.tableName, auditors);
  265. // 计算该审批人最终数据
  266. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  267. // 复制一份最新数据给原报
  268. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  269. await transaction.commit();
  270. } catch (err) {
  271. await transaction.rollback();
  272. throw err;
  273. }
  274. }
  275. async _checkNoPre(stageId, checkData, times) {
  276. const time = new Date();
  277. // 整理当前流程审核人状态更新
  278. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  279. if (!audit || audit.order <= 1) {
  280. throw '审核数据错误';
  281. }
  282. const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  283. const transaction = await this.db.beginTransaction();
  284. try {
  285. await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  286. // 顺移气候审核人流程顺序
  287. this.initSqlBuilder();
  288. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  289. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  290. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  291. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  292. const data = await transaction.query(sql, sqlParam);
  293. // 上一审批人,当前审批人 再次添加至流程
  294. const newAuditors = [];
  295. newAuditors.push({
  296. tid: preAuditor.tid, sid: preAuditor.sid, aid: preAuditor.aid,
  297. times: preAuditor.times, order: preAuditor.order + 2, status: auditConst.status.checking,
  298. begin_time: time,
  299. });
  300. newAuditors.push({
  301. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  302. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck
  303. });
  304. await transaction.insert(this.tableName, newAuditors);
  305. // 计算该审批人最终数据
  306. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  307. // 复制一份最新数据给下一人
  308. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  309. await transaction.commit();
  310. } catch(err) {
  311. await transaction.rollback();
  312. throw err;
  313. }
  314. }
  315. /**
  316. * 审批
  317. * @param {Number} stageId - 标段id
  318. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  319. * @param {Number} times - 第几次审批
  320. * @returns {Promise<void>}
  321. */
  322. async check(stageId, checkData, times = 1) {
  323. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  324. throw '提交数据错误';
  325. }
  326. // // 整理当前流程审核人状态更新
  327. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  328. // if (!audit) {
  329. // throw '审核数据错误';
  330. // }
  331. //const time = new Date();
  332. switch (checkData.checkType) {
  333. case auditConst.status.checked:
  334. await this._checked(stageId, checkData, times);
  335. break;
  336. case auditConst.status.checkNo:
  337. await this._checkNo(stageId, checkData, times);
  338. break;
  339. case auditConst.status.checkNoPre:
  340. await this._checkNoPre(stageId, checkData, times);
  341. break;
  342. default:
  343. throw '无效审批操作';
  344. }
  345. // const transaction = await this.db.beginTransaction();
  346. // try {
  347. // // 更新当前审核流程
  348. // await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  349. // if (checkData.checkType === auditConst.status.checked) { // 审批通过
  350. // const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  351. // // 无下一审核人表示,审核结束
  352. // if (nextAudit) {
  353. // // 计算该审批人最终数据
  354. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  355. // // 复制一份下一审核人数据
  356. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  357. // // 流程至下一审批人
  358. // await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  359. // // 同步 期信息
  360. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  361. // await transaction.update(this.ctx.service.stage.tableName, {
  362. // id: stageId, status: auditConst.status.checking,
  363. // contract_tp: tpData.contract_tp,
  364. // qc_tp: tpData.qc_tp,
  365. // });
  366. // } else {
  367. // // 本期结束
  368. // // 生成截止本期数据 final数据
  369. // await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  370. // await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  371. // // 计算并合同支付最终数据
  372. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  373. // // 同步 期信息
  374. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  375. // await transaction.update(this.ctx.service.stage.tableName, {
  376. // id: stageId, status: checkData.checkType,
  377. // contract_tp: tpData.contract_tp,
  378. // qc_tp: tpData.qc_tp,
  379. // });
  380. // }
  381. // } else if (checkData.checkType === auditConst.status.checkNo) { // 审批退回 原报, times+1
  382. // // 同步 期信息
  383. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  384. // await transaction.update(this.ctx.service.stage.tableName, {
  385. // id: stageId, status: checkData.checkType,
  386. // contract_tp: tpData.contract_tp,
  387. // qc_tp: tpData.qc_tp,
  388. // times: times + 1,
  389. // });
  390. // // 拷贝新一次审核流程列表
  391. // // const auditors = await this.getAllDataByCondition({
  392. // // where: {sid: stageId, times: times},
  393. // // columns: ['tid', 'sid', 'aid', 'order']
  394. // // });
  395. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  396. // const sqlParam = [this.tableName, stageId, times];
  397. // const auditors = await this.db.query(sql, sqlParam);
  398. // let order = 1;
  399. // for (const a of auditors) {
  400. // a.times = times + 1;
  401. // a.order = order;
  402. // a.status = auditConst.status.uncheck;
  403. // order++;
  404. // }
  405. // await transaction.insert(this.tableName, auditors);
  406. // // 计算该审批人最终数据
  407. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  408. // // 复制一份最新数据给原报
  409. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  410. // } else if (checkData.checkType === auditConst.status.checkNoPre) { // 审批退回 上一审批人
  411. // // 同步 期信息
  412. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  413. // await transaction.update(this.ctx.service.stage.tableName, {
  414. // id: stageId, status: checkData.checkType,
  415. // contract_tp: tpData.contract_tp,
  416. // qc_tp: tpData.qc_tp,
  417. // });
  418. // // 将当前审批人 与 上一审批人再次添加至流程,顺移其后审批人流程顺序
  419. // if (audit.order > 1) {
  420. // // 顺移气候审核人流程顺序
  421. // this.initSqlBuilder();
  422. // this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  423. // this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  424. // this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  425. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  426. // const data = await transaction.query(sql, sqlParam);
  427. //
  428. // // 上一审批人,当前审批人 再次添加至流程
  429. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  430. // const newAuditors = [];
  431. // newAuditors.push({
  432. // tid: preAuditor.tid, sid: preAuditor.sid, aid: preAuditor.aid,
  433. // times: preAuditor.times, order: preAuditor.order + 2, status: auditConst.status.checking,
  434. // begin_time: time,
  435. // });
  436. // newAuditors.push({
  437. // tid: audit.tid, sid: audit.sid, aid: audit.aid,
  438. // times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck
  439. // });
  440. // await transaction.insert(this.tableName, newAuditors);
  441. //
  442. // // 计算该审批人最终数据
  443. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  444. // // 复制一份最新数据给上一人
  445. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  446. // } else {
  447. // throw '审核数据错误';
  448. // }
  449. // } else {
  450. // throw '无效审批操作';
  451. // }
  452. //
  453. // await transaction.commit();
  454. // } catch (err) {
  455. // await transaction.rollback();
  456. // throw err;
  457. // }
  458. }
  459. /**
  460. * 获取审核人需要审核的期列表
  461. *
  462. * @param auditorId
  463. * @returns {Promise<*>}
  464. */
  465. async getAuditStage(auditorId) {
  466. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  467. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  468. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  469. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  470. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  471. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id`';
  472. 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];
  473. return await this.db.query(sql, sqlParam);
  474. }
  475. /**
  476. * 获取 某时间后 审批进度 更新的期
  477. * @param {Number} pid - 查询标段
  478. * @param {Number} uid - 查询人
  479. * @param {Date} time - 查询时间
  480. * @returns {Promise<*>}
  481. */
  482. async getNoticeStage(pid, uid, time) {
  483. const sql = 'SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  484. ' s.`order` As `s_order`, s.`status` As `s_status`, ' +
  485. ' sa.`aid`, sa.`times`, sa.`order`, sa.`end_time`, sa.`tid`, sa.`sid`, sa.`status`, ' +
  486. ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  487. ' FROM ?? As t' +
  488. ' LEFT JOIN ?? As s On t.`id` = s.`tid`' +
  489. ' LEFT JOIN ?? As sa ON s.`id` = sa.`sid`' +
  490. ' LEFT JOIN ?? As pa ON sa.`aid` = pa.`id`' +
  491. ' WHERE sa.`aid` <> ? and sa.`end_time` > ? and t.`project_id` = ?' +
  492. ' GROUP By t.`id`' +
  493. ' ORDER By sa.`end_time`';
  494. const sqlParam = [this.ctx.service.tender.tableName, this.ctx.service.stage.tableName, this.tableName,
  495. this.ctx.service.projectAccount.tableName, uid, time, pid];
  496. return await this.db.query(sql, sqlParam);
  497. }
  498. /**
  499. * 获取审核人流程列表
  500. *
  501. * @param auditorId
  502. * @returns {Promise<*>}
  503. */
  504. async getAuditGroupByList(stageId, times) {
  505. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  506. 'FROM ?? AS la, ?? AS pa ' +
  507. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid`';
  508. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  509. return await this.db.query(sql, sqlParam);
  510. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  511. // const sqlParam = [this.tableName, stageId, times];
  512. // return await this.db.query(sql, sqlParam);
  513. }
  514. /**
  515. * 复制上一期的审批人列表给最新一期
  516. *
  517. * @param transaction - 新增一期的事务
  518. * @param {Object} preStage - 上一期
  519. * @param {Object} newStage - 最新一期
  520. * @returns {Promise<*>}
  521. */
  522. async copyPreStageAuditors(transaction, preStage, newStage) {
  523. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  524. const newAuditors = [];
  525. for (const a of auditors) {
  526. const na = {
  527. tid: preStage.tid,
  528. sid: newStage.id,
  529. aid: a.aid,
  530. times: newStage.times,
  531. order: newAuditors.length + 1,
  532. status: auditConst.status.uncheck
  533. };
  534. newAuditors.push(na);
  535. }
  536. const result = await transaction.insert(this.tableName, newAuditors);
  537. return result.effectRows = auditors.length;
  538. }
  539. /**
  540. * 移除审核人
  541. *
  542. * @param {Number} stageId - 期id
  543. * @param {Number} status - 期状态
  544. * @param {Number} status - 期次数
  545. * @return {Promise<boolean>}
  546. */
  547. async getAuditorByStatus(stageId, status, times = 1) {
  548. let auditor = null;
  549. let sql = '';
  550. let sqlParam = '';
  551. switch (status) {
  552. case auditConst.status.checking :
  553. case auditConst.status.checked :
  554. case auditConst.status.checkNoPre :
  555. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  556. 'FROM ?? AS la, ?? AS pa ' +
  557. 'WHERE la.`sid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  558. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  559. auditor = await this.db.queryOne(sql, sqlParam);
  560. break;
  561. case auditConst.status.checkNo :
  562. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  563. 'FROM ?? AS la, ?? AS pa ' +
  564. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  565. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  566. auditor = await this.db.queryOne(sql, sqlParam);
  567. break;
  568. case auditConst.status.uncheck :
  569. default:break;
  570. }
  571. return auditor;
  572. }
  573. }
  574. return StageAudit;
  575. };