advance_audit.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. 'use strict';
  2. const auditConst = require('../const/audit').advance;
  3. const shenpiConst = require('../const/shenpi');
  4. const pushType = require('../const/audit').pushType;
  5. const smsTypeConst = require('../const/sms_type');
  6. const wxConst = require('../const/wechat_template.js');
  7. const advanceConst = require('../const/advance');
  8. module.exports = app => {
  9. class AdvanceAudit extends app.BaseService {
  10. constructor(ctx) {
  11. super(ctx);
  12. this.tableName = 'advance_audit';
  13. }
  14. /**
  15. * 获取审核人流程列表
  16. * @param {Number} vid 预付款记录id
  17. * @param {Number} times 审核次数
  18. * @return {Promise<Array>} 查询结果集
  19. */
  20. async getAuditGroupByList(vid, times = 1) {
  21. const sql =
  22. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  23. 'FROM ?? AS la, ?? AS pa ' +
  24. 'WHERE la.`vid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` GROUP BY la.`audit_id` ORDER BY la.`order`';
  25. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, times];
  26. return await this.db.query(sql, sqlParam);
  27. }
  28. /**
  29. * 获取审核人流程列表(包括原报)
  30. * @param {Number} vid 预付款记录id
  31. * @param {Number} times 审核次数
  32. * @return {Promise<Array>} 查询结果集(包括原报)
  33. */
  34. async getAuditorsWithOwner(vid, times = 1) {
  35. const result = await this.getAuditGroupByList(vid, times);
  36. const sql =
  37. 'SELECT pa.`id` As audit_id, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As vid, 0 As `order`' +
  38. ' FROM ' +
  39. this.ctx.service.advance.tableName +
  40. ' As s' +
  41. ' LEFT JOIN ' +
  42. this.ctx.service.projectAccount.tableName +
  43. ' As pa' +
  44. ' ON s.uid = pa.id' +
  45. ' WHERE s.id = ?';
  46. const sqlParam = [times, vid, vid];
  47. const user = await this.db.queryOne(sql, sqlParam);
  48. result.unshift(user);
  49. return result;
  50. }
  51. /**
  52. * 获取最新审核顺序
  53. * @param {Number} vid - 预付款id
  54. * @param {Number} times - 第几次审批
  55. * @return {Number} 审核顺序
  56. */
  57. async getNewOrder(vid, times = 1) {
  58. const sql = 'SELECT Max(??) As max_order FROM ?? Where `vid` = ? and `times` = ?';
  59. const sqlParam = ['order', this.tableName, vid, times];
  60. const result = await this.db.queryOne(sql, sqlParam);
  61. return result && result.max_order ? result.max_order + 1 : 1;
  62. }
  63. /**
  64. * 新增审核人
  65. * @param {Number} tid - 标段id
  66. * @param {Number} vid - 预付款id
  67. * @param {Number} audit_id - 审核人id
  68. * @param {Number} times - 第几次审批
  69. * @return {Boolean} 是否插入成功
  70. */
  71. async addAuditor(tid, vid, audit_id, times = 1, is_gdzs = 0) {
  72. const transaction = await this.db.beginTransaction();
  73. try {
  74. let newOrder = await this.getNewOrder(vid, times);
  75. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  76. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  77. if (is_gdzs) await this._syncOrderByDelete(transaction, vid, newOrder, times, '+');
  78. const record = {
  79. tid,
  80. vid,
  81. audit_id,
  82. times,
  83. order: newOrder,
  84. status: auditConst.status.uncheck,
  85. };
  86. const result = await transaction.insert(this.tableName, record);
  87. await transaction.commit();
  88. return result && result.affectedRows === 1;
  89. } catch (err) {
  90. await transaction.rollback();
  91. throw err;
  92. }
  93. return false;
  94. }
  95. /**
  96. * 移除审核人
  97. * @param {Number} vid - 预付款id
  98. * @param {Number} audit_id - 审核人id
  99. * @param {Number} times - 第几次审批
  100. * @return {Promise<boolean>}
  101. */
  102. async deleteAuditor(vid, audit_id, times = 1) {
  103. const transaction = await this.db.beginTransaction();
  104. try {
  105. const condition = { vid, audit_id, times };
  106. const auditor = await this.getDataByCondition(condition);
  107. if (!auditor) {
  108. throw '该审核人不存在';
  109. }
  110. await this._syncOrderByDelete(transaction, vid, auditor.order, times);
  111. await transaction.delete(this.tableName, condition);
  112. await transaction.commit();
  113. } catch (err) {
  114. await transaction.rollback();
  115. throw err;
  116. }
  117. return true;
  118. }
  119. /**
  120. * 移除审核人时,同步其后审核人order
  121. * @param {Function} transaction - 事务
  122. * @param {Number} vid - 预付款id
  123. * @param {Number} order - 审核顺序
  124. * @param {Number} times - 第几次审批
  125. * @return {Promise<*>} 查询结果集
  126. * @private
  127. */
  128. async _syncOrderByDelete(transaction, vid, order, times, selfOperate = '-') {
  129. this.initSqlBuilder();
  130. this.sqlBuilder.setAndWhere('vid', {
  131. value: this.db.escape(vid),
  132. operate: '=',
  133. });
  134. this.sqlBuilder.setAndWhere('order', {
  135. value: order,
  136. operate: '>=',
  137. });
  138. this.sqlBuilder.setAndWhere('times', {
  139. value: times,
  140. operate: '=',
  141. });
  142. this.sqlBuilder.setUpdateData('order', {
  143. value: 1,
  144. selfOperate: selfOperate,
  145. });
  146. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  147. const data = await transaction.query(sql, sqlParam);
  148. return data;
  149. }
  150. /**
  151. * 获取当前审核人
  152. * @param {Number} vid - 预付款id
  153. * @param {Number} times - 第几次审批
  154. * @return {Promise<Object>} 查询结果集
  155. */
  156. async getCurAuditor(vid, times = 1) {
  157. const sql =
  158. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`create_time`, la.`end_time` ' +
  159. 'FROM ?? AS la, ?? AS pa ' +
  160. 'WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ?' +
  161. ' and la.`audit_id` = pa.`id`';
  162. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checking, times];
  163. return await this.db.queryOne(sql, sqlParam);
  164. }
  165. /**
  166. * 获取审核列表信息
  167. * @param {Number} vid - 预付款id
  168. * @param {Number} times - 第几次审批
  169. * @return {Promise<Array>} 查询结果集
  170. */
  171. async getAuditors(vid, times = 1) {
  172. const sql =
  173. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`create_time`, la.`end_time`, g.`sort` ' +
  174. 'FROM ?? AS la, ?? AS pa, (SELECT `audit_id`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `vid` = ? AND `times` = ? GROUP BY `audit_id`) as g ' +
  175. 'WHERE la.`vid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` and g.`audit_id` = la.`audit_id` order by la.`order`';
  176. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, vid, times, vid, times];
  177. const result = await this.db.query(sql, sqlParam);
  178. const sql2 = 'SELECT COUNT(a.`audit_id`) as num FROM (SELECT `audit_id` FROM ?? WHERE `vid` = ? AND `times` = ? GROUP BY `audit_id`) as a';
  179. const sqlParam2 = [this.tableName, vid, times];
  180. const count = await this.db.queryOne(sql2, sqlParam2);
  181. for (const i in result) {
  182. result[i].max_sort = count.num;
  183. }
  184. return result;
  185. }
  186. /**
  187. * 获取审核人信息
  188. * @param {Number} vid - 预付款id
  189. * @param {Number} audit_id - 审核人id
  190. * @param {Number} times - 第几次审批
  191. * @return {Promise<*>} 查询结果
  192. */
  193. async getAuditor(vid, audit_id, times = 1) {
  194. const sql =
  195. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`create_time`, la.`end_time` ' +
  196. 'FROM ?? AS la, ?? AS pa ' +
  197. 'WHERE la.`vid` = ? and la.`audit_id` = ? and la.`times` = ?' +
  198. ' and la.`audit_id` = pa.`id`';
  199. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, audit_id, times];
  200. return await this.db.queryOne(sql, sqlParam);
  201. }
  202. /**
  203. * 开始审批
  204. * @param {Number} vid - 预付款id
  205. * @param {Number} times - 第几次审批
  206. * @param {Object} data - 载荷
  207. */
  208. async start(vid, times = 1, data) {
  209. const audit = await this.getDataByCondition({ vid, times, order: 1 });
  210. if (!audit) {
  211. if(this.ctx.tender.info.shenpi.advance === shenpiConst.sp_status.gdspl) {
  212. throw '请联系管理员添加审批人';
  213. } else {
  214. throw '请先选择审批人,再上报数据';
  215. }
  216. }
  217. const transaction = await this.db.beginTransaction();
  218. try {
  219. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, create_time: new Date() });
  220. await transaction.update(this.ctx.service.advance.tableName, {
  221. id: audit.vid,
  222. ...data,
  223. });
  224. // 微信模板通知
  225. const advanceInfo = await this.ctx.service.advance.getDataById(vid);
  226. const shenpiUrl = await this.ctx.helper.urlToShort(
  227. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + advanceInfo.tid + '/advance/' + advanceInfo.id + '/detail'
  228. );
  229. const wechatData = {
  230. wap_url: shenpiUrl,
  231. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  232. status: wxConst.status.check,
  233. tips: wxConst.tips.check,
  234. tp: parseFloat(advanceInfo.cur_amount),
  235. code: this.ctx.session.sessionProject.code,
  236. };
  237. await this.ctx.helper.sendWechat(audit.audit_id, smsTypeConst.const.YFK, smsTypeConst.judge.approval.toString(), wxConst.template.advance, wechatData);
  238. await transaction.commit();
  239. } catch (err) {
  240. await transaction.rollback();
  241. throw err;
  242. }
  243. return true;
  244. }
  245. async _checked(pid, advanceId, checkData, times) {
  246. const time = new Date();
  247. // 整理当前流程审核人状态更新
  248. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  249. if (!audit) {
  250. throw '审核数据错误';
  251. }
  252. // 获取审核人列表
  253. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC';
  254. const sqlParam = [this.tableName, advanceId, times];
  255. const auditors = await this.db.query(sql, sqlParam);
  256. const nextAudit = await this.getDataByCondition({ vid: advanceId, times, order: audit.order + 1 });
  257. const transaction = await this.db.beginTransaction();
  258. try {
  259. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  260. // 获取推送必要信息
  261. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id);
  262. // 添加推送
  263. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checked, content: noticeContent }];
  264. auditors.forEach(audit => {
  265. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checked, content: noticeContent });
  266. });
  267. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  268. // 无下一审核人表示,审核结束
  269. const advanceInfo = await this.ctx.service.advance.getDataById(advanceId);
  270. const shenpiUrl = await this.ctx.helper.urlToShort(
  271. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + advanceInfo.tid + '/advance/' + advanceInfo.id + '/detail'
  272. );
  273. if (nextAudit) {
  274. // 流程至下一审批人
  275. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, create_time: time });
  276. // 同步期信息
  277. await transaction.update(this.ctx.service.advance.tableName, {
  278. id: advanceId,
  279. status: auditConst.status.checking,
  280. });
  281. // 微信模板通知
  282. const wechatData = {
  283. wap_url: shenpiUrl,
  284. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  285. status: wxConst.status.check,
  286. tips: wxConst.tips.check,
  287. tp: parseFloat(advanceInfo.cur_amount),
  288. code: this.ctx.session.sessionProject.code,
  289. };
  290. await this.ctx.helper.sendWechat(nextAudit.audit_id, smsTypeConst.const.YFK, smsTypeConst.judge.approval.toString(), wxConst.template.advance, wechatData);
  291. } else {
  292. await transaction.update(this.ctx.service.advance.tableName, {
  293. id: advanceId,
  294. status: checkData.checkType,
  295. end_time: time,
  296. });
  297. // 微信模板通知
  298. const users = this._.uniq(this._.concat(this._.map(auditors, 'audit_id'), advanceInfo.uid));
  299. const wechatData = {
  300. wap_url: shenpiUrl,
  301. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  302. status: wxConst.status.success,
  303. tips: wxConst.tips.success,
  304. tp: parseFloat(advanceInfo.cur_amount),
  305. code: this.ctx.session.sessionProject.code,
  306. };
  307. await this.ctx.helper.sendWechat(users, smsTypeConst.const.YFK, smsTypeConst.judge.result.toString(), wxConst.template.advance, wechatData);
  308. }
  309. await transaction.commit();
  310. } catch (err) {
  311. await transaction.rollback();
  312. throw err;
  313. }
  314. }
  315. async _checkNo(pid, advanceId, checkData, times) {
  316. const time = new Date();
  317. // 整理当前流程审核人状态更新
  318. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  319. if (!audit) {
  320. throw '审核数据错误';
  321. }
  322. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC';
  323. const sqlParam = [this.tableName, advanceId, times];
  324. const auditors = await this.db.query(sql, sqlParam);
  325. let order = 1;
  326. for (const a of auditors) {
  327. a.times = times + 1;
  328. a.order = order;
  329. a.status = auditConst.status.uncheck;
  330. order++;
  331. }
  332. const transaction = await this.db.beginTransaction();
  333. try {
  334. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  335. // 添加到消息推送表
  336. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id);
  337. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNo, content: noticeContent }];
  338. auditors.forEach(audit => {
  339. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNo, content: noticeContent });
  340. });
  341. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  342. // 同步期信息
  343. await transaction.update(this.ctx.service.advance.tableName, {
  344. id: advanceId,
  345. status: checkData.checkType,
  346. times: times + 1,
  347. });
  348. // 微信模板通知
  349. const advanceInfo = await this.ctx.service.advance.getDataById(advanceId);
  350. const users = this._.uniq(this._.concat(this._.map(auditors, 'audit_id'), advanceInfo.uid));
  351. const shenpiUrl = await this.ctx.helper.urlToShort(
  352. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + advanceInfo.tid + '/advance/' + advanceInfo.id + '/detail'
  353. );
  354. const wechatData = {
  355. wap_url: shenpiUrl,
  356. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  357. status: wxConst.status.back,
  358. tips: wxConst.tips.back,
  359. tp: parseFloat(advanceInfo.cur_amount),
  360. code: this.ctx.session.sessionProject.code,
  361. };
  362. await this.ctx.helper.sendWechat(users, smsTypeConst.const.YFK, smsTypeConst.judge.result.toString(), wxConst.template.advance, wechatData);
  363. // 拷贝新一次审核流程列表
  364. await transaction.insert(this.tableName, auditors);
  365. await transaction.commit();
  366. } catch (err) {
  367. await transaction.rollback();
  368. throw err;
  369. }
  370. }
  371. async _checkNoPre(pid, advanceId, checkData, times) {
  372. const time = new Date();
  373. // 整理当前流程审核人状态更新
  374. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  375. if (!audit || audit.order <= 1) {
  376. throw '审核数据错误';
  377. }
  378. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  379. const auditors2 = await this.getAuditGroupByList(advanceId, times);
  380. const auditorIndex = await auditors2.findIndex(function(item) {
  381. return item.audit_id === audit.audit_id;
  382. });
  383. const preAuditor = auditors2[auditorIndex - 1];
  384. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id);
  385. const transaction = await this.db.beginTransaction();
  386. try {
  387. // 添加到消息推送表
  388. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNoPre, content: noticeContent }];
  389. auditors2.forEach(audit => {
  390. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNoPre, content: noticeContent });
  391. });
  392. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  393. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  394. // 顺移气候审核人流程顺序
  395. this.initSqlBuilder();
  396. this.sqlBuilder.setAndWhere('vid', { value: advanceId, operate: '=' });
  397. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  398. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  399. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  400. await transaction.query(sql, sqlParam);
  401. const newAuditors = [];
  402. newAuditors.push({
  403. tid: audit.tid,
  404. vid: audit.vid,
  405. audit_id: preAuditor.audit_id,
  406. times: audit.times,
  407. order: audit.order + 1,
  408. status: auditConst.status.checking,
  409. create_time: time,
  410. });
  411. newAuditors.push({
  412. tid: audit.tid,
  413. vid: audit.vid,
  414. audit_id: audit.audit_id,
  415. times: audit.times,
  416. order: audit.order + 2,
  417. status: auditConst.status.uncheck,
  418. });
  419. // 微信模板通知
  420. const advanceInfo = await this.ctx.service.advance.getDataById(advanceId);
  421. const shenpiUrl = await this.ctx.helper.urlToShort(
  422. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + advanceInfo.tid + '/advance/' + advanceInfo.id + '/detail'
  423. );
  424. const wechatData = {
  425. wap_url: shenpiUrl,
  426. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  427. status: wxConst.status.check,
  428. tips: wxConst.tips.check,
  429. tp: parseFloat(advanceInfo.cur_amount),
  430. code: this.ctx.session.sessionProject.code,
  431. };
  432. await this.ctx.helper.sendWechat(preAuditor.audit_id, smsTypeConst.const.YFK, smsTypeConst.judge.approval.toString(), wxConst.template.advance, wechatData);
  433. await transaction.insert(this.tableName, newAuditors);
  434. await transaction.commit();
  435. } catch (error) {
  436. await transaction.rollback();
  437. throw error;
  438. }
  439. }
  440. /**
  441. * 审批
  442. * @param {Number} advanceId - 预付款id
  443. * @param {auditConst.status.checked|auditConst.status.checkNo} checkData - 审批结果
  444. * @param {Number} times - 第几次审批
  445. * @return {Promise<void>}
  446. */
  447. async check(advanceId, checkData, times = 1) {
  448. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  449. throw '提交数据错误';
  450. }
  451. const pid = this.ctx.session.sessionProject.id;
  452. switch (checkData.checkType) {
  453. case auditConst.status.checked:
  454. await this._checked(pid, advanceId, checkData, times);
  455. break;
  456. case auditConst.status.checkNo:
  457. await this._checkNo(pid, advanceId, checkData, times);
  458. break;
  459. case auditConst.status.checkNoPre:
  460. await this._checkNoPre(pid, advanceId, checkData, times);
  461. break;
  462. default:
  463. throw '无效审批操作';
  464. }
  465. }
  466. /**
  467. * 用于添加推送所需的content内容
  468. * @param {Number} pid 项目id
  469. * @param {Number} tid 台账id
  470. * @param {Number} vid 预付款id
  471. * @param {Number} uid 审批人id
  472. */
  473. async getNoticeContent(pid, tid, vid, uid) {
  474. const noticeSql =
  475. 'SELECT * FROM (SELECT ' +
  476. ' t.`id` As `tid`, ad.`vid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  477. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  478. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  479. ' LEFT JOIN ?? As ad ON m.`id` = ad.`vid`' +
  480. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  481. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  482. const noticeSqlParam = [
  483. this.ctx.service.tender.tableName,
  484. tid,
  485. this.ctx.service.advance.tableName,
  486. vid,
  487. this.tableName,
  488. this.ctx.service.projectAccount.tableName,
  489. uid,
  490. pid,
  491. ];
  492. const content = await this.db.query(noticeSql, noticeSqlParam);
  493. return content.length ? JSON.stringify(content[0]) : '';
  494. }
  495. /**
  496. * 通过状态获取审核人信息
  497. * @param {Number} vid - 预付款id
  498. * @param {Number} status - 期状态
  499. * @param {Number} times - 审批次数
  500. * @return {Object} auditor 审核人信息
  501. */
  502. async getAuditorByStatus(vid, status, times = 1) {
  503. let auditor = null;
  504. let sql = '';
  505. let sqlParam = '';
  506. switch (status) {
  507. case auditConst.status.checking:
  508. case auditConst.status.checked:
  509. case auditConst.status.checkNoPre:
  510. sql =
  511. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  512. 'FROM ?? AS la, ?? AS pa ' +
  513. 'WHERE la.`vid` = ? and la.`status` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`order` desc';
  514. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, status];
  515. auditor = await this.db.queryOne(sql, sqlParam);
  516. break;
  517. case auditConst.status.checkNo:
  518. sql =
  519. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  520. 'FROM ?? AS la, ?? AS pa ' +
  521. 'WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ? and la.`audit_id` = pa.`id` order by la.`times` desc, la.`order` desc';
  522. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checkNo, parseInt(times) - 1];
  523. auditor = await this.db.queryOne(sql, sqlParam);
  524. break;
  525. case auditConst.status.uncheck:
  526. default:
  527. break;
  528. }
  529. return auditor;
  530. }
  531. /**
  532. * 获取所有审核人
  533. * @param {Number} tenderId 标段id
  534. */
  535. async getAllAuditors(tenderId) {
  536. const sql = 'SELECT au.audit_id, au.tid FROM ' + this.tableName + ' au' +
  537. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On au.tid = t.id' +
  538. ' WHERE t.id = ?' +
  539. ' GROUP BY au.audit_id';
  540. const sqlParam = [tenderId];
  541. return this.db.query(sql, sqlParam);
  542. }
  543. /**
  544. * 获取待处理审核列表
  545. * @param {Number} auditorId 审核人id
  546. */
  547. async getAuditAdvance(auditorId) {
  548. const sql = 'SELECT ma.`audit_id`, ma.`times`, ma.`order`, ma.`create_time`, ma.`end_time`, ma.`tid`, ma.`vid`,' +
  549. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  550. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  551. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  552. ' WHERE ((ma.`audit_id` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  553. ' and ma.`vid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`create_time` DESC';
  554. const sqlParam = [this.tableName, this.ctx.service.advance.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  555. return await this.db.query(sql, sqlParam);
  556. }
  557. async updateNewAuditList(advance, newIdList) {
  558. const transaction = await this.db.beginTransaction();
  559. try {
  560. // 先删除旧的审批流,再添加新的
  561. await transaction.delete(this.tableName, { vid: advance.id, times: advance.times });
  562. const newAuditors = [];
  563. let order = 1;
  564. for (const aid of newIdList) {
  565. newAuditors.push({
  566. tid: advance.tid, vid: advance.id, audit_id: aid,
  567. times: advance.times, order, status: auditConst.status.uncheck,
  568. });
  569. order++;
  570. }
  571. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  572. await transaction.commit();
  573. } catch (err) {
  574. await transaction.rollback();
  575. throw err;
  576. }
  577. }
  578. async updateLastAudit(advance, auditList, lastId) {
  579. const transaction = await this.db.beginTransaction();
  580. try {
  581. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  582. const idList = this._.map(auditList, 'audit_id');
  583. let order = idList.length + 1;
  584. if (idList.indexOf(lastId) !== -1) {
  585. await transaction.delete(this.tableName, { vid: advance.id, times: advance.times, audit_id: lastId });
  586. const audit = this._.find(auditList, { 'audit_id': lastId });
  587. // 顺移之后审核人流程顺序
  588. await this._syncOrderByDelete(transaction, advance.id, audit.order, advance.times);
  589. order = order - 1;
  590. }
  591. // 添加终审
  592. const newAuditor = {
  593. tid: advance.tid, vid: advance.id, audit_id: lastId,
  594. times: advance.times, order, status: auditConst.status.uncheck,
  595. };
  596. await transaction.insert(this.tableName, newAuditor);
  597. await transaction.commit();
  598. } catch (err) {
  599. await transaction.rollback();
  600. throw err;
  601. }
  602. }
  603. /**
  604. * 取待审批期列表(wap用)
  605. *
  606. * @param auditorId
  607. * @return {Promise<*>}
  608. */
  609. async getAuditAdvanceByWap(auditorId) {
  610. const sql =
  611. 'SELECT ad.`id`, ad.`tid`, ad.`pay_ratio`, ad.`cur_amount`, ad.`prev_amount`, ad.`prev_total_amount`, ad.`type`, ad.`order`, ' +
  612. // ' s.`order` As `sorder`, s.`status` As `sstatus`, s.`s_time`, s.`contract_tp`, s.`qc_tp`, s.`pre_contract_tp`, s.`pre_qc_tp`, s.`yf_tp`, s.`pre_yf_tp`, ' +
  613. ' t.`name`, t.`project_id`, t.`user_id`,' +
  614. ' ti.`deal_info`, ti.`deal_param` ' +
  615. ' FROM ?? AS aa, ?? AS ad, ?? As t, ?? AS ti ' +
  616. ' WHERE aa.`audit_id` = ? and aa.`status` = ?' +
  617. ' and aa.`vid` = ad.`id` and aa.`tid` = t.`id` and ti.`tid` = t.`id`';
  618. const sqlParam = [
  619. this.tableName,
  620. this.ctx.service.advance.tableName,
  621. this.ctx.service.tender.tableName,
  622. this.ctx.service.tenderInfo.tableName,
  623. auditorId,
  624. auditConst.status.checking,
  625. ];
  626. return await this.db.query(sql, sqlParam);
  627. }
  628. }
  629. return AdvanceAudit;
  630. };