revise_audit.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. await this.ctx.service.tenderTag.saveTenderTag(revise.tid, {revise_time: new Date()}, transaction);
  408. // 最新一期跟台账相关的缓存数据应过期
  409. const lastStage = await this.ctx.service.stage.getLastestStage(revise.tid, true);
  410. const cacheTime = new Date();
  411. if (lastStage) {
  412. await transaction.update(this.ctx.service.stage.tableName, {
  413. id: lastStage.id,
  414. cache_time_l: cacheTime,
  415. cache_time_r: cacheTime,
  416. });
  417. }
  418. // 拷贝修订数据至台账
  419. await this._replaceLedgerByRevise(transaction, revise);
  420. const sum = await this.ctx.service.reviseBills.addUp({
  421. tender_id: revise.tid, /* , is_leaf: true*/
  422. });
  423. await transaction.update(this.ctx.service.tender.tableName, {
  424. id: revise.tid,
  425. total_price: sum.total_price,
  426. deal_tp: sum.deal_tp,
  427. });
  428. // 短信通知-审批通过提醒功能
  429. // 下一人
  430. // const msg = '台账修订审批通过,请登录系统处理。';
  431. // await this.ctx.helper.sendUserSms(revise.uid, smsTypeConst.const.XD,
  432. // smsTypeConst.judge.result.toString(), msg);
  433. await this.ctx.helper.sendAliSms(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result, {
  434. status: SmsAliConst.status.success,
  435. });
  436. // 其他参与人
  437. const users = this._.pull(this._.map(auditList, 'audit_id'), revise.uid);
  438. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
  439. // smsTypeConst.judge.result.toString(), '台账修订审批通过。');
  440. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result2, {
  441. status: SmsAliConst.status.success,
  442. });
  443. users.push(revise.uid);
  444. // 微信模板通知
  445. const wechatData2 = {
  446. status: wxConst.status.success,
  447. tips: wxConst.tips.success,
  448. begin_time: Date.parse(revise.begin_time),
  449. };
  450. await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  451. }
  452. } else {
  453. // 同步修订信息
  454. await transaction.update(this.ctx.service.ledgerRevise.tableName, {
  455. id: revise.id,
  456. times: times + 1,
  457. status: checkType,
  458. });
  459. // 拷贝新一次审核流程列表
  460. const auditors = await this.getAllDataByCondition({
  461. where: { rid: revise.id, times },
  462. columns: ['tender_id', 'rid', 'audit_order', 'audit_id'],
  463. });
  464. for (const a of auditors) {
  465. a.times = times + 1;
  466. a.status = auditConst.status.uncheck;
  467. a.in_time = time;
  468. }
  469. await transaction.insert(this.tableName, auditors);
  470. // 短信通知-审批退回提醒功能
  471. // 下一人
  472. // await this.ctx.helper.sendUserSms(revise.uid, smsTypeConst.const.XD,
  473. // smsTypeConst.judge.result.toString(), '台账修订审批退回,请登录系统处理。');
  474. await this.ctx.helper.sendAliSms(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result, {
  475. status: SmsAliConst.status.back,
  476. });
  477. // 微信模板通知
  478. const wechatData = {
  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(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData);
  484. // 其他参与人
  485. // await this.ctx.helper.sendUserSms(this._.map(auditors, 'user_id'),
  486. // smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), '台账修订审批退回。');
  487. await this.ctx.helper.sendAliSms(
  488. this._.map(auditors, 'user_id'),
  489. smsTypeConst.const.XD,
  490. smsTypeConst.judge.result.toString(),
  491. SmsAliConst.template.revise_result2,
  492. {
  493. status: SmsAliConst.status.back,
  494. }
  495. );
  496. // 微信模板通知
  497. const wechatData2 = {
  498. status: wxConst.status.back,
  499. tips: wxConst.tips.back,
  500. begin_time: Date.parse(revise.begin_time),
  501. };
  502. await this.ctx.helper.sendWechat(this._.map(auditors, 'user_id'), smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  503. }
  504. await transaction.commit();
  505. } catch (err) {
  506. await transaction.rollback();
  507. throw err;
  508. }
  509. }
  510. /**
  511. * 获取审核人需要审核的标段列表
  512. *
  513. * @param auditorId
  514. * @return {Promise<*>}
  515. */
  516. async getAuditRevise(auditorId) {
  517. const sql =
  518. 'SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`begin_time`, ra.`end_time`,' +
  519. ' r.id, r.corder, r.uid, r.status, r.content,' +
  520. ' 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, ' +
  521. ' p.name As audit_name, p.role As audit_role, p.company As audit_company' +
  522. ' FROM ' + this.tableName + ' AS ra' +
  523. ' Left Join ' + this.ctx.service.ledgerRevise.tableName + ' As r On ra.rid = r.id' +
  524. ' Left Join ' + this.ctx.service.tender.tableName + ' AS t On r.tid = t.id' +
  525. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' As p On ra.audit_id = p.id' +
  526. ' WHERE r.`valid` != 0 and ((ra.`audit_id` = ? and ra.`status` = ?) OR' +
  527. ' (r.`uid` = ? and r.`status` = ? and ra.`status` = ? and ra.`times` = (r.`times`-1))) ORDER BY ra.`begin_time` DESC';
  528. const sqlParam = [auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  529. return await this.db.query(sql, sqlParam);
  530. }
  531. /**
  532. * 获取 某时间后 审批进度 更新的台账
  533. * @param {Integer} pid - 项目id
  534. * @param {Integer} uid - 查询人id
  535. * @param {Date} noticeTime - 查询事件
  536. * @return {Promise<*>}
  537. */
  538. async getNoticeRevise(pid, uid, noticeTime) {
  539. // const sql = 'SELECT * FROM (SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`end_time`, ra.`status`,' +
  540. // ' r.id, r.corder, r.uid, r.status As `r_status`, r.content, ' +
  541. // ' 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, ' +
  542. // ' pa.name As `ru_name`, pa.role As `ru_role`, pa.company As `ru_company`' +
  543. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tender_id` FROM ?? WHERE `audit_id` = ? GROUP BY `tender_id`)) As t' +
  544. // ' LEFT JOIN ' + this.ctx.service.ledgerRevise.tableName + ' As r ON r.`tid` = t.`id`' +
  545. // ' LEFT JOIN ' + this.tableName + ' As ra ON ra.rid = r.id' +
  546. // ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON ra.`audit_id` = pa.`id`' +
  547. // ' WHERE ra.end_time > ? and t.project_id = ?' +
  548. // ' ORDER By ra.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id`' +
  549. // ' ORDER By new_t.`end_time`';
  550. // const sqlParam = [this.ctx.service.tender.tableName, auditorId, this.tableName, auditorId, noticeTime, projectId];
  551. // return await this.db.query(sql, sqlParam);
  552. let notice = await this.db.select('zh_notice', {
  553. where: { pid, type: pushType.revise, uid },
  554. orders: [['create_time', 'desc']],
  555. limit: 10,
  556. offset: 0,
  557. });
  558. notice = notice.map(v => {
  559. const extra = JSON.parse(v.content);
  560. delete v.content;
  561. return { ...v, ...extra };
  562. });
  563. return notice;
  564. }
  565. /**
  566. * 用于添加推送所需的content内容
  567. * @param {Number} pid 项目id
  568. * @param {Number} tid 台账id
  569. * @param {Number} rid 修订id
  570. * @param {Number} uid 审核人id
  571. */
  572. async getNoticeContent(pid, tid, rid, uid) {
  573. const noticeSql =
  574. 'SELECT * FROM (SELECT ' +
  575. ' t.`id` As `tid`, t.`name`, r.`corder`, pa.`name` As `su_name`, pa.role As `su_role`' +
  576. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  577. ' LEFT JOIN ?? As r On r.`id` = ? ' +
  578. ' LEFT JOIN ?? As pa ON pa.`id` = ? ' +
  579. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  580. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.ledgerRevise.tableName, rid, this.ctx.service.projectAccount.tableName, uid, pid];
  581. const content = await this.db.query(noticeSql, noticeSqlParam);
  582. return content.length ? JSON.stringify(content[0]) : '';
  583. }
  584. /**
  585. * 获取最新的审批人状态
  586. *
  587. * @param {Number} rid - 修订id
  588. * @param {Number} status - 修订状态
  589. * @param {Number} times - 修订次数
  590. * @return {Promise<boolean>}
  591. */
  592. async getAuditorByStatus(rid, status, times = 1) {
  593. let auditor = null;
  594. let sql = '';
  595. let sqlParam = '';
  596. switch (status) {
  597. case auditConst.status.checking:
  598. case auditConst.status.checked:
  599. case auditConst.status.checkNoPre:
  600. sql =
  601. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  602. 'FROM ?? AS la, ?? AS pa ' +
  603. 'WHERE la.`rid` = ? and la.`status` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`audit_order` desc';
  604. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, status];
  605. auditor = await this.db.queryOne(sql, sqlParam);
  606. break;
  607. case auditConst.status.checkNo:
  608. sql =
  609. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  610. 'FROM ?? AS la, ?? AS pa ' +
  611. 'WHERE la.`rid` = ? and la.`status` = ? and la.`times` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`audit_order` desc';
  612. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, auditConst.status.checkNo, parseInt(times) - 1];
  613. auditor = await this.db.queryOne(sql, sqlParam);
  614. break;
  615. case auditConst.status.uncheck:
  616. default:
  617. break;
  618. }
  619. return auditor;
  620. }
  621. /**
  622. * 获取审核人流程列表
  623. *
  624. * @param auditorId
  625. * @return {Promise<*>}
  626. */
  627. async getAuditGroupByList(rid, times) {
  628. const sql =
  629. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  630. 'FROM ?? AS la, ?? AS pa ' +
  631. 'WHERE la.`rid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
  632. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, times];
  633. return await this.db.query(sql, sqlParam);
  634. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  635. // const sqlParam = [this.tableName, stageId, times];
  636. // return await this.db.query(sql, sqlParam);
  637. }
  638. /**
  639. * 获取审核人流程列表(包括原报)
  640. * @param {Number} rid 修订id
  641. * @param {Number} times 审核次数
  642. * @return {Promise<Array>} 查询结果集(包括原报)
  643. */
  644. async getAuditorsWithOwner(rid, times = 1) {
  645. const result = await this.getAuditGroupByList(rid, times);
  646. const sql =
  647. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As rid, 0 As `order`' +
  648. ' FROM ' +
  649. this.ctx.service.ledgerRevise.tableName +
  650. ' As s' +
  651. ' LEFT JOIN ' +
  652. this.ctx.service.projectAccount.tableName +
  653. ' As pa' +
  654. ' ON s.uid = pa.id' +
  655. ' WHERE s.id = ?';
  656. const sqlParam = [times, rid, rid];
  657. const user = await this.db.queryOne(sql, sqlParam);
  658. result.unshift(user);
  659. return result;
  660. }
  661. async getAllAuditors(tenderId) {
  662. const sql =
  663. 'SELECT ra.audit_id, ra.tender_id FROM ' +
  664. this.tableName +
  665. ' ra' +
  666. ' LEFT JOIN ' +
  667. this.ctx.service.tender.tableName +
  668. ' t On ra.tender_id = t.id' +
  669. ' WHERE t.id = ?' +
  670. ' GROUP BY ra.audit_id';
  671. const sqlParam = [tenderId];
  672. return this.db.query(sql, sqlParam);
  673. }
  674. async updateNewAuditList(revise, newIdList) {
  675. const transaction = await this.db.beginTransaction();
  676. try {
  677. // 先删除旧的审批流,再添加新的
  678. await transaction.delete(this.tableName, { rid: revise.id, times: revise.times });
  679. const newAuditors = [];
  680. let order = 1;
  681. for (const aid of newIdList) {
  682. newAuditors.push({
  683. tender_id: revise.tid, audit_id: aid,
  684. times: revise.times, audit_order: order, status: auditConst.status.uncheck,
  685. rid: revise.id,in_time: new Date(),
  686. });
  687. order++;
  688. }
  689. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  690. await transaction.commit();
  691. } catch (err) {
  692. await transaction.rollback();
  693. throw err;
  694. }
  695. }
  696. async updateLastAudit(revise, auditList, lastId) {
  697. const transaction = await this.db.beginTransaction();
  698. try {
  699. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  700. const idList = this._.map(auditList, 'audit_id');
  701. let order = idList.length + 1;
  702. if (idList.indexOf(lastId) !== -1) {
  703. await transaction.delete(this.tableName, { rid: revise.id, times: revise.times, audit_id: lastId });
  704. const audit = this._.find(auditList, { 'audit_id': lastId });
  705. // 顺移之后审核人流程顺序
  706. await this._syncOrderByDelete(transaction, revise.id, audit.audit_order, revise.times);
  707. order = order - 1;
  708. }
  709. // 添加终审
  710. const newAuditor = {
  711. tender_id: revise.tid, audit_id: lastId,
  712. times: revise.times, audit_order: order, status: auditConst.status.uncheck,
  713. rid: revise.id, in_time: new Date(),
  714. };
  715. await transaction.insert(this.tableName, newAuditor);
  716. await transaction.commit();
  717. } catch (err) {
  718. await transaction.rollback();
  719. throw err;
  720. }
  721. }
  722. }
  723. return ReviseAudit;
  724. };