stage_audit.js 44 KB

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