stage_audit.js 47 KB

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