ledger_audit.js 28 KB

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