revise_audit.js 42 KB

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