project_spread.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 = SpreadConst.BaseSetCol[colSetType];
  79. const baseSpreadColSetting = SpreadConst.BaseSpreadColSetting[colSetType];
  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 initProjectSpreadByTemplate(id, spreadTemplate) {
  90. const data = JSON.parse(JSON.stringify(spreadTemplate.template));
  91. [data.tz_ledger_bills_spread, data.tz_ledger_pos_spread] = this.generateRelaSpread('tz_ledger_set', data.tz_ledger_set);
  92. [data.tz_stage_bills_spread, data.tz_stage_pos_spread] = this.generateRelaSpread('tz_stage_set', data.tz_stage_set);
  93. [data.gcl_ledger_bills_spread, data.gcl_ledger_pos_spread] = this.generateRelaSpread('gcl_ledger_set', data.gcl_ledger_set);
  94. [data.gcl_stage_bills_spread, data.gcl_stage_pos_spread] = this.generateRelaSpread('gcl_stage_set', data.gcl_stage_set);
  95. const updateData = { pid: id, code: spreadTemplate.code, name: spreadTemplate.name, is_default: spreadTemplate.isDefault ? 1 : 0 };
  96. JsonFields.forEach(jf => { if (data[jf]) updateData[jf] = JSON.stringify(data[jf]) });
  97. await this.db.insert(this.tableName, updateData);
  98. }
  99. async initProjectSpread(id) {
  100. for (const template of SpreadConst.ProjectSpreadTemplate) {
  101. await this.initProjectSpreadByTemplate(id, template);
  102. }
  103. }
  104. async getProjectSpread(id, code) {
  105. const curSet = await this.loadProjectSpread(id, code);
  106. if (curSet) return curSet;
  107. await this.initProjectSpread(id);
  108. return await this.loadProjectSpread(id, code);
  109. }
  110. async getProjectSpreadType(id) {
  111. const result = await this.getAllDataByCondition({
  112. columns: ['code', 'name', 'is_default'],
  113. where: { pid: id },
  114. });
  115. if (result.length === SpreadConst.ProjectSpreadTemplate.length) return result;
  116. if (result.length === 0) {
  117. await this.initProjectSpread(id);
  118. } else {
  119. for (const spreadTemplate of SpreadConst.ProjectSpreadTemplate) {
  120. const SpreadType = result.find(x => { return x.code === spreadTemplate.code; });
  121. if (!SpreadType) await this.initProjectSpreadByTemplate(id, spreadTemplate);
  122. }
  123. }
  124. return await this.getAllDataByCondition({
  125. columns: ['code', 'name', 'is_default'],
  126. where: { pid: id },
  127. });
  128. }
  129. async updateProjectSet(id, code, colSetType, colSet) {
  130. const updateData = {};
  131. const [billsSpread, posSpread] = this.generateRelaSpread(colSetType, colSet);
  132. updateData[colSetType.replace('_set', '_bills_spread')] = JSON.stringify(billsSpread);
  133. updateData[colSetType.replace('_set', '_pos_spread')] = JSON.stringify(posSpread);
  134. updateData[colSetType] = JSON.stringify(colSet);
  135. await this.defaultUpdate(updateData, { where: { pid: id, code } });
  136. }
  137. async resetProjectSet(id, code, colSetType, resetCode) {
  138. const SpreadTemplate = SpreadConst.ProjectSpreadTemplate.find(x => { return x.code === resetCode; });
  139. if (!SpreadTemplate) throw '选择模板不存在';
  140. const colSet = SpreadTemplate.template[colSetType];
  141. const updateData = {};
  142. const [billsSpread, posSpread] = this.generateRelaSpread(colSetType, colSet);
  143. updateData[colSetType.replace('_set', '_bills_spread')] = JSON.stringify(billsSpread);
  144. updateData[colSetType.replace('_set', '_pos_spread')] = JSON.stringify(posSpread);
  145. updateData[colSetType] = JSON.stringify(colSet);
  146. await this.defaultUpdate(updateData, { where: { pid: id, code } });
  147. }
  148. }
  149. return ProjectSpread;
  150. };