revise_audit.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').revise;
  10. const smsTypeConst = require('../const/sms_type');
  11. const SmsAliConst = require('../const/sms_alitemplate');
  12. const wxConst = require('../const/wechat_template');
  13. const shenpiConst = require('../const/shenpi');
  14. const pushType = require('../const/audit').pushType;
  15. module.exports = app => {
  16. class ReviseAudit extends app.BaseService {
  17. /**
  18. * 构造函数
  19. *
  20. * @param {Object} ctx - egg全局变量
  21. * @return {void}
  22. */
  23. constructor(ctx) {
  24. super(ctx);
  25. this.tableName = 'revise_audit';
  26. }
  27. /**
  28. * 获取标段审核人信息
  29. *
  30. * @param {Number} reviseId - 修订id
  31. * @param {Number} auditorId - 审核人id
  32. * @param {Number} times - 第几次审批
  33. * @return {Promise<*>}
  34. */
  35. async getAuditor(reviseId, auditorId, times = 1) {
  36. const sql =
  37. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  38. 'FROM ?? AS la, ?? AS pa ' +
  39. 'WHERE la.`rid` = ? and la.`audit_id` = ? and la.`times` = ?' +
  40. ' and la.`audit_id` = pa.`id`';
  41. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, reviseId, auditorId, times];
  42. return await this.db.queryOne(sql, sqlParam);
  43. }
  44. /**
  45. * 获取标段审核列表信息
  46. *
  47. * @param {Number} reviseId - 修订id
  48. * @param {Number} times - 第几次审批
  49. * @return {Promise<*>}
  50. */
  51. async getAuditors(reviseId, times = 1) {
  52. const sql =
  53. 'SELECT la.`audit_id`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`,' +
  54. ' pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`' +
  55. ' FROM ' +
  56. this.tableName +
  57. ' AS la ' +
  58. ' INNER JOIN ' +
  59. this.ctx.service.projectAccount.tableName +
  60. ' AS pa ON la.`rid` = ? and la.`times` = ? and la.`audit_id` = pa.`id`' +
  61. ' ORDER BY la.`audit_order`';
  62. const sqlParam = [reviseId, times];
  63. return await this.db.query(sql, sqlParam);
  64. }
  65. /**
  66. * 获取 审核列表信息(修订列表页审批流程用)
  67. *
  68. * @param {Number} rid - 修订id
  69. * @param {Number} times - 第几次审批
  70. * @return {Promise<*>}
  71. */
  72. async getAuditors2ReviseList(rid, times = 1) {
  73. const sql =
  74. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, g.`sort` ' +
  75. 'FROM ?? AS la, ?? AS pa, (SELECT `audit_id`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `rid` = ? AND `times` = ? GROUP BY `audit_id`) as g ' +
  76. 'WHERE la.`rid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` and g.`audit_id` = la.`audit_id` order by la.`audit_order`';
  77. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, rid, times, rid, times];
  78. const result = await this.db.query(sql, sqlParam);
  79. const sql2 = 'SELECT COUNT(a.`audit_id`) as num FROM (SELECT `audit_id` FROM ?? WHERE `rid` = ? AND `times` = ? GROUP BY `audit_id`) as a';
  80. const sqlParam2 = [this.tableName, rid, times];
  81. const count = await this.db.queryOne(sql2, sqlParam2);
  82. for (const i in result) {
  83. result[i].max_sort = count.num;
  84. }
  85. return result;
  86. }
  87. /**
  88. * 获取标段当前审核人
  89. *
  90. * @param {Number} reviseId - 修订id
  91. * @param {Number} times - 第几次审批
  92. * @return {Promise<*>}
  93. */
  94. async getCurAuditor(reviseId, times = 1) {
  95. const sql =
  96. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  97. 'FROM ?? AS la, ?? AS pa ' +
  98. 'WHERE la.`rid` = ? and la.`status` = ? and la.`times` = ?' +
  99. ' and la.`audit_id` = pa.`id`';
  100. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, reviseId, auditConst.status.checking, times];
  101. return await this.db.queryOne(sql, sqlParam);
  102. }
  103. /**
  104. * 获取最新审核顺序
  105. *
  106. * @param {Number} reviseId - 修订id
  107. * @param {Number} times - 第几次审批
  108. * @return {Promise<number>}
  109. */
  110. async getNewOrder(reviseId, times = 1) {
  111. const sql = 'SELECT Max(??) As max_order FROM ?? Where `rid` = ? and `times` = ?';
  112. const sqlParam = ['audit_order', this.tableName, reviseId, times];
  113. const result = await this.db.queryOne(sql, sqlParam);
  114. return result && result.max_order ? result.max_order + 1 : 1;
  115. }
  116. /**
  117. * 新增审核人
  118. *
  119. * @param {Object} revise - 修订
  120. * @param {Number} auditorId - 审核人id
  121. * @param {Number} times - 第几次审批
  122. * @return {Promise<number>}
  123. */
  124. async addAuditor(revise, auditorId, is_gdzs = 0) {
  125. const transaction = await this.db.beginTransaction();
  126. try {
  127. const times = revise.times ? revise.times : 1;
  128. let newOrder = await this.getNewOrder(revise.id, times);
  129. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  130. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  131. if (is_gdzs) await this._syncOrderByDelete(transaction, revise.id, newOrder, times, '+');
  132. const data = {
  133. tender_id: revise.tid,
  134. audit_id: auditorId,
  135. times,
  136. audit_order: newOrder,
  137. status: auditConst.status.uncheck,
  138. rid: revise.id,
  139. in_time: new Date(),
  140. };
  141. const result = await transaction.insert(this.tableName, data);
  142. await transaction.commit();
  143. return (result.effectRows = 1);
  144. } catch (err) {
  145. await transaction.rollback();
  146. throw err;
  147. }
  148. return false;
  149. }
  150. /**
  151. * 移除审核人时,同步其后审核人order
  152. * @param transaction - 事务
  153. * @param {Number} reviseId - 修订id
  154. * @param {Number} auditorId - 审核人id
  155. * @param {Number} times - 第几次审批
  156. * @return {Promise<*>}
  157. * @private
  158. */
  159. async _syncOrderByDelete(transaction, reviseId, order, times, selfOperate = '-') {
  160. this.initSqlBuilder();
  161. this.sqlBuilder.setAndWhere('rid', {
  162. value: this.db.escape(reviseId),
  163. operate: '=',
  164. });
  165. this.sqlBuilder.setAndWhere('audit_order', {
  166. value: order,
  167. operate: '>=',
  168. });
  169. this.sqlBuilder.setAndWhere('times', {
  170. value: times,
  171. operate: '=',
  172. });
  173. this.sqlBuilder.setUpdateData('audit_order', {
  174. value: 1,
  175. selfOperate: selfOperate,
  176. });
  177. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  178. const data = await transaction.query(sql, sqlParam);
  179. return data;
  180. }
  181. /**
  182. * 移除审核人
  183. *
  184. * @param {Object} revise - 修订
  185. * @param {Number} auditorId - 审核人id
  186. * @param {Number} times - 第几次审批
  187. * @return {Promise<boolean>}
  188. */
  189. async deleteAuditor(revise, auditorId) {
  190. const times = revise.times ? revise.times : 1;
  191. const transaction = await this.db.beginTransaction();
  192. try {
  193. const condition = { rid: revise.id, audit_id: auditorId, times };
  194. const auditor = await this.getDataByCondition(condition);
  195. if (!auditor) {
  196. throw '该审核人不存在';
  197. }
  198. await this._syncOrderByDelete(transaction, revise.id, auditor.audit_order, times);
  199. await transaction.delete(this.tableName, condition);
  200. await transaction.commit();
  201. } catch (err) {
  202. await transaction.rollback();
  203. throw err;
  204. }
  205. return true;
  206. }
  207. /**
  208. * 开始审批
  209. *
  210. * @param {Object} revise - 修订
  211. * @param {Number} times - 第几次审批
  212. * @return {Promise<boolean>}
  213. */
  214. async start(revise, times = 1) {
  215. const audit = await this.getDataByCondition({ rid: revise.id, times, audit_order: 1 });
  216. if (!audit) {
  217. if(this.ctx.tender.info.shenpi.revise === shenpiConst.sp_status.gdspl) {
  218. throw '请联系管理员添加审批人';
  219. } else {
  220. throw '请先选择审批人,再上报数据';
  221. }
  222. }
  223. const time = new Date();
  224. // 拷贝备份台账数据
  225. const [billsHis, posHis] = await this.ctx.service.ledgerRevise.backupReviseHistoryFile(revise);
  226. const transaction = await this.db.beginTransaction();
  227. try {
  228. await transaction.update(this.tableName, {
  229. id: audit.id,
  230. status: auditConst.status.checking,
  231. begin_time: time,
  232. bills_file: revise.bills_file,
  233. pos_file: revise.pos_file,
  234. });
  235. const reviseData = {
  236. id: revise.id,
  237. status: auditConst.status.checking,
  238. bills_file: billsHis,
  239. pos_file: posHis,
  240. };
  241. if (revise.times === 1) {
  242. reviseData.begin_time = time;
  243. }
  244. await transaction.update(this.ctx.service.ledgerRevise.tableName, reviseData);
  245. // 形象进度改变状态
  246. await transaction.update(this.ctx.service.schedule.tableName, { revising: 1 }, { where: { tid: this.ctx.tender.id } });
  247. // 添加短信通知-需要审批提醒功能
  248. // 下一人
  249. // await this.ctx.helper.sendUserSms(audit.audit_id, smsTypeConst.const.XD,
  250. // smsTypeConst.judge.approval.toString(), '台账修订需要您审批,请登录系统处理。');
  251. await this.ctx.helper.sendAliSms(audit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), SmsAliConst.template.revise_check);
  252. // 微信模板通知
  253. const wechatData = {
  254. status: wxConst.status.check,
  255. tips: wxConst.tips.check,
  256. begin_time: Date.parse(time),
  257. };
  258. await this.ctx.helper.sendWechat(audit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), wxConst.template.revise, wechatData);
  259. // 其他参与人
  260. const auditList = await this.getAuditors(revise.id, times);
  261. const users = this._.pull(this._.map(auditList, 'user_id'), audit.audit_id);
  262. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
  263. // smsTypeConst.judge.result.toString(), '台账修订已上报。');
  264. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_report);
  265. // 微信模板通知
  266. const wechatData2 = {
  267. status: wxConst.status.report,
  268. tips: wxConst.tips.report,
  269. begin_time: Date.parse(time),
  270. };
  271. await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  272. await transaction.commit();
  273. } catch (err) {
  274. await transaction.rollback();
  275. throw err;
  276. }
  277. return true;
  278. }
  279. async _replaceLedgerByRevise(transaction, revise) {
  280. const sqlParam = [revise.tid];
  281. await transaction.delete(this.ctx.service.ledger.tableName, { tender_id: revise.tid });
  282. const bSql =
  283. 'Insert Into ' +
  284. this.ctx.service.ledger.tableName +
  285. ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  286. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  287. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id, is_tp,' +
  288. ' sgfh_expr, sjcl_expr, qtcl_expr, check_calc,' +
  289. ' gxby_status, dagl_status, dagl_url, gxby_limit, gxby_ratio, dagl_limit, dagl_ratio)' +
  290. ' Select id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  291. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  292. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id, is_tp, ' +
  293. ' sgfh_expr, sjcl_expr, qtcl_expr, check_calc,' +
  294. ' gxby_status, dagl_status, dagl_url, gxby_limit, gxby_ratio, dagl_limit, dagl_ratio' +
  295. ' From ' +
  296. this.ctx.service.reviseBills.tableName +
  297. ' Where `tender_id` = ?';
  298. await transaction.query(bSql, sqlParam);
  299. await transaction.delete(this.ctx.service.pos.tableName, { tid: revise.tid });
  300. const pSql =
  301. 'Insert Into ' +
  302. this.ctx.service.pos.tableName +
  303. ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  304. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder, position, ' +
  305. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
  306. ' gxby_status, dagl_status, dagl_url, gxby_limit, gxby_ratio, dagl_limit, dagl_ratio)' +
  307. ' Select id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  308. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder, position,' +
  309. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
  310. ' gxby_status, dagl_status, dagl_url, gxby_limit, gxby_ratio, dagl_limit, dagl_ratio' +
  311. ' From ' +
  312. this.ctx.service.revisePos.tableName +
  313. ' Where `tid` = ?';
  314. await transaction.query(pSql, sqlParam);
  315. }
  316. /**
  317. * 审批
  318. * @param {Object} revise - 修订
  319. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  320. * @param {String} opinion - 审批意见
  321. * @param {Number} times - 第几次审批
  322. * @return {Promise<void>}
  323. */
  324. async check(revise, checkType, opinion, times = 1) {
  325. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) throw '提交数据错误';
  326. const audit = await this.getDataByCondition({
  327. rid: revise.id,
  328. times,
  329. status: auditConst.status.checking,
  330. });
  331. if (!audit) throw '审核数据错误';
  332. const pid = this.ctx.session.sessionProject.id;
  333. const transaction = await this.db.beginTransaction();
  334. try {
  335. const auditList = await this.getAuditors(revise.id, times);
  336. // 审核通过添加到推送表
  337. const noticeContent = await this.getNoticeContent(pid, audit.tender_id, audit.rid, audit.audit_id);
  338. const records = [
  339. {
  340. pid,
  341. type: pushType.revise,
  342. uid: revise.uid,
  343. status: checkType,
  344. content: noticeContent,
  345. },
  346. ];
  347. auditList.forEach(audit => {
  348. records.push({
  349. pid,
  350. type: pushType.revise,
  351. uid: audit.audit_id,
  352. status: checkType,
  353. content: noticeContent,
  354. });
  355. });
  356. await transaction.insert('zh_notice', records);
  357. // 整理当前流程审核人状态更新
  358. const time = new Date();
  359. // 更新当前审核流程
  360. await transaction.update(this.tableName, {
  361. id: audit.id,
  362. status: checkType,
  363. opinion,
  364. end_time: time,
  365. });
  366. if (checkType === auditConst.status.checked) {
  367. const nextAudit = await this.getDataByCondition({
  368. rid: revise.id,
  369. times,
  370. audit_order: audit.audit_order + 1,
  371. });
  372. // 无下一审核人表示,审核结束
  373. if (nextAudit) {
  374. await transaction.update(this.tableName, {
  375. id: nextAudit.id,
  376. status: auditConst.status.checking,
  377. begin_time: time,
  378. });
  379. // 短信通知-需要审批提醒功能
  380. // 下一人
  381. // await this.ctx.helper.sendUserSms(nextAudit.user_id, smsTypeConst.const.XD,
  382. // smsTypeConst.judge.approval.toString(), '台账修订需要您审批,请登录系统处理。');
  383. await this.ctx.helper.sendAliSms(nextAudit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), SmsAliConst.template.revise_check);
  384. // 微信模板通知
  385. const wechatData = {
  386. status: wxConst.status.check,
  387. tips: wxConst.tips.check,
  388. begin_time: Date.parse(revise.begin_time),
  389. };
  390. await this.ctx.helper.sendWechat(nextAudit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), wxConst.template.revise, wechatData);
  391. // 其他参与人
  392. const users = this._.pull(this._.map(auditList, 'audit_id'), audit.audit_id);
  393. users.push(revise.uid);
  394. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
  395. // smsTypeConst.judge.result.toString(), '台账修订审批通过。');
  396. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result2, {
  397. status: SmsAliConst.status.success,
  398. });
  399. // 微信模板通知
  400. // const wechatData2 = {
  401. // status: wxConst.status.success,
  402. // tips: wxConst.tips.success,
  403. // begin_time: Date.parse(revise.begin_time),
  404. // };
  405. // await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  406. } else {
  407. // 同步修订信息
  408. await transaction.update(this.ctx.service.ledgerRevise.tableName, {
  409. id: revise.id,
  410. status: checkType,
  411. end_time: time,
  412. });
  413. await this.ctx.service.tenderTag.saveTenderTag(revise.tid, {revise_time: new Date()}, transaction);
  414. // 最新一期跟台账相关的缓存数据应过期
  415. const lastStage = await this.ctx.service.stage.getLastestStage(revise.tid, true);
  416. const cacheTime = new Date();
  417. if (lastStage) {
  418. await transaction.update(this.ctx.service.stage.tableName, {
  419. id: lastStage.id,
  420. cache_time_l: cacheTime,
  421. cache_time_r: cacheTime,
  422. });
  423. }
  424. // 拷贝修订数据至台账
  425. await this._replaceLedgerByRevise(transaction, revise);
  426. const sum = await this.ctx.service.reviseBills.addUp({
  427. tender_id: revise.tid, /* , is_leaf: true*/
  428. });
  429. await transaction.update(this.ctx.service.tender.tableName, {
  430. id: revise.tid,
  431. total_price: sum.total_price,
  432. deal_tp: sum.deal_tp,
  433. });
  434. // 短信通知-审批通过提醒功能
  435. // 下一人
  436. // const msg = '台账修订审批通过,请登录系统处理。';
  437. // await this.ctx.helper.sendUserSms(revise.uid, smsTypeConst.const.XD,
  438. // smsTypeConst.judge.result.toString(), msg);
  439. await this.ctx.helper.sendAliSms(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result, {
  440. status: SmsAliConst.status.success,
  441. });
  442. // 其他参与人
  443. const users = this._.pull(this._.map(auditList, 'audit_id'), revise.uid);
  444. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
  445. // smsTypeConst.judge.result.toString(), '台账修订审批通过。');
  446. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result2, {
  447. status: SmsAliConst.status.success,
  448. });
  449. users.push(revise.uid);
  450. // 微信模板通知
  451. const wechatData2 = {
  452. status: wxConst.status.success,
  453. tips: wxConst.tips.success,
  454. begin_time: Date.parse(revise.begin_time),
  455. };
  456. await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  457. }
  458. } else {
  459. // 同步修订信息
  460. await transaction.update(this.ctx.service.ledgerRevise.tableName, {
  461. id: revise.id,
  462. times: times + 1,
  463. status: checkType,
  464. });
  465. // 拷贝新一次审核流程列表
  466. const auditors = await this.getAllDataByCondition({
  467. where: { rid: revise.id, times },
  468. columns: ['tender_id', 'rid', 'audit_order', 'audit_id'],
  469. });
  470. for (const a of auditors) {
  471. a.times = times + 1;
  472. a.status = auditConst.status.uncheck;
  473. a.in_time = time;
  474. }
  475. await transaction.insert(this.tableName, auditors);
  476. // 短信通知-审批退回提醒功能
  477. // 下一人
  478. // await this.ctx.helper.sendUserSms(revise.uid, smsTypeConst.const.XD,
  479. // smsTypeConst.judge.result.toString(), '台账修订审批退回,请登录系统处理。');
  480. await this.ctx.helper.sendAliSms(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result, {
  481. status: SmsAliConst.status.back,
  482. });
  483. // 微信模板通知
  484. const wechatData = {
  485. status: wxConst.status.back,
  486. tips: wxConst.tips.back,
  487. begin_time: Date.parse(revise.begin_time),
  488. };
  489. await this.ctx.helper.sendWechat(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData);
  490. // 其他参与人
  491. // await this.ctx.helper.sendUserSms(this._.map(auditors, 'user_id'),
  492. // smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), '台账修订审批退回。');
  493. await this.ctx.helper.sendAliSms(
  494. this._.map(auditors, 'user_id'),
  495. smsTypeConst.const.XD,
  496. smsTypeConst.judge.result.toString(),
  497. SmsAliConst.template.revise_result2,
  498. {
  499. status: SmsAliConst.status.back,
  500. }
  501. );
  502. // 微信模板通知
  503. const wechatData2 = {
  504. status: wxConst.status.back,
  505. tips: wxConst.tips.back,
  506. begin_time: Date.parse(revise.begin_time),
  507. };
  508. await this.ctx.helper.sendWechat(this._.map(auditors, 'user_id'), smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  509. }
  510. await transaction.commit();
  511. } catch (err) {
  512. await transaction.rollback();
  513. throw err;
  514. }
  515. }
  516. /**
  517. * 获取审核人需要审核的标段列表
  518. *
  519. * @param auditorId
  520. * @return {Promise<*>}
  521. */
  522. async getAuditRevise(auditorId) {
  523. const sql =
  524. 'SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`begin_time`, ra.`end_time`,' +
  525. ' r.id, r.corder, r.uid, r.status, r.content,' +
  526. ' t.id As t_id, t.`name` As t_name, t.`project_id` As t_pid, t.`type` As t_type, t.`user_id` As t_uid, t.`status` As t_status, ' +
  527. ' p.name As audit_name, p.role As audit_role, p.company As audit_company' +
  528. ' FROM ' + this.tableName + ' AS ra' +
  529. ' Left Join ' + this.ctx.service.ledgerRevise.tableName + ' As r On ra.rid = r.id' +
  530. ' Left Join ' + this.ctx.service.tender.tableName + ' AS t On r.tid = t.id' +
  531. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' As p On ra.audit_id = p.id' +
  532. ' WHERE r.`valid` != 0 and ((ra.`audit_id` = ? and ra.`status` = ?) OR' +
  533. ' (r.`uid` = ? and r.`status` = ? and ra.`status` = ? and ra.`times` = (r.`times`-1))) ORDER BY ra.`begin_time` DESC';
  534. const sqlParam = [auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  535. return await this.db.query(sql, sqlParam);
  536. }
  537. /**
  538. * 获取 某时间后 审批进度 更新的台账
  539. * @param {Integer} pid - 项目id
  540. * @param {Integer} uid - 查询人id
  541. * @param {Date} noticeTime - 查询事件
  542. * @return {Promise<*>}
  543. */
  544. async getNoticeRevise(pid, uid, noticeTime) {
  545. // const sql = 'SELECT * FROM (SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`end_time`, ra.`status`,' +
  546. // ' r.id, r.corder, r.uid, r.status As `r_status`, r.content, ' +
  547. // ' t.`id` As t_id, t.`name` As t_name, t.`project_id` As t_pid, t.`type` As t_type, t.`user_id` As t_uid, ' +
  548. // ' pa.name As `ru_name`, pa.role As `ru_role`, pa.company As `ru_company`' +
  549. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tender_id` FROM ?? WHERE `audit_id` = ? GROUP BY `tender_id`)) As t' +
  550. // ' LEFT JOIN ' + this.ctx.service.ledgerRevise.tableName + ' As r ON r.`tid` = t.`id`' +
  551. // ' LEFT JOIN ' + this.tableName + ' As ra ON ra.rid = r.id' +
  552. // ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON ra.`audit_id` = pa.`id`' +
  553. // ' WHERE ra.end_time > ? and t.project_id = ?' +
  554. // ' ORDER By ra.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id`' +
  555. // ' ORDER By new_t.`end_time`';
  556. // const sqlParam = [this.ctx.service.tender.tableName, auditorId, this.tableName, auditorId, noticeTime, projectId];
  557. // return await this.db.query(sql, sqlParam);
  558. let notice = await this.db.select('zh_notice', {
  559. where: { pid, type: pushType.revise, uid },
  560. orders: [['create_time', 'desc']],
  561. limit: 10,
  562. offset: 0,
  563. });
  564. notice = notice.map(v => {
  565. const extra = JSON.parse(v.content);
  566. delete v.content;
  567. return { ...v, ...extra };
  568. });
  569. return notice;
  570. }
  571. /**
  572. * 用于添加推送所需的content内容
  573. * @param {Number} pid 项目id
  574. * @param {Number} tid 台账id
  575. * @param {Number} rid 修订id
  576. * @param {Number} uid 审核人id
  577. */
  578. async getNoticeContent(pid, tid, rid, uid) {
  579. const noticeSql =
  580. 'SELECT * FROM (SELECT ' +
  581. ' t.`id` As `tid`, t.`name`, r.`corder`, pa.`name` As `su_name`, pa.role As `su_role`' +
  582. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  583. ' LEFT JOIN ?? As r On r.`id` = ? ' +
  584. ' LEFT JOIN ?? As pa ON pa.`id` = ? ' +
  585. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  586. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.ledgerRevise.tableName, rid, this.ctx.service.projectAccount.tableName, uid, pid];
  587. const content = await this.db.query(noticeSql, noticeSqlParam);
  588. return content.length ? JSON.stringify(content[0]) : '';
  589. }
  590. /**
  591. * 获取最新的审批人状态
  592. *
  593. * @param {Number} rid - 修订id
  594. * @param {Number} status - 修订状态
  595. * @param {Number} times - 修订次数
  596. * @return {Promise<boolean>}
  597. */
  598. async getAuditorByStatus(rid, status, times = 1) {
  599. let auditor = null;
  600. let sql = '';
  601. let sqlParam = '';
  602. switch (status) {
  603. case auditConst.status.checking:
  604. case auditConst.status.checked:
  605. case auditConst.status.checkNoPre:
  606. sql =
  607. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  608. 'FROM ?? AS la, ?? AS pa ' +
  609. 'WHERE la.`rid` = ? and la.`status` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`audit_order` desc';
  610. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, status];
  611. auditor = await this.db.queryOne(sql, sqlParam);
  612. break;
  613. case auditConst.status.checkNo:
  614. sql =
  615. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  616. 'FROM ?? AS la, ?? AS pa ' +
  617. 'WHERE la.`rid` = ? and la.`status` = ? and la.`times` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`audit_order` desc';
  618. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, auditConst.status.checkNo, parseInt(times) - 1];
  619. auditor = await this.db.queryOne(sql, sqlParam);
  620. break;
  621. case auditConst.status.uncheck:
  622. default:
  623. break;
  624. }
  625. return auditor;
  626. }
  627. /**
  628. * 获取审核人流程列表
  629. *
  630. * @param auditorId
  631. * @return {Promise<*>}
  632. */
  633. async getAuditGroupByList(rid, times) {
  634. const sql =
  635. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  636. 'FROM ?? AS la, ?? AS pa ' +
  637. 'WHERE la.`rid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
  638. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, times];
  639. return await this.db.query(sql, sqlParam);
  640. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  641. // const sqlParam = [this.tableName, stageId, times];
  642. // return await this.db.query(sql, sqlParam);
  643. }
  644. /**
  645. * 获取审核人流程列表(包括原报)
  646. * @param {Number} rid 修订id
  647. * @param {Number} times 审核次数
  648. * @return {Promise<Array>} 查询结果集(包括原报)
  649. */
  650. async getAuditorsWithOwner(rid, times = 1) {
  651. const result = await this.getAuditGroupByList(rid, times);
  652. const sql =
  653. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As rid, 0 As `order`' +
  654. ' FROM ' +
  655. this.ctx.service.ledgerRevise.tableName +
  656. ' As s' +
  657. ' LEFT JOIN ' +
  658. this.ctx.service.projectAccount.tableName +
  659. ' As pa' +
  660. ' ON s.uid = pa.id' +
  661. ' WHERE s.id = ?';
  662. const sqlParam = [times, rid, rid];
  663. const user = await this.db.queryOne(sql, sqlParam);
  664. result.unshift(user);
  665. return result;
  666. }
  667. async getAllAuditors(tenderId) {
  668. const sql =
  669. 'SELECT ra.audit_id, ra.tender_id FROM ' +
  670. this.tableName +
  671. ' ra' +
  672. ' LEFT JOIN ' +
  673. this.ctx.service.tender.tableName +
  674. ' t On ra.tender_id = t.id' +
  675. ' WHERE t.id = ?' +
  676. ' GROUP BY ra.audit_id';
  677. const sqlParam = [tenderId];
  678. return this.db.query(sql, sqlParam);
  679. }
  680. async updateNewAuditList(revise, newIdList) {
  681. const transaction = await this.db.beginTransaction();
  682. try {
  683. // 先删除旧的审批流,再添加新的
  684. await transaction.delete(this.tableName, { rid: revise.id, times: revise.times });
  685. const newAuditors = [];
  686. let order = 1;
  687. for (const aid of newIdList) {
  688. newAuditors.push({
  689. tender_id: revise.tid, audit_id: aid,
  690. times: revise.times, audit_order: order, status: auditConst.status.uncheck,
  691. rid: revise.id,in_time: new Date(),
  692. });
  693. order++;
  694. }
  695. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  696. await transaction.commit();
  697. } catch (err) {
  698. await transaction.rollback();
  699. throw err;
  700. }
  701. }
  702. async updateLastAudit(revise, auditList, lastId) {
  703. const transaction = await this.db.beginTransaction();
  704. try {
  705. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  706. const idList = this._.map(auditList, 'audit_id');
  707. let order = idList.length + 1;
  708. if (idList.indexOf(lastId) !== -1) {
  709. await transaction.delete(this.tableName, { rid: revise.id, times: revise.times, audit_id: lastId });
  710. const audit = this._.find(auditList, { 'audit_id': lastId });
  711. // 顺移之后审核人流程顺序
  712. await this._syncOrderByDelete(transaction, revise.id, audit.audit_order, revise.times);
  713. order = order - 1;
  714. }
  715. // 添加终审
  716. const newAuditor = {
  717. tender_id: revise.tid, audit_id: lastId,
  718. times: revise.times, audit_order: order, status: auditConst.status.uncheck,
  719. rid: revise.id, in_time: new Date(),
  720. };
  721. await transaction.insert(this.tableName, newAuditor);
  722. await transaction.commit();
  723. } catch (err) {
  724. await transaction.rollback();
  725. throw err;
  726. }
  727. }
  728. }
  729. return ReviseAudit;
  730. };