ledger_audit.js 29 KB

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