rpt_custom_define.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const JV = require('../reports/rpt_component/jpc_value_define');
  10. module.exports = app => {
  11. class RptCustomDefine extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'rpt_custom_define';
  21. }
  22. async getCustomDefine(tid, sid, rid) {
  23. const data = await this.getDataByCondition({ tid: tid, sid: sid, rid: rid });
  24. if (data && data.audit_select) data.audit_select = JSON.parse(data.audit_select);
  25. if (data && data.gather_select) data.gather_select = JSON.parse(data.gather_select);
  26. if (data && data.stage_select) data.stage_select = JSON.parse(data.stage_select);
  27. if (data && data.material_sum_select) data.material_sum_select = JSON.parse(data.material_sum_select);
  28. return data;
  29. }
  30. async addInitialStageData(stage, preStage, transaction) {
  31. if (!stage || !preStage) {
  32. throw '标段数据有误';
  33. }
  34. const preDatas = await this.getAllDataByCondition({
  35. where: {sid: preStage.id}
  36. });
  37. if (preDatas.length > 0) {
  38. for (const pd of preDatas) {
  39. delete pd.id;
  40. pd.sid = stage.id;
  41. }
  42. const result = await transaction.insert(this.tableName, preDatas);
  43. return result.affectedRows === preDatas.length;
  44. } else {
  45. return true;
  46. }
  47. }
  48. async saveCustomSelect(data) {
  49. const sid = data.audit_select ? data.stage_id : -1;
  50. const filter = {tid: data.tender_id, sid: sid, rid: data.rpt_tpl_id};
  51. const count = await this.count(filter);
  52. const updateData = {};
  53. if (data.audit_select) updateData.audit_select = JSON.stringify(data.audit_select);
  54. if (data.gather_select) updateData.gather_select = JSON.stringify(data.gather_select);
  55. if (data.stage_select) updateData.stage_select = JSON.stringify(data.stage_select);
  56. if (data.material_sum_select) updateData.material_sum_select = JSON.stringify(data.material_sum_select);
  57. if (count > 0) {
  58. await this.update(updateData, filter);
  59. } else {
  60. updateData.tid = data.tender_id;
  61. updateData.sid = sid;
  62. updateData.rid = data.rpt_tpl_id;
  63. await this.db.insert(this.tableName, updateData);
  64. }
  65. }
  66. async getCustomSelectByRpt(cid, rpt_c_type, tid, sid) {
  67. const result = await this.ctx.service.rptTpl.getAllDataByCondition({
  68. where: {cid: cid, rpt_c_type: rpt_c_type}
  69. });
  70. const customSelect = await this.getAllDataByCondition({
  71. where: {tid: tid, sid: sid}
  72. });
  73. for (const r of result) {
  74. const rptObj = JSON.parse(r.rpt_content);
  75. if (rptObj) r.custom_define = rptObj[JV.NODE_CUSTOM_DEFINE];
  76. delete r.rpt_content;
  77. const cs = this.ctx.helper._.find(customSelect, {rid: r.id});
  78. if (!cs) continue;
  79. r.tid = tid;
  80. r.sid = sid;
  81. if (cs.audit_select) r.audit_select = JSON.parse(cs.audit_select);
  82. if (cs.gather_select) r.gather_select = JSON.parse(cs.gather_select);
  83. if (cs.stage_select) r.stage_select = JSON.parse(cs.stage_select);
  84. if (cs.material_sum_select) r.material_sum_select = JSON.parse(cs.material_sum_select);
  85. }
  86. return result;
  87. }
  88. async getDataSelectByRpt(cid, rpt_d_type) {
  89. const result = await this.ctx.service.rptTpl.getAllDataByCondition({
  90. where: {cid: cid, rpt_d_type: rpt_d_type}
  91. });
  92. for (const r of result) {
  93. const rptObj = JSON.parse(r.rpt_content);
  94. if (rptObj) r.custom_define = rptObj[JV.NODE_CUSTOM_DEFINE];
  95. delete r.rpt_content;
  96. }
  97. return result;
  98. }
  99. }
  100. return RptCustomDefine;
  101. };