ledger_audit.js 28 KB

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