revise_audit.js 42 KB

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