advance_audit.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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.`type`, 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. * 重新审批
  508. * @param { string } cid - 查询的清单
  509. * @return {Promise<*>} - 可用的变更令列表
  510. */
  511. async checkAgain(advance) {
  512. const accountId = this.ctx.session.sessionUser.accountId;
  513. // 初始化事务
  514. const time = new Date();
  515. const transaction = await this.db.beginTransaction();
  516. let result = false;
  517. try {
  518. const maxOrder = advance.auditors[advance.auditors.length - 1].order;
  519. const audits = advance.auditors.filter(x => { return x.order === maxOrder; });
  520. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  521. const selfAudit = audits.find(x => { return x.audit_id === accountId; });
  522. if (!selfAudit) throw '当前标段您无权审批';
  523. // 当前审批人2次添加至流程中
  524. const checkAgainAuditors = [];
  525. audits.forEach(x => {
  526. checkAgainAuditors.push({
  527. tid: advance.tid, vid: advance.id, audit_id: x.audit_id, type: x.type,
  528. times: x.times, order: maxOrder + 1,
  529. status: auditConst.status.checkAgain,
  530. create_time: time, end_time: time, opinion: '',
  531. });
  532. });
  533. const checkingAuditors = [];
  534. audits.forEach(x => {
  535. checkingAuditors.push({
  536. tid: advance.tid, vid: advance.id, audit_id: x.audit_id, type: x.type,
  537. times: x.times, order: maxOrder + 2,
  538. status: auditConst.status.checking,
  539. create_time: time,
  540. });
  541. });
  542. await transaction.insert(this.tableName, checkAgainAuditors);
  543. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  544. // 获取刚批量添加的所有list
  545. // for (let j = 0; j < checkingAuditors.length; j++) {
  546. // checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  547. // }
  548. // 设置审批中
  549. await transaction.update(this.ctx.service.advance.tableName, {
  550. id: advance.id,
  551. status: auditConst.status.checking,
  552. end_time: null,
  553. });
  554. await transaction.commit();
  555. result = true;
  556. } catch (error) {
  557. console.log(error);
  558. await transaction.rollback();
  559. result = false;
  560. }
  561. return result;
  562. }
  563. /**
  564. * 用于添加推送所需的content内容
  565. * @param {Number} pid 项目id
  566. * @param {Number} tid 台账id
  567. * @param {Number} vid 预付款id
  568. * @param {Number} uid 审批人id
  569. */
  570. async getNoticeContent(pid, tid, vid, uid, opinion = '') {
  571. const noticeSql =
  572. 'SELECT * FROM (SELECT ' +
  573. ' 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`' +
  574. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  575. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  576. ' LEFT JOIN ?? As ad ON m.`id` = ad.`vid`' +
  577. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  578. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  579. const noticeSqlParam = [
  580. this.ctx.service.tender.tableName,
  581. tid,
  582. this.ctx.service.advance.tableName,
  583. vid,
  584. this.tableName,
  585. this.ctx.service.projectAccount.tableName,
  586. uid,
  587. pid,
  588. ];
  589. const content = await this.db.query(noticeSql, noticeSqlParam);
  590. if (content.length) {
  591. content[0].opinion = opinion;
  592. }
  593. return content.length ? JSON.stringify(content[0]) : '';
  594. }
  595. /**
  596. * 通过状态获取审核人信息
  597. * @param {Number} vid - 预付款id
  598. * @param {Number} status - 期状态
  599. * @param {Number} times - 审批次数
  600. * @return {Object} auditor 审核人信息
  601. */
  602. async getAuditorByStatus(vid, status, times = 1) {
  603. let auditor = null;
  604. let sql = '';
  605. let sqlParam = '';
  606. switch (status) {
  607. case auditConst.status.checking:
  608. case auditConst.status.checked:
  609. case auditConst.status.checkNoPre:
  610. sql =
  611. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  612. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id` ' +
  613. ' WHERE la.`vid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  614. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, status];
  615. auditor = await this.db.queryOne(sql, sqlParam);
  616. break;
  617. case auditConst.status.checkNo:
  618. sql =
  619. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  620. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  621. ' WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  622. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checkNo, parseInt(times) - 1];
  623. auditor = await this.db.queryOne(sql, sqlParam);
  624. break;
  625. case auditConst.status.uncheck:
  626. default:
  627. break;
  628. }
  629. return auditor;
  630. }
  631. /**
  632. * 获取所有审核人
  633. * @param {Number} tenderId 标段id
  634. */
  635. async getAllAuditors(tenderId) {
  636. const sql = 'SELECT au.audit_id, au.tid FROM ' + this.tableName + ' au' +
  637. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On au.tid = t.id' +
  638. ' WHERE t.id = ?' +
  639. ' GROUP BY au.audit_id';
  640. const sqlParam = [tenderId];
  641. return this.db.query(sql, sqlParam);
  642. }
  643. /**
  644. * 获取待处理审核列表
  645. * @param {Number} auditorId 审核人id
  646. */
  647. async getAuditAdvance(auditorId) {
  648. const sql = 'SELECT ma.`audit_id`, ma.`times`, ma.`order`, ma.`create_time`, ma.`end_time`, ma.`tid`, ma.`vid`,' +
  649. ' m.`order` As `morder`, m.`status` As `mstatus`, m.`type` As `mtype`,' +
  650. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  651. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  652. ' WHERE ((ma.`audit_id` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  653. ' and ma.`vid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`create_time` DESC';
  654. 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];
  655. return await this.db.query(sql, sqlParam);
  656. }
  657. /**
  658. * 获取tender下所有的审核列表(报表用)
  659. * @param {Number} tenderId
  660. */
  661. async getAuditAdvanceByTender(tenderId) {
  662. const sql = 'SELECT ma.`tid`, ma.`vid`, ma.`type`, ma.`times`, ma.`order`, ma.`audit_id`, ma.`status`, ma.`create_time`, ma.`end_time`, ma.`opinion`' +
  663. ' FROM ?? AS ma ' +
  664. ' WHERE ma.`tid` = ?';
  665. const sqlParam = [this.tableName, tenderId];
  666. return await this.db.query(sql, sqlParam);
  667. }
  668. /**
  669. * 获取审核人审核的次数
  670. *
  671. * @param auditorId
  672. * @return {Promise<*>}
  673. */
  674. async getCountByChecked(auditorId) {
  675. return await this.db.count(this.tableName, { audit_id: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  676. }
  677. /**
  678. * 获取最近一次审批结束时间
  679. *
  680. * @param auditorId
  681. * @return {Promise<*>}
  682. */
  683. async getLastEndTimeByChecked(auditorId) {
  684. const sql = 'SELECT `end_time` FROM ?? WHERE `audit_id` = ? ' +
  685. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
  686. const sqlParam = [this.tableName, auditorId];
  687. const result = await this.db.queryOne(sql, sqlParam);
  688. return result ? result.end_time : null;
  689. }
  690. async updateNewAuditList(advance, newIdList) {
  691. const transaction = await this.db.beginTransaction();
  692. try {
  693. // 先删除旧的审批流,再添加新的
  694. await transaction.delete(this.tableName, { vid: advance.id, times: advance.times });
  695. const newAuditors = [];
  696. let order = 1;
  697. for (const aid of newIdList) {
  698. newAuditors.push({
  699. tid: advance.tid, vid: advance.id, audit_id: aid,
  700. times: advance.times, order, status: auditConst.status.uncheck,
  701. });
  702. order++;
  703. }
  704. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  705. await transaction.commit();
  706. } catch (err) {
  707. await transaction.rollback();
  708. throw err;
  709. }
  710. }
  711. async updateLastAudit(advance, auditList, lastId) {
  712. const transaction = await this.db.beginTransaction();
  713. try {
  714. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  715. const idList = this._.map(auditList, 'audit_id');
  716. let order = idList.length + 1;
  717. if (idList.indexOf(lastId) !== -1) {
  718. await transaction.delete(this.tableName, { vid: advance.id, times: advance.times, audit_id: lastId });
  719. const audit = this._.find(auditList, { 'audit_id': lastId });
  720. // 顺移之后审核人流程顺序
  721. await this._syncOrderByDelete(transaction, advance.id, audit.order, advance.times);
  722. order = order - 1;
  723. }
  724. // 添加终审
  725. const newAuditor = {
  726. tid: advance.tid, vid: advance.id, audit_id: lastId,
  727. times: advance.times, order, status: auditConst.status.uncheck,
  728. };
  729. await transaction.insert(this.tableName, newAuditor);
  730. await transaction.commit();
  731. } catch (err) {
  732. await transaction.rollback();
  733. throw err;
  734. }
  735. }
  736. /**
  737. * 取待审批期列表(wap用)
  738. *
  739. * @param auditorId
  740. * @return {Promise<*>}
  741. */
  742. async getAuditAdvanceByWap(auditorId) {
  743. const sql =
  744. 'SELECT ad.`id`, ad.`tid`, ad.`pay_ratio`, ad.`cur_amount`, ad.`prev_amount`, ad.`prev_total_amount`, ad.`type`, ad.`order`, ' +
  745. // ' 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`, ' +
  746. ' t.`name`, t.`project_id`, t.`user_id`,' +
  747. ' ti.`deal_info`, ti.`deal_param` ' +
  748. ' FROM ?? AS aa, ?? AS ad, ?? As t, ?? AS ti ' +
  749. ' WHERE aa.`audit_id` = ? and aa.`status` = ?' +
  750. ' and aa.`vid` = ad.`id` and aa.`tid` = t.`id` and ti.`tid` = t.`id`';
  751. const sqlParam = [
  752. this.tableName,
  753. this.ctx.service.advance.tableName,
  754. this.ctx.service.tender.tableName,
  755. this.ctx.service.tenderInfo.tableName,
  756. auditorId,
  757. auditConst.status.checking,
  758. ];
  759. return await this.db.query(sql, sqlParam);
  760. }
  761. }
  762. return AdvanceAudit;
  763. };