ledger_audit.js 26 KB

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