revise_audit.js 43 KB

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