project_spread.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2024/3/5
  7. * @version
  8. */
  9. const SpreadConst = require('../const/spread');
  10. const JsonFields = [
  11. 'tz_ledger_set', 'tz_ledger_bills_spread', 'tz_ledger_pos_spread',
  12. 'gcl_ledger_set', 'gcl_ledger_bills_spread', 'gcl_ledger_pos_spread',
  13. 'tz_stage_set', 'tz_stage_bills_spread', 'tz_stage_pos_spread',
  14. 'gcl_stage_set', 'gcl_stage_bills_spread', 'gcl_stage_pos_spread',
  15. ];
  16. module.exports = app => {
  17. class ProjectSpread extends app.BaseService {
  18. /**
  19. * 构造函数
  20. *
  21. * @param {Object} ctx - egg全局变量
  22. * @return {void}
  23. */
  24. constructor(ctx) {
  25. super(ctx);
  26. this.tableName = 'project_spread';
  27. }
  28. _analysisData(data) {
  29. if (!data) return;
  30. JsonFields.forEach(jf => { if(data[jf]) data[jf] = JSON.parse(data[jf]); });
  31. }
  32. async loadProjectSpread(id, code = '') {
  33. const result = code ? await this.getDataByCondition({ pid: id, code }) : await this.getDataByCondition({ pid: id, is_default: 1 });
  34. this._analysisData(result);
  35. return result;
  36. }
  37. generateSpreadSetting(colSet, emptyBase, BaseSetCol, BaseSpreadColSetting, spreadType = 'bills') {
  38. const spreadSetting = JSON.parse(JSON.stringify(emptyBase));
  39. for (const col of colSet) {
  40. if (!col.valid) continue;
  41. const dc = BaseSetCol.find(x => { return x.key === col.key; });
  42. if (!dc) continue;
  43. const orgBaseCols = BaseSpreadColSetting[col.key];
  44. if (!orgBaseCols) continue;
  45. const baseCols = JSON.parse(JSON.stringify(orgBaseCols));
  46. if (dc.fixed.indexOf('alias') < 0 && col.alias) {
  47. if (baseCols.length === 1) {
  48. if (baseCols[0].aliasFormat) {
  49. baseCols[0].title = baseCols[0].aliasFormat.replace('{%s}', col.alias);
  50. delete baseCols[0].aliasFormat;
  51. } else {
  52. baseCols[0].title = col.alias;
  53. }
  54. } else {
  55. baseCols.forEach(x => {
  56. if (x.aliasFormat) {
  57. x.title = x.aliasFormat.replace('{%s}', col.alias);
  58. delete x.aliasFormat;
  59. }
  60. });
  61. }
  62. }
  63. spreadSetting.cols.push(...baseCols);
  64. }
  65. return spreadSetting;
  66. }
  67. updateSpreadWithSpec(spreadSetting, specSetting) {
  68. if (!specSetting) return;
  69. for (const s of specSetting) {
  70. for (const c of spreadSetting.cols) {
  71. if (s.condition.value.indexOf(c[s.condition.key]) >= 0) {
  72. this._.assignIn(c, s.update);
  73. }
  74. }
  75. }
  76. }
  77. generateRelaSpread(colSetType, colSet) {
  78. const baseSetCol = colSetType.indexOf('stage') > 0 ? SpreadConst.BaseSetCol.Stage : SpreadConst.BaseSetCol.Ledger;
  79. const baseSpreadColSetting = colSetType.indexOf('stage') > 0 ? SpreadConst.BaseSpreadColSetting.Stage : SpreadConst.BaseSpreadColSetting.Ledger;
  80. const billsSpread = this.generateSpreadSetting(colSet, SpreadConst.EmptySpreadSetting[colSetType].bills, baseSetCol, baseSpreadColSetting.bills);
  81. const posSpread = this.generateSpreadSetting(colSet, SpreadConst.EmptySpreadSetting[colSetType].pos, baseSetCol, baseSpreadColSetting.pos, 'pos');
  82. const spreadSpec = SpreadConst.SpreadSpec[colSetType];
  83. if (spreadSpec) {
  84. this.updateSpreadWithSpec(billsSpread, spreadSpec.bills);
  85. this.updateSpreadWithSpec(posSpread, spreadSpec.pos);
  86. }
  87. return [billsSpread, posSpread];
  88. }
  89. async initProjectSpread(id) {
  90. for (const SpreadTemplate of SpreadConst.ProjectSpreadTemplate) {
  91. const data = JSON.parse(JSON.stringify(SpreadTemplate.template));
  92. [data.tz_ledger_bills_spread, data.tz_ledger_pos_spread] = this.generateRelaSpread('tz_ledger_set', data.tz_ledger_set);
  93. [data.tz_stage_bills_spread, data.tz_stage_pos_spread] = this.generateRelaSpread('tz_stage_set', data.tz_stage_set);
  94. [data.gcl_ledger_bills_spread, data.gcl_ledger_pos_spread] = this.generateRelaSpread('gcl_ledger_set', data.gcl_ledger_set);
  95. [data.gcl_stage_bills_spread, data.gcl_stage_pos_spread] = this.generateRelaSpread('gcl_stage_set', data.gcl_stage_set);
  96. const updateData = { pid: id, code: SpreadTemplate.code, name: SpreadTemplate.name, is_default: SpreadTemplate.isDefault };
  97. JsonFields.forEach(jf => { if (data[jf]) updateData[jf] = JSON.stringify(data[jf]) });
  98. await this.db.insert(this.tableName, updateData);
  99. }
  100. }
  101. async getProjectSpread(id, code) {
  102. const curSet = await this.loadProjectSpread(id, code);
  103. if (curSet) return curSet;
  104. await this.initProjectSpread(id);
  105. return await this.loadProjectSpread(id, code);
  106. }
  107. async getProjectSpreadType(id) {
  108. const result = await this.getAllDataByCondition({
  109. columns: ['code', 'name', 'is_default'],
  110. where: { pid: id },
  111. });
  112. if (result.length > 0) return result;
  113. await this.initProjectSpread(id);
  114. return await this.getAllDataByCondition({
  115. columns: ['code', 'name', 'is_default'],
  116. where: { pid: id },
  117. });
  118. }
  119. async updateProjectSet(id, code, colSetType, colSet) {
  120. const updateData = {};
  121. const [billsSpread, posSpread] = this.generateRelaSpread(colSetType, colSet);
  122. updateData[colSetType.replace('_set', '_bills_spread')] = JSON.stringify(billsSpread);
  123. updateData[colSetType.replace('_set', '_pos_spread')] = JSON.stringify(posSpread);
  124. updateData[colSetType] = JSON.stringify(colSet);
  125. await this.defaultUpdate(updateData, { where: { pid: id, code } });
  126. }
  127. async resetProjectSet(id, code, colSetType, resetCode) {
  128. const SpreadTemplate = SpreadConst.ProjectSpreadTemplate.find(x => { return x.code === resetCode; });
  129. if (!SpreadTemplate) throw '选择模板不存在';
  130. const colSet = SpreadTemplate.template[colSetType];
  131. const updateData = {};
  132. const [billsSpread, posSpread] = this.generateRelaSpread(colSetType, colSet);
  133. updateData[colSetType.replace('_set', '_bills_spread')] = JSON.stringify(billsSpread);
  134. updateData[colSetType.replace('_set', '_pos_spread')] = JSON.stringify(posSpread);
  135. updateData[colSetType] = JSON.stringify(colSet);
  136. await this.defaultUpdate(updateData, { where: { pid: id, code } });
  137. }
  138. }
  139. return ProjectSpread;
  140. };