ledger_audit.js 27 KB

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