ledger_audit.js 27 KB

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