revise_audit.js 34 KB

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