revise_audit.js 33 KB

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