revise_audit.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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.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(users, 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.audit_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.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), wxConst.template.revise, wechatData);
  367. // 其他参与人
  368. const users = this._.pull(this._.map(auditList, 'audit_id'), audit.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 users = this._.pull(this._.map(auditList, 'audit_id'), revise.uid);
  419. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
  420. // smsTypeConst.judge.result.toString(), '台账修订审批通过。');
  421. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result2, {
  422. status: SmsAliConst.status.success,
  423. });
  424. users.push(revise.uid);
  425. // 微信模板通知
  426. const wechatData2 = {
  427. status: wxConst.status.success,
  428. tips: wxConst.tips.success,
  429. begin_time: Date.parse(revise.begin_time),
  430. };
  431. await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  432. }
  433. } else {
  434. // 同步修订信息
  435. await transaction.update(this.ctx.service.ledgerRevise.tableName, {
  436. id: revise.id,
  437. times: times + 1,
  438. status: checkType,
  439. });
  440. // 拷贝新一次审核流程列表
  441. const auditors = await this.getAllDataByCondition({
  442. where: { rid: revise.id, times },
  443. columns: ['tender_id', 'rid', 'audit_order', 'audit_id'],
  444. });
  445. for (const a of auditors) {
  446. a.times = times + 1;
  447. a.status = auditConst.status.uncheck;
  448. a.in_time = time;
  449. }
  450. await transaction.insert(this.tableName, auditors);
  451. // 短信通知-审批退回提醒功能
  452. // 下一人
  453. // await this.ctx.helper.sendUserSms(revise.uid, smsTypeConst.const.XD,
  454. // smsTypeConst.judge.result.toString(), '台账修订审批退回,请登录系统处理。');
  455. await this.ctx.helper.sendAliSms(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result, {
  456. status: SmsAliConst.status.back,
  457. });
  458. // 微信模板通知
  459. const wechatData = {
  460. status: wxConst.status.back,
  461. tips: wxConst.tips.back,
  462. begin_time: Date.parse(revise.begin_time),
  463. };
  464. await this.ctx.helper.sendWechat(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData);
  465. // 其他参与人
  466. // await this.ctx.helper.sendUserSms(this._.map(auditors, 'user_id'),
  467. // smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), '台账修订审批退回。');
  468. await this.ctx.helper.sendAliSms(
  469. this._.map(auditors, 'user_id'),
  470. smsTypeConst.const.XD,
  471. smsTypeConst.judge.result.toString(),
  472. SmsAliConst.template.revise_result2,
  473. {
  474. status: SmsAliConst.status.back,
  475. }
  476. );
  477. // 微信模板通知
  478. const wechatData2 = {
  479. status: wxConst.status.back,
  480. tips: wxConst.tips.back,
  481. begin_time: Date.parse(revise.begin_time),
  482. };
  483. await this.ctx.helper.sendWechat(this._.map(auditors, 'user_id'), smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  484. }
  485. await transaction.commit();
  486. } catch (err) {
  487. await transaction.rollback();
  488. throw err;
  489. }
  490. }
  491. /**
  492. * 获取审核人需要审核的标段列表
  493. *
  494. * @param auditorId
  495. * @return {Promise<*>}
  496. */
  497. async getAuditRevise(auditorId) {
  498. const sql =
  499. 'SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`begin_time`, ra.`end_time`,' +
  500. ' r.id, r.corder, r.uid, r.status, r.content,' +
  501. ' 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, ' +
  502. ' p.name As audit_name, p.role As audit_role, p.company As audit_company' +
  503. ' FROM ' + this.tableName + ' AS ra' +
  504. ' Left Join ' + this.ctx.service.ledgerRevise.tableName + ' As r On ra.rid = r.id' +
  505. ' Left Join ' + this.ctx.service.tender.tableName + ' AS t On r.tid = t.id' +
  506. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' As p On ra.audit_id = p.id' +
  507. ' WHERE r.`valid` != 0 and ((ra.`audit_id` = ? and ra.`status` = ?) OR' +
  508. ' (r.`uid` = ? and r.`status` = ? and ra.`status` = ? and ra.`times` = (r.`times`-1))) ORDER BY ra.`begin_time` DESC';
  509. const sqlParam = [auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  510. return await this.db.query(sql, sqlParam);
  511. }
  512. /**
  513. * 获取 某时间后 审批进度 更新的台账
  514. * @param {Integer} pid - 项目id
  515. * @param {Integer} uid - 查询人id
  516. * @param {Date} noticeTime - 查询事件
  517. * @return {Promise<*>}
  518. */
  519. async getNoticeRevise(pid, uid, noticeTime) {
  520. // const sql = 'SELECT * FROM (SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`end_time`, ra.`status`,' +
  521. // ' r.id, r.corder, r.uid, r.status As `r_status`, r.content, ' +
  522. // ' 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, ' +
  523. // ' pa.name As `ru_name`, pa.role As `ru_role`, pa.company As `ru_company`' +
  524. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tender_id` FROM ?? WHERE `audit_id` = ? GROUP BY `tender_id`)) As t' +
  525. // ' LEFT JOIN ' + this.ctx.service.ledgerRevise.tableName + ' As r ON r.`tid` = t.`id`' +
  526. // ' LEFT JOIN ' + this.tableName + ' As ra ON ra.rid = r.id' +
  527. // ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON ra.`audit_id` = pa.`id`' +
  528. // ' WHERE ra.end_time > ? and t.project_id = ?' +
  529. // ' ORDER By ra.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id`' +
  530. // ' ORDER By new_t.`end_time`';
  531. // const sqlParam = [this.ctx.service.tender.tableName, auditorId, this.tableName, auditorId, noticeTime, projectId];
  532. // return await this.db.query(sql, sqlParam);
  533. let notice = await this.db.select('zh_notice', {
  534. where: { pid, type: pushType.revise, uid },
  535. orders: [['create_time', 'desc']],
  536. limit: 10,
  537. offset: 0,
  538. });
  539. notice = notice.map(v => {
  540. const extra = JSON.parse(v.content);
  541. delete v.content;
  542. return { ...v, ...extra };
  543. });
  544. return notice;
  545. }
  546. /**
  547. * 用于添加推送所需的content内容
  548. * @param {Number} pid 项目id
  549. * @param {Number} tid 台账id
  550. * @param {Number} rid 修订id
  551. * @param {Number} uid 审核人id
  552. */
  553. async getNoticeContent(pid, tid, rid, uid) {
  554. const noticeSql =
  555. 'SELECT * FROM (SELECT ' +
  556. ' t.`id` As `tid`, t.`name`, r.`corder`, pa.`name` As `su_name`, pa.role As `su_role`' +
  557. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  558. ' LEFT JOIN ?? As r On r.`id` = ? ' +
  559. ' LEFT JOIN ?? As pa ON pa.`id` = ? ' +
  560. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  561. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.ledgerRevise.tableName, rid, this.ctx.service.projectAccount.tableName, uid, pid];
  562. const content = await this.db.query(noticeSql, noticeSqlParam);
  563. return content.length ? JSON.stringify(content[0]) : '';
  564. }
  565. /**
  566. * 获取最新的审批人状态
  567. *
  568. * @param {Number} rid - 修订id
  569. * @param {Number} status - 修订状态
  570. * @param {Number} times - 修订次数
  571. * @return {Promise<boolean>}
  572. */
  573. async getAuditorByStatus(rid, status, times = 1) {
  574. let auditor = null;
  575. let sql = '';
  576. let sqlParam = '';
  577. switch (status) {
  578. case auditConst.status.checking:
  579. case auditConst.status.checked:
  580. case auditConst.status.checkNoPre:
  581. sql =
  582. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  583. 'FROM ?? AS la, ?? AS pa ' +
  584. 'WHERE la.`rid` = ? and la.`status` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`audit_order` desc';
  585. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, status];
  586. auditor = await this.db.queryOne(sql, sqlParam);
  587. break;
  588. case auditConst.status.checkNo:
  589. sql =
  590. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  591. 'FROM ?? AS la, ?? AS pa ' +
  592. 'WHERE la.`rid` = ? and la.`status` = ? and la.`times` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`audit_order` desc';
  593. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, auditConst.status.checkNo, parseInt(times) - 1];
  594. auditor = await this.db.queryOne(sql, sqlParam);
  595. break;
  596. case auditConst.status.uncheck:
  597. default:
  598. break;
  599. }
  600. return auditor;
  601. }
  602. /**
  603. * 获取审核人流程列表
  604. *
  605. * @param auditorId
  606. * @return {Promise<*>}
  607. */
  608. async getAuditGroupByList(rid, times) {
  609. const sql =
  610. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  611. 'FROM ?? AS la, ?? AS pa ' +
  612. 'WHERE la.`rid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
  613. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, times];
  614. return await this.db.query(sql, sqlParam);
  615. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  616. // const sqlParam = [this.tableName, stageId, times];
  617. // return await this.db.query(sql, sqlParam);
  618. }
  619. /**
  620. * 获取审核人流程列表(包括原报)
  621. * @param {Number} rid 修订id
  622. * @param {Number} times 审核次数
  623. * @return {Promise<Array>} 查询结果集(包括原报)
  624. */
  625. async getAuditorsWithOwner(rid, times = 1) {
  626. const result = await this.getAuditGroupByList(rid, times);
  627. const sql =
  628. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As rid, 0 As `order`' +
  629. ' FROM ' +
  630. this.ctx.service.ledgerRevise.tableName +
  631. ' As s' +
  632. ' LEFT JOIN ' +
  633. this.ctx.service.projectAccount.tableName +
  634. ' As pa' +
  635. ' ON s.uid = pa.id' +
  636. ' WHERE s.id = ?';
  637. const sqlParam = [times, rid, rid];
  638. const user = await this.db.queryOne(sql, sqlParam);
  639. result.unshift(user);
  640. return result;
  641. }
  642. async getAllAuditors(tenderId) {
  643. const sql =
  644. 'SELECT ra.audit_id, ra.tender_id FROM ' +
  645. this.tableName +
  646. ' ra' +
  647. ' LEFT JOIN ' +
  648. this.ctx.service.tender.tableName +
  649. ' t On ra.tender_id = t.id' +
  650. ' WHERE t.id = ?' +
  651. ' GROUP BY ra.audit_id';
  652. const sqlParam = [tenderId];
  653. return this.db.query(sql, sqlParam);
  654. }
  655. }
  656. return ReviseAudit;
  657. };