stage_audit.js 47 KB

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