revise_audit.js 41 KB

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