advance_audit.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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, type, 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. type,
  79. audit_id,
  80. times,
  81. order: newOrder,
  82. status: auditConst.status.uncheck,
  83. };
  84. const result = await transaction.insert(this.tableName, record);
  85. await transaction.commit();
  86. return result && result.affectedRows === 1;
  87. } catch (err) {
  88. await transaction.rollback();
  89. throw err;
  90. }
  91. return false;
  92. }
  93. /**
  94. * 移除审核人
  95. * @param {Number} vid - 预付款id
  96. * @param {Number} audit_id - 审核人id
  97. * @param {Number} times - 第几次审批
  98. * @return {Promise<boolean>}
  99. */
  100. async deleteAuditor(vid, audit_id, times = 1) {
  101. const transaction = await this.db.beginTransaction();
  102. try {
  103. const condition = { vid, audit_id, times };
  104. const auditor = await this.getDataByCondition(condition);
  105. if (!auditor) {
  106. throw '该审核人不存在';
  107. }
  108. await this._syncOrderByDelete(transaction, vid, auditor.order, times);
  109. await transaction.delete(this.tableName, condition);
  110. await transaction.commit();
  111. } catch (err) {
  112. await transaction.rollback();
  113. throw err;
  114. }
  115. return true;
  116. }
  117. /**
  118. * 移除审核人时,同步其后审核人order
  119. * @param {Function} transaction - 事务
  120. * @param {Number} vid - 预付款id
  121. * @param {Number} order - 审核顺序
  122. * @param {Number} times - 第几次审批
  123. * @return {Promise<*>} 查询结果集
  124. * @private
  125. */
  126. async _syncOrderByDelete(transaction, vid, order, times, selfOperate = '-') {
  127. this.initSqlBuilder();
  128. this.sqlBuilder.setAndWhere('vid', {
  129. value: this.db.escape(vid),
  130. operate: '=',
  131. });
  132. this.sqlBuilder.setAndWhere('order', {
  133. value: order,
  134. operate: '>=',
  135. });
  136. this.sqlBuilder.setAndWhere('times', {
  137. value: times,
  138. operate: '=',
  139. });
  140. this.sqlBuilder.setUpdateData('order', {
  141. value: 1,
  142. selfOperate: selfOperate,
  143. });
  144. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  145. const data = await transaction.query(sql, sqlParam);
  146. return data;
  147. }
  148. /**
  149. * 获取当前审核人
  150. * @param {Number} vid - 预付款id
  151. * @param {Number} times - 第几次审批
  152. * @return {Promise<Object>} 查询结果集
  153. */
  154. async getCurAuditor(vid, times = 1) {
  155. const sql =
  156. '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` ' +
  157. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id` ' +
  158. ' WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ?';
  159. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checking, times];
  160. return await this.db.queryOne(sql, sqlParam);
  161. }
  162. /**
  163. * 获取审核列表信息
  164. * @param {Number} vid - 预付款id
  165. * @param {Number} times - 第几次审批
  166. * @return {Promise<Array>} 查询结果集
  167. */
  168. async getAuditors(vid, times = 1) {
  169. const sql =
  170. '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` ' +
  171. '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 ' +
  172. 'WHERE la.`vid` = ? and la.`times` = ? and la.`audit_id` = pa.`id` and g.`audit_id` = la.`audit_id` order by la.`order`';
  173. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, vid, times, vid, times];
  174. const result = await this.db.query(sql, sqlParam);
  175. const sql2 = 'SELECT COUNT(a.`audit_id`) as num FROM (SELECT `audit_id` FROM ?? WHERE `vid` = ? AND `times` = ? GROUP BY `audit_id`) as a';
  176. const sqlParam2 = [this.tableName, vid, times];
  177. const count = await this.db.queryOne(sql2, sqlParam2);
  178. for (const i in result) {
  179. result[i].max_sort = count.num;
  180. }
  181. return result;
  182. }
  183. /**
  184. * 获取审核人信息
  185. * @param {Number} vid - 预付款id
  186. * @param {Number} audit_id - 审核人id
  187. * @param {Number} times - 第几次审批
  188. * @return {Promise<*>} 查询结果
  189. */
  190. async getAuditor(vid, audit_id, times = 1) {
  191. const sql =
  192. '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` ' +
  193. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id` ' +
  194. ' WHERE la.`vid` = ? and la.`audit_id` = ? and la.`times` = ?';
  195. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, audit_id, times];
  196. return await this.db.queryOne(sql, sqlParam);
  197. }
  198. /**
  199. * 开始审批
  200. * @param {Number} vid - 预付款id
  201. * @param {Number} times - 第几次审批
  202. * @param {Object} data - 载荷
  203. */
  204. async start(vid, times = 1, data) {
  205. const audit = await this.getDataByCondition({ vid, times, order: 1 });
  206. if (!audit) {
  207. if(this.ctx.tender.info.shenpi.advance === shenpiConst.sp_status.gdspl) {
  208. throw '请联系管理员添加审批人';
  209. } else {
  210. throw '请先选择审批人,再上报数据';
  211. }
  212. }
  213. const transaction = await this.db.beginTransaction();
  214. try {
  215. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, create_time: new Date() });
  216. await transaction.update(this.ctx.service.advance.tableName, {
  217. id: audit.vid,
  218. ...data,
  219. });
  220. // 微信模板通知
  221. const advanceInfo = await this.ctx.service.advance.getDataById(vid);
  222. const shenpiUrl = await this.ctx.helper.urlToShort(
  223. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + advanceInfo.tid + '/advance/' + advanceInfo.id + '/detail'
  224. );
  225. const wechatData = {
  226. wap_url: shenpiUrl,
  227. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  228. status: wxConst.status.check,
  229. tips: wxConst.tips.check,
  230. tp: parseFloat(advanceInfo.cur_amount),
  231. code: this.ctx.session.sessionProject.code,
  232. };
  233. await this.ctx.helper.sendWechat(audit.audit_id, smsTypeConst.const.YFK, smsTypeConst.judge.approval.toString(), wxConst.template.advance, wechatData);
  234. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.YFK, {
  235. pid: this.ctx.session.sessionProject.id,
  236. tid: this.ctx.tender.id,
  237. uid: audit.audit_id,
  238. sp_type: 'advance',
  239. sp_id: audit.id,
  240. table_name: this.tableName,
  241. template: wxConst.template.advance,
  242. wx_data: wechatData,
  243. });
  244. // 审批通过 - 检查三方特殊推送
  245. await this.ctx.service.specMsg.addAdvanceMsg(transaction, this.ctx.session.sessionProject.id, advanceInfo, pushOperate.advance.flow);
  246. await transaction.commit();
  247. } catch (err) {
  248. await transaction.rollback();
  249. throw err;
  250. }
  251. return true;
  252. }
  253. async _checked(pid, advanceId, checkData, times) {
  254. const time = new Date();
  255. // 整理当前流程审核人状态更新
  256. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  257. if (!audit) {
  258. throw '审核数据错误';
  259. }
  260. // 获取审核人列表
  261. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC';
  262. const sqlParam = [this.tableName, advanceId, times];
  263. const auditors = await this.db.query(sql, sqlParam);
  264. const nextAudit = await this.getDataByCondition({ vid: advanceId, times, order: audit.order + 1 });
  265. const transaction = await this.db.beginTransaction();
  266. try {
  267. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  268. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  269. // 获取推送必要信息
  270. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id, checkData.opinion);
  271. // 添加推送
  272. const records = [{ pid, tid: audit.tid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checked, content: noticeContent }];
  273. auditors.forEach(audit => {
  274. records.push({ pid, tid: audit.tid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checked, content: noticeContent });
  275. });
  276. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  277. // 无下一审核人表示,审核结束
  278. const advanceInfo = await this.ctx.service.advance.getDataById(advanceId);
  279. const shenpiUrl = await this.ctx.helper.urlToShort(
  280. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + advanceInfo.tid + '/advance/' + advanceInfo.id + '/detail'
  281. );
  282. if (nextAudit) {
  283. // 流程至下一审批人
  284. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, create_time: time });
  285. // 同步期信息
  286. await transaction.update(this.ctx.service.advance.tableName, {
  287. id: advanceId,
  288. status: auditConst.status.checking,
  289. });
  290. // 微信模板通知
  291. const wechatData = {
  292. wap_url: shenpiUrl,
  293. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  294. status: wxConst.status.check,
  295. tips: wxConst.tips.check,
  296. tp: parseFloat(advanceInfo.cur_amount),
  297. code: this.ctx.session.sessionProject.code,
  298. };
  299. await this.ctx.helper.sendWechat(nextAudit.audit_id, smsTypeConst.const.YFK, smsTypeConst.judge.approval.toString(), wxConst.template.advance, wechatData);
  300. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.YFK, {
  301. pid,
  302. tid: this.ctx.tender.id,
  303. uid: nextAudit.audit_id,
  304. sp_type: 'advance',
  305. sp_id: nextAudit.id,
  306. table_name: this.tableName,
  307. template: wxConst.template.advance,
  308. wx_data: wechatData,
  309. });
  310. // 审批通过 - 检查三方特殊推送
  311. await this.ctx.service.specMsg.addAdvanceMsg(transaction, pid, advanceInfo, pushOperate.advance.flow);
  312. } else {
  313. await transaction.update(this.ctx.service.advance.tableName, {
  314. id: advanceId,
  315. status: checkData.checkType,
  316. end_time: time,
  317. });
  318. // 微信模板通知
  319. const users = this._.uniq(this._.concat(this._.map(auditors, 'audit_id'), advanceInfo.uid));
  320. const wechatData = {
  321. wap_url: shenpiUrl,
  322. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  323. status: wxConst.status.success,
  324. tips: wxConst.tips.success,
  325. tp: parseFloat(advanceInfo.cur_amount),
  326. code: this.ctx.session.sessionProject.code,
  327. };
  328. await this.ctx.helper.sendWechat(users, smsTypeConst.const.YFK, smsTypeConst.judge.result.toString(), wxConst.template.advance, wechatData);
  329. // 审批通过 - 检查三方特殊推送
  330. await this.ctx.service.specMsg.addAdvanceMsg(transaction, pid, advanceInfo, pushOperate.advance.flow);
  331. await this.ctx.service.specMsg.addAdvanceMsg(transaction, pid, advanceInfo, pushOperate.advance.checked);
  332. }
  333. await transaction.commit();
  334. if (!nextAudit) this.ctx.service.tenderCache.updateAdvanceCache(audit.tid);
  335. } catch (err) {
  336. await transaction.rollback();
  337. throw err;
  338. }
  339. }
  340. async _checkNo(pid, advanceId, checkData, times) {
  341. const time = new Date();
  342. // 整理当前流程审核人状态更新
  343. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  344. if (!audit) {
  345. throw '审核数据错误';
  346. }
  347. const sql = 'SELECT `tid`, `vid`, `audit_id`, `order` FROM ?? WHERE `vid` = ? and `times` = ? GROUP BY `audit_id` ORDER BY `id` ASC';
  348. const sqlParam = [this.tableName, advanceId, times];
  349. const auditors = await this.db.query(sql, sqlParam);
  350. let order = 1;
  351. for (const a of auditors) {
  352. a.times = times + 1;
  353. a.order = order;
  354. a.status = auditConst.status.uncheck;
  355. order++;
  356. }
  357. const transaction = await this.db.beginTransaction();
  358. try {
  359. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  360. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  361. // 添加到消息推送表
  362. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id, checkData.opinion);
  363. const records = [{ pid, tid: audit.tid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNo, content: noticeContent }];
  364. auditors.forEach(audit => {
  365. records.push({ pid, tid: audit.tid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNo, content: noticeContent });
  366. });
  367. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  368. // 同步期信息
  369. await transaction.update(this.ctx.service.advance.tableName, {
  370. id: advanceId,
  371. status: checkData.checkType,
  372. times: times + 1,
  373. });
  374. // 微信模板通知
  375. const advanceInfo = await this.ctx.service.advance.getDataById(advanceId);
  376. const users = this._.uniq(this._.concat(this._.map(auditors, 'audit_id'), advanceInfo.uid));
  377. const shenpiUrl = await this.ctx.helper.urlToShort(
  378. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + advanceInfo.tid + '/advance/' + advanceInfo.id + '/detail'
  379. );
  380. const wechatData = {
  381. wap_url: shenpiUrl,
  382. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  383. status: wxConst.status.back,
  384. tips: wxConst.tips.back,
  385. tp: parseFloat(advanceInfo.cur_amount),
  386. code: this.ctx.session.sessionProject.code,
  387. };
  388. await this.ctx.helper.sendWechat(users, smsTypeConst.const.YFK, smsTypeConst.judge.result.toString(), wxConst.template.advance, wechatData);
  389. // 拷贝新一次审核流程列表
  390. await transaction.insert(this.tableName, auditors);
  391. // 检查三方特殊推送
  392. await this.ctx.service.specMsg.addAdvanceMsg(transaction, pid, advanceInfo, pushOperate.advance.flow);
  393. await transaction.commit();
  394. } catch (err) {
  395. await transaction.rollback();
  396. throw err;
  397. }
  398. }
  399. async _checkNoPre(pid, advanceId, checkData, times, type) {
  400. const time = new Date();
  401. // 整理当前流程审核人状态更新
  402. const audit = await this.getDataByCondition({ vid: advanceId, times, status: auditConst.status.checking });
  403. if (!audit || audit.order <= 1) {
  404. throw '审核数据错误';
  405. }
  406. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  407. const auditors2 = await this.getAuditGroupByList(advanceId, times);
  408. const auditorIndex = await auditors2.findIndex(function(item) {
  409. return item.audit_id === audit.audit_id;
  410. });
  411. const preAuditor = auditors2[auditorIndex - 1];
  412. const noticeContent = await this.getNoticeContent(pid, audit.tid, advanceId, audit.audit_id, checkData.opinion);
  413. const transaction = await this.db.beginTransaction();
  414. try {
  415. // 添加到消息推送表
  416. const records = [{ pid, tid: audit.tid, type: pushType.advance, uid: this.ctx.advance.uid, status: auditConst.status.checkNoPre, content: noticeContent }];
  417. auditors2.forEach(audit => {
  418. records.push({ pid, tid: audit.tid, type: pushType.advance, uid: audit.audit_id, status: auditConst.status.checkNoPre, content: noticeContent });
  419. });
  420. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  421. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  422. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  423. // 顺移气候审核人流程顺序
  424. this.initSqlBuilder();
  425. this.sqlBuilder.setAndWhere('vid', { value: advanceId, operate: '=' });
  426. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  427. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  428. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  429. await transaction.query(sql, sqlParam);
  430. const newAuditors = {
  431. tid: audit.tid,
  432. vid: audit.vid,
  433. type,
  434. audit_id: preAuditor.audit_id,
  435. times: audit.times,
  436. order: audit.order + 1,
  437. status: auditConst.status.checking,
  438. create_time: time,
  439. };
  440. const checking_result = await transaction.insert(this.tableName, newAuditors);
  441. const uncheckNewAuditors = {
  442. tid: audit.tid,
  443. vid: audit.vid,
  444. type,
  445. audit_id: audit.audit_id,
  446. times: audit.times,
  447. order: audit.order + 2,
  448. status: auditConst.status.uncheck,
  449. };
  450. await transaction.insert(this.tableName, uncheckNewAuditors);
  451. // 微信模板通知
  452. const advanceInfo = await this.ctx.service.advance.getDataById(advanceId);
  453. const shenpiUrl = await this.ctx.helper.urlToShort(
  454. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + advanceInfo.tid + '/advance/' + advanceInfo.id + '/detail'
  455. );
  456. const wechatData = {
  457. wap_url: shenpiUrl,
  458. qi: advanceConst.typeCol[advanceInfo.type].name + '第' + advanceInfo.order + '期',
  459. status: wxConst.status.check,
  460. tips: wxConst.tips.check,
  461. tp: parseFloat(advanceInfo.cur_amount),
  462. code: this.ctx.session.sessionProject.code,
  463. };
  464. await this.ctx.helper.sendWechat(preAuditor.audit_id, smsTypeConst.const.YFK, smsTypeConst.judge.approval.toString(), wxConst.template.advance, wechatData);
  465. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.YFK, {
  466. pid,
  467. tid: this.ctx.tender.id,
  468. uid: preAuditor.audit_id,
  469. sp_type: 'advance',
  470. sp_id: checking_result.insertId,
  471. table_name: this.tableName,
  472. template: wxConst.template.advance,
  473. wx_data: wechatData,
  474. });
  475. // 检查三方特殊推送
  476. await this.ctx.service.specMsg.addAdvanceMsg(transaction, pid, advanceInfo, pushOperate.advance.flow);
  477. await transaction.commit();
  478. } catch (error) {
  479. await transaction.rollback();
  480. throw error;
  481. }
  482. }
  483. /**
  484. * 审批
  485. * @param {Number} advanceId - 预付款id
  486. * @param {auditConst.status.checked|auditConst.status.checkNo} checkData - 审批结果
  487. * @param {Number} times - 第几次审批
  488. * @return {Promise<void>}
  489. */
  490. async check(advanceId, checkData, times = 1, type) {
  491. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  492. throw '提交数据错误';
  493. }
  494. const pid = this.ctx.session.sessionProject.id;
  495. switch (checkData.checkType) {
  496. case auditConst.status.checked:
  497. await this._checked(pid, advanceId, checkData, times);
  498. break;
  499. case auditConst.status.checkNo:
  500. await this._checkNo(pid, advanceId, checkData, times);
  501. break;
  502. case auditConst.status.checkNoPre:
  503. await this._checkNoPre(pid, advanceId, checkData, times, type);
  504. break;
  505. default:
  506. throw '无效审批操作';
  507. }
  508. }
  509. /**
  510. * 重新审批
  511. * @param { string } cid - 查询的清单
  512. * @return {Promise<*>} - 可用的变更令列表
  513. */
  514. async checkAgain(advance) {
  515. const accountId = this.ctx.session.sessionUser.accountId;
  516. // 初始化事务
  517. const time = new Date();
  518. const transaction = await this.db.beginTransaction();
  519. let result = false;
  520. try {
  521. const maxOrder = advance.auditors[advance.auditors.length - 1].order;
  522. const audits = advance.auditors.filter(x => { return x.order === maxOrder; });
  523. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  524. const selfAudit = audits.find(x => { return x.audit_id === accountId; });
  525. if (!selfAudit) throw '当前标段您无权审批';
  526. // 当前审批人2次添加至流程中
  527. const checkAgainAuditors = [];
  528. audits.forEach(x => {
  529. checkAgainAuditors.push({
  530. tid: advance.tid, vid: advance.id, audit_id: x.audit_id, type: advance.type,
  531. times: x.times, order: maxOrder + 1,
  532. status: auditConst.status.checkAgain,
  533. create_time: time, end_time: time, opinion: '',
  534. });
  535. });
  536. const checkingAuditors = [];
  537. audits.forEach(x => {
  538. checkingAuditors.push({
  539. tid: advance.tid, vid: advance.id, audit_id: x.audit_id, type: advance.type,
  540. times: x.times, order: maxOrder + 2,
  541. status: auditConst.status.checking,
  542. create_time: time,
  543. });
  544. });
  545. await transaction.insert(this.tableName, checkAgainAuditors);
  546. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  547. // 获取刚批量添加的所有list
  548. // for (let j = 0; j < checkingAuditors.length; j++) {
  549. // checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  550. // }
  551. // 设置审批中
  552. await transaction.update(this.ctx.service.advance.tableName, {
  553. id: advance.id,
  554. status: auditConst.status.checking,
  555. end_time: null,
  556. });
  557. await transaction.commit();
  558. result = true;
  559. } catch (error) {
  560. console.log(error);
  561. await transaction.rollback();
  562. result = false;
  563. }
  564. return result;
  565. }
  566. /**
  567. * 用于添加推送所需的content内容
  568. * @param {Number} pid 项目id
  569. * @param {Number} tid 台账id
  570. * @param {Number} vid 预付款id
  571. * @param {Number} uid 审批人id
  572. */
  573. async getNoticeContent(pid, tid, vid, uid, opinion = '') {
  574. const noticeSql =
  575. 'SELECT * FROM (SELECT ' +
  576. ' 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`' +
  577. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  578. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  579. ' LEFT JOIN ?? As ad ON m.`id` = ad.`vid`' +
  580. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  581. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  582. const noticeSqlParam = [
  583. this.ctx.service.tender.tableName,
  584. tid,
  585. this.ctx.service.advance.tableName,
  586. vid,
  587. this.tableName,
  588. this.ctx.service.projectAccount.tableName,
  589. uid,
  590. pid,
  591. ];
  592. const content = await this.db.query(noticeSql, noticeSqlParam);
  593. if (content.length) {
  594. content[0].opinion = opinion;
  595. }
  596. return content.length ? JSON.stringify(content[0]) : '';
  597. }
  598. /**
  599. * 通过状态获取审核人信息
  600. * @param {Number} vid - 预付款id
  601. * @param {Number} status - 期状态
  602. * @param {Number} times - 审批次数
  603. * @return {Object} auditor 审核人信息
  604. */
  605. async getAuditorByStatus(vid, status, times = 1) {
  606. let auditor = null;
  607. let sql = '';
  608. let sqlParam = '';
  609. switch (status) {
  610. case auditConst.status.checking:
  611. case auditConst.status.checked:
  612. case auditConst.status.checkNoPre:
  613. sql =
  614. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  615. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id` ' +
  616. ' WHERE la.`vid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  617. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, status];
  618. auditor = await this.db.queryOne(sql, sqlParam);
  619. break;
  620. case auditConst.status.checkNo:
  621. sql =
  622. 'SELECT la.`audit_id`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`vid`, la.`order` ' +
  623. ' FROM ?? AS la Left Join ?? AS pa On la.`audit_id` = pa.`id`' +
  624. ' WHERE la.`vid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  625. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, vid, auditConst.status.checkNo, parseInt(times) - 1];
  626. auditor = await this.db.queryOne(sql, sqlParam);
  627. break;
  628. case auditConst.status.uncheck:
  629. default:
  630. break;
  631. }
  632. return auditor;
  633. }
  634. /**
  635. * 获取所有审核人
  636. * @param {Number} tenderId 标段id
  637. */
  638. async getAllAuditors(tenderId) {
  639. const sql = 'SELECT au.audit_id, au.tid FROM ' + this.tableName + ' au' +
  640. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On au.tid = t.id' +
  641. ' WHERE t.id = ?' +
  642. ' GROUP BY au.audit_id';
  643. const sqlParam = [tenderId];
  644. return this.db.query(sql, sqlParam);
  645. }
  646. /**
  647. * 获取待处理审核列表
  648. * @param {Number} auditorId 审核人id
  649. */
  650. async getAuditAdvance(auditorId, spid = '') {
  651. const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  652. const sql = 'SELECT ma.`audit_id`, ma.`times`, ma.`order`, ma.`create_time`, ma.`end_time`, ma.`tid`, ma.`vid`,' +
  653. ' m.`order` As `morder`, m.`status` As `mstatus`, m.`type` As `mtype`,' +
  654. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`, t.`spid` ' +
  655. ' FROM ?? AS ma' +
  656. ' LEFT JOIN ?? AS m On ma.vid = m.id' +
  657. ' LEFT JOIN ?? As t On m.tid = t.id' +
  658. ' WHERE ((ma.`audit_id` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' + spSql +
  659. ' ORDER BY ma.`create_time` DESC';
  660. 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];
  661. return await this.db.query(sql, sqlParam);
  662. }
  663. /**
  664. * 获取tender下所有的审核列表(报表用)
  665. * @param {Number} tenderId
  666. */
  667. async getAuditAdvanceByTender(tenderId) {
  668. 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`' +
  669. ' FROM ?? AS ma ' +
  670. ' WHERE ma.`tid` = ?';
  671. const sqlParam = [this.tableName, tenderId];
  672. return await this.db.query(sql, sqlParam);
  673. }
  674. /**
  675. * 获取审核人审核的次数
  676. *
  677. * @param auditorId
  678. * @return {Promise<*>}
  679. */
  680. async getCountByChecked(auditorId, spid = '') {
  681. if (spid) {
  682. const sql = 'SELECT count(*) AS count FROM ?? AS a LEFT JOIN ?? AS t ON a.tid = t.id WHERE a.`audit_id` = ? AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre]) + ') AND t.`spid` = ?';
  683. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, spid];
  684. const result = await this.db.queryOne(sql, sqlParam);
  685. return result.count ? result.count : 0;
  686. }
  687. return await this.db.count(this.tableName, { audit_id: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  688. }
  689. /**
  690. * 获取最近一次审批结束时间
  691. *
  692. * @param auditorId
  693. * @return {Promise<*>}
  694. */
  695. async getLastEndTimeByChecked(auditorId, spid = '') {
  696. const sqSql = spid ? ' AND t.`spid` = "' + spid + '"' : '';
  697. const sql = 'SELECT a.`end_time` FROM ?? AS a LEFT JOIN ?? AS t ON a.`tid` = t.`id` WHERE a.`audit_id` = ? ' +
  698. 'AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ')' + sqSql +
  699. ' ORDER BY a.`end_time` DESC';
  700. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId];
  701. const result = await this.db.queryOne(sql, sqlParam);
  702. return result ? result.end_time : null;
  703. }
  704. async updateNewAuditList(advance, newIdList) {
  705. const transaction = await this.db.beginTransaction();
  706. try {
  707. // 先删除旧的审批流,再添加新的
  708. await transaction.delete(this.tableName, { vid: advance.id, times: advance.times });
  709. const newAuditors = [];
  710. let order = 1;
  711. for (const aid of newIdList) {
  712. newAuditors.push({
  713. tid: advance.tid, vid: advance.id, type: advance.type, audit_id: aid,
  714. times: advance.times, order, status: auditConst.status.uncheck,
  715. });
  716. order++;
  717. }
  718. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  719. await transaction.commit();
  720. } catch (err) {
  721. await transaction.rollback();
  722. throw err;
  723. }
  724. }
  725. async updateLastAudit(advance, auditList, lastId) {
  726. const transaction = await this.db.beginTransaction();
  727. try {
  728. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  729. const idList = this._.map(auditList, 'audit_id');
  730. let order = idList.length + 1;
  731. if (idList.indexOf(lastId) !== -1) {
  732. await transaction.delete(this.tableName, { vid: advance.id, times: advance.times, audit_id: lastId });
  733. const audit = this._.find(auditList, { 'audit_id': lastId });
  734. // 顺移之后审核人流程顺序
  735. await this._syncOrderByDelete(transaction, advance.id, audit.order, advance.times);
  736. order = order - 1;
  737. }
  738. // 添加终审
  739. const newAuditor = {
  740. tid: advance.tid, vid: advance.id, type: advance.type, audit_id: lastId,
  741. times: advance.times, order, status: auditConst.status.uncheck,
  742. };
  743. await transaction.insert(this.tableName, newAuditor);
  744. await transaction.commit();
  745. } catch (err) {
  746. await transaction.rollback();
  747. throw err;
  748. }
  749. }
  750. /**
  751. * 取待审批期列表(wap用)
  752. *
  753. * @param auditorId
  754. * @return {Promise<*>}
  755. */
  756. async getAuditAdvanceByWap(auditorId) {
  757. const sql =
  758. 'SELECT ad.`id`, ad.`tid`, ad.`pay_ratio`, ad.`cur_amount`, ad.`prev_amount`, ad.`prev_total_amount`, ad.`type`, ad.`order`, ' +
  759. // ' 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`, ' +
  760. ' t.`name`, t.`project_id`, t.`user_id`,' +
  761. ' ti.`deal_info`, ti.`deal_param` ' +
  762. ' FROM ?? AS aa, ?? AS ad, ?? As t, ?? AS ti ' +
  763. ' WHERE aa.`audit_id` = ? and aa.`status` = ?' +
  764. ' and aa.`vid` = ad.`id` and aa.`tid` = t.`id` and ti.`tid` = t.`id`';
  765. const sqlParam = [
  766. this.tableName,
  767. this.ctx.service.advance.tableName,
  768. this.ctx.service.tender.tableName,
  769. this.ctx.service.tenderInfo.tableName,
  770. auditorId,
  771. auditConst.status.checking,
  772. ];
  773. return await this.db.query(sql, sqlParam);
  774. }
  775. /**
  776. * 获取审核人已经审核过的审批信息(包括退回,通过,重新审批等)
  777. *
  778. * @param auditorId
  779. * @return {Promise<*>}
  780. */
  781. async getDonesByAudit(auditorId, spid = '') {
  782. const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  783. const status = [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre, auditConst.status.checkAgain];
  784. const sql =
  785. 'SELECT la.`status`, la.`end_time` as `shenpi_time`, la.`vid`, t.`id`, v.`order` As `vorder`, v.`type`, t.`name`, t.`spid` ' +
  786. ' FROM ?? AS la Left Join ?? AS t ON la.`tid` = t.`id` LEFT JOIN ?? AS v ON la.`vid` = v.`id`' +
  787. ' WHERE la.`audit_id` = ? AND la.`status` in (' + this.ctx.helper.getInArrStrSqlFilter(status) +')' + spSql +
  788. ' ORDER BY la.`end_time` DESC';
  789. const sqlParam = [
  790. this.tableName,
  791. this.ctx.service.tender.tableName,
  792. this.ctx.service.advance.tableName,
  793. auditorId,
  794. ];
  795. return await this.db.query(sql, sqlParam);
  796. }
  797. }
  798. return AdvanceAudit;
  799. };