stage_audit.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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 !== '' && 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 tenderName = await sms.contentChange(tenderInfo.name);
  194. const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  195. sms.send(smsUser.auth_mobile, content);
  196. }
  197. }
  198. // todo 更新标段tender状态 ?
  199. await transaction.commit();
  200. } catch(err) {
  201. await transaction.rollback();
  202. throw err;
  203. }
  204. return true;
  205. }
  206. async _checked(stageId, checkData, times) {
  207. const time = new Date();
  208. // 整理当前流程审核人状态更新
  209. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  210. if (!audit) {
  211. throw '审核数据错误';
  212. }
  213. const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  214. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  215. const transaction = await this.db.beginTransaction();
  216. try {
  217. await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  218. // 计算并合同支付最终数据
  219. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  220. // 无下一审核人表示,审核结束
  221. if (nextAudit) {
  222. // 复制一份下一审核人数据
  223. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  224. // 流程至下一审批人
  225. await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  226. // 同步 期信息
  227. await transaction.update(this.ctx.service.stage.tableName, {
  228. id: stageId, status: auditConst.status.checking,
  229. contract_tp: tpData.contract_tp,
  230. qc_tp: tpData.qc_tp,
  231. });
  232. // 添加短信通知-需要审批提醒功能
  233. const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  234. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  235. const smsType = JSON.parse(smsUser.sms_type);
  236. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  237. const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  238. const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  239. const sms = new SMS(this.ctx);
  240. const tenderName = await sms.contentChange(tenderInfo.name);
  241. const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  242. sms.send(smsUser.auth_mobile, content);
  243. }
  244. }
  245. } else {
  246. // 本期结束
  247. // 生成截止本期数据 final数据
  248. console.time('generatePre');
  249. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  250. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  251. console.timeEnd('generatePre');
  252. // 同步 期信息
  253. await transaction.update(this.ctx.service.stage.tableName, {
  254. id: stageId, status: checkData.checkType,
  255. contract_tp: tpData.contract_tp,
  256. qc_tp: tpData.qc_tp,
  257. });
  258. // 添加短信通知-审批通过提醒功能
  259. const mobile_array = [];
  260. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  261. const auditList = await this.getAuditors(stageId, stageInfo.times);
  262. const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  263. if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  264. const smsType = JSON.parse(smsUser1.sms_type);
  265. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  266. mobile_array.push(smsUser1.auth_mobile);
  267. }
  268. }
  269. for (const user of auditList) {
  270. const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  271. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  272. const smsType = JSON.parse(smsUser.sms_type);
  273. if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  274. mobile_array.push(smsUser.auth_mobile);
  275. }
  276. }
  277. }
  278. if (mobile_array.length > 0) {
  279. const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  280. const sms = new SMS(this.ctx);
  281. const tenderName = await sms.contentChange(tenderInfo.name);
  282. const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批通过。';
  283. sms.send(mobile_array, content);
  284. }
  285. }
  286. await transaction.commit();
  287. } catch (err) {
  288. await transaction.rollback();
  289. throw err;
  290. }
  291. }
  292. async _checkNo(stageId, checkData, times) {
  293. const time = new Date();
  294. // 整理当前流程审核人状态更新
  295. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  296. if (!audit) {
  297. throw '审核数据错误';
  298. }
  299. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  300. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  301. const sqlParam = [this.tableName, stageId, times];
  302. const auditors = await this.db.query(sql, sqlParam);
  303. let order = 1;
  304. for (const a of auditors) {
  305. a.times = times + 1;
  306. a.order = order;
  307. a.status = auditConst.status.uncheck;
  308. order++;
  309. }
  310. const transaction = await this.db.beginTransaction();
  311. try {
  312. await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  313. // 同步 期信息
  314. await transaction.update(this.ctx.service.stage.tableName, {
  315. id: stageId, status: checkData.checkType,
  316. contract_tp: tpData.contract_tp,
  317. qc_tp: tpData.qc_tp,
  318. times: times + 1,
  319. });
  320. // 拷贝新一次审核流程列表
  321. await transaction.insert(this.tableName, auditors);
  322. // 计算该审批人最终数据
  323. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  324. // 复制一份最新数据给原报
  325. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  326. // 添加短信通知-审批退回提醒功能
  327. const mobile_array = [];
  328. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  329. const auditList = await this.getAuditors(stageId, stageInfo.times);
  330. const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  331. if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  332. const smsType = JSON.parse(smsUser1.sms_type);
  333. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  334. mobile_array.push(smsUser1.auth_mobile);
  335. }
  336. }
  337. for (const user of auditList) {
  338. const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  339. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  340. const smsType = JSON.parse(smsUser.sms_type);
  341. if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  342. mobile_array.push(smsUser.auth_mobile);
  343. }
  344. }
  345. }
  346. if (mobile_array.length > 0) {
  347. const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  348. const sms = new SMS(this.ctx);
  349. const tenderName = await sms.contentChange(tenderInfo.name);
  350. const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批退回。';
  351. sms.send(mobile_array, content);
  352. }
  353. await transaction.commit();
  354. } catch (err) {
  355. await transaction.rollback();
  356. throw err;
  357. }
  358. }
  359. async _checkNoPre(stageId, checkData, times) {
  360. const time = new Date();
  361. // 整理当前流程审核人状态更新
  362. const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  363. if (!audit || audit.order <= 1) {
  364. throw '审核数据错误';
  365. }
  366. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  367. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  368. const auditors2 = await this.getAuditGroupByList(stageId, times);
  369. const auditorIndex = await auditors2.findIndex(function(item) {
  370. return item.aid === audit.aid;
  371. });
  372. const preAuditor = auditors2[auditorIndex - 1];
  373. const transaction = await this.db.beginTransaction();
  374. try {
  375. await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  376. // 顺移气候审核人流程顺序
  377. this.initSqlBuilder();
  378. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  379. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  380. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  381. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  382. const data = await transaction.query(sql, sqlParam);
  383. // 上一审批人,当前审批人 再次添加至流程
  384. const newAuditors = [];
  385. newAuditors.push({
  386. tid: audit.tid, sid: audit.sid, aid: preAuditor.aid,
  387. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  388. begin_time: time,
  389. });
  390. newAuditors.push({
  391. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  392. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  393. });
  394. await transaction.insert(this.tableName, newAuditors);
  395. // 计算该审批人最终数据
  396. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  397. // 复制一份最新数据给下一人
  398. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  399. // 添加短信通知-需要审批提醒功能
  400. const smsUser = await this.ctx.service.projectAccount.getDataById(preAuditor.aid);
  401. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  402. const smsType = JSON.parse(smsUser.sms_type);
  403. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  404. const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  405. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  406. const sms = new SMS(this.ctx);
  407. const tenderName = await sms.contentChange(tenderInfo.name);
  408. const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  409. sms.send(smsUser.auth_mobile, content);
  410. }
  411. }
  412. await transaction.commit();
  413. } catch(err) {
  414. await transaction.rollback();
  415. throw err;
  416. }
  417. }
  418. /**
  419. * 审批
  420. * @param {Number} stageId - 标段id
  421. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  422. * @param {Number} times - 第几次审批
  423. * @returns {Promise<void>}
  424. */
  425. async check(stageId, checkData, times = 1) {
  426. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  427. throw '提交数据错误';
  428. }
  429. // // 整理当前流程审核人状态更新
  430. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  431. // if (!audit) {
  432. // throw '审核数据错误';
  433. // }
  434. //const time = new Date();
  435. switch (checkData.checkType) {
  436. case auditConst.status.checked:
  437. await this._checked(stageId, checkData, times);
  438. break;
  439. case auditConst.status.checkNo:
  440. await this._checkNo(stageId, checkData, times);
  441. break;
  442. case auditConst.status.checkNoPre:
  443. await this._checkNoPre(stageId, checkData, times);
  444. break;
  445. default:
  446. throw '无效审批操作';
  447. }
  448. // const transaction = await this.db.beginTransaction();
  449. // try {
  450. // // 更新当前审核流程
  451. // await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  452. // if (checkData.checkType === auditConst.status.checked) { // 审批通过
  453. // const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  454. // // 无下一审核人表示,审核结束
  455. // if (nextAudit) {
  456. // // 计算该审批人最终数据
  457. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  458. // // 复制一份下一审核人数据
  459. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  460. // // 流程至下一审批人
  461. // await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  462. // // 同步 期信息
  463. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  464. // await transaction.update(this.ctx.service.stage.tableName, {
  465. // id: stageId, status: auditConst.status.checking,
  466. // contract_tp: tpData.contract_tp,
  467. // qc_tp: tpData.qc_tp,
  468. // });
  469. // } else {
  470. // // 本期结束
  471. // // 生成截止本期数据 final数据
  472. // await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  473. // await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  474. // // 计算并合同支付最终数据
  475. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  476. // // 同步 期信息
  477. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  478. // await transaction.update(this.ctx.service.stage.tableName, {
  479. // id: stageId, status: checkData.checkType,
  480. // contract_tp: tpData.contract_tp,
  481. // qc_tp: tpData.qc_tp,
  482. // });
  483. // }
  484. // } else if (checkData.checkType === auditConst.status.checkNo) { // 审批退回 原报, times+1
  485. // // 同步 期信息
  486. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  487. // await transaction.update(this.ctx.service.stage.tableName, {
  488. // id: stageId, status: checkData.checkType,
  489. // contract_tp: tpData.contract_tp,
  490. // qc_tp: tpData.qc_tp,
  491. // times: times + 1,
  492. // });
  493. // // 拷贝新一次审核流程列表
  494. // // const auditors = await this.getAllDataByCondition({
  495. // // where: {sid: stageId, times: times},
  496. // // columns: ['tid', 'sid', 'aid', 'order']
  497. // // });
  498. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  499. // const sqlParam = [this.tableName, stageId, times];
  500. // const auditors = await this.db.query(sql, sqlParam);
  501. // let order = 1;
  502. // for (const a of auditors) {
  503. // a.times = times + 1;
  504. // a.order = order;
  505. // a.status = auditConst.status.uncheck;
  506. // order++;
  507. // }
  508. // await transaction.insert(this.tableName, auditors);
  509. // // 计算该审批人最终数据
  510. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  511. // // 复制一份最新数据给原报
  512. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  513. // } else if (checkData.checkType === auditConst.status.checkNoPre) { // 审批退回 上一审批人
  514. // // 同步 期信息
  515. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  516. // await transaction.update(this.ctx.service.stage.tableName, {
  517. // id: stageId, status: checkData.checkType,
  518. // contract_tp: tpData.contract_tp,
  519. // qc_tp: tpData.qc_tp,
  520. // });
  521. // // 将当前审批人 与 上一审批人再次添加至流程,顺移其后审批人流程顺序
  522. // if (audit.order > 1) {
  523. // // 顺移气候审核人流程顺序
  524. // this.initSqlBuilder();
  525. // this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  526. // this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  527. // this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  528. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  529. // const data = await transaction.query(sql, sqlParam);
  530. //
  531. // // 上一审批人,当前审批人 再次添加至流程
  532. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  533. // const newAuditors = [];
  534. // newAuditors.push({
  535. // tid: preAuditor.tid, sid: preAuditor.sid, aid: preAuditor.aid,
  536. // times: preAuditor.times, order: preAuditor.order + 2, status: auditConst.status.checking,
  537. // begin_time: time,
  538. // });
  539. // newAuditors.push({
  540. // tid: audit.tid, sid: audit.sid, aid: audit.aid,
  541. // times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck
  542. // });
  543. // await transaction.insert(this.tableName, newAuditors);
  544. //
  545. // // 计算该审批人最终数据
  546. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  547. // // 复制一份最新数据给上一人
  548. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  549. // } else {
  550. // throw '审核数据错误';
  551. // }
  552. // } else {
  553. // throw '无效审批操作';
  554. // }
  555. //
  556. // await transaction.commit();
  557. // } catch (err) {
  558. // await transaction.rollback();
  559. // throw err;
  560. // }
  561. }
  562. /**
  563. * 审批
  564. * @param {Number} stageId - 标段id
  565. * @param {Number} times - 第几次审批
  566. * @returns {Promise<void>}
  567. */
  568. async checkAgain(stageId, times = 1) {
  569. const time = new Date();
  570. // 整理当前流程审核人状态更新
  571. const audit = (await this.getAllDataByCondition({ where: { sid: stageId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  572. if (!audit || audit.order <= 1) {
  573. throw '审核数据错误';
  574. }
  575. const transaction = await this.db.beginTransaction();
  576. try {
  577. // 当前审批人2次添加至流程中
  578. const newAuditors = [];
  579. newAuditors.push({
  580. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  581. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  582. begin_time: time, end_time: time, opinion: '',
  583. });
  584. newAuditors.push({
  585. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  586. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  587. begin_time: time,
  588. });
  589. await transaction.insert(this.tableName, newAuditors);
  590. // 复制一份最新数据给下一人
  591. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  592. // 本期结束
  593. // 生成截止本期数据 final数据
  594. await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  595. await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  596. // 同步 期信息
  597. await transaction.update(this.ctx.service.stage.tableName, {
  598. id: stageId, status: auditConst.status.checking,
  599. });
  600. // 添加短信通知-需要审批提醒功能
  601. const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  602. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  603. const smsType = JSON.parse(smsUser.sms_type);
  604. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  605. const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  606. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  607. const sms = new SMS(this.ctx);
  608. const tenderName = await sms.contentChange(tenderInfo.name);
  609. const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  610. sms.send(smsUser.auth_mobile, content);
  611. }
  612. }
  613. await transaction.commit();
  614. } catch (err) {
  615. await transaction.rollback();
  616. throw err;
  617. }
  618. }
  619. /**
  620. * 获取审核人需要审核的期列表
  621. *
  622. * @param auditorId
  623. * @returns {Promise<*>}
  624. */
  625. async getAuditStage(auditorId) {
  626. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  627. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  628. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  629. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  630. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  631. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id`';
  632. 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];
  633. return await this.db.query(sql, sqlParam);
  634. }
  635. /**
  636. * 获取 某时间后 审批进度 更新的期
  637. * @param {Number} pid - 查询标段
  638. * @param {Number} uid - 查询人
  639. * @param {Date} time - 查询时间
  640. * @returns {Promise<*>}
  641. */
  642. async getNoticeStage(pid, uid, time) {
  643. const sql = 'SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  644. ' s.`order` As `s_order`, s.`status` As `s_status`, ' +
  645. ' sa.`aid`, sa.`times`, sa.`order`, sa.`end_time`, sa.`tid`, sa.`sid`, sa.`status`, ' +
  646. ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  647. ' FROM ?? As t' +
  648. ' LEFT JOIN ?? As s On t.`id` = s.`tid`' +
  649. ' LEFT JOIN ?? As sa ON s.`id` = sa.`sid`' +
  650. ' LEFT JOIN ?? As pa ON sa.`aid` = pa.`id`' +
  651. ' WHERE sa.`aid` <> ? and sa.`end_time` > ? and t.`project_id` = ?' +
  652. ' GROUP By t.`id`' +
  653. ' ORDER By sa.`end_time`';
  654. const sqlParam = [this.ctx.service.tender.tableName, this.ctx.service.stage.tableName, this.tableName,
  655. this.ctx.service.projectAccount.tableName, uid, time, pid];
  656. return await this.db.query(sql, sqlParam);
  657. }
  658. /**
  659. * 获取审核人流程列表
  660. *
  661. * @param auditorId
  662. * @returns {Promise<*>}
  663. */
  664. async getAuditGroupByList(stageId, times) {
  665. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  666. 'FROM ?? AS la, ?? AS pa ' +
  667. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`';
  668. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  669. return await this.db.query(sql, sqlParam);
  670. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  671. // const sqlParam = [this.tableName, stageId, times];
  672. // return await this.db.query(sql, sqlParam);
  673. }
  674. /**
  675. * 复制上一期的审批人列表给最新一期
  676. *
  677. * @param transaction - 新增一期的事务
  678. * @param {Object} preStage - 上一期
  679. * @param {Object} newStage - 最新一期
  680. * @returns {Promise<*>}
  681. */
  682. async copyPreStageAuditors(transaction, preStage, newStage) {
  683. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  684. const newAuditors = [];
  685. for (const a of auditors) {
  686. const na = {
  687. tid: preStage.tid,
  688. sid: newStage.id,
  689. aid: a.aid,
  690. times: newStage.times,
  691. order: newAuditors.length + 1,
  692. status: auditConst.status.uncheck
  693. };
  694. newAuditors.push(na);
  695. }
  696. const result = await transaction.insert(this.tableName, newAuditors);
  697. return result.effectRows = auditors.length;
  698. }
  699. /**
  700. * 移除审核人
  701. *
  702. * @param {Number} stageId - 期id
  703. * @param {Number} status - 期状态
  704. * @param {Number} status - 期次数
  705. * @return {Promise<boolean>}
  706. */
  707. async getAuditorByStatus(stageId, status, times = 1) {
  708. let auditor = null;
  709. let sql = '';
  710. let sqlParam = '';
  711. switch (status) {
  712. case auditConst.status.checking :
  713. case auditConst.status.checked :
  714. case auditConst.status.checkNoPre :
  715. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  716. 'FROM ?? AS la, ?? AS pa ' +
  717. 'WHERE la.`sid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  718. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  719. auditor = await this.db.queryOne(sql, sqlParam);
  720. break;
  721. case auditConst.status.checkNo :
  722. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  723. 'FROM ?? AS la, ?? AS pa ' +
  724. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  725. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  726. auditor = await this.db.queryOne(sql, sqlParam);
  727. break;
  728. case auditConst.status.uncheck :
  729. default:break;
  730. }
  731. return auditor;
  732. }
  733. /**
  734. * 取某一期已批准审核信息(报表用)
  735. *
  736. * @param {Number} stageId - 期id
  737. * @param {Number} times - 期次数
  738. * @return {Promise<boolean>}
  739. */
  740. async getStageAudit(stageId, times = 1) {
  741. const sql = 'SELECT a1.aid, a1.end_time, a1.opinion ' +
  742. 'FROM ?? AS a1 ' +
  743. 'WHERE a1.`sid` = ? and a1.`status` = 3 and a1.`times` = ?';
  744. const sqlParam = [this.tableName, stageId, times];
  745. const rst = await this.db.query(sql, sqlParam);
  746. return rst;
  747. }
  748. }
  749. return StageAudit;
  750. };