payment_detail_audit.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. 'use strict';
  2. /**
  3. * 版本数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/25
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. const shenpiConst = require('../const/shenpi');
  11. module.exports = app => {
  12. class PaymentDetailAudit extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'payment_detail_audit';
  22. }
  23. /**
  24. * 获取审核人流程列表
  25. *
  26. * @param auditorId
  27. * @return {Promise<*>}
  28. */
  29. async getAuditGroupByList(detailId, times) {
  30. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`td_id`, la.`aid`, la.`order` ' +
  31. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  32. ' WHERE la.`td_id` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  33. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, detailId, times];
  34. return await this.db.query(sql, sqlParam);
  35. }
  36. /**
  37. * 复制上一期的审批人列表给最新一期
  38. *
  39. * @param transaction - 新增一期的事务
  40. * @param {Object} preStage - 上一期
  41. * @param {Object} newStage - 最新一期
  42. * @return {Promise<*>}
  43. */
  44. async copyPreDetailAuditors(transaction, preDetail, newDetail) {
  45. const auditors = await this.getAuditGroupByList(preDetail.id, preDetail.times);
  46. const newAuditors = [];
  47. for (const a of auditors) {
  48. if (a.aid !== newDetail.uid) {
  49. const na = {
  50. tender_id: preDetail.tender_id,
  51. tr_id: preDetail.tr_id,
  52. td_id: newDetail.id,
  53. aid: a.aid,
  54. times: newDetail.times,
  55. order: newAuditors.length + 1,
  56. status: auditConst.status.uncheck,
  57. };
  58. newAuditors.push(na);
  59. }
  60. }
  61. const result = await transaction.insert(this.tableName, newAuditors);
  62. return (result.effectRows = auditors.length);
  63. }
  64. /**
  65. * 获取 审核列表信息
  66. *
  67. * @param {Number} detailId - 支付审批表单详情id
  68. * @param {Number} times - 第几次审批
  69. * @return {Promise<*>}
  70. */
  71. async getAuditors(detailId, times = 1) {
  72. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, g.`sort` ' +
  73. 'FROM ?? AS la, ?? AS pa, (SELECT `aid`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `td_id` = ? AND `times` = ? GROUP BY `aid`) as g ' +
  74. 'WHERE la.`td_id` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order`';
  75. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, detailId, times, detailId, times];
  76. const result = await this.db.query(sql, sqlParam);
  77. const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `td_id` = ? AND `times` = ? GROUP BY `aid`) as a';
  78. const sqlParam2 = [this.tableName, detailId, times];
  79. const count = await this.db.queryOne(sql2, sqlParam2);
  80. for (const i in result) {
  81. result[i].max_sort = count.num;
  82. }
  83. return result;
  84. }
  85. /**
  86. * 获取 当前审核人
  87. *
  88. * @param {Number} materialId - 支付审批表单详情id
  89. * @param {Number} times - 第几次审批
  90. * @return {Promise<*>}
  91. */
  92. async getCurAuditor(detailId, times = 1) {
  93. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  94. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  95. ' WHERE la.`td_id` = ? and la.`status` = ? and la.`times` = ?';
  96. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, detailId, auditConst.status.checking, times];
  97. return await this.db.queryOne(sql, sqlParam);
  98. }
  99. async updateNewAuditList(detail, newIdList) {
  100. const transaction = await this.db.beginTransaction();
  101. try {
  102. // 先删除旧的审批流,再添加新的
  103. await transaction.delete(this.tableName, { td_id: detail.id, times: detail.times });
  104. const newAuditors = [];
  105. let order = 1;
  106. for (const aid of newIdList) {
  107. newAuditors.push({
  108. tender_id: detail.tender_id, tr_id: detail.tr_id, td_id: detail.id, aid,
  109. times: detail.times, order, status: auditConst.status.uncheck,
  110. });
  111. order++;
  112. }
  113. if (newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  114. await transaction.commit();
  115. } catch (err) {
  116. await transaction.rollback();
  117. throw err;
  118. }
  119. }
  120. async updateLastAudit(detail, auditList, lastId) {
  121. const transaction = await this.db.beginTransaction();
  122. try {
  123. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  124. const idList = this._.map(auditList, 'aid');
  125. let order = idList.length + 1;
  126. if (idList.indexOf(lastId) !== -1) {
  127. await transaction.delete(this.tableName, { td_id: detail.id, times: detail.times, aid: lastId });
  128. const audit = this._.find(auditList, { aid: lastId });
  129. // 顺移之后审核人流程顺序
  130. await this._syncOrderByDelete(transaction, detail.id, audit.order, detail.times);
  131. order = order - 1;
  132. }
  133. // 添加终审
  134. const newAuditor = {
  135. tender_id: detail.tender_id, tr_id: detail.tr_id, td_id: detail.id, aid: lastId,
  136. times: detail.times, order, status: auditConst.status.uncheck,
  137. };
  138. await transaction.insert(this.tableName, newAuditor);
  139. await transaction.commit();
  140. } catch (err) {
  141. await transaction.rollback();
  142. throw err;
  143. }
  144. }
  145. /**
  146. * 移除审核人时,同步其后审核人order
  147. * @param transaction - 事务
  148. * @param {Number} materialId - 材料调差期id
  149. * @param {Number} auditorId - 审核人id
  150. * @param {Number} times - 第几次审批
  151. * @return {Promise<*>}
  152. * @private
  153. */
  154. async _syncOrderByDelete(transaction, detailId, order, times, selfOperate = '-') {
  155. this.initSqlBuilder();
  156. this.sqlBuilder.setAndWhere('td_id', {
  157. value: detailId,
  158. operate: '=',
  159. });
  160. this.sqlBuilder.setAndWhere('order', {
  161. value: order,
  162. operate: '>=',
  163. });
  164. this.sqlBuilder.setAndWhere('times', {
  165. value: times,
  166. operate: '=',
  167. });
  168. this.sqlBuilder.setUpdateData('order', {
  169. value: 1,
  170. selfOperate,
  171. });
  172. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  173. const data = await transaction.query(sql, sqlParam);
  174. return data;
  175. }
  176. /**
  177. * 获取审核人流程列表(包括原报)
  178. * @param {Number} materialId 调差id
  179. * @param {Number} times 审核次数
  180. * @return {Promise<Array>} 查询结果集(包括原报)
  181. */
  182. async getAuditorsWithOwner(detailId, times = 1) {
  183. const result = await this.getAuditGroupByList(detailId, times);
  184. const sql =
  185. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`' +
  186. ' FROM ' +
  187. this.ctx.service.paymentDetail.tableName +
  188. ' As s' +
  189. ' LEFT JOIN ' +
  190. this.ctx.service.projectAccount.tableName +
  191. ' As pa' +
  192. ' ON s.uid = pa.id' +
  193. ' WHERE s.id = ?';
  194. const sqlParam = [times, detailId, detailId];
  195. const user = await this.db.queryOne(sql, sqlParam);
  196. result.unshift(user);
  197. return result;
  198. }
  199. /**
  200. * 获取 最新审核顺序
  201. *
  202. * @param {Number} materialId - 材料调差期id
  203. * @param {Number} times - 第几次审批
  204. * @return {Promise<number>}
  205. */
  206. async getNewOrder(detailId, times = 1) {
  207. const sql = 'SELECT Max(??) As max_order FROM ?? Where `td_id` = ? and `times` = ?';
  208. const sqlParam = ['order', this.tableName, detailId, times];
  209. const result = await this.db.queryOne(sql, sqlParam);
  210. return result && result.max_order ? result.max_order + 1 : 1;
  211. }
  212. /**
  213. * 新增审核人
  214. *
  215. * @param {Number} materialId - 材料调差期id
  216. * @param {Number} auditorId - 审核人id
  217. * @param {Number} times - 第几次审批
  218. * @return {Promise<number>}
  219. */
  220. async addAuditor(detailId, auditorId, times = 1, is_gdzs = 0) {
  221. const transaction = await this.db.beginTransaction();
  222. try {
  223. let newOrder = await this.getNewOrder(detailId, times);
  224. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  225. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  226. if (is_gdzs) await this._syncOrderByDelete(transaction, detailId, newOrder, times, '+');
  227. const data = {
  228. tender_id: this.ctx.tender.id,
  229. tr_id: this.ctx.trInfo.id,
  230. td_id: detailId,
  231. aid: auditorId,
  232. times,
  233. order: newOrder,
  234. status: auditConst.status.uncheck,
  235. };
  236. const result = await transaction.insert(this.tableName, data);
  237. await transaction.commit();
  238. return result.affectedRows === 1;
  239. } catch (err) {
  240. await transaction.rollback();
  241. throw err;
  242. }
  243. return false;
  244. }
  245. /**
  246. * 移除审核人
  247. *
  248. * @param {Number} materialId - 材料调差期id
  249. * @param {Number} auditorId - 审核人id
  250. * @param {Number} times - 第几次审批
  251. * @return {Promise<boolean>}
  252. */
  253. async deleteAuditor(detailId, auditorId, times = 1) {
  254. const transaction = await this.db.beginTransaction();
  255. try {
  256. const condition = { td_id: detailId, aid: auditorId, times };
  257. const auditor = await this.getDataByCondition(condition);
  258. if (!auditor) {
  259. throw '该审核人不存在';
  260. }
  261. await this._syncOrderByDelete(transaction, detailId, auditor.order, times);
  262. await transaction.delete(this.tableName, condition);
  263. await transaction.commit();
  264. } catch (err) {
  265. await transaction.rollback();
  266. throw err;
  267. }
  268. return true;
  269. }
  270. /**
  271. * 移除审核人
  272. *
  273. * @param {Number} materialId - 材料调差期id
  274. * @param {Number} status - 期状态
  275. * @param {Number} status - 期次数
  276. * @return {Promise<boolean>}
  277. */
  278. async getAuditorByStatus(detailId, status, times = 1) {
  279. let auditor = null;
  280. let sql = '';
  281. let sqlParam = '';
  282. switch (status) {
  283. case auditConst.status.checking :
  284. case auditConst.status.checked :
  285. case auditConst.status.checkNoPre :
  286. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tr_id`, la.`td_id`, la.`aid`, la.`order` ' +
  287. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  288. ' WHERE la.`td_id` = ? and la.`status` = ? ' +
  289. ' ORDER BY la.`times` desc, la.`order` desc';
  290. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, detailId, status];
  291. auditor = await this.db.queryOne(sql, sqlParam);
  292. break;
  293. case auditConst.status.checkNo :
  294. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tr_id`, la.`td_id`, la.`aid`, la.`order` ' +
  295. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  296. ' WHERE la.`td_id` = ? and la.`status` = ? and la.`times` = ?' +
  297. ' ORDER BY la.`times` desc, la.`order` desc';
  298. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, detailId, auditConst.status.checkNo, parseInt(times) - 1];
  299. auditor = await this.db.queryOne(sql, sqlParam);
  300. break;
  301. case auditConst.status.uncheck :
  302. default:break;
  303. }
  304. return auditor;
  305. }
  306. /**
  307. * 开始审批
  308. * @param {Number} materialId - 材料调差期id
  309. * @param {Number} times - 第几次审批
  310. * @return {Promise<boolean>}
  311. */
  312. async start(detailId, times = 1) {
  313. const audit = await this.getDataByCondition({ td_id: detailId, times, order: 1 });
  314. if (!audit) {
  315. if (this.ctx.trinfo.sp_status === shenpiConst.sp_status.gdspl) {
  316. throw '请联系管理员添加审批人';
  317. } else {
  318. throw '请先选择审批人,再上报数据';
  319. }
  320. }
  321. const transaction = await this.db.beginTransaction();
  322. try {
  323. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date() });
  324. // 如果是报表角色则需要更新到detail中
  325. const updateDetailData = {
  326. id: detailId, status: auditConst.status.checking,
  327. };
  328. const rptAudit = await this.ctx.service.paymentRptAudit.getDataByCondition({ td_id: detailId, uid: this.ctx.session.sessionUser.accountId });
  329. if (rptAudit && rptAudit.signature_msg) {
  330. const report_json = JSON.parse(this.ctx.detail.report_json);
  331. const sign_msg = JSON.parse(rptAudit.signature_msg);
  332. updateDetailData.report_json = JSON.stringify(await this.ctx.service.paymentDetail.signOneSignatureData(report_json, rptAudit.signature_name, sign_msg));
  333. }
  334. await transaction.update(this.ctx.service.paymentDetail.tableName, updateDetailData);
  335. // 判断用户是否有权限查看支付审批,没有则自动加入到权限中
  336. const auditList = await this.getAllDataByCondition({ where: { td_id: detailId, times } });
  337. const rptAuditList = await this.ctx.service.paymentRptAudit.getAllDataByCondition({ where: { td_id: detailId } });
  338. const auditIdList = this._.map(auditList, 'aid');
  339. const rptAuditIdList = this._.map(rptAuditList, 'uid');
  340. const permissionAuditList = await this.ctx.service.paymentPermissionAudit.getAllDataByCondition({ where: { pid: this.ctx.session.sessionProject.id } });
  341. const paIdList = this._.map(permissionAuditList, 'uid');
  342. const detailIdList = this._.union(auditIdList, rptAuditIdList);
  343. const newAudits = this._.difference(detailIdList, paIdList);
  344. if (newAudits.length > 0) {
  345. const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
  346. where: { project_id: this.ctx.session.sessionProject.id, id: newAudits },
  347. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  348. });
  349. await this.ctx.service.paymentPermissionAudit.saveAudits(this.ctx.session.sessionProject.id, accountList, transaction);
  350. }
  351. // todo 更新标段tender状态 ?
  352. await transaction.commit();
  353. } catch (err) {
  354. await transaction.rollback();
  355. throw err;
  356. }
  357. return true;
  358. }
  359. async _checked(pid, detailId, checkData, times) {
  360. const time = new Date();
  361. // 整理当前流程审核人状态更新
  362. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  363. if (!audit) {
  364. throw '审核数据错误';
  365. }
  366. const nextAudit = await this.getDataByCondition({ td_id: detailId, times, order: audit.order + 1 });
  367. const transaction = await this.db.beginTransaction();
  368. try {
  369. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  370. // 如果是报表角色则需要更新到detail中
  371. const updateDetailData = {
  372. id: detailId,
  373. };
  374. const rptAudit = await this.ctx.service.paymentRptAudit.getDataByCondition({ td_id: detailId, uid: audit.aid });
  375. if (rptAudit && rptAudit.signature_msg) {
  376. const report_json = JSON.parse(this.ctx.detail.report_json);
  377. const sign_msg = JSON.parse(rptAudit.signature_msg);
  378. updateDetailData.report_json = JSON.stringify(await this.ctx.service.paymentDetail.signOneSignatureData(report_json, rptAudit.signature_name, sign_msg));
  379. }
  380. // 无下一审核人表示,审核结束
  381. if (nextAudit) {
  382. // 流程至下一审批人
  383. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  384. // 同步 期信息
  385. updateDetailData.status = auditConst.status.checking;
  386. } else {
  387. // 本期结束
  388. // 同步 期信息
  389. updateDetailData.status = checkData.checkType;
  390. }
  391. await transaction.update(this.ctx.service.paymentDetail.tableName, updateDetailData);
  392. await transaction.commit();
  393. } catch (err) {
  394. await transaction.rollback();
  395. throw err;
  396. }
  397. }
  398. async _checkNo(pid, detailId, checkData, times) {
  399. const time = new Date();
  400. const detailInfo = await ctx.service.paymentDetail.getDataById(detailId);
  401. // 整理当前流程审核人状态更新
  402. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  403. if (!audit) {
  404. throw '审核数据错误';
  405. }
  406. const sql = 'SELECT `tender_id`, `tr_id`, `td_id`, `aid`, `order` FROM ?? WHERE `td_id` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  407. const sqlParam = [this.tableName, detailId, times];
  408. const auditors = await this.db.query(sql, sqlParam);
  409. let order = 1;
  410. for (const a of auditors) {
  411. a.times = times + 1;
  412. a.order = order;
  413. a.status = auditConst.status.uncheck;
  414. order++;
  415. }
  416. // 可能更换了上报人且存在于审批流程中,需要删除
  417. const userIndex = this._.findIndex(auditors, { aid: detailInfo.uid });
  418. if (userIndex !== -1) {
  419. auditors.splice(userIndex, 1);
  420. }
  421. const transaction = await this.db.beginTransaction();
  422. try {
  423. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  424. // 清空所有签名数据
  425. let report_json = JSON.parse(this.ctx.detail.report_json);
  426. report_json = await this.ctx.service.paymentDetail.clearAllSignatureData(report_json);
  427. // 同步期信息
  428. await transaction.update(this.ctx.service.paymentDetail.tableName, {
  429. id: detailId, status: checkData.checkType,
  430. times: times + 1,
  431. report_json: JSON.stringify(report_json),
  432. });
  433. // 清空签名
  434. await transaction.update(this.ctx.service.paymentRptAudit.tableName, {
  435. signature_msg: null,
  436. }, {
  437. where: {
  438. td_id: detailId,
  439. },
  440. });
  441. // 拷贝新一次审核流程列表
  442. await transaction.insert(this.tableName, auditors);
  443. await transaction.commit();
  444. } catch (err) {
  445. await transaction.rollback();
  446. throw err;
  447. }
  448. }
  449. async _checkNoPre(pid, detailId, checkData, times) {
  450. const time = new Date();
  451. // 整理当前流程审核人状态更新
  452. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  453. if (!audit || audit.order <= 1) {
  454. throw '审核数据错误';
  455. }
  456. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  457. const auditors2 = await this.getAuditGroupByList(detailId, times);
  458. const auditorIndex = await auditors2.findIndex(function(item) {
  459. return item.aid === audit.aid;
  460. });
  461. const preAuditor = auditors2[auditorIndex - 1];
  462. const transaction = await this.db.beginTransaction();
  463. try {
  464. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  465. // 顺移气候审核人流程顺序
  466. this.initSqlBuilder();
  467. this.sqlBuilder.setAndWhere('td_id', { value: detailId, operate: '=' });
  468. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  469. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  470. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  471. const data = await transaction.query(sql, sqlParam);
  472. const newAuditors = [];
  473. newAuditors.push({
  474. tender_id: audit.tender_id, tr_id: audit.tr_id, td_id: audit.td_id, aid: preAuditor.aid,
  475. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  476. begin_time: time,
  477. });
  478. newAuditors.push({
  479. tender_id: audit.tender_id, tr_id: audit.tr_id, td_id: audit.td_id, aid: audit.aid,
  480. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  481. });
  482. await transaction.insert(this.tableName, newAuditors);
  483. // 清除本人的签章
  484. await this.ctx.service.paymentRptAudit.clearSignatureMsg(transaction, detailId, audit.aid);
  485. await transaction.commit();
  486. } catch (error) {
  487. await transaction.rollback();
  488. throw error;
  489. }
  490. }
  491. /**
  492. * 审批
  493. * @param {Number} materialId - 材料调差期id
  494. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  495. * @param {Number} times - 第几次审批
  496. * @return {Promise<void>}
  497. */
  498. async check(detailId, checkData, times = 1) {
  499. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  500. throw '提交数据错误';
  501. }
  502. const pid = this.ctx.session.sessionProject.id;
  503. switch (checkData.checkType) {
  504. case auditConst.status.checked:
  505. await this._checked(pid, detailId, checkData, times);
  506. break;
  507. case auditConst.status.checkNo:
  508. await this._checkNo(pid, detailId, checkData, times);
  509. break;
  510. case auditConst.status.checkNoPre:
  511. await this._checkNoPre(pid, detailId, checkData, times);
  512. break;
  513. default:
  514. throw '无效审批操作';
  515. }
  516. }
  517. async followAuditByRptAudit(detail) {
  518. const transaction = await this.db.beginTransaction();
  519. try {
  520. const newAuditIds = this._.map(detail.rptAudits, 'uid');
  521. // 去除上报人
  522. if (this._.includes(newAuditIds, detail.uid)) {
  523. this._.pull(newAuditIds, detail.uid);
  524. }
  525. // 如果存在固定终审,判断它是否在报表人员里,存在则把该id移到最后,不存在则插入newAuditIds最后;
  526. if (this.ctx.trInfo.sp_status === shenpiConst.sp_status.gdzs) {
  527. const gdzsInfo = await this.ctx.service.paymentShenpiAudit.getAudit(detail.tender_id, detail.tr_id, shenpiConst.sp_status.gdzs);
  528. if (gdzsInfo && gdzsInfo.audit_id !== newAuditIds[newAuditIds.length - 1]) {
  529. if (this._.includes(newAuditIds, gdzsInfo.audit_id)) {
  530. this._.pull(newAuditIds, gdzsInfo.audit_id);
  531. }
  532. newAuditIds.push(gdzsInfo.audit_id);
  533. }
  534. }
  535. // 删除原审批流
  536. await transaction.delete(this.tableName, { td_id: detail.id, times: detail.times });
  537. // 添加新审批流
  538. const insertDatas = [];
  539. let order = 1;
  540. for (const id of newAuditIds) {
  541. insertDatas.push({
  542. tender_id: detail.tender_id,
  543. tr_id: detail.tr_id,
  544. td_id: detail.id,
  545. aid: id,
  546. order,
  547. times: detail.times,
  548. status: auditConst.status.uncheck,
  549. });
  550. order = order + 1;
  551. }
  552. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  553. await transaction.commit();
  554. return true;
  555. } catch (error) {
  556. await transaction.rollback();
  557. throw error;
  558. }
  559. }
  560. }
  561. return PaymentDetailAudit;
  562. };