stage_audit.js 46 KB

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