stage_audit.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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` ORDER BY `id` ASC';
  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. await transaction.update(this.ctx.service.stage.tableName, {
  411. id: stageId, status: checkData.checkType,
  412. });
  413. // 添加短信通知-需要审批提醒功能
  414. const smsUser = await this.ctx.service.projectAccount.getDataById(preAuditor.aid);
  415. if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  416. const smsType = JSON.parse(smsUser.sms_type);
  417. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  418. const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  419. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  420. const sms = new SMS(this.ctx);
  421. const tenderName = await sms.contentChange(tenderInfo.name);
  422. const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  423. sms.send(smsUser.auth_mobile, content);
  424. }
  425. }
  426. await transaction.commit();
  427. } catch(err) {
  428. await transaction.rollback();
  429. throw err;
  430. }
  431. }
  432. /**
  433. * 审批
  434. * @param {Number} stageId - 标段id
  435. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  436. * @param {Number} times - 第几次审批
  437. * @returns {Promise<void>}
  438. */
  439. async check(stageId, checkData, times = 1) {
  440. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  441. throw '提交数据错误';
  442. }
  443. // // 整理当前流程审核人状态更新
  444. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  445. // if (!audit) {
  446. // throw '审核数据错误';
  447. // }
  448. //const time = new Date();
  449. switch (checkData.checkType) {
  450. case auditConst.status.checked:
  451. await this._checked(stageId, checkData, times);
  452. break;
  453. case auditConst.status.checkNo:
  454. await this._checkNo(stageId, checkData, times);
  455. break;
  456. case auditConst.status.checkNoPre:
  457. await this._checkNoPre(stageId, checkData, times);
  458. break;
  459. default:
  460. throw '无效审批操作';
  461. }
  462. // const transaction = await this.db.beginTransaction();
  463. // try {
  464. // // 更新当前审核流程
  465. // await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  466. // if (checkData.checkType === auditConst.status.checked) { // 审批通过
  467. // const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  468. // // 无下一审核人表示,审核结束
  469. // if (nextAudit) {
  470. // // 计算该审批人最终数据
  471. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  472. // // 复制一份下一审核人数据
  473. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  474. // // 流程至下一审批人
  475. // await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  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: auditConst.status.checking,
  480. // contract_tp: tpData.contract_tp,
  481. // qc_tp: tpData.qc_tp,
  482. // });
  483. // } else {
  484. // // 本期结束
  485. // // 生成截止本期数据 final数据
  486. // await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  487. // await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  488. // // 计算并合同支付最终数据
  489. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  490. // // 同步 期信息
  491. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  492. // await transaction.update(this.ctx.service.stage.tableName, {
  493. // id: stageId, status: checkData.checkType,
  494. // contract_tp: tpData.contract_tp,
  495. // qc_tp: tpData.qc_tp,
  496. // });
  497. // }
  498. // } else if (checkData.checkType === auditConst.status.checkNo) { // 审批退回 原报, times+1
  499. // // 同步 期信息
  500. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  501. // await transaction.update(this.ctx.service.stage.tableName, {
  502. // id: stageId, status: checkData.checkType,
  503. // contract_tp: tpData.contract_tp,
  504. // qc_tp: tpData.qc_tp,
  505. // times: times + 1,
  506. // });
  507. // // 拷贝新一次审核流程列表
  508. // // const auditors = await this.getAllDataByCondition({
  509. // // where: {sid: stageId, times: times},
  510. // // columns: ['tid', 'sid', 'aid', 'order']
  511. // // });
  512. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  513. // const sqlParam = [this.tableName, stageId, times];
  514. // const auditors = await this.db.query(sql, sqlParam);
  515. // let order = 1;
  516. // for (const a of auditors) {
  517. // a.times = times + 1;
  518. // a.order = order;
  519. // a.status = auditConst.status.uncheck;
  520. // order++;
  521. // }
  522. // await transaction.insert(this.tableName, auditors);
  523. // // 计算该审批人最终数据
  524. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  525. // // 复制一份最新数据给原报
  526. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  527. // } else if (checkData.checkType === auditConst.status.checkNoPre) { // 审批退回 上一审批人
  528. // // 同步 期信息
  529. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  530. // await transaction.update(this.ctx.service.stage.tableName, {
  531. // id: stageId, status: checkData.checkType,
  532. // contract_tp: tpData.contract_tp,
  533. // qc_tp: tpData.qc_tp,
  534. // });
  535. // // 将当前审批人 与 上一审批人再次添加至流程,顺移其后审批人流程顺序
  536. // if (audit.order > 1) {
  537. // // 顺移气候审核人流程顺序
  538. // this.initSqlBuilder();
  539. // this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  540. // this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  541. // this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  542. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  543. // const data = await transaction.query(sql, sqlParam);
  544. //
  545. // // 上一审批人,当前审批人 再次添加至流程
  546. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  547. // const newAuditors = [];
  548. // newAuditors.push({
  549. // tid: preAuditor.tid, sid: preAuditor.sid, aid: preAuditor.aid,
  550. // times: preAuditor.times, order: preAuditor.order + 2, status: auditConst.status.checking,
  551. // begin_time: time,
  552. // });
  553. // newAuditors.push({
  554. // tid: audit.tid, sid: audit.sid, aid: audit.aid,
  555. // times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck
  556. // });
  557. // await transaction.insert(this.tableName, newAuditors);
  558. //
  559. // // 计算该审批人最终数据
  560. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  561. // // 复制一份最新数据给上一人
  562. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  563. // } else {
  564. // throw '审核数据错误';
  565. // }
  566. // } else {
  567. // throw '无效审批操作';
  568. // }
  569. //
  570. // await transaction.commit();
  571. // } catch (err) {
  572. // await transaction.rollback();
  573. // throw err;
  574. // }
  575. }
  576. /**
  577. * 审批
  578. * @param {Number} stageId - 标段id
  579. * @param {Number} times - 第几次审批
  580. * @returns {Promise<void>}
  581. */
  582. async checkAgain(stageId, times = 1) {
  583. const time = new Date();
  584. // 整理当前流程审核人状态更新
  585. const audit = (await this.getAllDataByCondition({ where: { sid: stageId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  586. if (!audit || audit.order < 1) {
  587. throw '审核数据错误';
  588. }
  589. const transaction = await this.db.beginTransaction();
  590. try {
  591. // 当前审批人2次添加至流程中
  592. const newAuditors = [];
  593. newAuditors.push({
  594. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  595. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  596. begin_time: time, end_time: time, opinion: '',
  597. });
  598. newAuditors.push({
  599. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  600. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  601. begin_time: time,
  602. });
  603. await transaction.insert(this.tableName, newAuditors);
  604. // 复制一份最新数据给下一人
  605. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  606. // 本期结束
  607. // 生成截止本期数据 final数据
  608. await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  609. await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  610. // 同步 期信息
  611. await transaction.update(this.ctx.service.stage.tableName, {
  612. id: stageId, status: auditConst.status.checking,
  613. });
  614. // 添加短信通知-需要审批提醒功能
  615. const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  616. if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  617. const smsType = JSON.parse(smsUser.sms_type);
  618. if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  619. const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  620. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  621. const sms = new SMS(this.ctx);
  622. const tenderName = await sms.contentChange(tenderInfo.name);
  623. const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  624. sms.send(smsUser.auth_mobile, content);
  625. }
  626. }
  627. await transaction.commit();
  628. } catch (err) {
  629. await transaction.rollback();
  630. throw err;
  631. }
  632. }
  633. /**
  634. * 获取审核人需要审核的期列表
  635. *
  636. * @param auditorId
  637. * @returns {Promise<*>}
  638. */
  639. async getAuditStage(auditorId) {
  640. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  641. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  642. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  643. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  644. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  645. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id`';
  646. 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];
  647. return await this.db.query(sql, sqlParam);
  648. }
  649. /**
  650. * 获取 某时间后 审批进度 更新的期
  651. * @param {Number} pid - 查询标段
  652. * @param {Number} uid - 查询人
  653. * @param {Date} time - 查询时间
  654. * @returns {Promise<*>}
  655. */
  656. async getNoticeStage(pid, uid, time) {
  657. const sql = 'SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  658. ' s.`order` As `s_order`, s.`status` As `s_status`, ' +
  659. ' sa.`aid`, sa.`times`, sa.`order`, sa.`end_time`, sa.`tid`, sa.`sid`, sa.`status`, ' +
  660. ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  661. ' FROM ?? As t' +
  662. ' LEFT JOIN ?? As s On t.`id` = s.`tid`' +
  663. ' LEFT JOIN ?? As sa ON s.`id` = sa.`sid`' +
  664. ' LEFT JOIN ?? As pa ON sa.`aid` = pa.`id`' +
  665. ' WHERE sa.`aid` <> ? and sa.`end_time` > ? and t.`project_id` = ?' +
  666. ' GROUP By t.`id`' +
  667. ' ORDER By sa.`end_time`';
  668. const sqlParam = [this.ctx.service.tender.tableName, this.ctx.service.stage.tableName, this.tableName,
  669. this.ctx.service.projectAccount.tableName, uid, time, pid];
  670. return await this.db.query(sql, sqlParam);
  671. }
  672. /**
  673. * 获取审核人流程列表
  674. *
  675. * @param auditorId
  676. * @returns {Promise<*>}
  677. */
  678. async getAuditGroupByList(stageId, times) {
  679. const 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.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`';
  682. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  683. return await this.db.query(sql, sqlParam);
  684. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  685. // const sqlParam = [this.tableName, stageId, times];
  686. // return await this.db.query(sql, sqlParam);
  687. }
  688. /**
  689. * 复制上一期的审批人列表给最新一期
  690. *
  691. * @param transaction - 新增一期的事务
  692. * @param {Object} preStage - 上一期
  693. * @param {Object} newStage - 最新一期
  694. * @returns {Promise<*>}
  695. */
  696. async copyPreStageAuditors(transaction, preStage, newStage) {
  697. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  698. const newAuditors = [];
  699. for (const a of auditors) {
  700. const na = {
  701. tid: preStage.tid,
  702. sid: newStage.id,
  703. aid: a.aid,
  704. times: newStage.times,
  705. order: newAuditors.length + 1,
  706. status: auditConst.status.uncheck
  707. };
  708. newAuditors.push(na);
  709. }
  710. const result = await transaction.insert(this.tableName, newAuditors);
  711. return result.effectRows = auditors.length;
  712. }
  713. /**
  714. * 移除审核人
  715. *
  716. * @param {Number} stageId - 期id
  717. * @param {Number} status - 期状态
  718. * @param {Number} status - 期次数
  719. * @return {Promise<boolean>}
  720. */
  721. async getAuditorByStatus(stageId, status, times = 1) {
  722. let auditor = null;
  723. let sql = '';
  724. let sqlParam = '';
  725. switch (status) {
  726. case auditConst.status.checking :
  727. case auditConst.status.checked :
  728. case auditConst.status.checkNoPre :
  729. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  730. 'FROM ?? AS la, ?? AS pa ' +
  731. 'WHERE la.`sid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  732. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  733. auditor = await this.db.queryOne(sql, sqlParam);
  734. break;
  735. case auditConst.status.checkNo :
  736. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  737. 'FROM ?? AS la, ?? AS pa ' +
  738. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  739. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  740. auditor = await this.db.queryOne(sql, sqlParam);
  741. break;
  742. case auditConst.status.uncheck :
  743. default:break;
  744. }
  745. return auditor;
  746. }
  747. /**
  748. * 取某一期已批准审核信息(报表用)
  749. *
  750. * @param {Number} stageId - 期id
  751. * @param {Number} times - 期次数
  752. * @return {Promise<boolean>}
  753. */
  754. async getStageAudit(stageId, times = 1) {
  755. const sql = 'SELECT a1.aid, a1.end_time, a1.opinion ' +
  756. 'FROM ?? AS a1 ' +
  757. 'WHERE a1.`sid` = ? and a1.`status` = 3 and a1.`times` = ?';
  758. const sqlParam = [this.tableName, stageId, times];
  759. const rst = await this.db.query(sql, sqlParam);
  760. return rst;
  761. }
  762. }
  763. return StageAudit;
  764. };