revise_audit.js 37 KB

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