stage_audit.js 44 KB

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