ledger_audit.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. 'use strict';
  2. /**
  3. * 台账审批流程表
  4. *
  5. * @author Mai
  6. * @date 2018/5/25
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').ledger;
  10. const smsTypeConst = require('../const/sms_type');
  11. const SMS = require('../lib/sms');
  12. const SmsAliConst = require('../const/sms_alitemplate');
  13. const wxConst = require('../const/wechat_template');
  14. const shenpiConst = require('../const/shenpi');
  15. const pushType = require('../const/audit').pushType;
  16. const pushOperate = require('../const/spec_3f').pushOperate;
  17. module.exports = app => {
  18. class LedgerAudit extends app.BaseService {
  19. /**
  20. * 构造函数
  21. *
  22. * @param {Object} ctx - egg全局变量
  23. * @return {void}
  24. */
  25. constructor(ctx) {
  26. super(ctx);
  27. this.tableName = 'ledger_audit';
  28. }
  29. /**
  30. * 获取标段审核人信息
  31. *
  32. * @param {Number} tenderId - 标段id
  33. * @param {Number} auditorId - 审核人id
  34. * @param {Number} times - 第几次审批
  35. * @return {Promise<*>}
  36. */
  37. async getAuditor(tenderId, auditorId, times = 1) {
  38. const sql = '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.`tender_id` = ? and la.`audit_id` = ? and la.`times` = ?';
  41. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditorId, times];
  42. return await this.db.queryOne(sql, sqlParam);
  43. }
  44. async getAuditorByOrder(tenderId, order, times = 1) {
  45. const sql = '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` ' +
  46. ' FROM ?? AS la Left Join ?? AS pa ON la.`audit_id` = pa.`id`' +
  47. ' WHERE la.`tender_id` = ? and la.`audit_order` = ? and la.`times` = ?';
  48. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, order, times];
  49. return await this.db.queryOne(sql, sqlParam);
  50. }
  51. async getLastestAuditor(tenderId, times, status) {
  52. const sql =
  53. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  54. ' la.`times`, la.`audit_order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  55. ' FROM ' + this.tableName + ' AS la' +
  56. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`audit_id` = pa.`id`' +
  57. ' WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?' +
  58. ' ORDER BY `audit_order` DESC';
  59. const sqlParam = [tenderId, status, times ? times : 1];
  60. return await this.db.queryOne(sql, sqlParam);
  61. }
  62. /**
  63. * 获取标段审核人最后一位的名称
  64. *
  65. * @param {Number} tenderId - 标段id
  66. * @param {Number} auditorId - 审核人id
  67. * @param {Number} times - 第几次审批
  68. * @return {Promise<*>}
  69. */
  70. async getStatusName(tenderId, times) {
  71. const sql = 'SELECT pa.`name` FROM ?? AS la Left Join ?? AS pa ON la.`audit_id` = pa.`id`' +
  72. ' WHERE la.`tender_id` = ? and la.`status` != ? ORDER BY la.`times` DESC, la.`audit_order` DESC';
  73. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.uncheck];
  74. return await this.db.queryOne(sql, sqlParam);
  75. }
  76. /**
  77. * 获取标段审核列表信息
  78. *
  79. * @param {Number} tenderId - 标段id
  80. * @param {Number} times - 第几次审批
  81. * @return {Promise<*>}
  82. */
  83. async getAuditors(tenderId, times = 1) {
  84. const sql =
  85. '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` ' +
  86. 'FROM ?? AS la, ?? AS pa, (SELECT t1.`audit_id`,(@i:=@i+1) as `sort` FROM (SELECT t.`audit_id`, t.`audit_order` FROM (select `audit_id`, `audit_order` from ?? WHERE `tender_id` = ? AND `times` = ? ORDER BY `audit_order` LIMIT 200) t GROUP BY t.`audit_id` ORDER BY t.`audit_order`) t1, (select @i:=0) as it) as g ' +
  87. 'WHERE la.`tender_id` = ? and la.`times` = ? and la.`audit_id` = pa.`id` and g.`audit_id` = la.`audit_id` order by la.`audit_order`';
  88. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, tenderId, times, tenderId, times];
  89. const result = await this.db.query(sql, sqlParam);
  90. const sql2 = 'SELECT COUNT(a.`audit_id`) as num FROM (SELECT `audit_id` FROM ?? WHERE `tender_id` = ? AND `times` = ? GROUP BY `audit_id`) as a';
  91. const sqlParam2 = [this.tableName, tenderId, times];
  92. const count = await this.db.queryOne(sql2, sqlParam2);
  93. for (const i in result) {
  94. result[i].max_sort = count.num;
  95. }
  96. return result;
  97. }
  98. async getFinalAuditGroup(tenderId, times = 1) {
  99. const sql =
  100. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.`sign_path`, la.`times`, la.`tender_id`, Max(la.`audit_order`) as max_order ' +
  101. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  102. ' WHERE la.`tender_id` = ? and la.`times` = ? GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
  103. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, times];
  104. const result = await this.db.query(sql, sqlParam);
  105. for (const r of result) {
  106. const auditor = await this.getDataByCondition({tender_id: tenderId, times: r.times, audit_order: r.max_order});
  107. r.status = auditor.status;
  108. r.opinion = auditor.opinion;
  109. r.begin_time = auditor.begin_time;
  110. r.end_time = auditor.end_time;
  111. }
  112. return result;
  113. }
  114. /**
  115. * 获取标段当前审核人
  116. *
  117. * @param {Number} tenderId - 标段id
  118. * @param {Number} times - 第几次审批
  119. * @return {Promise<*>}
  120. */
  121. async getCurAuditor(tenderId, times = 1) {
  122. const sql =
  123. '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` ' +
  124. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  125. ' WHERE la.`tender_id` = ? and la.`status` = ? and la.`times` = ?';
  126. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId, auditConst.status.checking, times];
  127. return await this.db.queryOne(sql, sqlParam);
  128. }
  129. /**
  130. * 获取最新审核顺序
  131. *
  132. * @param {Number} tenderId - 标段id
  133. * @param {Number} times - 第几次审批
  134. * @return {Promise<number>}
  135. */
  136. async getNewOrder(tenderId, times = 1) {
  137. const sql = 'SELECT Max(??) As max_order FROM ?? Where `tender_id` = ? and `times` = ?';
  138. const sqlParam = ['audit_order', this.tableName, tenderId, times];
  139. const result = await this.db.queryOne(sql, sqlParam);
  140. return result && result.max_order ? result.max_order + 1 : 1;
  141. }
  142. /**
  143. * 新增审核人
  144. *
  145. * @param {Number} tenderId - 标段id
  146. * @param {Number} auditorId - 审核人id
  147. * @param {Number} times - 第几次审批
  148. * @return {Promise<number>}
  149. */
  150. async addAuditor(tenderId, auditorId, times = 1, is_gdzs = 0) {
  151. const transaction = await this.db.beginTransaction();
  152. try {
  153. let newOrder = await this.getNewOrder(tenderId, times);
  154. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  155. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  156. if (is_gdzs) await this._syncOrderByDelete(transaction, tenderId, newOrder, times, '+');
  157. const data = {
  158. tender_id: tenderId,
  159. audit_id: auditorId,
  160. times,
  161. audit_order: newOrder,
  162. status: auditConst.status.uncheck,
  163. };
  164. const result = await transaction.insert(this.tableName, data);
  165. await transaction.commit();
  166. return (result.effectRows = 1);
  167. } catch (err) {
  168. await transaction.rollback();
  169. throw err;
  170. }
  171. return false;
  172. }
  173. /**
  174. * 移除审核人时,同步其后审核人order
  175. * @param transaction - 事务
  176. * @param {Number} tenderId - 标段id
  177. * @param {Number} auditorId - 审核人id
  178. * @param {Number} times - 第几次审批
  179. * @return {Promise<*>}
  180. * @private
  181. */
  182. async _syncOrderByDelete(transaction, tenderId, order, times, selfOperate = '-') {
  183. this.initSqlBuilder();
  184. this.sqlBuilder.setAndWhere('tender_id', {
  185. value: tenderId,
  186. operate: '=',
  187. });
  188. this.sqlBuilder.setAndWhere('audit_order', {
  189. value: order,
  190. operate: '>=',
  191. });
  192. this.sqlBuilder.setAndWhere('times', {
  193. value: times,
  194. operate: '=',
  195. });
  196. this.sqlBuilder.setUpdateData('audit_order', {
  197. value: 1,
  198. selfOperate: selfOperate,
  199. });
  200. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  201. const data = await transaction.query(sql, sqlParam);
  202. return data;
  203. }
  204. /**
  205. * 移除审核人
  206. *
  207. * @param {Number} tenderId - 标段id
  208. * @param {Number} auditorId - 审核人id
  209. * @param {Number} times - 第几次审批
  210. * @return {Promise<boolean>}
  211. */
  212. async deleteAuditor(tenderId, auditorId, times = 1) {
  213. const transaction = await this.db.beginTransaction();
  214. try {
  215. const condition = { tender_id: tenderId, audit_id: auditorId, times };
  216. const auditor = await this.getDataByCondition(condition);
  217. if (!auditor) {
  218. throw '该审核人不存在';
  219. }
  220. await this._syncOrderByDelete(transaction, tenderId, auditor.audit_order, times);
  221. await transaction.delete(this.tableName, condition);
  222. await transaction.commit();
  223. } catch (err) {
  224. await transaction.rollback();
  225. throw err;
  226. }
  227. return true;
  228. }
  229. /**
  230. * 开始审批
  231. *
  232. * @param {Number} tenderId - 标段id
  233. * @param {Number} times - 第几次审批
  234. * @return {Promise<boolean>}
  235. */
  236. async start(tenderId, times = 1) {
  237. const audit = await this.getDataByCondition({ tender_id: tenderId, times, audit_order: 1 });
  238. if (!audit) {
  239. if(this.ctx.tender.info.shenpi.ledger === shenpiConst.sp_status.gdspl) {
  240. throw '请联系管理员添加审批人';
  241. } else {
  242. throw '请先选择审批人,再上报数据';
  243. }
  244. }
  245. const sum = await this.ctx.service.ledger.addUp({ tender_id: tenderId /* , is_leaf: true*/ });
  246. // 拷贝备份台账数据
  247. const his_id = await this.ctx.service.ledgerHistory.backupLedgerHistory(this.ctx.tender.data);
  248. const transaction = await this.db.beginTransaction();
  249. try {
  250. await transaction.update(this.tableName, {
  251. id: audit.id,
  252. status: auditConst.status.checking,
  253. begin_time: new Date(),
  254. });
  255. await transaction.update(this.ctx.service.tender.tableName, {
  256. id: tenderId,
  257. ledger_status: auditConst.status.checking,
  258. total_price: sum.total_price,
  259. deal_tp: sum.deal_tp,
  260. his_id: his_id,
  261. });
  262. await this.ctx.service.tenderCache.updateLedgerCache4Start(transaction, tenderId, auditConst.status.checking, audit, sum);
  263. // 添加短信通知-需要审批提醒功能
  264. await this.ctx.helper.sendAliSms(audit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  265. // 微信模板通知
  266. const wechatData = {
  267. status: wxConst.status.check,
  268. tips: wxConst.tips.check,
  269. begin_time: Date.parse(new Date()),
  270. };
  271. await this.ctx.helper.sendWechat(audit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
  272. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TZ, {
  273. pid: this.ctx.session.sessionProject.id,
  274. tid: tenderId,
  275. uid: audit.audit_id,
  276. sp_type: 'ledger',
  277. sp_id: audit.id,
  278. table_name: this.tableName,
  279. template: wxConst.template.ledger,
  280. wx_data: wechatData,
  281. });
  282. await transaction.commit();
  283. } catch (err) {
  284. await transaction.rollback();
  285. throw err;
  286. }
  287. return true;
  288. }
  289. /**
  290. * 审批
  291. * @param {Number} tenderId - 标段id
  292. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  293. * @param {String} opinion 审批意见
  294. * @param {Number} times - 第几次审批
  295. * @return {Promise<void>}
  296. */
  297. async check(tenderId, checkType, opinion, times = 1) {
  298. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
  299. throw '提交数据错误';
  300. }
  301. const pid = this.ctx.session.sessionProject.id;
  302. const transaction = await this.db.beginTransaction();
  303. try {
  304. // 整理当前流程审核人状态更新
  305. const time = new Date();
  306. const audit = await this.getDataByCondition({
  307. tender_id: tenderId,
  308. times,
  309. status: auditConst.status.checking,
  310. });
  311. if (!audit) {
  312. throw '审核数据错误';
  313. }
  314. // 获取审核人列表
  315. const auditList = await this.getAuditors(tenderId, times);
  316. // 添加推送
  317. const noticeContent = await this.getNoticeContent(audit.tender_id, pid, audit.audit_id, opinion);
  318. const records = [
  319. {
  320. pid,
  321. type: pushType.ledger,
  322. uid: this.ctx.tender.data.user_id,
  323. status: checkType,
  324. content: noticeContent,
  325. },
  326. ];
  327. auditList.forEach(audit => {
  328. records.push({
  329. pid,
  330. type: pushType.ledger,
  331. uid: audit.audit_id,
  332. status: checkType,
  333. content: noticeContent,
  334. });
  335. });
  336. await transaction.insert('zh_notice', records);
  337. // 更新当前审核流程
  338. await transaction.update(this.tableName, { id: audit.id, status: checkType, opinion, end_time: time });
  339. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  340. const begin_audit = await this.getDataByCondition({
  341. tender_id: tenderId,
  342. audit_order: 1,
  343. });
  344. const tenderMsg = await this.ctx.service.tender.getDataById(tenderId);
  345. const users = this._.uniq(this._.concat(this._.map(auditList, 'audit_id'), tenderMsg.user_id));
  346. if (checkType === auditConst.status.checked) {
  347. const nextAudit = await this.getDataByCondition({
  348. tender_id: tenderId,
  349. times,
  350. audit_order: audit.audit_order + 1,
  351. });
  352. // 无下一审核人表示,审核结束
  353. if (nextAudit) {
  354. await transaction.update(this.tableName, {
  355. id: nextAudit.id,
  356. status: auditConst.status.checking,
  357. begin_time: time,
  358. });
  359. await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, auditConst.status.checking, checkType, nextAudit);
  360. await this.ctx.helper.sendAliSms(nextAudit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  361. // 微信模板通知
  362. const wechatData = {
  363. status: wxConst.status.check,
  364. tips: wxConst.tips.check,
  365. begin_time: Date.parse(begin_audit.begin_time),
  366. };
  367. await this.ctx.helper.sendWechat(nextAudit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
  368. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TZ, {
  369. pid: this.ctx.session.sessionProject.id,
  370. tid: tenderId,
  371. uid: nextAudit.audit_id,
  372. sp_type: 'ledger',
  373. sp_id: nextAudit.id,
  374. table_name: this.tableName,
  375. template: wxConst.template.ledger,
  376. wx_data: wechatData,
  377. });
  378. } else {
  379. // 同步标段信息
  380. await transaction.update(this.ctx.service.tender.tableName, {
  381. id: tenderId,
  382. ledger_status: checkType,
  383. });
  384. await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, checkType, checkType);
  385. await this.ctx.service.tenderTag.saveTenderTag(tenderId, {ledger_time: new Date()}, transaction);
  386. // const users = this._.pull(this._.map(auditList, 'audit_id'), audit.id);
  387. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, {
  388. status: SmsAliConst.status.success,
  389. });
  390. // 微信模板通知
  391. const wechatData = {
  392. status: wxConst.status.success,
  393. tips: wxConst.tips.success,
  394. begin_time: Date.parse(begin_audit.begin_time),
  395. };
  396. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), wxConst.template.ledger, wechatData);
  397. // 审批通过 - 检查三方特殊推送
  398. await this.ctx.service.specMsg.addLedgerMsg(transaction, pid, this.ctx.tender, pushOperate.ledger.checked);
  399. }
  400. } else {
  401. // 同步标段信息
  402. await transaction.update(this.ctx.service.tender.tableName, {
  403. id: tenderId,
  404. ledger_times: times + 1,
  405. ledger_status: checkType,
  406. });
  407. await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, checkType, checkType, { audit_id: this.ctx.tender.data.user_id });
  408. // 拷贝新一次审核流程列表
  409. const auditors = await this.getAllDataByCondition({
  410. where: { tender_id: tenderId, times },
  411. columns: ['tender_id', 'audit_order', 'audit_id'],
  412. });
  413. const insertAuditors = [];
  414. for (const a of auditors) {
  415. if (insertAuditors.find(x => { return x.audit_id === a.audit_id; })) continue;
  416. a.audit_order = insertAuditors.length + 1;
  417. a.times = times + 1;
  418. a.status = auditConst.status.uncheck;
  419. insertAuditors.push(a)
  420. }
  421. await transaction.insert(this.tableName, insertAuditors);
  422. // const users = this._.pull(this._.map(auditList, 'audit_id'), audit.id);
  423. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, {
  424. status: SmsAliConst.status.back,
  425. });
  426. // 微信模板通知
  427. const wechatData = {
  428. status: wxConst.status.back,
  429. tips: wxConst.tips.back,
  430. begin_time: Date.parse(begin_audit.begin_time),
  431. };
  432. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), wxConst.template.ledger, wechatData);
  433. }
  434. await transaction.commit();
  435. } catch (err) {
  436. await transaction.rollback();
  437. throw err;
  438. }
  439. }
  440. /**
  441. * 重新审批
  442. * @param {Number} stageId - 标段id
  443. * @param {Number} times - 第几次审批
  444. * @return {Promise<void>}
  445. */
  446. async checkAgain(tenderId, times = 1) {
  447. const time = new Date();
  448. // 整理当前流程审核人状态更新
  449. const auditors = await this.getAllDataByCondition({
  450. where: { tender_id: tenderId, times },
  451. orders: [['audit_order', 'desc']],
  452. });
  453. if (auditors.length <= 0) throw '台账审核数据错误';
  454. const selfAudit = auditors[0];
  455. if (selfAudit.audit_id !== this.ctx.session.sessionUser.accountId) throw '当前台账您无权重审';
  456. const tender = this.ctx.service.tender.getDataById(tenderId);
  457. if (!tender) throw '标段数据错误';
  458. let otherAuditIds = [tender.user_id, ...auditors.map(x => { return x.audit_id })];
  459. otherAuditIds = this._.uniq(otherAuditIds).filter(x => { return x !== selfAudit.audit_id; });
  460. const checkAgainAuditor = {
  461. tender_id: tenderId, times, audit_order: selfAudit.audit_order + 1, audit_id: selfAudit.audit_id,
  462. begin_time: time, end_time: time, opinion: '', status: auditConst.status.checkAgain,
  463. };
  464. const checkingAuditor = {
  465. tender_id: tenderId, times, audit_order: selfAudit.audit_order + 2, audit_id: selfAudit.audit_id,
  466. begin_time: time, end_time: null, opinion: '', status: auditConst.status.checking,
  467. };
  468. const transaction = await this.db.beginTransaction();
  469. try {
  470. // 当前审批人2次添加至流程中
  471. await transaction.insert(this.tableName, checkAgainAuditor);
  472. const checking_result = await transaction.insert(this.tableName, checkingAuditor);
  473. // 同步标段信息
  474. await transaction.update(this.ctx.service.tender.tableName, {
  475. id: tenderId,
  476. ledger_status: auditConst.status.checking,
  477. });
  478. await this.ctx.service.tenderCache.updateLedgerCache(transaction, tenderId, auditConst.status.checking, auditConst.status.checkAgain, checkingAuditor);
  479. if (otherAuditIds.length > 0) {
  480. await this.ctx.helper.sendAliSms(otherAuditIds, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  481. // 微信模板通知
  482. const wechatData = {
  483. status: wxConst.status.check,
  484. tips: wxConst.tips.check,
  485. begin_time: Date.parse(time),
  486. };
  487. await this.ctx.helper.sendWechat(selfAudit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
  488. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TZ, {
  489. pid: this.ctx.session.sessionProject.id,
  490. tid: this.ctx.tender.id,
  491. uid: selfAudit.audit_id,
  492. sp_type: 'ledger',
  493. sp_id: checking_result.insertId,
  494. table_name: this.tableName,
  495. template: wxConst.template.ledger,
  496. wx_data: wechatData,
  497. });
  498. }
  499. await transaction.commit();
  500. } catch (err) {
  501. await transaction.rollback();
  502. throw err;
  503. }
  504. }
  505. /**
  506. * 获取审核人需要审核的标段列表
  507. *
  508. * @param auditorId
  509. * @return {Promise<*>}
  510. */
  511. async getAuditTender(auditorId) {
  512. const sql =
  513. 'SELECT la.`audit_id`, la.`times`, la.`audit_order`, la.`begin_time`, la.`end_time`, t.`id`, t.`name`, t.`project_id`, t.`type`, t.`user_id`, t.`ledger_status` ' +
  514. ' FROM ?? AS la Left Join ?? AS t ON la.`tender_id` = t.`id` ' +
  515. ' WHERE ((la.`audit_id` = ? and la.`status` = ?) OR (t.`user_id` = ? and t.`ledger_status` = ? and la.`status` = ? and la.`times` = (t.`ledger_times`-1)))' +
  516. ' ORDER BY la.`begin_time` DESC';
  517. const sqlParam = [
  518. this.tableName,
  519. this.ctx.service.tender.tableName,
  520. auditorId,
  521. auditConst.status.checking,
  522. auditorId,
  523. auditConst.status.checkNo,
  524. auditConst.status.checkNo,
  525. ];
  526. return await this.db.query(sql, sqlParam);
  527. }
  528. /**
  529. * 获取审核人审核的次数
  530. *
  531. * @param auditorId
  532. * @return {Promise<*>}
  533. */
  534. async getCountByChecked(auditorId) {
  535. return await this.db.count(this.tableName, { audit_id: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo] });
  536. }
  537. /**
  538. * 获取最近一次审批结束时间
  539. *
  540. * @param auditorId
  541. * @return {Promise<*>}
  542. */
  543. async getLastEndTimeByChecked(auditorId) {
  544. const sql = 'SELECT `end_time` FROM ?? WHERE `audit_id` = ? ' +
  545. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
  546. const sqlParam = [this.tableName, auditorId];
  547. const result = await this.db.queryOne(sql, sqlParam);
  548. return result ? result.end_time : null;
  549. }
  550. /**
  551. * 用于添加推送所需的content内容
  552. * @param {Number} id 标段id
  553. * @param {Number} pid 项目id
  554. * @param {Number} uid 审核人id
  555. */
  556. async getNoticeContent(id, pid, uid, opinion = '') {
  557. const noticeSql =
  558. 'SELECT * FROM (SELECT ' +
  559. ' t.`id` As `tid`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  560. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  561. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  562. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  563. const noticeSqlParam = [this.ctx.service.tender.tableName, id, this.ctx.service.projectAccount.tableName, uid, pid];
  564. const content = await this.db.query(noticeSql, noticeSqlParam);
  565. if (content.length) {
  566. content[0].opinion = opinion;
  567. }
  568. return content.length ? JSON.stringify(content[0]) : '';
  569. }
  570. /**
  571. * 获取审核人流程列表
  572. * @param {Number} tender_id - 标段id
  573. * @param {Number} times 审核次数
  574. * @return {Promise<Array>} 查询结果集
  575. */
  576. async getAuditGroupByList(tender_id, times = 1) {
  577. const sql =
  578. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tender_id`, la.`audit_order` ' +
  579. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  580. ' WHERE la.`tender_id` = ? and la.`times` = ? GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
  581. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tender_id, times];
  582. return await this.db.query(sql, sqlParam);
  583. }
  584. /**
  585. * 获取审核人流程列表(包括原报)
  586. * @param {Number} tender_id - 标段id
  587. * @param {Number} times 审核次数
  588. * @return {Promise<Array>} 查询结果集(包括原报)
  589. */
  590. async getAuditorsWithOwner(tender_id, times = 1) {
  591. const result = await this.getAuditGroupByList(tender_id, times);
  592. const sql =
  593. 'SELECT pa.`id` As audit_id, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As tender_id, 0 As `audit_order`' +
  594. ' FROM ' + this.ctx.service.tender.tableName + ' As s' +
  595. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  596. ' ON s.user_id = pa.id' +
  597. ' WHERE s.id = ?';
  598. const sqlParam = [times, tender_id, tender_id];
  599. const user = await this.db.queryOne(sql, sqlParam);
  600. result.unshift(user);
  601. return result;
  602. }
  603. async updateNewAuditList(tender, newIdList) {
  604. const transaction = await this.db.beginTransaction();
  605. try {
  606. // 先删除旧的审批流,再添加新的
  607. await transaction.delete(this.tableName, { tender_id: tender.id, times: tender.ledger_times });
  608. const newAuditors = [];
  609. let order = 1;
  610. for (const aid of newIdList) {
  611. newAuditors.push({
  612. tender_id: tender.id, audit_id: aid,
  613. times: tender.ledger_times, audit_order: order, status: auditConst.status.uncheck,
  614. });
  615. order++;
  616. }
  617. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  618. await transaction.commit();
  619. } catch (err) {
  620. await transaction.rollback();
  621. throw err;
  622. }
  623. }
  624. async updateLastAudit(tender, auditList, lastId) {
  625. const transaction = await this.db.beginTransaction();
  626. try {
  627. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  628. const idList = this._.map(auditList, 'audit_id');
  629. let order = idList.length + 1;
  630. if (idList.indexOf(lastId) !== -1) {
  631. await transaction.delete(this.tableName, { tender_id: tender.id, times: tender.ledger_times, audit_id: lastId });
  632. const audit = this._.find(auditList, { 'audit_id': lastId });
  633. // 顺移之后审核人流程顺序
  634. await this._syncOrderByDelete(transaction, tender.id, audit.audit_order, tender.ledger_times);
  635. order = order - 1;
  636. }
  637. // 添加终审
  638. const newAuditor = {
  639. tender_id: tender.id, audit_id: lastId,
  640. times: tender.ledger_times, audit_order: order, status: auditConst.status.uncheck,
  641. };
  642. await transaction.insert(this.tableName, newAuditor);
  643. await transaction.commit();
  644. } catch (err) {
  645. await transaction.rollback();
  646. throw err;
  647. }
  648. }
  649. }
  650. return LedgerAudit;
  651. };