revise_audit.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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 Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  39. ' WHERE la.`rid` = ? and la.`audit_id` = ? and la.`times` = ?';
  40. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, reviseId, auditorId, times];
  41. return await this.db.queryOne(sql, sqlParam);
  42. }
  43. /**
  44. * 获取标段审核列表信息
  45. *
  46. * @param {Number} reviseId - 修订id
  47. * @param {Number} times - 第几次审批
  48. * @return {Promise<*>}
  49. */
  50. async getAuditors(reviseId, times = 1) {
  51. const sql =
  52. 'SELECT la.`audit_id`, la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`,' +
  53. ' pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`' +
  54. ' FROM ' + this.tableName + ' AS la ' +
  55. ' INNER JOIN ' + this.ctx.service.projectAccount.tableName +
  56. ' AS pa ON la.`rid` = ? and la.`times` = ? and la.`audit_id` = pa.`id`' +
  57. ' ORDER BY la.`audit_order`';
  58. const sqlParam = [reviseId, times];
  59. return await this.db.query(sql, sqlParam);
  60. }
  61. /**
  62. * 获取 审核列表信息(修订列表页审批流程用)
  63. *
  64. * @param {Number} rid - 修订id
  65. * @param {Number} times - 第几次审批
  66. * @return {Promise<*>}
  67. */
  68. async getAuditors2ReviseList(rid, times = 1) {
  69. const sql =
  70. '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` ' +
  71. '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 ' +
  72. 'WHERE la.`rid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` and g.`audit_id` = la.`audit_id` order by la.`audit_order`';
  73. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, rid, times, rid, times];
  74. const result = await this.db.query(sql, sqlParam);
  75. const sql2 = 'SELECT COUNT(a.`audit_id`) as num FROM (SELECT `audit_id` FROM ?? WHERE `rid` = ? AND `times` = ? GROUP BY `audit_id`) as a';
  76. const sqlParam2 = [this.tableName, rid, times];
  77. const count = await this.db.queryOne(sql2, sqlParam2);
  78. for (const i in result) {
  79. result[i].max_sort = count.num;
  80. }
  81. return result;
  82. }
  83. /**
  84. * 获取标段当前审核人
  85. *
  86. * @param {Number} reviseId - 修订id
  87. * @param {Number} times - 第几次审批
  88. * @return {Promise<*>}
  89. */
  90. async getCurAuditor(reviseId, times = 1) {
  91. const sql =
  92. '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` ' +
  93. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  94. ' WHERE la.`rid` = ? and la.`status` = ? and la.`times` = ?';
  95. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, reviseId, auditConst.status.checking, times];
  96. return await this.db.queryOne(sql, sqlParam);
  97. }
  98. /**
  99. * 获取最新审核顺序
  100. *
  101. * @param {Number} reviseId - 修订id
  102. * @param {Number} times - 第几次审批
  103. * @return {Promise<number>}
  104. */
  105. async getNewOrder(reviseId, times = 1) {
  106. const sql = 'SELECT Max(??) As max_order FROM ?? Where `rid` = ? and `times` = ?';
  107. const sqlParam = ['audit_order', this.tableName, reviseId, times];
  108. const result = await this.db.queryOne(sql, sqlParam);
  109. return result && result.max_order ? result.max_order + 1 : 1;
  110. }
  111. /**
  112. * 新增审核人
  113. *
  114. * @param {Object} revise - 修订
  115. * @param {Number} auditorId - 审核人id
  116. * @param {Number} times - 第几次审批
  117. * @return {Promise<number>}
  118. */
  119. async addAuditor(revise, auditorId, is_gdzs = 0) {
  120. const transaction = await this.db.beginTransaction();
  121. try {
  122. const times = revise.times ? revise.times : 1;
  123. let newOrder = await this.getNewOrder(revise.id, times);
  124. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  125. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  126. if (is_gdzs) await this._syncOrderByDelete(transaction, revise.id, newOrder, times, '+');
  127. const data = {
  128. tender_id: revise.tid,
  129. audit_id: auditorId,
  130. times,
  131. audit_order: newOrder,
  132. status: auditConst.status.uncheck,
  133. rid: revise.id,
  134. in_time: new Date(),
  135. };
  136. const result = await transaction.insert(this.tableName, data);
  137. await transaction.commit();
  138. return (result.effectRows = 1);
  139. } catch (err) {
  140. await transaction.rollback();
  141. throw err;
  142. }
  143. return false;
  144. }
  145. /**
  146. * 移除审核人时,同步其后审核人order
  147. * @param transaction - 事务
  148. * @param {Number} reviseId - 修订id
  149. * @param {Number} auditorId - 审核人id
  150. * @param {Number} times - 第几次审批
  151. * @return {Promise<*>}
  152. * @private
  153. */
  154. async _syncOrderByDelete(transaction, reviseId, order, times, selfOperate = '-') {
  155. this.initSqlBuilder();
  156. this.sqlBuilder.setAndWhere('rid', {
  157. value: this.db.escape(reviseId),
  158. operate: '=',
  159. });
  160. this.sqlBuilder.setAndWhere('audit_order', {
  161. value: order,
  162. operate: '>=',
  163. });
  164. this.sqlBuilder.setAndWhere('times', {
  165. value: times,
  166. operate: '=',
  167. });
  168. this.sqlBuilder.setUpdateData('audit_order', {
  169. value: 1,
  170. selfOperate: selfOperate,
  171. });
  172. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  173. const data = await transaction.query(sql, sqlParam);
  174. return data;
  175. }
  176. /**
  177. * 移除审核人
  178. *
  179. * @param {Object} revise - 修订
  180. * @param {Number} auditorId - 审核人id
  181. * @param {Number} times - 第几次审批
  182. * @return {Promise<boolean>}
  183. */
  184. async deleteAuditor(revise, auditorId) {
  185. const times = revise.times ? revise.times : 1;
  186. const transaction = await this.db.beginTransaction();
  187. try {
  188. const condition = { rid: revise.id, audit_id: auditorId, times };
  189. const auditor = await this.getDataByCondition(condition);
  190. if (!auditor) {
  191. throw '该审核人不存在';
  192. }
  193. await this._syncOrderByDelete(transaction, revise.id, auditor.audit_order, times);
  194. await transaction.delete(this.tableName, condition);
  195. await transaction.commit();
  196. } catch (err) {
  197. await transaction.rollback();
  198. throw err;
  199. }
  200. return true;
  201. }
  202. /**
  203. * 开始审批
  204. *
  205. * @param {Object} revise - 修订
  206. * @param {Number} times - 第几次审批
  207. * @return {Promise<boolean>}
  208. */
  209. async start(revise, times = 1) {
  210. const audit = await this.getDataByCondition({ rid: revise.id, times, audit_order: 1 });
  211. if (!audit) {
  212. if(this.ctx.tender.info.shenpi.revise === shenpiConst.sp_status.gdspl) {
  213. throw '请联系管理员添加审批人';
  214. } else {
  215. throw '请先选择审批人,再上报数据';
  216. }
  217. }
  218. const time = new Date();
  219. // 拷贝备份台账数据
  220. const his_id = await this.ctx.service.ledgerHistory.backupReviseLedgerHistory(revise);
  221. const transaction = await this.db.beginTransaction();
  222. try {
  223. await transaction.update(this.tableName, {
  224. id: audit.id,
  225. status: auditConst.status.checking,
  226. begin_time: time,
  227. bills_file: revise.bills_file,
  228. pos_file: revise.pos_file,
  229. });
  230. const reviseData = {
  231. id: revise.id,
  232. status: auditConst.status.checking,
  233. his_id,
  234. };
  235. if (revise.times === 1) {
  236. reviseData.begin_time = time;
  237. }
  238. await transaction.update(this.ctx.service.ledgerRevise.tableName, reviseData);
  239. // 投资进度改变状态
  240. await transaction.update(this.ctx.service.schedule.tableName, { revising: 1 }, { where: { tid: this.ctx.tender.id } });
  241. // 添加短信通知-需要审批提醒功能
  242. // 下一人
  243. // await this.ctx.helper.sendUserSms(audit.audit_id, smsTypeConst.const.XD,
  244. // smsTypeConst.judge.approval.toString(), '台账修订需要您审批,请登录系统处理。');
  245. await this.ctx.helper.sendAliSms(audit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), SmsAliConst.template.revise_check);
  246. // 微信模板通知
  247. const shenpiUrl = await this.ctx.helper.urlToShort(
  248. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + revise.tid + '/revise/' + revise.id + '/info'
  249. );
  250. const wechatData = {
  251. wap_url: shenpiUrl,
  252. status: wxConst.status.check,
  253. tips: wxConst.tips.check,
  254. code: this.ctx.session.sessionProject.code,
  255. begin_time: Date.parse(time),
  256. };
  257. await this.ctx.helper.sendWechat(audit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), wxConst.template.revise, wechatData);
  258. // 其他参与人
  259. const auditList = await this.getAuditors(revise.id, times);
  260. const users = this._.pull(this._.map(auditList, 'user_id'), audit.audit_id);
  261. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
  262. // smsTypeConst.judge.result.toString(), '台账修订已上报。');
  263. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_report);
  264. // 微信模板通知
  265. const wechatData2 = {
  266. wap_url: shenpiUrl,
  267. status: wxConst.status.report,
  268. tips: wxConst.tips.report,
  269. begin_time: Date.parse(time),
  270. code: this.ctx.session.sessionProject.code,
  271. };
  272. await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  273. await transaction.commit();
  274. } catch (err) {
  275. await transaction.rollback();
  276. throw err;
  277. }
  278. return true;
  279. }
  280. async _replaceLedgerByRevise(transaction, revise) {
  281. const sqlParam = [revise.tid];
  282. await transaction.delete(this.ctx.service.ledger.tableName, { tender_id: revise.tid });
  283. const bSql =
  284. 'Insert Into ' +
  285. this.ctx.service.ledger.tableName +
  286. ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  287. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  288. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id, is_tp,' +
  289. ' sgfh_expr, sjcl_expr, qtcl_expr, check_calc,' +
  290. ' gxby_status, dagl_status, gxby_url, dagl_url, gxby_limit, dagl_limit,' +
  291. ' ex_memo1, ex_memo2, ex_memo3)' +
  292. ' Select 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, gxby_url, dagl_url, gxby_limit, dagl_limit,' +
  297. ' ex_memo1, ex_memo2, ex_memo3' +
  298. ' From ' +
  299. this.ctx.service.reviseBills.tableName +
  300. ' Where `tender_id` = ?';
  301. await transaction.query(bSql, sqlParam);
  302. await transaction.delete(this.ctx.service.pos.tableName, { tid: revise.tid });
  303. const pSql =
  304. 'Insert Into ' +
  305. this.ctx.service.pos.tableName +
  306. ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_stage_order, add_times, add_user,' +
  307. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder, position, ' +
  308. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
  309. ' gxby_status, dagl_status, gxby_url, dagl_url, gxby_limit, dagl_limit,' +
  310. ' ex_memo1, ex_memo2, ex_memo3)' +
  311. ' Select id, tid, lid, name, drawing_code, quantity, add_stage, add_stage_order, add_times, add_user,' +
  312. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, porder, position,' +
  313. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
  314. ' gxby_status, dagl_status, gxby_url, dagl_url, gxby_limit, dagl_limit,' +
  315. ' ex_memo1, ex_memo2, ex_memo3' +
  316. ' From ' +
  317. this.ctx.service.revisePos.tableName +
  318. ' Where `tid` = ?';
  319. await transaction.query(pSql, sqlParam);
  320. }
  321. /**
  322. * 审批
  323. * @param {Object} revise - 修订
  324. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  325. * @param {String} opinion - 审批意见
  326. * @param {Number} times - 第几次审批
  327. * @return {Promise<void>}
  328. */
  329. async check(revise, checkType, opinion, times = 1) {
  330. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) throw '提交数据错误';
  331. const audit = await this.getDataByCondition({
  332. rid: revise.id,
  333. times,
  334. status: auditConst.status.checking,
  335. });
  336. if (!audit) throw '审核数据错误';
  337. const pid = this.ctx.session.sessionProject.id;
  338. const transaction = await this.db.beginTransaction();
  339. try {
  340. const auditList = await this.getAuditors(revise.id, times);
  341. // 审核通过添加到推送表
  342. const noticeContent = await this.getNoticeContent(pid, audit.tender_id, audit.rid, audit.audit_id);
  343. const records = [
  344. {
  345. pid,
  346. type: pushType.revise,
  347. uid: revise.uid,
  348. status: checkType,
  349. content: noticeContent,
  350. },
  351. ];
  352. auditList.forEach(audit => {
  353. records.push({
  354. pid,
  355. type: pushType.revise,
  356. uid: audit.audit_id,
  357. status: checkType,
  358. content: noticeContent,
  359. });
  360. });
  361. await transaction.insert('zh_notice', records);
  362. // 整理当前流程审核人状态更新
  363. const time = new Date();
  364. // 更新当前审核流程
  365. await transaction.update(this.tableName, {
  366. id: audit.id,
  367. status: checkType,
  368. opinion,
  369. end_time: time,
  370. });
  371. if (checkType === auditConst.status.checked) {
  372. const nextAudit = await this.getDataByCondition({
  373. rid: revise.id,
  374. times,
  375. audit_order: audit.audit_order + 1,
  376. });
  377. // 无下一审核人表示,审核结束
  378. if (nextAudit) {
  379. await transaction.update(this.tableName, {
  380. id: nextAudit.id,
  381. status: auditConst.status.checking,
  382. begin_time: time,
  383. });
  384. // 短信通知-需要审批提醒功能
  385. // 下一人
  386. // await this.ctx.helper.sendUserSms(nextAudit.user_id, smsTypeConst.const.XD,
  387. // smsTypeConst.judge.approval.toString(), '台账修订需要您审批,请登录系统处理。');
  388. await this.ctx.helper.sendAliSms(nextAudit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), SmsAliConst.template.revise_check);
  389. // 微信模板通知
  390. const shenpiUrl = await this.ctx.helper.urlToShort(
  391. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + revise.tid + '/revise/' + revise.id + '/info'
  392. );
  393. const wechatData = {
  394. wap_url: shenpiUrl,
  395. status: wxConst.status.check,
  396. tips: wxConst.tips.check,
  397. code: this.ctx.session.sessionProject.code,
  398. begin_time: Date.parse(revise.begin_time),
  399. };
  400. await this.ctx.helper.sendWechat(nextAudit.audit_id, smsTypeConst.const.XD, smsTypeConst.judge.approval.toString(), wxConst.template.revise, wechatData);
  401. // 其他参与人
  402. const users = this._.pull(this._.map(auditList, 'audit_id'), audit.audit_id);
  403. users.push(revise.uid);
  404. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
  405. // smsTypeConst.judge.result.toString(), '台账修订审批通过。');
  406. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result2, {
  407. status: SmsAliConst.status.success,
  408. });
  409. // 微信模板通知
  410. // const wechatData2 = {
  411. // status: wxConst.status.success,
  412. // tips: wxConst.tips.success,
  413. // begin_time: Date.parse(revise.begin_time),
  414. // };
  415. // await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  416. } else {
  417. // 同步修订信息
  418. await transaction.update(this.ctx.service.ledgerRevise.tableName, {
  419. id: revise.id,
  420. status: checkType,
  421. end_time: time,
  422. });
  423. await this.ctx.service.tenderTag.saveTenderTag(revise.tid, {revise_time: new Date()}, transaction);
  424. // 最新一期跟台账相关的缓存数据应过期
  425. const lastStage = await this.ctx.service.stage.getLastestStage(revise.tid, true);
  426. const cacheTime = new Date();
  427. if (lastStage) {
  428. await transaction.update(this.ctx.service.stage.tableName, {
  429. id: lastStage.id,
  430. cache_time_l: cacheTime,
  431. cache_time_r: cacheTime,
  432. });
  433. }
  434. // 拷贝修订数据至台账
  435. await this._replaceLedgerByRevise(transaction, revise);
  436. const sum = await this.ctx.service.reviseBills.addUp({
  437. tender_id: revise.tid, /* , is_leaf: true*/
  438. });
  439. await transaction.update(this.ctx.service.tender.tableName, {
  440. id: revise.tid,
  441. total_price: sum.total_price,
  442. deal_tp: sum.deal_tp,
  443. });
  444. // 短信通知-审批通过提醒功能
  445. // 下一人
  446. // const msg = '台账修订审批通过,请登录系统处理。';
  447. // await this.ctx.helper.sendUserSms(revise.uid, smsTypeConst.const.XD,
  448. // smsTypeConst.judge.result.toString(), msg);
  449. await this.ctx.helper.sendAliSms(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result, {
  450. status: SmsAliConst.status.success,
  451. });
  452. // 其他参与人
  453. const users = this._.pull(this._.map(auditList, 'audit_id'), revise.uid);
  454. // await this.ctx.helper.sendUserSms(users, smsTypeConst.const.XD,
  455. // smsTypeConst.judge.result.toString(), '台账修订审批通过。');
  456. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result2, {
  457. status: SmsAliConst.status.success,
  458. });
  459. users.push(revise.uid);
  460. // 微信模板通知
  461. const shenpiUrl = await this.ctx.helper.urlToShort(
  462. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + revise.tid + '/revise/' + revise.id + '/info'
  463. );
  464. const wechatData2 = {
  465. wap_url: shenpiUrl,
  466. status: wxConst.status.success,
  467. tips: wxConst.tips.success,
  468. begin_time: Date.parse(revise.begin_time),
  469. code: this.ctx.session.sessionProject.code,
  470. };
  471. await this.ctx.helper.sendWechat(users, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  472. }
  473. } else {
  474. // 同步修订信息
  475. await transaction.update(this.ctx.service.ledgerRevise.tableName, {
  476. id: revise.id,
  477. times: times + 1,
  478. status: checkType,
  479. });
  480. // 拷贝新一次审核流程列表
  481. const auditors = await this.getAllDataByCondition({
  482. where: { rid: revise.id, times },
  483. columns: ['tender_id', 'rid', 'audit_order', 'audit_id'],
  484. });
  485. for (const a of auditors) {
  486. a.times = times + 1;
  487. a.status = auditConst.status.uncheck;
  488. a.in_time = time;
  489. }
  490. await transaction.insert(this.tableName, auditors);
  491. // 短信通知-审批退回提醒功能
  492. // 下一人
  493. // await this.ctx.helper.sendUserSms(revise.uid, smsTypeConst.const.XD,
  494. // smsTypeConst.judge.result.toString(), '台账修订审批退回,请登录系统处理。');
  495. await this.ctx.helper.sendAliSms(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), SmsAliConst.template.revise_result, {
  496. status: SmsAliConst.status.back,
  497. });
  498. // 微信模板通知
  499. const shenpiUrl = await this.ctx.helper.urlToShort(
  500. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + revise.tid + '/revise/' + revise.id + '/info'
  501. );
  502. const wechatData = {
  503. wap_url: shenpiUrl,
  504. status: wxConst.status.back,
  505. tips: wxConst.tips.back,
  506. begin_time: Date.parse(revise.begin_time),
  507. code: this.ctx.session.sessionProject.code,
  508. };
  509. await this.ctx.helper.sendWechat(revise.uid, smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData);
  510. // 其他参与人
  511. // await this.ctx.helper.sendUserSms(this._.map(auditors, 'user_id'),
  512. // smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), '台账修订审批退回。');
  513. await this.ctx.helper.sendAliSms(
  514. this._.map(auditors, 'user_id'),
  515. smsTypeConst.const.XD,
  516. smsTypeConst.judge.result.toString(),
  517. SmsAliConst.template.revise_result2,
  518. {
  519. status: SmsAliConst.status.back,
  520. }
  521. );
  522. // 微信模板通知
  523. const wechatData2 = {
  524. wap_url: shenpiUrl,
  525. status: wxConst.status.back,
  526. tips: wxConst.tips.back,
  527. begin_time: Date.parse(revise.begin_time),
  528. code: this.ctx.session.sessionProject.code,
  529. };
  530. await this.ctx.helper.sendWechat(this._.map(auditors, 'user_id'), smsTypeConst.const.XD, smsTypeConst.judge.result.toString(), wxConst.template.revise, wechatData2);
  531. }
  532. await transaction.commit();
  533. } catch (err) {
  534. await transaction.rollback();
  535. throw err;
  536. }
  537. }
  538. /**
  539. * 取待审批修订列表(wap用)
  540. *
  541. * @param auditorId
  542. * @return {Promise<*>}
  543. */
  544. async getAuditReviseByWap(auditorId) {
  545. const sql =
  546. 'SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`begin_time`, ra.`end_time`,' +
  547. ' r.id, r.corder, r.uid, r.status, r.content, r.in_time, ' +
  548. ' 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, ' +
  549. ' ti.`deal_info`, ' +
  550. ' p.name As audit_name, p.role As audit_role, p.company As audit_company' +
  551. ' FROM ' + this.tableName + ' AS ra' +
  552. ' Left Join ' + this.ctx.service.ledgerRevise.tableName + ' As r On ra.rid = r.id' +
  553. ' Left Join ' + this.ctx.service.tender.tableName + ' AS t On r.tid = t.id' +
  554. ' Left Join ' + this.ctx.service.tenderInfo.tableName + ' AS ti On r.tid = ti.tid' +
  555. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' As p On ra.audit_id = p.id' +
  556. ' WHERE r.`valid` != 0 and ra.`audit_id` = ? and ra.`status` = ?' +
  557. ' ORDER BY ra.`begin_time` DESC';
  558. const sqlParam = [auditorId, auditConst.status.checking];
  559. return await this.db.query(sql, sqlParam);
  560. }
  561. /**
  562. * 获取审核人需要审核的标段列表
  563. *
  564. * @param auditorId
  565. * @return {Promise<*>}
  566. */
  567. async getAuditRevise(auditorId) {
  568. const sql =
  569. 'SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`begin_time`, ra.`end_time`,' +
  570. ' r.id, r.corder, r.uid, r.status, r.content,' +
  571. ' 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, ' +
  572. ' p.name As audit_name, p.role As audit_role, p.company As audit_company' +
  573. ' FROM ' + this.tableName + ' AS ra' +
  574. ' Left Join ' + this.ctx.service.ledgerRevise.tableName + ' As r On ra.rid = r.id' +
  575. ' Left Join ' + this.ctx.service.tender.tableName + ' AS t On r.tid = t.id' +
  576. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' As p On ra.audit_id = p.id' +
  577. ' WHERE r.`valid` != 0 and ((ra.`audit_id` = ? and ra.`status` = ?) OR' +
  578. ' (r.`uid` = ? and r.`status` = ? and ra.`status` = ? and ra.`times` = (r.`times`-1))) ORDER BY ra.`begin_time` DESC';
  579. const sqlParam = [auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  580. return await this.db.query(sql, sqlParam);
  581. }
  582. /**
  583. * 获取 某时间后 审批进度 更新的台账
  584. * @param {Integer} pid - 项目id
  585. * @param {Integer} uid - 查询人id
  586. * @param {Date} noticeTime - 查询事件
  587. * @return {Promise<*>}
  588. */
  589. async getNoticeRevise(pid, uid, noticeTime) {
  590. // const sql = 'SELECT * FROM (SELECT ra.`audit_id`, ra.`times`, ra.`audit_order`, ra.`end_time`, ra.`status`,' +
  591. // ' r.id, r.corder, r.uid, r.status As `r_status`, r.content, ' +
  592. // ' 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, ' +
  593. // ' pa.name As `ru_name`, pa.role As `ru_role`, pa.company As `ru_company`' +
  594. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tender_id` FROM ?? WHERE `audit_id` = ? GROUP BY `tender_id`)) As t' +
  595. // ' LEFT JOIN ' + this.ctx.service.ledgerRevise.tableName + ' As r ON r.`tid` = t.`id`' +
  596. // ' LEFT JOIN ' + this.tableName + ' As ra ON ra.rid = r.id' +
  597. // ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON ra.`audit_id` = pa.`id`' +
  598. // ' WHERE ra.end_time > ? and t.project_id = ?' +
  599. // ' ORDER By ra.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`id`' +
  600. // ' ORDER By new_t.`end_time`';
  601. // const sqlParam = [this.ctx.service.tender.tableName, auditorId, this.tableName, auditorId, noticeTime, projectId];
  602. // return await this.db.query(sql, sqlParam);
  603. let notice = await this.db.select('zh_notice', {
  604. where: { pid, type: pushType.revise, uid },
  605. orders: [['create_time', 'desc']],
  606. limit: 10,
  607. offset: 0,
  608. });
  609. notice = notice.map(v => {
  610. const extra = JSON.parse(v.content);
  611. delete v.content;
  612. return { ...v, ...extra };
  613. });
  614. return notice;
  615. }
  616. /**
  617. * 用于添加推送所需的content内容
  618. * @param {Number} pid 项目id
  619. * @param {Number} tid 台账id
  620. * @param {Number} rid 修订id
  621. * @param {Number} uid 审核人id
  622. */
  623. async getNoticeContent(pid, tid, rid, uid) {
  624. const noticeSql =
  625. 'SELECT * FROM (SELECT ' +
  626. ' t.`id` As `tid`, t.`name`, r.`corder`, r.`id` As rid, pa.`name` As `su_name`, pa.role As `su_role`' +
  627. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  628. ' LEFT JOIN ?? As r On r.`id` = ? ' +
  629. ' LEFT JOIN ?? As pa ON pa.`id` = ? ' +
  630. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  631. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.ledgerRevise.tableName, rid, this.ctx.service.projectAccount.tableName, uid, pid];
  632. const content = await this.db.query(noticeSql, noticeSqlParam);
  633. return content.length ? JSON.stringify(content[0]) : '';
  634. }
  635. /**
  636. * 获取最新的审批人状态
  637. *
  638. * @param {Number} rid - 修订id
  639. * @param {Number} status - 修订状态
  640. * @param {Number} times - 修订次数
  641. * @return {Promise<boolean>}
  642. */
  643. async getAuditorByStatus(rid, status, times = 1) {
  644. let auditor = null;
  645. let sql = '';
  646. let sqlParam = '';
  647. switch (status) {
  648. case auditConst.status.checking:
  649. case auditConst.status.checked:
  650. case auditConst.status.checkNoPre:
  651. sql =
  652. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  653. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id` ' +
  654. ' WHERE la.`rid` = ? and la.`status` = ? order by la.`times` desc, la.`audit_order` desc';
  655. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, status];
  656. auditor = await this.db.queryOne(sql, sqlParam);
  657. break;
  658. case auditConst.status.checkNo:
  659. sql =
  660. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  661. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  662. ' WHERE la.`rid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`audit_order` desc';
  663. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, auditConst.status.checkNo, parseInt(times) - 1];
  664. auditor = await this.db.queryOne(sql, sqlParam);
  665. break;
  666. case auditConst.status.uncheck:
  667. default:
  668. break;
  669. }
  670. return auditor;
  671. }
  672. /**
  673. * 获取审核人流程列表
  674. *
  675. * @param auditorId
  676. * @return {Promise<*>}
  677. */
  678. async getAuditGroupByList(rid, times) {
  679. const sql =
  680. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`rid`, la.`audit_order` ' +
  681. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  682. ' WHERE la.`rid` = ? and la.`times` = ? GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
  683. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, rid, times];
  684. return await this.db.query(sql, sqlParam);
  685. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  686. // const sqlParam = [this.tableName, stageId, times];
  687. // return await this.db.query(sql, sqlParam);
  688. }
  689. /**
  690. * 获取审核人流程列表(包括原报)
  691. * @param {Number} rid 修订id
  692. * @param {Number} times 审核次数
  693. * @return {Promise<Array>} 查询结果集(包括原报)
  694. */
  695. async getAuditorsWithOwner(rid, times = 1) {
  696. const result = await this.getAuditGroupByList(rid, times);
  697. const sql =
  698. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As rid, 0 As `order`' +
  699. ' FROM ' +
  700. this.ctx.service.ledgerRevise.tableName +
  701. ' As s' +
  702. ' LEFT JOIN ' +
  703. this.ctx.service.projectAccount.tableName +
  704. ' As pa' +
  705. ' ON s.uid = pa.id' +
  706. ' WHERE s.id = ?';
  707. const sqlParam = [times, rid, rid];
  708. const user = await this.db.queryOne(sql, sqlParam);
  709. result.unshift(user);
  710. return result;
  711. }
  712. async getAllAuditors(tenderId) {
  713. const sql =
  714. 'SELECT ra.audit_id, ra.tender_id FROM ' + this.tableName + ' ra' +
  715. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ra.tender_id = t.id' +
  716. ' WHERE t.id = ?' +
  717. ' GROUP BY ra.audit_id';
  718. const sqlParam = [tenderId];
  719. return this.db.query(sql, sqlParam);
  720. }
  721. async updateNewAuditList(revise, newIdList) {
  722. const transaction = await this.db.beginTransaction();
  723. try {
  724. // 先删除旧的审批流,再添加新的
  725. await transaction.delete(this.tableName, { rid: revise.id, times: revise.times });
  726. const newAuditors = [];
  727. let order = 1;
  728. for (const aid of newIdList) {
  729. newAuditors.push({
  730. tender_id: revise.tid, audit_id: aid,
  731. times: revise.times, audit_order: order, status: auditConst.status.uncheck,
  732. rid: revise.id,in_time: new Date(),
  733. });
  734. order++;
  735. }
  736. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  737. await transaction.commit();
  738. } catch (err) {
  739. await transaction.rollback();
  740. throw err;
  741. }
  742. }
  743. async updateLastAudit(revise, auditList, lastId) {
  744. const transaction = await this.db.beginTransaction();
  745. try {
  746. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  747. const idList = this._.map(auditList, 'audit_id');
  748. let order = idList.length + 1;
  749. if (idList.indexOf(lastId) !== -1) {
  750. await transaction.delete(this.tableName, { rid: revise.id, times: revise.times, audit_id: lastId });
  751. const audit = this._.find(auditList, { 'audit_id': lastId });
  752. // 顺移之后审核人流程顺序
  753. await this._syncOrderByDelete(transaction, revise.id, audit.audit_order, revise.times);
  754. order = order - 1;
  755. }
  756. // 添加终审
  757. const newAuditor = {
  758. tender_id: revise.tid, audit_id: lastId,
  759. times: revise.times, audit_order: order, status: auditConst.status.uncheck,
  760. rid: revise.id, in_time: new Date(),
  761. };
  762. await transaction.insert(this.tableName, newAuditor);
  763. await transaction.commit();
  764. } catch (err) {
  765. await transaction.rollback();
  766. throw err;
  767. }
  768. }
  769. async getNumByMonth(tid, startMonth, endMonth) {
  770. const sql = 'SELECT COUNT(*) as num FROM ?? WHERE id in (SELECT MAX(id) FROM ?? WHERE tender_id = ? GROUP BY rid) AND status = ? AND end_time between ? and ?';
  771. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  772. const result = await this.db.queryOne(sql, sqlParam);
  773. return result ? result.num : 0;
  774. }
  775. }
  776. return ReviseAudit;
  777. };