revise_audit.js 44 KB

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