advance_audit.js 33 KB

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