advance_audit.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.YFK, {
  234. pid: this.ctx.session.sessionProject.id,
  235. tid: this.ctx.tender.id,
  236. uid: audit.audit_id,
  237. sp_type: 'advance',
  238. sp_id: audit.id,
  239. table_name: this.tableName,
  240. template: wxConst.template.advance,
  241. wx_data: wechatData,
  242. });
  243. // 审批通过 - 检查三方特殊推送
  244. await this.ctx.service.specMsg.addAdvanceMsg(transaction, this.ctx.session.sessionProject.id, advanceInfo, pushOperate.advance.flow);
  245. await transaction.commit();
  246. } catch (err) {
  247. await transaction.rollback();
  248. throw err;
  249. }
  250. return true;
  251. }
  252. async _checked(pid, advanceId, checkData, times) {
  253. const time = new Date();
  254. // 整理当前流程审核人状态更新
  255. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  256. if (!audit) {
  257. throw '审核数据错误';
  258. }
  259. // 获取审核人列表
  260. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC';
  261. const sqlParam = [this.tableName, advanceId, times];
  262. const auditors = await this.db.query(sql, sqlParam);
  263. const nextAudit = await this.getDataByCondition({ vid: advanceId, times, order: audit.order + 1 });
  264. const transaction = await this.db.beginTransaction();
  265. try {
  266. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  267. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  268. // 获取推送必要信息
  269. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id, checkData.opinion);
  270. // 添加推送
  271. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checked, content: noticeContent }];
  272. auditors.forEach(audit => {
  273. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checked, content: noticeContent });
  274. });
  275. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  276. // 无下一审核人表示,审核结束
  277. const advanceInfo = await this.ctx.service.advance.getDataById(advanceId);
  278. const shenpiUrl = await this.ctx.helper.urlToShort(
  279. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + advanceInfo.tid + '/advance/' + advanceInfo.id + '/detail'
  280. );
  281. if (nextAudit) {
  282. // 流程至下一审批人
  283. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, create_time: time });
  284. // 同步期信息
  285. await transaction.update(this.ctx.service.advance.tableName, {
  286. id: advanceId,
  287. status: auditConst.status.checking,
  288. });
  289. // 微信模板通知
  290. const wechatData = {
  291. wap_url: shenpiUrl,
  292. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  293. status: wxConst.status.check,
  294. tips: wxConst.tips.check,
  295. tp: parseFloat(advanceInfo.cur_amount),
  296. code: this.ctx.session.sessionProject.code,
  297. };
  298. await this.ctx.helper.sendWechat(nextAudit.audit_id, smsTypeConst.const.YFK, smsTypeConst.judge.approval.toString(), wxConst.template.advance, wechatData);
  299. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.YFK, {
  300. pid,
  301. tid: this.ctx.tender.id,
  302. uid: nextAudit.audit_id,
  303. sp_type: 'advance',
  304. sp_id: nextAudit.id,
  305. table_name: this.tableName,
  306. template: wxConst.template.advance,
  307. wx_data: wechatData,
  308. });
  309. // 审批通过 - 检查三方特殊推送
  310. await this.ctx.service.specMsg.addAdvanceMsg(transaction, pid, advanceInfo, pushOperate.advance.flow);
  311. } else {
  312. await transaction.update(this.ctx.service.advance.tableName, {
  313. id: advanceId,
  314. status: checkData.checkType,
  315. end_time: time,
  316. });
  317. // 微信模板通知
  318. const users = this._.uniq(this._.concat(this._.map(auditors, 'audit_id'), advanceInfo.uid));
  319. const wechatData = {
  320. wap_url: shenpiUrl,
  321. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  322. status: wxConst.status.success,
  323. tips: wxConst.tips.success,
  324. tp: parseFloat(advanceInfo.cur_amount),
  325. code: this.ctx.session.sessionProject.code,
  326. };
  327. await this.ctx.helper.sendWechat(users, smsTypeConst.const.YFK, smsTypeConst.judge.result.toString(), wxConst.template.advance, wechatData);
  328. // 审批通过 - 检查三方特殊推送
  329. await this.ctx.service.specMsg.addAdvanceMsg(transaction, pid, advanceInfo, pushOperate.advance.flow);
  330. await this.ctx.service.specMsg.addAdvanceMsg(transaction, pid, advanceInfo, pushOperate.advance.checked);
  331. }
  332. await transaction.commit();
  333. if (!nextAudit) this.ctx.service.tenderCache.updateAdvanceCache(audit.tid);
  334. } catch (err) {
  335. await transaction.rollback();
  336. throw err;
  337. }
  338. }
  339. async _checkNo(pid, advanceId, checkData, times) {
  340. const time = new Date();
  341. // 整理当前流程审核人状态更新
  342. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  343. if (!audit) {
  344. throw '审核数据错误';
  345. }
  346. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC';
  347. const sqlParam = [this.tableName, advanceId, times];
  348. const auditors = await this.db.query(sql, sqlParam);
  349. let order = 1;
  350. for (const a of auditors) {
  351. a.times = times + 1;
  352. a.order = order;
  353. a.status = auditConst.status.uncheck;
  354. order++;
  355. }
  356. const transaction = await this.db.beginTransaction();
  357. try {
  358. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  359. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  360. // 添加到消息推送表
  361. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id, checkData.opinion);
  362. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNo, content: noticeContent }];
  363. auditors.forEach(audit => {
  364. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNo, content: noticeContent });
  365. });
  366. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  367. // 同步期信息
  368. await transaction.update(this.ctx.service.advance.tableName, {
  369. id: advanceId,
  370. status: checkData.checkType,
  371. times: times + 1,
  372. });
  373. // 微信模板通知
  374. const advanceInfo = await this.ctx.service.advance.getDataById(advanceId);
  375. const users = this._.uniq(this._.concat(this._.map(auditors, 'audit_id'), advanceInfo.uid));
  376. const shenpiUrl = await this.ctx.helper.urlToShort(
  377. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + advanceInfo.tid + '/advance/' + advanceInfo.id + '/detail'
  378. );
  379. const wechatData = {
  380. wap_url: shenpiUrl,
  381. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  382. status: wxConst.status.back,
  383. tips: wxConst.tips.back,
  384. tp: parseFloat(advanceInfo.cur_amount),
  385. code: this.ctx.session.sessionProject.code,
  386. };
  387. await this.ctx.helper.sendWechat(users, smsTypeConst.const.YFK, smsTypeConst.judge.result.toString(), wxConst.template.advance, wechatData);
  388. // 拷贝新一次审核流程列表
  389. await transaction.insert(this.tableName, auditors);
  390. // 检查三方特殊推送
  391. await this.ctx.service.specMsg.addAdvanceMsg(transaction, pid, advanceInfo, pushOperate.advance.flow);
  392. await transaction.commit();
  393. } catch (err) {
  394. await transaction.rollback();
  395. throw err;
  396. }
  397. }
  398. async _checkNoPre(pid, advanceId, checkData, times) {
  399. const time = new Date();
  400. // 整理当前流程审核人状态更新
  401. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  402. if (!audit || audit.order <= 1) {
  403. throw '审核数据错误';
  404. }
  405. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  406. const auditors2 = await this.getAuditGroupByList(advanceId, times);
  407. const auditorIndex = await auditors2.findIndex(function(item) {
  408. return item.audit_id === audit.audit_id;
  409. });
  410. const preAuditor = auditors2[auditorIndex - 1];
  411. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id, checkData.opinion);
  412. const transaction = await this.db.beginTransaction();
  413. try {
  414. // 添加到消息推送表
  415. const records = [{ pid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNoPre, content: noticeContent }];
  416. auditors2.forEach(audit => {
  417. records.push({ pid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNoPre, content: noticeContent });
  418. });
  419. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  420. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  421. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  422. // 顺移气候审核人流程顺序
  423. this.initSqlBuilder();
  424. this.sqlBuilder.setAndWhere('vid', { value: advanceId, operate: '=' });
  425. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  426. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  427. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  428. await transaction.query(sql, sqlParam);
  429. const newAuditors = {
  430. tid: audit.tid,
  431. vid: audit.vid,
  432. audit_id: preAuditor.audit_id,
  433. times: audit.times,
  434. order: audit.order + 1,
  435. status: auditConst.status.checking,
  436. create_time: time,
  437. };
  438. const checking_result = await transaction.insert(this.tableName, newAuditors);
  439. const uncheckNewAuditors = {
  440. tid: audit.tid,
  441. vid: audit.vid,
  442. audit_id: audit.audit_id,
  443. times: audit.times,
  444. order: audit.order + 2,
  445. status: auditConst.status.uncheck,
  446. };
  447. await transaction.insert(this.tableName, uncheckNewAuditors);
  448. // 微信模板通知
  449. const advanceInfo = await this.ctx.service.advance.getDataById(advanceId);
  450. const shenpiUrl = await this.ctx.helper.urlToShort(
  451. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + advanceInfo.tid + '/advance/' + advanceInfo.id + '/detail'
  452. );
  453. const wechatData = {
  454. wap_url: shenpiUrl,
  455. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  456. status: wxConst.status.check,
  457. tips: wxConst.tips.check,
  458. tp: parseFloat(advanceInfo.cur_amount),
  459. code: this.ctx.session.sessionProject.code,
  460. };
  461. await this.ctx.helper.sendWechat(preAuditor.audit_id, smsTypeConst.const.YFK, smsTypeConst.judge.approval.toString(), wxConst.template.advance, wechatData);
  462. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.YFK, {
  463. pid,
  464. tid: this.ctx.tender.id,
  465. uid: preAuditor.audit_id,
  466. sp_type: 'advance',
  467. sp_id: checking_result.insertId,
  468. table_name: this.tableName,
  469. template: wxConst.template.advance,
  470. wx_data: wechatData,
  471. });
  472. // 检查三方特殊推送
  473. await this.ctx.service.specMsg.addAdvanceMsg(transaction, pid, advanceInfo, pushOperate.advance.flow);
  474. await transaction.commit();
  475. } catch (error) {
  476. await transaction.rollback();
  477. throw error;
  478. }
  479. }
  480. /**
  481. * 审批
  482. * @param {Number} advanceId - 预付款id
  483. * @param {auditConst.status.checked|auditConst.status.checkNo} checkData - 审批结果
  484. * @param {Number} times - 第几次审批
  485. * @return {Promise<void>}
  486. */
  487. async check(advanceId, checkData, times = 1) {
  488. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  489. throw '提交数据错误';
  490. }
  491. const pid = this.ctx.session.sessionProject.id;
  492. switch (checkData.checkType) {
  493. case auditConst.status.checked:
  494. await this._checked(pid, advanceId, checkData, times);
  495. break;
  496. case auditConst.status.checkNo:
  497. await this._checkNo(pid, advanceId, checkData, times);
  498. break;
  499. case auditConst.status.checkNoPre:
  500. await this._checkNoPre(pid, advanceId, checkData, times);
  501. break;
  502. default:
  503. throw '无效审批操作';
  504. }
  505. }
  506. /**
  507. * 用于添加推送所需的content内容
  508. * @param {Number} pid 项目id
  509. * @param {Number} tid 台账id
  510. * @param {Number} vid 预付款id
  511. * @param {Number} uid 审批人id
  512. */
  513. async getNoticeContent(pid, tid, vid, uid, opinion = '') {
  514. const noticeSql =
  515. 'SELECT * FROM (SELECT ' +
  516. ' 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`' +
  517. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  518. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  519. ' LEFT JOIN ?? As ad ON m.`id` = ad.`vid`' +
  520. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  521. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  522. const noticeSqlParam = [
  523. this.ctx.service.tender.tableName,
  524. tid,
  525. this.ctx.service.advance.tableName,
  526. vid,
  527. this.tableName,
  528. this.ctx.service.projectAccount.tableName,
  529. uid,
  530. pid,
  531. ];
  532. const content = await this.db.query(noticeSql, noticeSqlParam);
  533. if (content.length) {
  534. content[0].opinion = opinion;
  535. }
  536. return content.length ? JSON.stringify(content[0]) : '';
  537. }
  538. /**
  539. * 通过状态获取审核人信息
  540. * @param {Number} vid - 预付款id
  541. * @param {Number} status - 期状态
  542. * @param {Number} times - 审批次数
  543. * @return {Object} auditor 审核人信息
  544. */
  545. async getAuditorByStatus(vid, status, times = 1) {
  546. let auditor = null;
  547. let sql = '';
  548. let sqlParam = '';
  549. switch (status) {
  550. case auditConst.status.checking:
  551. case auditConst.status.checked:
  552. case auditConst.status.checkNoPre:
  553. sql =
  554. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  555. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id` ' +
  556. ' WHERE la.`vid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  557. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, status];
  558. auditor = await this.db.queryOne(sql, sqlParam);
  559. break;
  560. case auditConst.status.checkNo:
  561. sql =
  562. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  563. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  564. ' WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  565. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checkNo, parseInt(times) - 1];
  566. auditor = await this.db.queryOne(sql, sqlParam);
  567. break;
  568. case auditConst.status.uncheck:
  569. default:
  570. break;
  571. }
  572. return auditor;
  573. }
  574. /**
  575. * 获取所有审核人
  576. * @param {Number} tenderId 标段id
  577. */
  578. async getAllAuditors(tenderId) {
  579. const sql = 'SELECT au.audit_id, au.tid FROM ' + this.tableName + ' au' +
  580. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On au.tid = t.id' +
  581. ' WHERE t.id = ?' +
  582. ' GROUP BY au.audit_id';
  583. const sqlParam = [tenderId];
  584. return this.db.query(sql, sqlParam);
  585. }
  586. /**
  587. * 获取待处理审核列表
  588. * @param {Number} auditorId 审核人id
  589. */
  590. async getAuditAdvance(auditorId) {
  591. const sql = 'SELECT ma.`audit_id`, ma.`times`, ma.`order`, ma.`create_time`, ma.`end_time`, ma.`tid`, ma.`vid`,' +
  592. ' m.`order` As `morder`, m.`status` As `mstatus`, m.`type` As `mtype`,' +
  593. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  594. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  595. ' WHERE ((ma.`audit_id` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  596. ' and ma.`vid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`create_time` DESC';
  597. 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];
  598. return await this.db.query(sql, sqlParam);
  599. }
  600. /**
  601. * 获取审核人审核的次数
  602. *
  603. * @param auditorId
  604. * @return {Promise<*>}
  605. */
  606. async getCountByChecked(auditorId) {
  607. return await this.db.count(this.tableName, { audit_id: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  608. }
  609. /**
  610. * 获取最近一次审批结束时间
  611. *
  612. * @param auditorId
  613. * @return {Promise<*>}
  614. */
  615. async getLastEndTimeByChecked(auditorId) {
  616. const sql = 'SELECT `end_time` FROM ?? WHERE `audit_id` = ? ' +
  617. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
  618. const sqlParam = [this.tableName, auditorId];
  619. const result = await this.db.queryOne(sql, sqlParam);
  620. return result ? result.end_time : null;
  621. }
  622. async updateNewAuditList(advance, newIdList) {
  623. const transaction = await this.db.beginTransaction();
  624. try {
  625. // 先删除旧的审批流,再添加新的
  626. await transaction.delete(this.tableName, { vid: advance.id, times: advance.times });
  627. const newAuditors = [];
  628. let order = 1;
  629. for (const aid of newIdList) {
  630. newAuditors.push({
  631. tid: advance.tid, vid: advance.id, audit_id: aid,
  632. times: advance.times, order, status: auditConst.status.uncheck,
  633. });
  634. order++;
  635. }
  636. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  637. await transaction.commit();
  638. } catch (err) {
  639. await transaction.rollback();
  640. throw err;
  641. }
  642. }
  643. async updateLastAudit(advance, auditList, lastId) {
  644. const transaction = await this.db.beginTransaction();
  645. try {
  646. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  647. const idList = this._.map(auditList, 'audit_id');
  648. let order = idList.length + 1;
  649. if (idList.indexOf(lastId) !== -1) {
  650. await transaction.delete(this.tableName, { vid: advance.id, times: advance.times, audit_id: lastId });
  651. const audit = this._.find(auditList, { 'audit_id': lastId });
  652. // 顺移之后审核人流程顺序
  653. await this._syncOrderByDelete(transaction, advance.id, audit.order, advance.times);
  654. order = order - 1;
  655. }
  656. // 添加终审
  657. const newAuditor = {
  658. tid: advance.tid, vid: advance.id, audit_id: lastId,
  659. times: advance.times, order, status: auditConst.status.uncheck,
  660. };
  661. await transaction.insert(this.tableName, newAuditor);
  662. await transaction.commit();
  663. } catch (err) {
  664. await transaction.rollback();
  665. throw err;
  666. }
  667. }
  668. /**
  669. * 取待审批期列表(wap用)
  670. *
  671. * @param auditorId
  672. * @return {Promise<*>}
  673. */
  674. async getAuditAdvanceByWap(auditorId) {
  675. const sql =
  676. 'SELECT ad.`id`, ad.`tid`, ad.`pay_ratio`, ad.`cur_amount`, ad.`prev_amount`, ad.`prev_total_amount`, ad.`type`, ad.`order`, ' +
  677. // ' 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`, ' +
  678. ' t.`name`, t.`project_id`, t.`user_id`,' +
  679. ' ti.`deal_info`, ti.`deal_param` ' +
  680. ' FROM ?? AS aa, ?? AS ad, ?? As t, ?? AS ti ' +
  681. ' WHERE aa.`audit_id` = ? and aa.`status` = ?' +
  682. ' and aa.`vid` = ad.`id` and aa.`tid` = t.`id` and ti.`tid` = t.`id`';
  683. const sqlParam = [
  684. this.tableName,
  685. this.ctx.service.advance.tableName,
  686. this.ctx.service.tender.tableName,
  687. this.ctx.service.tenderInfo.tableName,
  688. auditorId,
  689. auditConst.status.checking,
  690. ];
  691. return await this.db.query(sql, sqlParam);
  692. }
  693. }
  694. return AdvanceAudit;
  695. };