payment_detail_audit.js 33 KB

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