revise_audit.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. // 下一人
  247. // await this.ctx.helper.sendUserSms(audit.audit_id, smsTypeConst.const.XD,
  248. // smsTypeConst.judge.approval.toString(), '台账修订需要您审批,请登录系统处理。');
  249. await this.ctx.helper.sendAliSms(audit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), SmsAliConst.template.revise_check);
  250. // 微信模板通知
  251. const wechatData = {
  252. status: wxConst.status.check,
  253. tips: wxConst.tips.check,
  254. begin_time: Date.parse(time),
  255. };
  256. await this.ctx.helper.sendWechat(audit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), wxConst.template.revise, wechatData);
  257. // 其他参与人
  258. const auditList = await this.getAuditors(revise.id, times);
  259. const users = this._.pull(this._.map(auditList, 'user_id'), audit.audit_id);
  260. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
  261. // smsTypeConst.judge.result.toString(), '台账修订已上报。');
  262. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_report);
  263. // 微信模板通知
  264. const wechatData2 = {
  265. status: wxConst.status.report,
  266. tips: wxConst.tips.report,
  267. begin_time: Date.parse(time),
  268. };
  269. await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  270. await transaction.commit();
  271. } catch (err) {
  272. await transaction.rollback();
  273. throw err;
  274. }
  275. return true;
  276. }
  277. async _replaceLedgerByRevise(transaction, revise) {
  278. const sqlParam = [revise.tid];
  279. await transaction.delete(this.ctx.service.ledger.tableName, { tender_id: revise.tid });
  280. const bSql =
  281. 'Insert Into ' +
  282. this.ctx.service.ledger.tableName +
  283. ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  284. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  285. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id, is_tp,' +
  286. ' sgfh_expr, sjcl_expr, qtcl_expr, gxby_status, dagl_status)' +
  287. ' Select id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  288. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  289. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id, is_tp, ' +
  290. ' sgfh_expr, sjcl_expr, qtcl_expr, gxby_status, dagl_status' +
  291. ' From ' +
  292. this.ctx.service.reviseBills.tableName +
  293. ' Where `tender_id` = ?';
  294. await transaction.query(bSql, sqlParam);
  295. await transaction.delete(this.ctx.service.pos.tableName, { tid: revise.tid });
  296. const pSql =
  297. 'Insert Into ' +
  298. this.ctx.service.pos.tableName +
  299. ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  300. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder, position, ' +
  301. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty, gxby_status, dagl_status)' +
  302. ' Select id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  303. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder, position,' +
  304. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty, gxby_status, dagl_status' +
  305. ' From ' +
  306. this.ctx.service.revisePos.tableName +
  307. ' Where `tid` = ?';
  308. await transaction.query(pSql, sqlParam);
  309. }
  310. /**
  311. * 审批
  312. * @param {Object} revise - 修订
  313. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  314. * @param {String} opinion - 审批意见
  315. * @param {Number} times - 第几次审批
  316. * @return {Promise<void>}
  317. */
  318. async check(revise, checkType, opinion, times = 1) {
  319. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) throw '提交数据错误';
  320. const audit = await this.getDataByCondition({
  321. rid: revise.id,
  322. times,
  323. status: auditConst.status.checking,
  324. });
  325. if (!audit) throw '审核数据错误';
  326. const pid = this.ctx.session.sessionProject.id;
  327. const transaction = await this.db.beginTransaction();
  328. try {
  329. const auditList = await this.getAuditors(revise.id, times);
  330. // 审核通过添加到推送表
  331. const noticeContent = await this.getNoticeContent(pid, audit.tender_id, audit.rid, audit.audit_id);
  332. const records = [
  333. {
  334. pid,
  335. type: pushType.revise,
  336. uid: revise.uid,
  337. status: checkType,
  338. content: noticeContent,
  339. },
  340. ];
  341. auditList.forEach(audit => {
  342. records.push({
  343. pid,
  344. type: pushType.revise,
  345. uid: audit.audit_id,
  346. status: checkType,
  347. content: noticeContent,
  348. });
  349. });
  350. await transaction.insert('zh_notice', records);
  351. // 整理当前流程审核人状态更新
  352. const time = new Date();
  353. // 更新当前审核流程
  354. await transaction.update(this.tableName, {
  355. id: audit.id,
  356. status: checkType,
  357. opinion,
  358. end_time: time,
  359. });
  360. if (checkType === auditConst.status.checked) {
  361. const nextAudit = await this.getDataByCondition({
  362. rid: revise.id,
  363. times,
  364. audit_order: audit.audit_order + 1,
  365. });
  366. // 无下一审核人表示,审核结束
  367. if (nextAudit) {
  368. await transaction.update(this.tableName, {
  369. id: nextAudit.id,
  370. status: auditConst.status.checking,
  371. begin_time: time,
  372. });
  373. // 短信通知-需要审批提醒功能
  374. // 下一人
  375. // await this.ctx.helper.sendUserSms(nextAudit.user_id, smsTypeConst.const.XD,
  376. // smsTypeConst.judge.approval.toString(), '台账修订需要您审批,请登录系统处理。');
  377. await this.ctx.helper.sendAliSms(nextAudit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), SmsAliConst.template.revise_check);
  378. // 微信模板通知
  379. const wechatData = {
  380. status: wxConst.status.check,
  381. tips: wxConst.tips.check,
  382. begin_time: Date.parse(revise.begin_time),
  383. };
  384. await this.ctx.helper.sendWechat(nextAudit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), wxConst.template.revise, wechatData);
  385. // 其他参与人
  386. const users = this._.pull(this._.map(auditList, 'audit_id'), audit.audit_id);
  387. users.push(revise.uid);
  388. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
  389. // smsTypeConst.judge.result.toString(), '台账修订审批通过。');
  390. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result2, {
  391. status: SmsAliConst.status.success,
  392. });
  393. // 微信模板通知
  394. // const wechatData2 = {
  395. // status: wxConst.status.success,
  396. // tips: wxConst.tips.success,
  397. // begin_time: Date.parse(revise.begin_time),
  398. // };
  399. // await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  400. } else {
  401. // 同步修订信息
  402. await transaction.update(this.ctx.service.ledgerRevise.tableName, {
  403. id: revise.id,
  404. status: checkType,
  405. end_time: time,
  406. });
  407. // 最新一期跟台账相关的缓存数据应过期
  408. const lastStage = await this.ctx.service.stage.getLastestStage(revise.tid, true);
  409. const cacheTime = new Date();
  410. if (lastStage) {
  411. await transaction.update(this.ctx.service.stage.tableName, {
  412. id: lastStage.id,
  413. cache_time_l: cacheTime,
  414. cache_time_r: cacheTime,
  415. });
  416. }
  417. // 拷贝修订数据至台账
  418. await this._replaceLedgerByRevise(transaction, revise);
  419. const sum = await this.ctx.service.reviseBills.addUp({
  420. tender_id: revise.tid, /* , is_leaf: true*/
  421. });
  422. await transaction.update(this.ctx.service.tender.tableName, {
  423. id: revise.tid,
  424. total_price: sum.total_price,
  425. deal_tp: sum.deal_tp,
  426. });
  427. // 短信通知-审批通过提醒功能
  428. // 下一人
  429. // const msg = '台账修订审批通过,请登录系统处理。';
  430. // await this.ctx.helper.sendUserSms(revise.uid, smsTypeConst.const.XD,
  431. // smsTypeConst.judge.result.toString(), msg);
  432. await this.ctx.helper.sendAliSms(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result, {
  433. status: SmsAliConst.status.success,
  434. });
  435. // 其他参与人
  436. const users = this._.pull(this._.map(auditList, 'audit_id'), revise.uid);
  437. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
  438. // smsTypeConst.judge.result.toString(), '台账修订审批通过。');
  439. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result2, {
  440. status: SmsAliConst.status.success,
  441. });
  442. users.push(revise.uid);
  443. // 微信模板通知
  444. const wechatData2 = {
  445. status: wxConst.status.success,
  446. tips: wxConst.tips.success,
  447. begin_time: Date.parse(revise.begin_time),
  448. };
  449. await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  450. }
  451. } else {
  452. // 同步修订信息
  453. await transaction.update(this.ctx.service.ledgerRevise.tableName, {
  454. id: revise.id,
  455. times: times + 1,
  456. status: checkType,
  457. });
  458. // 拷贝新一次审核流程列表
  459. const auditors = await this.getAllDataByCondition({
  460. where: { rid: revise.id, times },
  461. columns: ['tender_id', 'rid', 'audit_order', 'audit_id'],
  462. });
  463. for (const a of auditors) {
  464. a.times = times + 1;
  465. a.status = auditConst.status.uncheck;
  466. a.in_time = time;
  467. }
  468. await transaction.insert(this.tableName, auditors);
  469. // 短信通知-审批退回提醒功能
  470. // 下一人
  471. // await this.ctx.helper.sendUserSms(revise.uid, smsTypeConst.const.XD,
  472. // smsTypeConst.judge.result.toString(), '台账修订审批退回,请登录系统处理。');
  473. await this.ctx.helper.sendAliSms(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result, {
  474. status: SmsAliConst.status.back,
  475. });
  476. // 微信模板通知
  477. const wechatData = {
  478. status: wxConst.status.back,
  479. tips: wxConst.tips.back,
  480. begin_time: Date.parse(revise.begin_time),
  481. };
  482. await this.ctx.helper.sendWechat(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData);
  483. // 其他参与人
  484. // await this.ctx.helper.sendUserSms(this._.map(auditors, 'user_id'),
  485. // smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), '台账修订审批退回。');
  486. await this.ctx.helper.sendAliSms(
  487. this._.map(auditors, 'user_id'),
  488. smsTypeConst.const.XD,
  489. smsTypeConst.judge.result.toString(),
  490. SmsAliConst.template.revise_result2,
  491. {
  492. status: SmsAliConst.status.back,
  493. }
  494. );
  495. // 微信模板通知
  496. const wechatData2 = {
  497. status: wxConst.status.back,
  498. tips: wxConst.tips.back,
  499. begin_time: Date.parse(revise.begin_time),
  500. };
  501. await this.ctx.helper.sendWechat(this._.map(auditors, 'user_id'), smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  502. }
  503. await transaction.commit();
  504. } catch (err) {
  505. await transaction.rollback();
  506. throw err;
  507. }
  508. }
  509. /**
  510. * 获取审核人需要审核的标段列表
  511. *
  512. * @param auditorId
  513. * @return {Promise<*>}
  514. */
  515. async getAuditRevise(auditorId) {
  516. const sql =
  517. 'SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`begin_time`, ra.`end_time`,' +
  518. ' r.id, r.corder, r.uid, r.status, r.content,' +
  519. ' 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, ' +
  520. ' p.name As audit_name, p.role As audit_role, p.company As audit_company' +
  521. ' FROM ' + this.tableName + ' AS ra' +
  522. ' Left Join ' + this.ctx.service.ledgerRevise.tableName + ' As r On ra.rid = r.id' +
  523. ' Left Join ' + this.ctx.service.tender.tableName + ' AS t On r.tid = t.id' +
  524. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' As p On ra.audit_id = p.id' +
  525. ' WHERE r.`valid` != 0 and ((ra.`audit_id` = ? and ra.`status` = ?) OR' +
  526. ' (r.`uid` = ? and r.`status` = ? and ra.`status` = ? and ra.`times` = (r.`times`-1))) ORDER BY ra.`begin_time` DESC';
  527. const sqlParam = [auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  528. return await this.db.query(sql, sqlParam);
  529. }
  530. /**
  531. * 获取 某时间后 审批进度 更新的台账
  532. * @param {Integer} pid - 项目id
  533. * @param {Integer} uid - 查询人id
  534. * @param {Date} noticeTime - 查询事件
  535. * @return {Promise<*>}
  536. */
  537. async getNoticeRevise(pid, uid, noticeTime) {
  538. // const sql = 'SELECT * FROM (SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`end_time`, ra.`status`,' +
  539. // ' r.id, r.corder, r.uid, r.status As `r_status`, r.content, ' +
  540. // ' 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, ' +
  541. // ' pa.name As `ru_name`, pa.role As `ru_role`, pa.company As `ru_company`' +
  542. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tender_id` FROM ?? WHERE `audit_id` = ? GROUP BY `tender_id`)) As t' +
  543. // ' LEFT JOIN ' + this.ctx.service.ledgerRevise.tableName + ' As r ON r.`tid` = t.`id`' +
  544. // ' LEFT JOIN ' + this.tableName + ' As ra ON ra.rid = r.id' +
  545. // ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON ra.`audit_id` = pa.`id`' +
  546. // ' WHERE ra.end_time > ? and t.project_id = ?' +
  547. // ' ORDER By ra.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id`' +
  548. // ' ORDER By new_t.`end_time`';
  549. // const sqlParam = [this.ctx.service.tender.tableName, auditorId, this.tableName, auditorId, noticeTime, projectId];
  550. // return await this.db.query(sql, sqlParam);
  551. let notice = await this.db.select('zh_notice', {
  552. where: { pid, type: pushType.revise, uid },
  553. orders: [['create_time', 'desc']],
  554. limit: 10,
  555. offset: 0,
  556. });
  557. notice = notice.map(v => {
  558. const extra = JSON.parse(v.content);
  559. delete v.content;
  560. return { ...v, ...extra };
  561. });
  562. return notice;
  563. }
  564. /**
  565. * 用于添加推送所需的content内容
  566. * @param {Number} pid 项目id
  567. * @param {Number} tid 台账id
  568. * @param {Number} rid 修订id
  569. * @param {Number} uid 审核人id
  570. */
  571. async getNoticeContent(pid, tid, rid, uid) {
  572. const noticeSql =
  573. 'SELECT * FROM (SELECT ' +
  574. ' t.`id` As `tid`, t.`name`, r.`corder`, pa.`name` As `su_name`, pa.role As `su_role`' +
  575. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  576. ' LEFT JOIN ?? As r On r.`id` = ? ' +
  577. ' LEFT JOIN ?? As pa ON pa.`id` = ? ' +
  578. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  579. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.ledgerRevise.tableName, rid, this.ctx.service.projectAccount.tableName, uid, pid];
  580. const content = await this.db.query(noticeSql, noticeSqlParam);
  581. return content.length ? JSON.stringify(content[0]) : '';
  582. }
  583. /**
  584. * 获取最新的审批人状态
  585. *
  586. * @param {Number} rid - 修订id
  587. * @param {Number} status - 修订状态
  588. * @param {Number} times - 修订次数
  589. * @return {Promise<boolean>}
  590. */
  591. async getAuditorByStatus(rid, status, times = 1) {
  592. let auditor = null;
  593. let sql = '';
  594. let sqlParam = '';
  595. switch (status) {
  596. case auditConst.status.checking:
  597. case auditConst.status.checked:
  598. case auditConst.status.checkNoPre:
  599. sql =
  600. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  601. 'FROM ?? AS la, ?? AS pa ' +
  602. 'WHERE la.`rid` = ? and la.`status` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`audit_order` desc';
  603. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, status];
  604. auditor = await this.db.queryOne(sql, sqlParam);
  605. break;
  606. case auditConst.status.checkNo:
  607. sql =
  608. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  609. 'FROM ?? AS la, ?? AS pa ' +
  610. 'WHERE la.`rid` = ? and la.`status` = ? and la.`times` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`audit_order` desc';
  611. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, auditConst.status.checkNo, parseInt(times) - 1];
  612. auditor = await this.db.queryOne(sql, sqlParam);
  613. break;
  614. case auditConst.status.uncheck:
  615. default:
  616. break;
  617. }
  618. return auditor;
  619. }
  620. /**
  621. * 获取审核人流程列表
  622. *
  623. * @param auditorId
  624. * @return {Promise<*>}
  625. */
  626. async getAuditGroupByList(rid, times) {
  627. const sql =
  628. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  629. 'FROM ?? AS la, ?? AS pa ' +
  630. 'WHERE la.`rid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
  631. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, times];
  632. return await this.db.query(sql, sqlParam);
  633. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  634. // const sqlParam = [this.tableName, stageId, times];
  635. // return await this.db.query(sql, sqlParam);
  636. }
  637. /**
  638. * 获取审核人流程列表(包括原报)
  639. * @param {Number} rid 修订id
  640. * @param {Number} times 审核次数
  641. * @return {Promise<Array>} 查询结果集(包括原报)
  642. */
  643. async getAuditorsWithOwner(rid, times = 1) {
  644. const result = await this.getAuditGroupByList(rid, times);
  645. const sql =
  646. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As rid, 0 As `order`' +
  647. ' FROM ' +
  648. this.ctx.service.ledgerRevise.tableName +
  649. ' As s' +
  650. ' LEFT JOIN ' +
  651. this.ctx.service.projectAccount.tableName +
  652. ' As pa' +
  653. ' ON s.uid = pa.id' +
  654. ' WHERE s.id = ?';
  655. const sqlParam = [times, rid, rid];
  656. const user = await this.db.queryOne(sql, sqlParam);
  657. result.unshift(user);
  658. return result;
  659. }
  660. async getAllAuditors(tenderId) {
  661. const sql =
  662. 'SELECT ra.audit_id, ra.tender_id FROM ' +
  663. this.tableName +
  664. ' ra' +
  665. ' LEFT JOIN ' +
  666. this.ctx.service.tender.tableName +
  667. ' t On ra.tender_id = t.id' +
  668. ' WHERE t.id = ?' +
  669. ' GROUP BY ra.audit_id';
  670. const sqlParam = [tenderId];
  671. return this.db.query(sql, sqlParam);
  672. }
  673. async updateNewAuditList(revise, newIdList) {
  674. const transaction = await this.db.beginTransaction();
  675. try {
  676. // 先删除旧的审批流,再添加新的
  677. await transaction.delete(this.tableName, { rid: revise.id, times: revise.times });
  678. const newAuditors = [];
  679. let order = 1;
  680. for (const aid of newIdList) {
  681. newAuditors.push({
  682. tender_id: revise.tid, audit_id: aid,
  683. times: revise.times, audit_order: order, status: auditConst.status.uncheck,
  684. rid: revise.id,in_time: new Date(),
  685. });
  686. order++;
  687. }
  688. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  689. await transaction.commit();
  690. } catch (err) {
  691. await transaction.rollback();
  692. throw err;
  693. }
  694. }
  695. async updateLastAudit(revise, auditList, lastId) {
  696. const transaction = await this.db.beginTransaction();
  697. try {
  698. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  699. const idList = this._.map(auditList, 'audit_id');
  700. let order = idList.length + 1;
  701. if (idList.indexOf(lastId) !== -1) {
  702. await transaction.delete(this.tableName, { rid: revise.id, times: revise.times, audit_id: lastId });
  703. const audit = this._.find(auditList, { 'audit_id': lastId });
  704. // 顺移之后审核人流程顺序
  705. await this._syncOrderByDelete(transaction, revise.id, audit.audit_order, revise.times);
  706. order = order - 1;
  707. }
  708. // 添加终审
  709. const newAuditor = {
  710. tender_id: revise.tid, audit_id: lastId,
  711. times: revise.times, audit_order: order, status: auditConst.status.uncheck,
  712. rid: revise.id, in_time: new Date(),
  713. };
  714. await transaction.insert(this.tableName, newAuditor);
  715. await transaction.commit();
  716. } catch (err) {
  717. await transaction.rollback();
  718. throw err;
  719. }
  720. }
  721. }
  722. return ReviseAudit;
  723. };