project_spread.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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) {
  33. const result = await this.getDataByCondition({ pid: id, is_default: 1 });
  34. this._analysisData(result);
  35. return result;
  36. }
  37. async loadProjectSpreadByCode(id, code) {
  38. const result = await this.getDataByCondition({ pid: id, code });
  39. this._analysisData(result);
  40. return result;
  41. }
  42. generateSpreadSetting(colSet, emptyBase, BaseSetCol, BaseSpreadColSetting, spreadType = 'bills') {
  43. const spreadSetting = JSON.parse(JSON.stringify(emptyBase));
  44. for (const col of colSet) {
  45. if (!col.valid) continue;
  46. const dc = BaseSetCol.find(x => { return x.key === col.key; });
  47. if (!dc) continue;
  48. const orgBaseCols = BaseSpreadColSetting[col.key];
  49. if (!orgBaseCols) continue;
  50. const baseCols = JSON.parse(JSON.stringify(orgBaseCols));
  51. if (dc.fixed.indexOf('alias') < 0 && col.alias) {
  52. if (baseCols.length === 1) {
  53. if (baseCols[0].aliasFormat) {
  54. baseCols[0].title = baseCols[0].aliasFormat.replace('{%s}', col.alias);
  55. delete baseCols[0].aliasFormat;
  56. } else {
  57. baseCols[0].title = col.alias;
  58. }
  59. } else {
  60. baseCols.forEach(x => {
  61. if (x.aliasFormat) {
  62. x.title = x.aliasFormat.replace('{%s}', col.alias);
  63. delete x.aliasFormat;
  64. }
  65. });
  66. }
  67. }
  68. spreadSetting.cols.push(...baseCols);
  69. }
  70. return spreadSetting;
  71. }
  72. generateRelaSpread(colSetType, colSet) {
  73. const baseSetCol = colSetType.indexOf('stage') > 0 ? SpreadConst.BaseSetCol.Stage : SpreadConst.BaseSetCol.Ledger;
  74. const baseSpreadColSetting = colSetType.indexOf('stage') > 0 ? SpreadConst.BaseSpreadColSetting.Stage : SpreadConst.BaseSpreadColSetting.Ledger;
  75. const billsSpread = this.generateSpreadSetting(colSet, SpreadConst.EmptySpreadSetting[colSetType].bills, baseSetCol, baseSpreadColSetting.bills);
  76. const posSpread = this.generateSpreadSetting(colSet, SpreadConst.EmptySpreadSetting[colSetType].pos, baseSetCol, baseSpreadColSetting.pos, 'pos');
  77. return [billsSpread, posSpread];
  78. }
  79. async initProjectSpread(id) {
  80. for (const SpreadTemplate of SpreadConst.ProjectSpreadTemplate) {
  81. const data = JSON.parse(JSON.stringify(SpreadTemplate.template));
  82. [data.tz_ledger_bills_spread, data.tz_ledger_pos_spread] = this.generateRelaSpread('tz_ledger_set', data.tz_ledger_set);
  83. [data.tz_stage_bills_spread, data.tz_stage_pos_spread] = this.generateRelaSpread('tz_stage_set', data.tz_stage_set);
  84. [data.gcl_ledger_bills_spread, data.gcl_ledger_pos_spread] = this.generateRelaSpread('gcl_ledger_set', data.gcl_ledger_set);
  85. [data.gcl_stage_bills_spread, data.gcl_stage_pos_spread] = this.generateRelaSpread('gcl_stage_set', data.gcl_stage_set);
  86. const updateData = { pid: id, code: SpreadTemplate.code, name: SpreadTemplate.name, is_default: SpreadTemplate.isDefault };
  87. JsonFields.forEach(jf => { if (data[jf]) updateData[jf] = JSON.stringify(data[jf]) });
  88. await this.db.insert(this.tableName, updateData);
  89. }
  90. return await this.getProjectSpread(id);
  91. }
  92. async getProjectSpread(id) {
  93. const curSet = await this.loadProjectSpread(id);
  94. if (curSet) return curSet;
  95. return await this.initProjectSpread(id);
  96. }
  97. async getProjectSpreadByCode(id, code) {
  98. const curSet = await this.loadProjectSpreadByCode(id, code);
  99. if (curSet) return curSet;
  100. return await this.initProjectSpread(id);
  101. }
  102. async getProjectSpreadType(id) {
  103. const result = await this.getAllDataByCondition({
  104. columns: ['code', 'name', 'is_default'],
  105. where: { pid: id },
  106. });
  107. if (result.length > 0) return result;
  108. await this.initProjectSpread(id);
  109. return await this.getAllDataByCondition({
  110. columns: ['code', 'name', 'is_default'],
  111. where: { pid: id },
  112. });
  113. }
  114. async updateProjectSet(id, code, colSetType, colSet) {
  115. const updateData = {};
  116. const [billsSpread, posSpread] = this.generateRelaSpread(colSetType, colSet);
  117. updateData[colSetType.replace('_set', '_bills_spread')] = JSON.stringify(billsSpread);
  118. updateData[colSetType.replace('_set', '_pos_spread')] = JSON.stringify(posSpread);
  119. updateData[colSetType] = JSON.stringify(colSet);
  120. await this.defaultUpdate(updateData, { where: { pid: id, code } });
  121. }
  122. async resetProjectSet(id, code, colSetType, resetCode) {
  123. const SpreadTemplate = SpreadConst.ProjectSpreadTemplate.find(x => { return x.code === resetCode; });
  124. if (!SpreadTemplate) throw '选择模板不存在';
  125. const colSet = SpreadTemplate.template[colSetType];
  126. const updateData = {};
  127. const [billsSpread, posSpread] = this.generateRelaSpread(colSetType, colSet);
  128. updateData[colSetType.replace('_set', '_bills_spread')] = JSON.stringify(billsSpread);
  129. updateData[colSetType.replace('_set', '_pos_spread')] = JSON.stringify(posSpread);
  130. updateData[colSetType] = JSON.stringify(colSet);
  131. await this.defaultUpdate(updateData, { where: { pid: id, code } });
  132. }
  133. }
  134. return ProjectSpread;
  135. };