payment_detail_audit.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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 this.ctx.service.paymentRptAudit.updateSignTime(transaction, rptAudit.id);
  335. }
  336. await transaction.update(this.ctx.service.paymentDetail.tableName, updateDetailData);
  337. // 判断用户是否有权限查看支付审批,没有则自动加入到权限中
  338. const auditList = await this.getAllDataByCondition({ where: { td_id: detailId, times } });
  339. const rptAuditList = await this.ctx.service.paymentRptAudit.getAllDataByCondition({ where: { td_id: detailId } });
  340. const auditIdList = this._.map(auditList, 'aid');
  341. const rptAuditIdList = this._.map(rptAuditList, 'uid');
  342. const permissionAuditList = await this.ctx.service.paymentPermissionAudit.getAllDataByCondition({ where: { pid: this.ctx.session.sessionProject.id } });
  343. const paIdList = this._.map(permissionAuditList, 'uid');
  344. const detailIdList = this._.union(auditIdList, rptAuditIdList);
  345. const newAudits = this._.difference(detailIdList, paIdList);
  346. if (newAudits.length > 0) {
  347. const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
  348. where: { project_id: this.ctx.session.sessionProject.id, id: newAudits },
  349. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  350. });
  351. await this.ctx.service.paymentPermissionAudit.saveAudits(this.ctx.session.sessionProject.id, accountList, transaction);
  352. }
  353. // todo 更新标段tender状态 ?
  354. await transaction.commit();
  355. } catch (err) {
  356. await transaction.rollback();
  357. throw err;
  358. }
  359. return true;
  360. }
  361. async _checked(pid, detailId, checkData, times) {
  362. const time = new Date();
  363. // 整理当前流程审核人状态更新
  364. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  365. if (!audit) {
  366. throw '审核数据错误';
  367. }
  368. const nextAudit = await this.getDataByCondition({ td_id: detailId, times, order: audit.order + 1 });
  369. const transaction = await this.db.beginTransaction();
  370. try {
  371. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  372. // 如果是报表角色则需要更新到detail中
  373. const updateDetailData = {
  374. id: detailId,
  375. };
  376. const rptAudit = await this.ctx.service.paymentRptAudit.getDataByCondition({ td_id: detailId, uid: audit.aid });
  377. if (rptAudit && rptAudit.signature_msg) {
  378. const report_json = JSON.parse(this.ctx.detail.report_json);
  379. const sign_msg = JSON.parse(rptAudit.signature_msg);
  380. updateDetailData.report_json = JSON.stringify(await this.ctx.service.paymentDetail.signOneSignatureData(report_json, rptAudit.signature_name, sign_msg));
  381. await this.ctx.service.paymentRptAudit.updateSignTime(transaction, rptAudit.id);
  382. }
  383. // 无下一审核人表示,审核结束
  384. if (nextAudit) {
  385. // 流程至下一审批人
  386. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  387. // 同步 期信息
  388. updateDetailData.status = auditConst.status.checking;
  389. } else {
  390. // 本期结束
  391. // 同步 期信息
  392. updateDetailData.status = checkData.checkType;
  393. }
  394. await transaction.update(this.ctx.service.paymentDetail.tableName, updateDetailData);
  395. await transaction.commit();
  396. } catch (err) {
  397. await transaction.rollback();
  398. throw err;
  399. }
  400. }
  401. async _checkNo(pid, detailId, checkData, times) {
  402. const time = new Date();
  403. const detailInfo = await this.ctx.service.paymentDetail.getDataById(detailId);
  404. const trInfo = await this.ctx.service.paymentTenderRpt.getDataById(detailInfo.tr_id);
  405. // 整理当前流程审核人状态更新
  406. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  407. if (!audit) {
  408. throw '审核数据错误';
  409. }
  410. const sql = 'SELECT `tender_id`, `tr_id`, `td_id`, `aid`, `order` FROM ?? WHERE `td_id` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  411. const sqlParam = [this.tableName, detailId, times];
  412. const auditors = await this.db.query(sql, sqlParam);
  413. // 可能更换了上报人且存在于审批流程中,需要删除
  414. const userIndex = this._.findIndex(auditors, { aid: trInfo.uid });
  415. if (userIndex !== -1) {
  416. auditors.splice(userIndex, 1);
  417. }
  418. let order = 1;
  419. for (const a of auditors) {
  420. a.times = times + 1;
  421. a.order = order;
  422. a.status = auditConst.status.uncheck;
  423. order++;
  424. }
  425. const transaction = await this.db.beginTransaction();
  426. try {
  427. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  428. // 清空所有签名数据
  429. let report_json = JSON.parse(this.ctx.detail.report_json);
  430. report_json = await this.ctx.service.paymentDetail.clearAllSignatureData(report_json);
  431. const detailUpdateData = {
  432. id: detailId, status: checkData.checkType,
  433. times: times + 1,
  434. report_json: JSON.stringify(report_json),
  435. };
  436. // 可能存在更换了上报人的情况,需要替换
  437. if (detailInfo.uid !== trInfo.uid) {
  438. detailUpdateData.uid = trInfo.uid;
  439. }
  440. // 同步期信息
  441. await transaction.update(this.ctx.service.paymentDetail.tableName, detailUpdateData);
  442. // 清空签名
  443. await transaction.update(this.ctx.service.paymentRptAudit.tableName, {
  444. signature_msg: null,
  445. sign_time: null,
  446. }, {
  447. where: {
  448. td_id: detailId,
  449. },
  450. });
  451. // 拷贝新一次审核流程列表
  452. await transaction.insert(this.tableName, auditors);
  453. await transaction.commit();
  454. } catch (err) {
  455. await transaction.rollback();
  456. throw err;
  457. }
  458. }
  459. async _checkNoPre(pid, detailId, checkData, times) {
  460. const time = new Date();
  461. // 整理当前流程审核人状态更新
  462. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  463. if (!audit || audit.order <= 1) {
  464. throw '审核数据错误';
  465. }
  466. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  467. const auditors2 = await this.getAuditGroupByList(detailId, times);
  468. const auditorIndex = await auditors2.findIndex(function(item) {
  469. return item.aid === audit.aid;
  470. });
  471. const preAuditor = auditors2[auditorIndex - 1];
  472. const transaction = await this.db.beginTransaction();
  473. try {
  474. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  475. // 顺移气候审核人流程顺序
  476. this.initSqlBuilder();
  477. this.sqlBuilder.setAndWhere('td_id', { value: detailId, operate: '=' });
  478. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  479. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  480. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  481. const data = await transaction.query(sql, sqlParam);
  482. const newAuditors = [];
  483. newAuditors.push({
  484. tender_id: audit.tender_id, tr_id: audit.tr_id, td_id: audit.td_id, aid: preAuditor.aid,
  485. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  486. begin_time: time,
  487. });
  488. newAuditors.push({
  489. tender_id: audit.tender_id, tr_id: audit.tr_id, td_id: audit.td_id, aid: audit.aid,
  490. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  491. });
  492. await transaction.insert(this.tableName, newAuditors);
  493. // 清除本人的签章
  494. await this.ctx.service.paymentRptAudit.clearSignatureMsg(transaction, detailId, audit.aid);
  495. await transaction.commit();
  496. } catch (error) {
  497. await transaction.rollback();
  498. throw error;
  499. }
  500. }
  501. /**
  502. * 审批
  503. * @param {Number} materialId - 材料调差期id
  504. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  505. * @param {Number} times - 第几次审批
  506. * @return {Promise<void>}
  507. */
  508. async check(detailId, checkData, times = 1) {
  509. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  510. throw '提交数据错误';
  511. }
  512. const pid = this.ctx.session.sessionProject.id;
  513. switch (checkData.checkType) {
  514. case auditConst.status.checked:
  515. await this._checked(pid, detailId, checkData, times);
  516. break;
  517. case auditConst.status.checkNo:
  518. await this._checkNo(pid, detailId, checkData, times);
  519. break;
  520. case auditConst.status.checkNoPre:
  521. await this._checkNoPre(pid, detailId, checkData, times);
  522. break;
  523. default:
  524. throw '无效审批操作';
  525. }
  526. }
  527. async followAuditByRptAudit(detail) {
  528. const transaction = await this.db.beginTransaction();
  529. try {
  530. const newAuditIds = this._.map(detail.rptAudits, 'uid');
  531. // 去除上报人
  532. if (this._.includes(newAuditIds, detail.uid)) {
  533. this._.pull(newAuditIds, detail.uid);
  534. }
  535. // 如果存在固定终审,判断它是否在报表人员里,存在则把该id移到最后,不存在则插入newAuditIds最后;
  536. if (this.ctx.trInfo.sp_status === shenpiConst.sp_status.gdzs) {
  537. const gdzsInfo = await this.ctx.service.paymentShenpiAudit.getAudit(detail.tender_id, detail.tr_id, shenpiConst.sp_status.gdzs);
  538. if (gdzsInfo && gdzsInfo.audit_id !== newAuditIds[newAuditIds.length - 1]) {
  539. if (this._.includes(newAuditIds, gdzsInfo.audit_id)) {
  540. this._.pull(newAuditIds, gdzsInfo.audit_id);
  541. }
  542. newAuditIds.push(gdzsInfo.audit_id);
  543. }
  544. }
  545. // 删除原审批流
  546. await transaction.delete(this.tableName, { td_id: detail.id, times: detail.times });
  547. // 添加新审批流
  548. const insertDatas = [];
  549. let order = 1;
  550. for (const id of newAuditIds) {
  551. insertDatas.push({
  552. tender_id: detail.tender_id,
  553. tr_id: detail.tr_id,
  554. td_id: detail.id,
  555. aid: id,
  556. order,
  557. times: detail.times,
  558. status: auditConst.status.uncheck,
  559. });
  560. order = order + 1;
  561. }
  562. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  563. await transaction.commit();
  564. return true;
  565. } catch (error) {
  566. await transaction.rollback();
  567. throw error;
  568. }
  569. }
  570. /**
  571. * 获取审核人需要审核的期列表
  572. *
  573. * @param auditorId
  574. * @return {Promise<*>}
  575. */
  576. async getAuditPayment(auditorId) {
  577. const sql =
  578. 'SELECT pda.`aid`, pda.`times`, pda.`order`, pda.`begin_time`, pda.`end_time`, pda.`tender_id`, pda.`tr_id`, pda.`td_id`,' +
  579. ' pd.`code` As `scode`, pd.`order` As `sorder`, pd.`status` As `sstatus`,' +
  580. ' pr.`rpt_name` As `rpt_name`,' +
  581. ' t.`name`, t.`pid`, t.`uid` ' +
  582. ' FROM ?? AS pda ' +
  583. ' Left Join ?? AS pd On pda.`td_id` = pd.`id` ' +
  584. ' Left Join ?? AS pr On pda.`tr_id` = pr.`id` ' +
  585. ' Left Join ?? As t ON pda.`tender_id` = t.`id`' +
  586. ' WHERE ((pda.`aid` = ? and pda.`status` = ?) OR (pd.`uid` = ? and pda.`status` = ? and pd.`status` = ? and pda.`times` = (pd.`times`-1)))' +
  587. ' ORDER BY pda.`begin_time` DESC';
  588. const sqlParam = [
  589. this.tableName,
  590. this.ctx.service.paymentDetail.tableName,
  591. this.ctx.service.paymentTenderRpt.tableName,
  592. this.ctx.service.paymentTender.tableName,
  593. auditorId,
  594. auditConst.status.checking,
  595. auditorId,
  596. auditConst.status.checkNo,
  597. auditConst.status.checkNo,
  598. ];
  599. return await this.db.query(sql, sqlParam);
  600. }
  601. async updateAuditByReport(transaction, td_id, times, uid) {
  602. const auditor = await this.getDataByCondition({ td_id, times, aid: uid });
  603. if (auditor) {
  604. // 更新order
  605. await this._syncOrderByDelete(transaction, td_id, auditor.order, times);
  606. return await transaction.delete(this.tableName, { id: auditor.id });
  607. }
  608. }
  609. }
  610. return PaymentDetailAudit;
  611. };