stage_audit.js 42 KB

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