payment_detail_audit.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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 t1.`aid`,(@i:=@i+1) as `sort` FROM (SELECT t.`aid`, t.`order` FROM (select `aid`, `order` from ?? WHERE `td_id` = ? AND `times` = ? ORDER BY `order` LIMIT 200) t GROUP BY t.`aid` ORDER BY t.`order`) t1, (select @i:=0) as it) 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. * @param detail
  88. * @returns {Promise<Array>}
  89. */
  90. async getViewFlow(detail) {
  91. const result = [];
  92. const user = await this.ctx.service.projectAccount.getDataById(detail.uid);
  93. if (detail.status === auditConst.status.uncheck) {
  94. if (detail.readOnly) return result;
  95. result.push({
  96. aid: user.id, name: user.name, company: user.company, role: user.role, mobile: user.mobile, telephone: user.telephone,
  97. times: 1, order: 0, status: auditConst.status.uncheck
  98. });
  99. } else {
  100. result.push({
  101. aid: user.id, name: user.name, company: user.company, role: user.role, mobile: user.mobile, telephone: user.telephone,
  102. times: detail.curTimes, order: 0, status: auditConst.status.uncheck, latest: this.ctx.session.sessionUser.accountId === user.id ? !detail.readOnly : false,
  103. });
  104. const sql = `SELECT pda.aid, pa.name, pa.company, pa.role, pa.mobile, pa.telephone, pda.times, pda.order, pda.status, pda.opinion, pda.begin_time, pda.end_time` +
  105. ` FROM ${this.tableName} pda LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON pda.aid = pa.id` +
  106. ' WHERE pda.td_id = ? AND pda.times = ? ORDER BY pda.order';
  107. //const flow = await this.db.query(sql, [detail.id, detail.status === auditConst.status.checkNo && !detail.readOnly ? detail.curTimes - 1 : detail.curTimes]);
  108. let flow = await this.db.query(sql, [detail.id, detail.curTimes]);
  109. flow = this.ctx.helper.filterLastestData(flow, ['aid'], 'times', 'order');
  110. flow.forEach(x => {
  111. if (x.status === auditConst.status.checked) result.push(x);
  112. if (x.status === auditConst.status.checking && !detail.readOnly) {
  113. x.latest = true;
  114. result.push(x);
  115. }
  116. });
  117. }
  118. return result;
  119. }
  120. /**
  121. * 获取 当前审核人
  122. *
  123. * @param {Number} materialId - 支付审批表单详情id
  124. * @param {Number} times - 第几次审批
  125. * @return {Promise<*>}
  126. */
  127. async getCurAuditor(detailId, times = 1) {
  128. 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` ' +
  129. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  130. ' WHERE la.`td_id` = ? and la.`status` = ? and la.`times` = ?';
  131. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, detailId, auditConst.status.checking, times];
  132. return await this.db.queryOne(sql, sqlParam);
  133. }
  134. async updateNewAuditList(detail, newIdList) {
  135. const transaction = await this.db.beginTransaction();
  136. try {
  137. // 先删除旧的审批流,再添加新的
  138. await transaction.delete(this.tableName, { td_id: detail.id, times: detail.times });
  139. const newAuditors = [];
  140. let order = 1;
  141. for (const aid of newIdList) {
  142. newAuditors.push({
  143. tender_id: detail.tender_id, tr_id: detail.tr_id, td_id: detail.id, aid,
  144. times: detail.times, order, status: auditConst.status.uncheck,
  145. });
  146. order++;
  147. }
  148. if (newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  149. await transaction.commit();
  150. } catch (err) {
  151. await transaction.rollback();
  152. throw err;
  153. }
  154. }
  155. async updateLastAudit(detail, auditList, lastId) {
  156. const transaction = await this.db.beginTransaction();
  157. try {
  158. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  159. const idList = this._.map(auditList, 'aid');
  160. let order = idList.length + 1;
  161. if (idList.indexOf(lastId) !== -1) {
  162. await transaction.delete(this.tableName, { td_id: detail.id, times: detail.times, aid: lastId });
  163. const audit = this._.find(auditList, { aid: lastId });
  164. // 顺移之后审核人流程顺序
  165. await this._syncOrderByDelete(transaction, detail.id, audit.order, detail.times);
  166. order = order - 1;
  167. }
  168. // 添加终审
  169. const newAuditor = {
  170. tender_id: detail.tender_id, tr_id: detail.tr_id, td_id: detail.id, aid: lastId,
  171. times: detail.times, order, status: auditConst.status.uncheck,
  172. };
  173. await transaction.insert(this.tableName, newAuditor);
  174. await transaction.commit();
  175. } catch (err) {
  176. await transaction.rollback();
  177. throw err;
  178. }
  179. }
  180. /**
  181. * 移除审核人时,同步其后审核人order
  182. * @param transaction - 事务
  183. * @param {Number} materialId - 材料调差期id
  184. * @param {Number} auditorId - 审核人id
  185. * @param {Number} times - 第几次审批
  186. * @return {Promise<*>}
  187. * @private
  188. */
  189. async _syncOrderByDelete(transaction, detailId, order, times, selfOperate = '-') {
  190. this.initSqlBuilder();
  191. this.sqlBuilder.setAndWhere('td_id', {
  192. value: detailId,
  193. operate: '=',
  194. });
  195. this.sqlBuilder.setAndWhere('order', {
  196. value: order,
  197. operate: '>=',
  198. });
  199. this.sqlBuilder.setAndWhere('times', {
  200. value: times,
  201. operate: '=',
  202. });
  203. this.sqlBuilder.setUpdateData('order', {
  204. value: 1,
  205. selfOperate,
  206. });
  207. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  208. const data = await transaction.query(sql, sqlParam);
  209. return data;
  210. }
  211. /**
  212. * 获取审核人流程列表(包括原报)
  213. * @param {Number} materialId 调差id
  214. * @param {Number} times 审核次数
  215. * @return {Promise<Array>} 查询结果集(包括原报)
  216. */
  217. async getAuditorsWithOwner(detailId, times = 1) {
  218. const result = await this.getAuditGroupByList(detailId, times);
  219. const sql =
  220. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`' +
  221. ' FROM ' +
  222. this.ctx.service.paymentDetail.tableName +
  223. ' As s' +
  224. ' LEFT JOIN ' +
  225. this.ctx.service.projectAccount.tableName +
  226. ' As pa' +
  227. ' ON s.uid = pa.id' +
  228. ' WHERE s.id = ?';
  229. const sqlParam = [times, detailId, detailId];
  230. const user = await this.db.queryOne(sql, sqlParam);
  231. result.unshift(user);
  232. return result;
  233. }
  234. /**
  235. * 获取 最新审核顺序
  236. *
  237. * @param {Number} materialId - 材料调差期id
  238. * @param {Number} times - 第几次审批
  239. * @return {Promise<number>}
  240. */
  241. async getNewOrder(detailId, times = 1) {
  242. const sql = 'SELECT Max(??) As max_order FROM ?? Where `td_id` = ? and `times` = ?';
  243. const sqlParam = ['order', this.tableName, detailId, times];
  244. const result = await this.db.queryOne(sql, sqlParam);
  245. return result && result.max_order ? result.max_order + 1 : 1;
  246. }
  247. /**
  248. * 新增审核人
  249. *
  250. * @param {Number} materialId - 材料调差期id
  251. * @param {Number} auditorId - 审核人id
  252. * @param {Number} times - 第几次审批
  253. * @return {Promise<number>}
  254. */
  255. async addAuditor(detailId, auditorId, times = 1, is_gdzs = 0) {
  256. const transaction = await this.db.beginTransaction();
  257. try {
  258. let newOrder = await this.getNewOrder(detailId, times);
  259. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  260. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  261. if (is_gdzs) await this._syncOrderByDelete(transaction, detailId, newOrder, times, '+');
  262. const data = {
  263. tender_id: this.ctx.paymentTender.id,
  264. tr_id: this.ctx.trInfo.id,
  265. td_id: detailId,
  266. aid: auditorId,
  267. times,
  268. order: newOrder,
  269. status: auditConst.status.uncheck,
  270. };
  271. const result = await transaction.insert(this.tableName, data);
  272. await transaction.commit();
  273. return result.affectedRows === 1;
  274. } catch (err) {
  275. await transaction.rollback();
  276. throw err;
  277. }
  278. return false;
  279. }
  280. /**
  281. * 移除审核人
  282. *
  283. * @param {Number} materialId - 材料调差期id
  284. * @param {Number} auditorId - 审核人id
  285. * @param {Number} times - 第几次审批
  286. * @return {Promise<boolean>}
  287. */
  288. async deleteAuditor(detailId, auditorId, times = 1) {
  289. const transaction = await this.db.beginTransaction();
  290. try {
  291. const condition = { td_id: detailId, aid: auditorId, times };
  292. const auditor = await this.getDataByCondition(condition);
  293. if (!auditor) {
  294. throw '该审核人不存在';
  295. }
  296. await this._syncOrderByDelete(transaction, detailId, auditor.order, times);
  297. await transaction.delete(this.tableName, condition);
  298. await transaction.commit();
  299. } catch (err) {
  300. await transaction.rollback();
  301. throw err;
  302. }
  303. return true;
  304. }
  305. /**
  306. * 移除审核人
  307. *
  308. * @param {Number} materialId - 材料调差期id
  309. * @param {Number} status - 期状态
  310. * @param {Number} status - 期次数
  311. * @return {Promise<boolean>}
  312. */
  313. async getAuditorByStatus(detailId, status, times = 1) {
  314. let auditor = null;
  315. let sql = '';
  316. let sqlParam = '';
  317. switch (status) {
  318. case auditConst.status.checking :
  319. case auditConst.status.checked :
  320. case auditConst.status.checkNoPre :
  321. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tr_id`, la.`td_id`, la.`aid`, la.`order` ' +
  322. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  323. ' WHERE la.`td_id` = ? and la.`status` = ? ' +
  324. ' ORDER BY la.`times` desc, la.`order` desc';
  325. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, detailId, status];
  326. auditor = await this.db.queryOne(sql, sqlParam);
  327. break;
  328. case auditConst.status.checkNo :
  329. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tr_id`, la.`td_id`, la.`aid`, la.`order` ' +
  330. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  331. ' WHERE la.`td_id` = ? and la.`status` = ? and la.`times` = ?' +
  332. ' ORDER BY la.`times` desc, la.`order` desc';
  333. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, detailId, auditConst.status.checkNo, parseInt(times) - 1];
  334. auditor = await this.db.queryOne(sql, sqlParam);
  335. break;
  336. case auditConst.status.uncheck :
  337. default:break;
  338. }
  339. return auditor;
  340. }
  341. /**
  342. * 开始审批
  343. * @param {Number} materialId - 材料调差期id
  344. * @param {Number} times - 第几次审批
  345. * @return {Promise<boolean>}
  346. */
  347. async start(detailId, times = 1) {
  348. const audit = await this.getDataByCondition({ td_id: detailId, times, order: 1 });
  349. if (!audit) {
  350. if (this.ctx.trinfo.sp_status === shenpiConst.sp_status.gdspl) {
  351. throw '请联系管理员添加审批人';
  352. } else {
  353. throw '请先选择审批人,再上报数据';
  354. }
  355. }
  356. const transaction = await this.db.beginTransaction();
  357. try {
  358. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date() });
  359. // 如果是报表角色则需要更新到detail中
  360. const updateDetailData = {
  361. id: detailId, status: auditConst.status.checking,
  362. };
  363. const rptAudit = await this.ctx.service.paymentRptAudit.getDataByCondition({ td_id: detailId, uid: this.ctx.session.sessionUser.accountId });
  364. if (rptAudit && rptAudit.signature_msg) {
  365. const report_json = JSON.parse(this.ctx.detail.report_json);
  366. const sign_msg = JSON.parse(rptAudit.signature_msg);
  367. updateDetailData.report_json = JSON.stringify(await this.ctx.service.paymentDetail.signOneSignatureData(report_json, rptAudit.signature_name, sign_msg));
  368. // 更新签字确定时间
  369. await this.ctx.service.paymentRptAudit.updateSignTime(transaction, rptAudit.id);
  370. }
  371. await transaction.update(this.ctx.service.paymentDetail.tableName, updateDetailData);
  372. // 安全生产费存储相关
  373. await this.ctx.service.paymentSafeBills.auditCache(transaction, detailId, {
  374. user_id: this.ctx.detail.uid, times, order: 0,
  375. });
  376. // 判断用户是否有权限查看支付审批,没有则自动加入到权限中
  377. const auditList = await this.getAllDataByCondition({ where: { td_id: detailId, times } });
  378. const rptAuditList = await this.ctx.service.paymentRptAudit.getAllDataByCondition({ where: { td_id: detailId } });
  379. const auditIdList = this._.map(auditList, 'aid');
  380. const rptAuditIdList = this._.map(rptAuditList, 'uid');
  381. const permissionAuditList = await this.ctx.service.subProjPermission.getPaymentAuditList(this.ctx.subProject.id);
  382. const paIdList = this._.map(permissionAuditList, 'uid');
  383. const detailIdList = this._.union(auditIdList, rptAuditIdList);
  384. const newAudits = this._.difference(detailIdList, paIdList);
  385. if (newAudits.length > 0) {
  386. await this.ctx.service.subProjPermission.savePaymentPermissionAudits(this.ctx.subProject.id, newAudits, 'add', transaction);
  387. }
  388. // todo 更新标段tender状态 ?
  389. await transaction.commit();
  390. } catch (err) {
  391. await transaction.rollback();
  392. throw err;
  393. }
  394. return true;
  395. }
  396. async _checked(pid, detailId, checkData, times) {
  397. const time = new Date();
  398. // 整理当前流程审核人状态更新
  399. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  400. if (!audit) {
  401. throw '审核数据错误';
  402. }
  403. const nextAudit = await this.getDataByCondition({ td_id: detailId, times, order: audit.order + 1 });
  404. const transaction = await this.db.beginTransaction();
  405. try {
  406. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  407. // 如果是报表角色则需要更新到detail中
  408. const updateDetailData = {
  409. id: detailId,
  410. };
  411. const rptAudit = await this.ctx.service.paymentRptAudit.getDataByCondition({ td_id: detailId, uid: audit.aid });
  412. if (rptAudit && rptAudit.signature_msg) {
  413. const report_json = JSON.parse(this.ctx.detail.report_json);
  414. const sign_msg = JSON.parse(rptAudit.signature_msg);
  415. updateDetailData.report_json = JSON.stringify(await this.ctx.service.paymentDetail.signOneSignatureData(report_json, rptAudit.signature_name, sign_msg));
  416. await this.ctx.service.paymentRptAudit.updateSignTime(transaction, rptAudit.id);
  417. }
  418. // 无下一审核人表示,审核结束
  419. if (nextAudit) {
  420. // 流程至下一审批人
  421. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  422. // 同步 期信息
  423. updateDetailData.status = auditConst.status.checking;
  424. } else {
  425. // 本期结束
  426. // 同步 期信息
  427. updateDetailData.status = checkData.checkType;
  428. }
  429. // 安全生产费存储相关
  430. await this.ctx.service.paymentSafeBills.auditCache(transaction, detailId, {
  431. user_id: audit.id, times: audit.times, order: audit.order,
  432. });
  433. await transaction.update(this.ctx.service.paymentDetail.tableName, updateDetailData);
  434. await transaction.commit();
  435. } catch (err) {
  436. await transaction.rollback();
  437. throw err;
  438. }
  439. }
  440. async _checkNo(pid, detailId, checkData, times) {
  441. const time = new Date();
  442. const detailInfo = await this.ctx.service.paymentDetail.getDataById(detailId);
  443. const trInfo = await this.ctx.service.paymentTenderRpt.getDataById(detailInfo.tr_id);
  444. // 整理当前流程审核人状态更新
  445. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  446. if (!audit) {
  447. throw '审核数据错误';
  448. }
  449. const sql = 'SELECT `tender_id`, `tr_id`, `td_id`, `aid`, `order` FROM ?? WHERE `td_id` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  450. const sqlParam = [this.tableName, detailId, times];
  451. const auditors = await this.db.query(sql, sqlParam);
  452. // 可能更换了上报人且存在于审批流程中,需要删除
  453. const userIndex = this._.findIndex(auditors, { aid: trInfo.uid });
  454. if (userIndex !== -1) {
  455. auditors.splice(userIndex, 1);
  456. }
  457. let order = 1;
  458. for (const a of auditors) {
  459. a.times = times + 1;
  460. a.order = order;
  461. a.status = auditConst.status.uncheck;
  462. order++;
  463. }
  464. const transaction = await this.db.beginTransaction();
  465. try {
  466. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  467. // 清空所有签名数据
  468. let report_json = JSON.parse(this.ctx.detail.report_json);
  469. if (report_json) {
  470. report_json = await this.ctx.service.paymentDetail.clearAllSignatureData(report_json);
  471. }
  472. const detailUpdateData = {
  473. id: detailId, status: checkData.checkType,
  474. times: times + 1,
  475. report_json: JSON.stringify(report_json),
  476. };
  477. // 可能存在更换了上报人的情况,需要替换
  478. if (detailInfo.uid !== trInfo.uid) {
  479. detailUpdateData.uid = trInfo.uid;
  480. }
  481. // 同步期信息
  482. await transaction.update(this.ctx.service.paymentDetail.tableName, detailUpdateData);
  483. // 清空签名
  484. await transaction.update(this.ctx.service.paymentRptAudit.tableName, {
  485. signature_msg: null,
  486. sign_time: null,
  487. }, {
  488. where: {
  489. td_id: detailId,
  490. },
  491. });
  492. // 拷贝新一次审核流程列表
  493. await transaction.insert(this.tableName, auditors);
  494. // 安全生产费存储相关
  495. await this.ctx.service.paymentSafeBills.auditCache(transaction, detailId, {
  496. user_id: audit.id, times: audit.times, order: audit.order,
  497. });
  498. await transaction.commit();
  499. } catch (err) {
  500. await transaction.rollback();
  501. throw err;
  502. }
  503. }
  504. async _checkNoPre(pid, detailId, checkData, times) {
  505. const time = new Date();
  506. // 整理当前流程审核人状态更新
  507. const audit = await this.getDataByCondition({ td_id: detailId, times, status: auditConst.status.checking });
  508. if (!audit || audit.order <= 1) {
  509. throw '审核数据错误';
  510. }
  511. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  512. const auditors2 = await this.getAuditGroupByList(detailId, times);
  513. const auditorIndex = await auditors2.findIndex(function(item) {
  514. return item.aid === audit.aid;
  515. });
  516. const preAuditor = auditors2[auditorIndex - 1];
  517. const transaction = await this.db.beginTransaction();
  518. try {
  519. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  520. // 顺移气候审核人流程顺序
  521. this.initSqlBuilder();
  522. this.sqlBuilder.setAndWhere('td_id', { value: detailId, operate: '=' });
  523. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  524. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  525. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  526. const data = await transaction.query(sql, sqlParam);
  527. const newAuditors = [];
  528. newAuditors.push({
  529. tender_id: audit.tender_id, tr_id: audit.tr_id, td_id: audit.td_id, aid: preAuditor.aid,
  530. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  531. begin_time: time,
  532. });
  533. newAuditors.push({
  534. tender_id: audit.tender_id, tr_id: audit.tr_id, td_id: audit.td_id, aid: audit.aid,
  535. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  536. });
  537. await transaction.insert(this.tableName, newAuditors);
  538. // 清除本人的签章
  539. await this.ctx.service.paymentRptAudit.clearSignatureMsg(transaction, detailId, audit.aid);
  540. // 安全生产费存储相关
  541. await this.ctx.service.paymentSafeBills.auditCache(transaction, detailId, {
  542. user_id: audit.id, times: audit.times, order: audit.order,
  543. });
  544. await transaction.commit();
  545. } catch (error) {
  546. await transaction.rollback();
  547. throw error;
  548. }
  549. }
  550. /**
  551. * 审批
  552. * @param {Number} materialId - 材料调差期id
  553. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  554. * @param {Number} times - 第几次审批
  555. * @return {Promise<void>}
  556. */
  557. async check(detailId, checkData, times = 1) {
  558. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  559. throw '提交数据错误';
  560. }
  561. const pid = this.ctx.session.sessionProject.id;
  562. switch (checkData.checkType) {
  563. case auditConst.status.checked:
  564. await this._checked(pid, detailId, checkData, times);
  565. break;
  566. case auditConst.status.checkNo:
  567. await this._checkNo(pid, detailId, checkData, times);
  568. break;
  569. case auditConst.status.checkNoPre:
  570. await this._checkNoPre(pid, detailId, checkData, times);
  571. break;
  572. default:
  573. throw '无效审批操作';
  574. }
  575. }
  576. async followAuditByRptAudit(detail) {
  577. const transaction = await this.db.beginTransaction();
  578. try {
  579. const newAuditIds = this._.map(detail.rptAudits, 'uid');
  580. // 去除上报人
  581. if (this._.includes(newAuditIds, detail.uid)) {
  582. this._.pull(newAuditIds, detail.uid);
  583. }
  584. // 如果存在固定终审,判断它是否在报表人员里,存在则把该id移到最后,不存在则插入newAuditIds最后;
  585. if (this.ctx.trInfo.sp_status === shenpiConst.sp_status.gdzs) {
  586. const gdzsInfo = await this.ctx.service.paymentShenpiAudit.getAudit(detail.tender_id, detail.tr_id, shenpiConst.sp_status.gdzs);
  587. if (gdzsInfo && gdzsInfo.audit_id !== newAuditIds[newAuditIds.length - 1]) {
  588. if (this._.includes(newAuditIds, gdzsInfo.audit_id)) {
  589. this._.pull(newAuditIds, gdzsInfo.audit_id);
  590. }
  591. newAuditIds.push(gdzsInfo.audit_id);
  592. }
  593. }
  594. // 删除原审批流
  595. await transaction.delete(this.tableName, { td_id: detail.id, times: detail.times });
  596. // 添加新审批流
  597. const insertDatas = [];
  598. let order = 1;
  599. for (const id of newAuditIds) {
  600. insertDatas.push({
  601. tender_id: detail.tender_id,
  602. tr_id: detail.tr_id,
  603. td_id: detail.id,
  604. aid: id,
  605. order,
  606. times: detail.times,
  607. status: auditConst.status.uncheck,
  608. });
  609. order = order + 1;
  610. }
  611. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  612. await transaction.commit();
  613. return true;
  614. } catch (error) {
  615. await transaction.rollback();
  616. throw error;
  617. }
  618. }
  619. /**
  620. * 获取审核人需要审核的期列表
  621. *
  622. * @param auditorId
  623. * @return {Promise<*>}
  624. */
  625. async getAuditPayment(auditorId) {
  626. const sql =
  627. 'SELECT pda.`aid`, pda.`times`, pda.`order`, pda.`begin_time`, pda.`end_time`, pda.`tender_id`, pda.`tr_id`, pda.`td_id`,' +
  628. ' pd.`code` As `scode`, pd.`order` As `sorder`, pd.`status` As `sstatus`, pd.type,' +
  629. ' pr.`rpt_name` As `rpt_name`,' +
  630. ' t.`name`, t.`pid`, t.`uid` ' +
  631. ' FROM ?? AS pda ' +
  632. ' Left Join ?? AS pd On pda.`td_id` = pd.`id` ' +
  633. ' Left Join ?? AS pr On pda.`tr_id` = pr.`id` ' +
  634. ' Left Join ?? As t ON pda.`tender_id` = t.`id`' +
  635. ' WHERE ((pda.`aid` = ? and pda.`status` = ?) OR (pd.`uid` = ? and pda.`status` = ? and pd.`status` = ? and pda.`times` = (pd.`times`-1)))' +
  636. ' ORDER BY pda.`begin_time` DESC';
  637. const sqlParam = [
  638. this.tableName,
  639. this.ctx.service.paymentDetail.tableName,
  640. this.ctx.service.paymentTenderRpt.tableName,
  641. this.ctx.service.paymentTender.tableName,
  642. auditorId,
  643. auditConst.status.checking,
  644. auditorId,
  645. auditConst.status.checkNo,
  646. auditConst.status.checkNo,
  647. ];
  648. return await this.db.query(sql, sqlParam);
  649. }
  650. async updateAuditByReport(transaction, td_id, times, uid) {
  651. const auditor = await this.getDataByCondition({ td_id, times, aid: uid });
  652. if (auditor) {
  653. // 更新order
  654. await this._syncOrderByDelete(transaction, td_id, auditor.order, times);
  655. return await transaction.delete(this.tableName, { id: auditor.id });
  656. }
  657. }
  658. }
  659. return PaymentDetailAudit;
  660. };