stage_audit.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. const smsTypeConst = require('../const/sms_type');
  11. const SMS = require('../lib/sms');
  12. module.exports = app => {
  13. class StageAudit extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'stage_audit';
  23. }
  24. /**
  25. * 获取 审核人信息
  26. *
  27. * @param {Number} stageId - 期id
  28. * @param {Number} auditorId - 审核人id
  29. * @param {Number} times - 第几次审批
  30. * @returns {Promise<*>}
  31. */
  32. async getAuditor(stageId, auditorId, times = 1) {
  33. 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` ' +
  34. 'FROM ?? AS la, ?? AS pa ' +
  35. 'WHERE la.`sid` = ? and la.`aid` = ? and la.`times` = ?' +
  36. ' and la.`aid` = pa.`id`';
  37. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditorId, times];
  38. return await this.db.queryOne(sql, sqlParam);
  39. }
  40. /**
  41. * 获取 审核列表信息
  42. *
  43. * @param {Number} stageId - 期id
  44. * @param {Number} times - 第几次审批
  45. * @returns {Promise<*>}
  46. */
  47. async getAuditors(stageId, times = 1) {
  48. 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` ' +
  49. 'FROM ?? AS la, ?? AS pa ' +
  50. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`order`';
  51. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  52. return await this.db.query(sql, sqlParam);
  53. }
  54. /**
  55. * 获取 当前审核人
  56. *
  57. * @param {Number} stageId - 期id
  58. * @param {Number} times - 第几次审批
  59. * @returns {Promise<*>}
  60. */
  61. async getCurAuditor(stageId, times = 1) {
  62. 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` ' +
  63. 'FROM ?? AS la, ?? AS pa ' +
  64. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
  65. ' and la.`aid` = pa.`id`';
  66. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
  67. return await this.db.queryOne(sql, sqlParam);
  68. }
  69. /**
  70. * 获取 最新审核顺序
  71. *
  72. * @param {Number} stageId - 期id
  73. * @param {Number} times - 第几次审批
  74. * @returns {Promise<number>}
  75. */
  76. async getNewOrder(stageId, times = 1) {
  77. const sql = 'SELECT Max(??) As max_order FROM ?? Where `sid` = ? and `times` = ?';
  78. const sqlParam = ['order', this.tableName, stageId, times];
  79. const result = await this.db.queryOne(sql, sqlParam);
  80. return result && result.max_order ? result.max_order + 1 : 1;
  81. }
  82. /**
  83. * 新增审核人
  84. *
  85. * @param {Number} stageId - 期id
  86. * @param {Number} auditorId - 审核人id
  87. * @param {Number} times - 第几次审批
  88. * @returns {Promise<number>}
  89. */
  90. async addAuditor(stageId, auditorId, times = 1) {
  91. const newOrder = await this.getNewOrder(stageId, times);
  92. const data = {
  93. tid: this.ctx.tender.id,
  94. sid: stageId,
  95. aid: auditorId,
  96. times: times,
  97. order: newOrder,
  98. status: auditConst.status.uncheck,
  99. };
  100. const result = await this.db.insert(this.tableName, data);
  101. return result.effectRows = 1;
  102. }
  103. /**
  104. * 移除审核人时,同步其后审核人order
  105. * @param transaction - 事务
  106. * @param {Number} stageId - 标段id
  107. * @param {Number} auditorId - 审核人id
  108. * @param {Number} times - 第几次审批
  109. * @returns {Promise<*>}
  110. * @private
  111. */
  112. async _syncOrderByDelete(transaction, stageId, order, times) {
  113. this.initSqlBuilder();
  114. this.sqlBuilder.setAndWhere('sid', {
  115. value: stageId,
  116. operate: '='
  117. });
  118. this.sqlBuilder.setAndWhere('order', {
  119. value: order,
  120. operate: '>=',
  121. });
  122. this.sqlBuilder.setAndWhere('times', {
  123. value: times,
  124. operate: '=',
  125. });
  126. this.sqlBuilder.setUpdateData('order', {
  127. value: 1,
  128. selfOperate: '-',
  129. });
  130. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  131. const data = await transaction.query(sql, sqlParam);
  132. return data;
  133. }
  134. /**
  135. * 移除审核人
  136. *
  137. * @param {Number} stageId - 期id
  138. * @param {Number} auditorId - 审核人id
  139. * @param {Number} times - 第几次审批
  140. * @returns {Promise<boolean>}
  141. */
  142. async deleteAuditor(stageId, auditorId, times = 1) {
  143. const transaction = await this.db.beginTransaction();
  144. try {
  145. const condition = {sid: stageId, aid: auditorId, times: times};
  146. const auditor = await this.getDataByCondition(condition);
  147. if (!auditor) {
  148. throw '该审核人不存在';
  149. }
  150. await this._syncOrderByDelete(transaction, stageId, auditor.order, times);
  151. await transaction.delete(this.tableName, condition);
  152. await transaction.commit();
  153. } catch(err) {
  154. await transaction.rollback();
  155. throw err;
  156. }
  157. return true;
  158. }
  159. /**
  160. * 开始审批
  161. *
  162. * @param {Number} stageId - 期id
  163. * @param {Number} times - 第几次审批
  164. * @returns {Promise<boolean>}
  165. */
  166. async start(stageId, times = 1) {
  167. const audit = await this.getDataByCondition({sid: stageId, times: times, order: 1});
  168. if (!audit) {
  169. throw '请先选择审批人,再上报数据';
  170. }
  171. const transaction = await this.db.beginTransaction();
  172. try {
  173. await transaction.update(this.tableName, {id: audit.id, status: auditConst.status.checking, begin_time: new Date()});
  174. // 计算原报最终数据
  175. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  176. // 复制一份下一审核人数据
  177. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  178. // 更新期数据
  179. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  180. await transaction.update(this.ctx.service.stage.tableName, {
  181. id: stageId, status: auditConst.status.checking,
  182. contract_tp: tpData.contract_tp,
  183. qc_tp: tpData.qc_tp,
  184. });
  185. // 添加短信通知-需要审批提醒功能
  186. const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  187. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  188. const smsType = JSON.parse(smsUser.sms_type);
  189. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  190. const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  191. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  192. const sms = new SMS(this.ctx);
  193. const content = '【纵横计量支付】' + tenderInfo.name + '第' + stageInfo.order + '期,需要您审批。';
  194. sms.send(smsUser.auth_mobile, content);
  195. }
  196. }
  197. // todo 更新标段tender状态 ?
  198. await transaction.commit();
  199. } catch(err) {
  200. await transaction.rollback();
  201. throw err;
  202. }
  203. return true;
  204. }
  205. async _checked(stageId, checkData, times) {
  206. const time = new Date();
  207. // 整理当前流程审核人状态更新
  208. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  209. if (!audit) {
  210. throw '审核数据错误';
  211. }
  212. const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  213. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  214. const transaction = await this.db.beginTransaction();
  215. try {
  216. await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  217. // 计算并合同支付最终数据
  218. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  219. // 无下一审核人表示,审核结束
  220. if (nextAudit) {
  221. // 复制一份下一审核人数据
  222. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  223. // 流程至下一审批人
  224. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  225. // 同步 期信息
  226. await transaction.update(this.ctx.service.stage.tableName, {
  227. id: stageId, status: auditConst.status.checking,
  228. contract_tp: tpData.contract_tp,
  229. qc_tp: tpData.qc_tp,
  230. });
  231. // 添加短信通知-需要审批提醒功能
  232. const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  233. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  234. const smsType = JSON.parse(smsUser.sms_type);
  235. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  236. const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  237. const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  238. const sms = new SMS(this.ctx);
  239. const content = '【纵横计量支付】' + tenderInfo.name + '第' + stageInfo.order + '期,需要您审批。';
  240. sms.send(smsUser.auth_mobile, content);
  241. }
  242. }
  243. } else {
  244. // 本期结束
  245. // 生成截止本期数据 final数据
  246. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  247. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  248. // 同步 期信息
  249. await transaction.update(this.ctx.service.stage.tableName, {
  250. id: stageId, status: checkData.checkType,
  251. contract_tp: tpData.contract_tp,
  252. qc_tp: tpData.qc_tp,
  253. });
  254. // 添加短信通知-审批通过提醒功能
  255. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  256. const smsUser = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  257. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  258. const smsType = JSON.parse(smsUser.sms_type);
  259. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  260. const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  261. const sms = new SMS(this.ctx);
  262. const content = '【纵横计量支付】' + tenderInfo.name + '第' + stageInfo.order + '期,审批通过。';
  263. sms.send(smsUser.auth_mobile, content);
  264. }
  265. }
  266. }
  267. await transaction.commit();
  268. } catch (err) {
  269. await transaction.rollback();
  270. throw err;
  271. }
  272. }
  273. async _checkNo(stageId, checkData, times) {
  274. const time = new Date();
  275. // 整理当前流程审核人状态更新
  276. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  277. if (!audit) {
  278. throw '审核数据错误';
  279. }
  280. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  281. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  282. const sqlParam = [this.tableName, stageId, times];
  283. const auditors = await this.db.query(sql, sqlParam);
  284. let order = 1;
  285. for (const a of auditors) {
  286. a.times = times + 1;
  287. a.order = order;
  288. a.status = auditConst.status.uncheck;
  289. order++;
  290. }
  291. const transaction = await this.db.beginTransaction();
  292. try {
  293. await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  294. // 同步 期信息
  295. await transaction.update(this.ctx.service.stage.tableName, {
  296. id: stageId, status: checkData.checkType,
  297. contract_tp: tpData.contract_tp,
  298. qc_tp: tpData.qc_tp,
  299. times: times + 1,
  300. });
  301. // 拷贝新一次审核流程列表
  302. await transaction.insert(this.tableName, auditors);
  303. // 计算该审批人最终数据
  304. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  305. // 复制一份最新数据给原报
  306. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  307. // 添加短信通知-审批退回提醒功能
  308. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  309. const smsUser = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  310. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  311. const smsType = JSON.parse(smsUser.sms_type);
  312. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  313. const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  314. const sms = new SMS(this.ctx);
  315. const content = '【纵横计量支付】' + tenderInfo.name + '第' + stageInfo.order + '期,审批退回。';
  316. sms.send(smsUser.auth_mobile, content);
  317. }
  318. }
  319. await transaction.commit();
  320. } catch (err) {
  321. await transaction.rollback();
  322. throw err;
  323. }
  324. }
  325. async _checkNoPre(stageId, checkData, times) {
  326. const time = new Date();
  327. // 整理当前流程审核人状态更新
  328. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  329. if (!audit || audit.order <= 1) {
  330. throw '审核数据错误';
  331. }
  332. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  333. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  334. const auditors2 = await this.getAuditGroupByList(stageId, times);
  335. const auditorIndex = await auditors2.findIndex(function(item) {
  336. return item.aid === audit.aid;
  337. });
  338. const preAuditor = auditors2[auditorIndex - 1];
  339. const transaction = await this.db.beginTransaction();
  340. try {
  341. await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  342. // 顺移气候审核人流程顺序
  343. this.initSqlBuilder();
  344. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  345. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  346. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  347. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  348. const data = await transaction.query(sql, sqlParam);
  349. // 上一审批人,当前审批人 再次添加至流程
  350. const newAuditors = [];
  351. newAuditors.push({
  352. tid: audit.tid, sid: audit.sid, aid: preAuditor.aid,
  353. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  354. begin_time: time,
  355. });
  356. newAuditors.push({
  357. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  358. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  359. });
  360. await transaction.insert(this.tableName, newAuditors);
  361. // 计算该审批人最终数据
  362. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  363. // 复制一份最新数据给下一人
  364. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  365. // 添加短信通知-需要审批提醒功能
  366. const smsUser = await this.ctx.service.projectAccount.getDataById(preAuditor.aid);
  367. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  368. const smsType = JSON.parse(smsUser.sms_type);
  369. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  370. const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  371. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  372. const sms = new SMS(this.ctx);
  373. const content = '【纵横计量支付】' + tenderInfo.name + '第' + stageInfo.order + '期,需要您审批。';
  374. sms.send(smsUser.auth_mobile, content);
  375. }
  376. }
  377. await transaction.commit();
  378. } catch(err) {
  379. await transaction.rollback();
  380. throw err;
  381. }
  382. }
  383. /**
  384. * 审批
  385. * @param {Number} stageId - 标段id
  386. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  387. * @param {Number} times - 第几次审批
  388. * @returns {Promise<void>}
  389. */
  390. async check(stageId, checkData, times = 1) {
  391. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  392. throw '提交数据错误';
  393. }
  394. // // 整理当前流程审核人状态更新
  395. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  396. // if (!audit) {
  397. // throw '审核数据错误';
  398. // }
  399. //const time = new Date();
  400. switch (checkData.checkType) {
  401. case auditConst.status.checked:
  402. await this._checked(stageId, checkData, times);
  403. break;
  404. case auditConst.status.checkNo:
  405. await this._checkNo(stageId, checkData, times);
  406. break;
  407. case auditConst.status.checkNoPre:
  408. await this._checkNoPre(stageId, checkData, times);
  409. break;
  410. default:
  411. throw '无效审批操作';
  412. }
  413. // const transaction = await this.db.beginTransaction();
  414. // try {
  415. // // 更新当前审核流程
  416. // await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  417. // if (checkData.checkType === auditConst.status.checked) { // 审批通过
  418. // const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  419. // // 无下一审核人表示,审核结束
  420. // if (nextAudit) {
  421. // // 计算该审批人最终数据
  422. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  423. // // 复制一份下一审核人数据
  424. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  425. // // 流程至下一审批人
  426. // await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  427. // // 同步 期信息
  428. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  429. // await transaction.update(this.ctx.service.stage.tableName, {
  430. // id: stageId, status: auditConst.status.checking,
  431. // contract_tp: tpData.contract_tp,
  432. // qc_tp: tpData.qc_tp,
  433. // });
  434. // } else {
  435. // // 本期结束
  436. // // 生成截止本期数据 final数据
  437. // await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  438. // await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  439. // // 计算并合同支付最终数据
  440. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  441. // // 同步 期信息
  442. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  443. // await transaction.update(this.ctx.service.stage.tableName, {
  444. // id: stageId, status: checkData.checkType,
  445. // contract_tp: tpData.contract_tp,
  446. // qc_tp: tpData.qc_tp,
  447. // });
  448. // }
  449. // } else if (checkData.checkType === auditConst.status.checkNo) { // 审批退回 原报, times+1
  450. // // 同步 期信息
  451. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  452. // await transaction.update(this.ctx.service.stage.tableName, {
  453. // id: stageId, status: checkData.checkType,
  454. // contract_tp: tpData.contract_tp,
  455. // qc_tp: tpData.qc_tp,
  456. // times: times + 1,
  457. // });
  458. // // 拷贝新一次审核流程列表
  459. // // const auditors = await this.getAllDataByCondition({
  460. // // where: {sid: stageId, times: times},
  461. // // columns: ['tid', 'sid', 'aid', 'order']
  462. // // });
  463. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  464. // const sqlParam = [this.tableName, stageId, times];
  465. // const auditors = await this.db.query(sql, sqlParam);
  466. // let order = 1;
  467. // for (const a of auditors) {
  468. // a.times = times + 1;
  469. // a.order = order;
  470. // a.status = auditConst.status.uncheck;
  471. // order++;
  472. // }
  473. // await transaction.insert(this.tableName, auditors);
  474. // // 计算该审批人最终数据
  475. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  476. // // 复制一份最新数据给原报
  477. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  478. // } else if (checkData.checkType === auditConst.status.checkNoPre) { // 审批退回 上一审批人
  479. // // 同步 期信息
  480. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  481. // await transaction.update(this.ctx.service.stage.tableName, {
  482. // id: stageId, status: checkData.checkType,
  483. // contract_tp: tpData.contract_tp,
  484. // qc_tp: tpData.qc_tp,
  485. // });
  486. // // 将当前审批人 与 上一审批人再次添加至流程,顺移其后审批人流程顺序
  487. // if (audit.order > 1) {
  488. // // 顺移气候审核人流程顺序
  489. // this.initSqlBuilder();
  490. // this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  491. // this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  492. // this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  493. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  494. // const data = await transaction.query(sql, sqlParam);
  495. //
  496. // // 上一审批人,当前审批人 再次添加至流程
  497. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  498. // const newAuditors = [];
  499. // newAuditors.push({
  500. // tid: preAuditor.tid, sid: preAuditor.sid, aid: preAuditor.aid,
  501. // times: preAuditor.times, order: preAuditor.order + 2, status: auditConst.status.checking,
  502. // begin_time: time,
  503. // });
  504. // newAuditors.push({
  505. // tid: audit.tid, sid: audit.sid, aid: audit.aid,
  506. // times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck
  507. // });
  508. // await transaction.insert(this.tableName, newAuditors);
  509. //
  510. // // 计算该审批人最终数据
  511. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  512. // // 复制一份最新数据给上一人
  513. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  514. // } else {
  515. // throw '审核数据错误';
  516. // }
  517. // } else {
  518. // throw '无效审批操作';
  519. // }
  520. //
  521. // await transaction.commit();
  522. // } catch (err) {
  523. // await transaction.rollback();
  524. // throw err;
  525. // }
  526. }
  527. /**
  528. * 审批
  529. * @param {Number} stageId - 标段id
  530. * @param {Number} times - 第几次审批
  531. * @returns {Promise<void>}
  532. */
  533. async checkAgain(stageId, times = 1) {
  534. const time = new Date();
  535. // 整理当前流程审核人状态更新
  536. const audit = (await this.getAllDataByCondition({ where: { sid: stageId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  537. if (!audit || audit.order <= 1) {
  538. throw '审核数据错误';
  539. }
  540. const transaction = await this.db.beginTransaction();
  541. try {
  542. // 当前审批人2次添加至流程中
  543. const newAuditors = [];
  544. newAuditors.push({
  545. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  546. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  547. begin_time: time, end_time: time, opinion: '',
  548. });
  549. newAuditors.push({
  550. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  551. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  552. begin_time: time,
  553. });
  554. await transaction.insert(this.tableName, newAuditors);
  555. // 复制一份最新数据给下一人
  556. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  557. // 本期结束
  558. // 生成截止本期数据 final数据
  559. await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  560. await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  561. // 同步 期信息
  562. await transaction.update(this.ctx.service.stage.tableName, {
  563. id: stageId, status: auditConst.status.checking,
  564. });
  565. // 添加短信通知-需要审批提醒功能
  566. const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  567. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  568. const smsType = JSON.parse(smsUser.sms_type);
  569. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  570. const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  571. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  572. const sms = new SMS(this.ctx);
  573. const content = '【纵横计量支付】' + tenderInfo.name + '第' + stageInfo.order + '期,需要您审批。';
  574. sms.send(smsUser.auth_mobile, content);
  575. }
  576. }
  577. await transaction.commit();
  578. } catch (err) {
  579. await transaction.rollback();
  580. throw err;
  581. }
  582. }
  583. /**
  584. * 获取审核人需要审核的期列表
  585. *
  586. * @param auditorId
  587. * @returns {Promise<*>}
  588. */
  589. async getAuditStage(auditorId) {
  590. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  591. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  592. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  593. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  594. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  595. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id`';
  596. 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];
  597. return await this.db.query(sql, sqlParam);
  598. }
  599. /**
  600. * 获取 某时间后 审批进度 更新的期
  601. * @param {Number} pid - 查询标段
  602. * @param {Number} uid - 查询人
  603. * @param {Date} time - 查询时间
  604. * @returns {Promise<*>}
  605. */
  606. async getNoticeStage(pid, uid, time) {
  607. const sql = 'SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  608. ' s.`order` As `s_order`, s.`status` As `s_status`, ' +
  609. ' sa.`aid`, sa.`times`, sa.`order`, sa.`end_time`, sa.`tid`, sa.`sid`, sa.`status`, ' +
  610. ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  611. ' FROM ?? As t' +
  612. ' LEFT JOIN ?? As s On t.`id` = s.`tid`' +
  613. ' LEFT JOIN ?? As sa ON s.`id` = sa.`sid`' +
  614. ' LEFT JOIN ?? As pa ON sa.`aid` = pa.`id`' +
  615. ' WHERE sa.`aid` <> ? and sa.`end_time` > ? and t.`project_id` = ?' +
  616. ' GROUP By t.`id`' +
  617. ' ORDER By sa.`end_time`';
  618. const sqlParam = [this.ctx.service.tender.tableName, this.ctx.service.stage.tableName, this.tableName,
  619. this.ctx.service.projectAccount.tableName, uid, time, pid];
  620. return await this.db.query(sql, sqlParam);
  621. }
  622. /**
  623. * 获取审核人流程列表
  624. *
  625. * @param auditorId
  626. * @returns {Promise<*>}
  627. */
  628. async getAuditGroupByList(stageId, times) {
  629. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  630. 'FROM ?? AS la, ?? AS pa ' +
  631. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid`';
  632. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  633. return await this.db.query(sql, sqlParam);
  634. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  635. // const sqlParam = [this.tableName, stageId, times];
  636. // return await this.db.query(sql, sqlParam);
  637. }
  638. /**
  639. * 复制上一期的审批人列表给最新一期
  640. *
  641. * @param transaction - 新增一期的事务
  642. * @param {Object} preStage - 上一期
  643. * @param {Object} newStage - 最新一期
  644. * @returns {Promise<*>}
  645. */
  646. async copyPreStageAuditors(transaction, preStage, newStage) {
  647. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  648. const newAuditors = [];
  649. for (const a of auditors) {
  650. const na = {
  651. tid: preStage.tid,
  652. sid: newStage.id,
  653. aid: a.aid,
  654. times: newStage.times,
  655. order: newAuditors.length + 1,
  656. status: auditConst.status.uncheck
  657. };
  658. newAuditors.push(na);
  659. }
  660. const result = await transaction.insert(this.tableName, newAuditors);
  661. return result.effectRows = auditors.length;
  662. }
  663. /**
  664. * 移除审核人
  665. *
  666. * @param {Number} stageId - 期id
  667. * @param {Number} status - 期状态
  668. * @param {Number} status - 期次数
  669. * @return {Promise<boolean>}
  670. */
  671. async getAuditorByStatus(stageId, status, times = 1) {
  672. let auditor = null;
  673. let sql = '';
  674. let sqlParam = '';
  675. switch (status) {
  676. case auditConst.status.checking :
  677. case auditConst.status.checked :
  678. case auditConst.status.checkNoPre :
  679. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  680. 'FROM ?? AS la, ?? AS pa ' +
  681. 'WHERE la.`sid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  682. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  683. auditor = await this.db.queryOne(sql, sqlParam);
  684. break;
  685. case auditConst.status.checkNo :
  686. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  687. 'FROM ?? AS la, ?? AS pa ' +
  688. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  689. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  690. auditor = await this.db.queryOne(sql, sqlParam);
  691. break;
  692. case auditConst.status.uncheck :
  693. default:break;
  694. }
  695. return auditor;
  696. }
  697. }
  698. return StageAudit;
  699. };