payment_tender_rpt.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. /**
  3. * 决策大屏用户查看权限-数据模型
  4. *
  5. * @author ellisran
  6. * @date 2021/09/23
  7. * @version
  8. */
  9. const accountGroup = require('../const/account_group').group;
  10. const paymentConst = require('../const/payment');
  11. module.exports = app => {
  12. class paymentTenderRpt extends app.BaseService {
  13. constructor(ctx) {
  14. super(ctx);
  15. this.tableName = 'payment_tender_rpt';
  16. }
  17. async getProcessList(id) {
  18. const sql = 'SELECT ptr.*, pa.name as user_name FROM ?? as ptr LEFT JOIN ?? as pa ON ptr.`uid` = pa.`id` WHERE ptr.`tender_id` = ?';
  19. const params = [this.tableName, this.ctx.service.projectAccount.tableName, id];
  20. return await this.db.query(sql, params);
  21. }
  22. async checkAndUpdateList(tenderRptList, rptProjectList) {
  23. if (tenderRptList.length > 0) {
  24. const updateDatas = [];
  25. const delDatas = [];
  26. for (const tr of tenderRptList) {
  27. const rptInfo = this._.find(rptProjectList, { ID: tr.rpt_id });
  28. // 判断是否已经新建过报表次
  29. const had_rpt = await this.ctx.service.paymentDetail.hadDetail(tr.id);
  30. if (tr.is_del === 0 && !rptInfo) {
  31. if (had_rpt) {
  32. updateDatas.push({
  33. id: tr.id,
  34. is_del: 1,
  35. });
  36. tr.is_del = 1;
  37. } else {
  38. delDatas.push(tr.id);
  39. }
  40. } else if (rptInfo && tr.rpt_name !== rptInfo.name) {
  41. updateDatas.push({
  42. id: tr.id,
  43. rpt_name: rptInfo.name,
  44. });
  45. tr.rpt_name = rptInfo.name;
  46. }
  47. rptInfo.had_rpt = had_rpt;
  48. }
  49. if (updateDatas.length > 0) await this.db.updateRows(this.tableName, updateDatas);
  50. if (delDatas.length > 0) {
  51. this._.remove(tenderRptList, function(item) {
  52. return delDatas.indexOf(item.id) !== -1;
  53. });
  54. await this.db.delete(this.tableName, { id: delDatas });
  55. }
  56. }
  57. return [tenderRptList, rptProjectList];
  58. }
  59. async setRpt(tid, rpt_list) {
  60. const transaction = await this.db.beginTransaction();
  61. try {
  62. const originList = await this.getAllDataByCondition({ where: { tender_id: tid, is_del: 0 } });
  63. const insertData = [];
  64. let deleteData = [];
  65. if (originList.length === 0 && rpt_list.length !== 0) {
  66. // 添加到tender_rpt
  67. for (const rpt of rpt_list) {
  68. insertData.push({
  69. tender_id: tid,
  70. rpt_id: rpt.id,
  71. rpt_name: rpt.name,
  72. uid: this.ctx.session.sessionUser.accountId,
  73. in_time: new Date(),
  74. });
  75. }
  76. } else if (originList.length !== 0 && rpt_list.length === 0) {
  77. // 删除原有
  78. deleteData = this._.map(originList, 'id');
  79. } else if (originList.length !== 0 && rpt_list.length !== 0) {
  80. const orginRptIds = this._.map(originList, 'rpt_id');
  81. const newIds = this._.map(rpt_list, 'id');
  82. console.log(orginRptIds, newIds);
  83. const insertRptIds = this._.difference(newIds, orginRptIds);
  84. const deleteRptIds = this._.difference(orginRptIds, newIds);
  85. if (deleteRptIds.length > 0) {
  86. for (const id of deleteRptIds) {
  87. const orginInfo = this._.find(originList, { rpt_id: id });
  88. deleteData.push(orginInfo.id);
  89. }
  90. }
  91. console.log(insertRptIds, deleteData);
  92. for (const id of insertRptIds) {
  93. const info = this._.find(rpt_list, { id });
  94. insertData.push({
  95. tender_id: tid,
  96. rpt_id: id,
  97. rpt_name: info.name,
  98. uid: this.ctx.session.sessionUser.accountId,
  99. in_time: new Date(),
  100. });
  101. }
  102. }
  103. if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
  104. if (deleteData.length > 0) await transaction.delete(this.tableName, { id: deleteData });
  105. await transaction.commit();
  106. return await this.getProcessList(tid);
  107. } catch (err) {
  108. await transaction.rollback();
  109. throw err;
  110. }
  111. }
  112. async setStatus(id, sp_status) {
  113. const transaction = await this.db.beginTransaction();
  114. try {
  115. await transaction.update(this.tableName, { id, sp_status });
  116. await transaction.commit();
  117. return await this.ctx.service.paymentShenpiAudit.getShenpiAudit(id, sp_status);
  118. } catch (err) {
  119. await transaction.rollback();
  120. throw err;
  121. }
  122. }
  123. }
  124. return paymentTenderRpt;
  125. };