project_spread.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. generateRelaSpread(colSetType, colSet) {
  68. const baseSetCol = colSetType.indexOf('stage') > 0 ? SpreadConst.BaseSetCol.Stage : SpreadConst.BaseSetCol.Ledger;
  69. const baseSpreadColSetting = colSetType.indexOf('stage') > 0 ? SpreadConst.BaseSpreadColSetting.Stage : SpreadConst.BaseSpreadColSetting.Ledger;
  70. const billsSpread = this.generateSpreadSetting(colSet, SpreadConst.EmptySpreadSetting[colSetType].bills, baseSetCol, baseSpreadColSetting.bills);
  71. const posSpread = this.generateSpreadSetting(colSet, SpreadConst.EmptySpreadSetting[colSetType].pos, baseSetCol, baseSpreadColSetting.pos, 'pos');
  72. return [billsSpread, posSpread];
  73. }
  74. async initProjectSpread(id) {
  75. for (const SpreadTemplate of SpreadConst.ProjectSpreadTemplate) {
  76. const data = JSON.parse(JSON.stringify(SpreadTemplate.template));
  77. [data.tz_ledger_bills_spread, data.tz_ledger_pos_spread] = this.generateRelaSpread('tz_ledger_set', data.tz_ledger_set);
  78. [data.tz_stage_bills_spread, data.tz_stage_pos_spread] = this.generateRelaSpread('tz_stage_set', data.tz_stage_set);
  79. [data.gcl_ledger_bills_spread, data.gcl_ledger_pos_spread] = this.generateRelaSpread('gcl_ledger_set', data.gcl_ledger_set);
  80. [data.gcl_stage_bills_spread, data.gcl_stage_pos_spread] = this.generateRelaSpread('gcl_stage_set', data.gcl_stage_set);
  81. const updateData = { pid: id, code: SpreadTemplate.code, name: SpreadTemplate.name, is_default: SpreadTemplate.isDefault };
  82. JsonFields.forEach(jf => { if (data[jf]) updateData[jf] = JSON.stringify(data[jf]) });
  83. await this.db.insert(this.tableName, updateData);
  84. }
  85. }
  86. async getProjectSpread(id, code) {
  87. const curSet = await this.loadProjectSpread(id, code);
  88. if (curSet) return curSet;
  89. await this.initProjectSpread(id);
  90. return await this.loadProjectSpread(id, code);
  91. }
  92. async getProjectSpreadType(id) {
  93. const result = await this.getAllDataByCondition({
  94. columns: ['code', 'name', 'is_default'],
  95. where: { pid: id },
  96. });
  97. if (result.length > 0) return result;
  98. await this.initProjectSpread(id);
  99. return await this.getAllDataByCondition({
  100. columns: ['code', 'name', 'is_default'],
  101. where: { pid: id },
  102. });
  103. }
  104. async updateProjectSet(id, code, colSetType, colSet) {
  105. const updateData = {};
  106. const [billsSpread, posSpread] = this.generateRelaSpread(colSetType, colSet);
  107. updateData[colSetType.replace('_set', '_bills_spread')] = JSON.stringify(billsSpread);
  108. updateData[colSetType.replace('_set', '_pos_spread')] = JSON.stringify(posSpread);
  109. updateData[colSetType] = JSON.stringify(colSet);
  110. await this.defaultUpdate(updateData, { where: { pid: id, code } });
  111. }
  112. async resetProjectSet(id, code, colSetType, resetCode) {
  113. const SpreadTemplate = SpreadConst.ProjectSpreadTemplate.find(x => { return x.code === resetCode; });
  114. if (!SpreadTemplate) throw '选择模板不存在';
  115. const colSet = SpreadTemplate.template[colSetType];
  116. const updateData = {};
  117. const [billsSpread, posSpread] = this.generateRelaSpread(colSetType, colSet);
  118. updateData[colSetType.replace('_set', '_bills_spread')] = JSON.stringify(billsSpread);
  119. updateData[colSetType.replace('_set', '_pos_spread')] = JSON.stringify(posSpread);
  120. updateData[colSetType] = JSON.stringify(colSet);
  121. await this.defaultUpdate(updateData, { where: { pid: id, code } });
  122. }
  123. }
  124. return ProjectSpread;
  125. };