ledger_audit.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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.getData(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 [billsHis, posHis] = await this.backupLedgerHistoryFile();
  263. const transaction = await this.db.beginTransaction();
  264. try {
  265. await transaction.update(this.tableName, {
  266. id: audit.id,
  267. status: auditConst.status.checking,
  268. begin_time: new Date(),
  269. });
  270. await transaction.update(this.ctx.service.tender.tableName, {
  271. id: tenderId,
  272. ledger_status: auditConst.status.checking,
  273. total_price: sum.total_price,
  274. deal_tp: sum.deal_tp,
  275. bills_file: billsHis,
  276. pos_file: posHis,
  277. });
  278. // 添加短信通知-需要审批提醒功能
  279. await this.ctx.helper.sendAliSms(audit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  280. // 微信模板通知
  281. const wechatData = {
  282. status: wxConst.status.check,
  283. tips: wxConst.tips.check,
  284. begin_time: Date.parse(new Date()),
  285. };
  286. await this.ctx.helper.sendWechat(audit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
  287. await transaction.commit();
  288. } catch (err) {
  289. await transaction.rollback();
  290. throw err;
  291. }
  292. return true;
  293. }
  294. /**
  295. * 审批
  296. * @param {Number} tenderId - 标段id
  297. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  298. * @param {String} opinion 审批意见
  299. * @param {Number} times - 第几次审批
  300. * @return {Promise<void>}
  301. */
  302. async check(tenderId, checkType, opinion, times = 1) {
  303. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
  304. throw '提交数据错误';
  305. }
  306. const pid = this.ctx.session.sessionProject.id;
  307. const transaction = await this.db.beginTransaction();
  308. try {
  309. // 整理当前流程审核人状态更新
  310. const time = new Date();
  311. const audit = await this.getDataByCondition({
  312. tender_id: tenderId,
  313. times,
  314. status: auditConst.status.checking,
  315. });
  316. if (!audit) {
  317. throw '审核数据错误';
  318. }
  319. // 获取审核人列表
  320. const auditList = await this.getAuditors(tenderId, times);
  321. // 添加推送
  322. const noticeContent = await this.getNoticeContent(audit.tender_id, pid, audit.audit_id);
  323. const records = [
  324. {
  325. pid,
  326. type: pushType.ledger,
  327. uid: this.ctx.tender.data.user_id,
  328. status: checkType,
  329. content: noticeContent,
  330. },
  331. ];
  332. auditList.forEach(audit => {
  333. records.push({
  334. pid,
  335. type: pushType.ledger,
  336. uid: audit.audit_id,
  337. status: checkType,
  338. content: noticeContent,
  339. });
  340. });
  341. await transaction.insert('zh_notice', records);
  342. // 更新当前审核流程
  343. await transaction.update(this.tableName, { id: audit.id, status: checkType, opinion, end_time: time });
  344. const begin_audit = await this.getDataByCondition({
  345. tender_id: tenderId,
  346. audit_order: 1,
  347. });
  348. if (checkType === auditConst.status.checked) {
  349. const nextAudit = await this.getDataByCondition({
  350. tender_id: tenderId,
  351. times,
  352. audit_order: audit.audit_order + 1,
  353. });
  354. // 无下一审核人表示,审核结束
  355. if (nextAudit) {
  356. await transaction.update(this.tableName, {
  357. id: nextAudit.id,
  358. status: auditConst.status.checking,
  359. begin_time: time,
  360. });
  361. await this.ctx.helper.sendAliSms(nextAudit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), SmsAliConst.template.ledger_check);
  362. // 微信模板通知
  363. const wechatData = {
  364. status: wxConst.status.check,
  365. tips: wxConst.tips.check,
  366. begin_time: Date.parse(begin_audit.begin_time),
  367. };
  368. await this.ctx.helper.sendWechat(nextAudit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.approval.toString(), wxConst.template.ledger, wechatData);
  369. } else {
  370. // 同步标段信息
  371. await transaction.update(this.ctx.service.tender.tableName, {
  372. id: tenderId,
  373. ledger_status: checkType,
  374. });
  375. await this.ctx.service.tenderTag.saveTenderTag(tenderId, {ledger_time: new Date()}, transaction);
  376. const users = this._.pull(this._.map(auditList, 'audit_id'), audit.id);
  377. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, {
  378. status: SmsAliConst.status.success,
  379. });
  380. // 微信模板通知
  381. const wechatData = {
  382. status: wxConst.status.success,
  383. tips: wxConst.tips.success,
  384. begin_time: Date.parse(begin_audit.begin_time),
  385. };
  386. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), wxConst.template.ledger, wechatData);
  387. }
  388. } else {
  389. // 同步标段信息
  390. await transaction.update(this.ctx.service.tender.tableName, {
  391. id: tenderId,
  392. ledger_times: times + 1,
  393. ledger_status: checkType,
  394. });
  395. // 拷贝新一次审核流程列表
  396. const auditors = await this.getAllDataByCondition({
  397. where: { tender_id: tenderId, times },
  398. columns: ['tender_id', 'audit_order', 'audit_id'],
  399. });
  400. for (const a of auditors) {
  401. a.times = times + 1;
  402. a.status = auditConst.status.uncheck;
  403. }
  404. await transaction.insert(this.tableName, auditors);
  405. const users = this._.pull(this._.map(auditList, 'audit_id'), audit.id);
  406. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), SmsAliConst.template.ledger_result, {
  407. status: SmsAliConst.status.back,
  408. });
  409. // 微信模板通知
  410. const wechatData = {
  411. status: wxConst.status.back,
  412. tips: wxConst.tips.back,
  413. begin_time: Date.parse(begin_audit.begin_time),
  414. };
  415. await this.ctx.helper.sendWechat(audit.audit_id, smsTypeConst.const.TZ, smsTypeConst.judge.result.toString(), wxConst.template.ledger, wechatData);
  416. }
  417. await transaction.commit();
  418. } catch (err) {
  419. await transaction.rollback();
  420. throw err;
  421. }
  422. }
  423. /**
  424. * 获取审核人需要审核的标段列表
  425. *
  426. * @param auditorId
  427. * @return {Promise<*>}
  428. */
  429. async getAuditTender(auditorId) {
  430. const sql =
  431. '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` ' +
  432. ' FROM ?? AS la Left Join ?? AS t ON la.`tender_id` = t.`id` ' +
  433. ' 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)))' +
  434. ' ORDER BY la.`begin_time` DESC';
  435. const sqlParam = [
  436. this.tableName,
  437. this.ctx.service.tender.tableName,
  438. auditorId,
  439. auditConst.status.checking,
  440. auditorId,
  441. auditConst.status.checkNo,
  442. auditConst.status.checkNo,
  443. ];
  444. return await this.db.query(sql, sqlParam);
  445. }
  446. /**
  447. * 获取 某时间后 审批进度 更新的台账
  448. * @param {Integer} pid - 项目id
  449. * @param {Integer} uid - 查询人id
  450. * @param {Date} noticeTime - 查询事件
  451. * @return {Promise<*>}
  452. */
  453. async getNoticeTender(pid, uid, noticeTime) {
  454. let notice = await this.db.select('zh_notice', {
  455. where: { pid, type: pushType.ledger, uid },
  456. orders: [['create_time', 'desc']],
  457. limit: 10,
  458. offset: 0,
  459. });
  460. notice = notice.map(v => {
  461. const extra = JSON.parse(v.content);
  462. delete v.content;
  463. return { ...v, ...extra };
  464. });
  465. return notice;
  466. }
  467. /**
  468. * 用于添加推送所需的content内容
  469. * @param {Number} id 标段id
  470. * @param {Number} pid 项目id
  471. * @param {Number} uid 审核人id
  472. */
  473. async getNoticeContent(id, pid, uid) {
  474. const noticeSql =
  475. 'SELECT * FROM (SELECT ' +
  476. ' t.`id` As `tid`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  477. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  478. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  479. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  480. const noticeSqlParam = [this.ctx.service.tender.tableName, id, this.ctx.service.projectAccount.tableName, uid, pid];
  481. const content = await this.db.query(noticeSql, noticeSqlParam);
  482. return content.length ? JSON.stringify(content[0]) : '';
  483. }
  484. /**
  485. * 获取审核人流程列表
  486. * @param {Number} tender_id - 标段id
  487. * @param {Number} times 审核次数
  488. * @return {Promise<Array>} 查询结果集
  489. */
  490. async getAuditGroupByList(tender_id, times = 1) {
  491. const sql =
  492. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tender_id`, la.`audit_order` ' +
  493. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  494. ' WHERE la.`tender_id` = ? and la.`times` = ? GROUP BY la.`audit_id` ORDER BY la.`audit_order`';
  495. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tender_id, times];
  496. return await this.db.query(sql, sqlParam);
  497. }
  498. /**
  499. * 获取审核人流程列表(包括原报)
  500. * @param {Number} tender_id - 标段id
  501. * @param {Number} times 审核次数
  502. * @return {Promise<Array>} 查询结果集(包括原报)
  503. */
  504. async getAuditorsWithOwner(tender_id, times = 1) {
  505. const result = await this.getAuditGroupByList(tender_id, times);
  506. const sql =
  507. 'SELECT pa.`id` As audit_id, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As tender_id, 0 As `audit_order`' +
  508. ' FROM ' + this.ctx.service.tender.tableName + ' As s' +
  509. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  510. ' ON s.user_id = pa.id' +
  511. ' WHERE s.id = ?';
  512. const sqlParam = [times, tender_id, tender_id];
  513. const user = await this.db.queryOne(sql, sqlParam);
  514. result.unshift(user);
  515. return result;
  516. }
  517. async updateNewAuditList(tender, newIdList) {
  518. const transaction = await this.db.beginTransaction();
  519. try {
  520. // 先删除旧的审批流,再添加新的
  521. await transaction.delete(this.tableName, { tender_id: tender.id, times: tender.ledger_times });
  522. const newAuditors = [];
  523. let order = 1;
  524. for (const aid of newIdList) {
  525. newAuditors.push({
  526. tender_id: tender.id, audit_id: aid,
  527. times: tender.ledger_times, audit_order: order, status: auditConst.status.uncheck,
  528. });
  529. order++;
  530. }
  531. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  532. await transaction.commit();
  533. } catch (err) {
  534. await transaction.rollback();
  535. throw err;
  536. }
  537. }
  538. async updateLastAudit(tender, auditList, lastId) {
  539. const transaction = await this.db.beginTransaction();
  540. try {
  541. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  542. const idList = this._.map(auditList, 'audit_id');
  543. let order = idList.length + 1;
  544. if (idList.indexOf(lastId) !== -1) {
  545. await transaction.delete(this.tableName, { tender_id: tender.id, times: tender.ledger_times, audit_id: lastId });
  546. const audit = this._.find(auditList, { 'audit_id': lastId });
  547. // 顺移之后审核人流程顺序
  548. await this._syncOrderByDelete(transaction, tender.id, audit.audit_order, tender.ledger_times);
  549. order = order - 1;
  550. }
  551. // 添加终审
  552. const newAuditor = {
  553. tender_id: tender.id, audit_id: lastId,
  554. times: tender.ledger_times, audit_order: order, status: auditConst.status.uncheck,
  555. };
  556. await transaction.insert(this.tableName, newAuditor);
  557. await transaction.commit();
  558. } catch (err) {
  559. await transaction.rollback();
  560. throw err;
  561. }
  562. }
  563. }
  564. return LedgerAudit;
  565. };