advance_audit.js 34 KB

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