revise_audit.js 42 KB

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