stage_audit.js 48 KB

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