ledger_audit.js 28 KB

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