stage_audit.js 45 KB

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