stage_audit.js 45 KB

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