spec_msg.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const pushOperate = require('../const/spec_3f').pushOperate;
  10. module.exports = app => {
  11. class SpecPull extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 's2b_spec_msg';
  21. }
  22. async tenderNeedMsg(pid, tid, timing) {
  23. const specProj = await this.db.get('zh_s2b_spec_proj', { id: pid });
  24. if (!specProj || !specProj.is_push) return false;
  25. switch (specProj.push_tender_type) {
  26. case 1:
  27. const filter = specProj.filter_tender ? specProj.filter_tender.split(',') : [];
  28. if (filter.indexOf(tid + '') >= 0) return false;
  29. break;
  30. case 2:
  31. const access = specProj.access_tender ? specProj.access_tender.split(',') : [];
  32. if (access.indexOf(tid + '') < 0) return false;
  33. break;
  34. }
  35. const soleTender = await this.db.get('zh_s2b_spec_tender', { id: tid, is_valid: 1 });
  36. const specPush = await this.db.get('zh_s2b_spec_push', { pid, tid: soleTender ? tid : 0, valid: 1, push_timing: timing });
  37. return !!specPush;
  38. }
  39. async reportNeedMsg(pid, tid) {
  40. const reportMsg = {};
  41. reportMsg.stage = { valid: await this.tenderNeedMsg(pid, tid, pushOperate.report.file), key: pushOperate.report.file};
  42. reportMsg.change = { valid: await this.tenderNeedMsg(pid, tid, pushOperate.report.change_file), key: pushOperate.report.change_file };
  43. reportMsg.change_plan = { valid: await this.tenderNeedMsg(pid, tid, pushOperate.report.change_plan_file), key: pushOperate.report.change_plan_file };
  44. reportMsg.change_apply = { valid: await this.tenderNeedMsg(pid, tid, pushOperate.report.change_apply_file), key: pushOperate.report.change_apply_file };
  45. return reportMsg;
  46. }
  47. async addLedgerMsg(transaction, pid, tender, timing) {
  48. const needMsg = await this.tenderNeedMsg(pid, tender.id, timing);
  49. if (!needMsg) return;
  50. await transaction.insert(this.tableName, { pid, tid: tender.id, timing });
  51. }
  52. async addReviseMsg(transaction, pid, revise, timing) {
  53. const needMsg = await this.tenderNeedMsg(pid, revise.tid, timing);
  54. if (!needMsg) return;
  55. await transaction.insert(this.tableName, { pid, tid: revise.tid, rid: revise.id, timing });
  56. }
  57. async addStageMsg(transaction, pid, stage, timing) {
  58. const needMsg = await this.tenderNeedMsg(pid, stage.tid, timing);
  59. if (!needMsg) return;
  60. await transaction.insert(this.tableName, { pid, tid: stage.tid, sid: stage.id, timing });
  61. }
  62. async addAdvanceMsg(transaction, pid, advance, timing) {
  63. const needMsg = await this.tenderNeedMsg(pid, advance.id, timing);
  64. if (!needMsg) return;
  65. await transaction.insert(this.tableName, { pid, tid: advance.tid, advance_id: advance.id, timing });
  66. }
  67. async addMaterialMsg(transaction, pid, material, timing) {
  68. const needMsg = await this.tenderNeedMsg(pid, material.tid, timing);
  69. if (!needMsg) return;
  70. await transaction.insert(this.tableName, { pid, tid: material.tid, material_id: material.id, timing });
  71. }
  72. async addSettleMsg(transaction, pid, settle, timing) {
  73. const needMsg = await this.tenderNeedMsg(pid, settle.tid, timing);
  74. if (!needMsg) return;
  75. await transaction.insert(this.tableName, { pid, tid: settle.tid, settle_id: settle.id, timing });
  76. }
  77. async addChangeMsg(transaction, pid, change, timing) {
  78. const needMsg = await this.tenderNeedMsg(pid, change.tid, timing);
  79. if (!needMsg) return;
  80. await transaction.insert(this.tableName, { pid, tid: change.tid, cid: change.cid, timing });
  81. }
  82. async addChangeApplyMsg(transaction, pid, change_apply, timing) {
  83. const needMsg = await this.tenderNeedMsg(pid, change_apply.tid, timing);
  84. if (!needMsg) return;
  85. await transaction.insert(this.tableName, { pid, tid: change_apply.tid, c_apply_id: change_apply.id, timing });
  86. }
  87. async addChangeProjectMsg(transaction, pid, change_project, timing) {
  88. const needMsg = await this.tenderNeedMsg(pid, change_project.tid, timing);
  89. if (!needMsg) return;
  90. await transaction.insert(this.tableName, { pid, tid: change_project.tid, c_proj_id: change_project.id, timing });
  91. }
  92. async addChangePlanMsg(transaction, pid, change_plan, timing) {
  93. const needMsg = await this.tenderNeedMsg(pid, change_plan.tid, timing);
  94. if (!needMsg) return;
  95. await transaction.insert(this.tableName, { pid, tid: change_plan.tid, c_plan_id: change_plan.id, timing });
  96. }
  97. async addReportMsg(transaction, pid, tender, stage, timing, subInfo) {
  98. const needMsg = await this.tenderNeedMsg(pid, stage.tid, timing);
  99. if (!needMsg) return;
  100. if (transaction) {
  101. await transaction.insert(this.tableName, { pid, tid: tender.id, sid: stage ? stage.id : 0, timing, extra_info: JSON.stringify(subInfo || {}) });
  102. } else {
  103. await this.db.insert(this.tableName, { pid, tid: tender.id, sid: stage ? stage.id : 0, timing, extra_info: JSON.stringify(subInfo || {}) });
  104. }
  105. }
  106. async _getOtherMsgBaseData(id, timing) {
  107. switch(timing) {
  108. case pushOperate.report.change_file:
  109. const change = await this.ctx.service.change.getAllDataByCondition({ where: { cid: id } });
  110. return change.map(x => { return { cid: x.cid } });
  111. case pushOperate.report.change_plan_file:
  112. const changePlan = await this.ctx.service.changePlan.getAllDataByCondition({ where: { id } });
  113. return changePlan.map(x => { return { c_plan_id: x.id } });
  114. case pushOperate.report.change_apply_file:
  115. const changeApply = await this.ctx.service.changeApply.getAllDataByCondition({ where: { id } });
  116. return changeApply.map(x => { return { c_apply_id: x.id } });
  117. default:
  118. return [];
  119. }
  120. }
  121. async addOtherReportMsg(transaction, pid, tender, id, timing, subInfo) {
  122. const needMsg = await this.tenderNeedMsg(pid, tender.id, timing);
  123. if (!needMsg) return;
  124. const data = await this._getOtherMsgBaseData(id, timing);
  125. const insertMsg = data.map(x => {
  126. return { pid, tid: tender.id, timing, extra_info: JSON.stringify(subInfo || {}), ...x };
  127. });
  128. if (transaction) {
  129. await transaction.insert(this.tableName, insertMsg);
  130. } else {
  131. await this.db.insert(this.tableName, insertMsg);
  132. }
  133. }
  134. }
  135. return SpecPull;
  136. };