123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2024/3/5
- * @version
- */
- const SpreadConst = require('../const/spread');
- const JsonFields = [
- 'tz_ledger_set', 'tz_ledger_bills_spread', 'tz_ledger_pos_spread',
- 'gcl_ledger_set', 'gcl_ledger_bills_spread', 'gcl_ledger_pos_spread',
- 'tz_stage_set', 'tz_stage_bills_spread', 'tz_stage_pos_spread',
- 'gcl_stage_set', 'gcl_stage_bills_spread', 'gcl_stage_pos_spread',
- ];
- module.exports = app => {
- class ProjectSpread extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'project_spread';
- }
- _analysisData(data) {
- if (!data) return;
- JsonFields.forEach(jf => { if(data[jf]) data[jf] = JSON.parse(data[jf]); });
- }
- async loadProjectSpread(id, code = '') {
- const result = code ? await this.getDataByCondition({ pid: id, code }) : await this.getDataByCondition({ pid: id, is_default: 1 });
- this._analysisData(result);
- return result;
- }
- generateSpreadSetting(colSet, emptyBase, BaseSetCol, BaseSpreadColSetting, spreadType = 'bills') {
- const spreadSetting = JSON.parse(JSON.stringify(emptyBase));
- for (const col of colSet) {
- if (!col.valid) continue;
- const dc = BaseSetCol.find(x => { return x.key === col.key; });
- if (!dc) continue;
- const orgBaseCols = BaseSpreadColSetting[col.key];
- if (!orgBaseCols) continue;
- const baseCols = JSON.parse(JSON.stringify(orgBaseCols));
- if (dc.fixed.indexOf('alias') < 0 && col.alias) {
- if (baseCols.length === 1) {
- if (baseCols[0].aliasFormat) {
- baseCols[0].title = baseCols[0].aliasFormat.replace('{%s}', col.alias);
- delete baseCols[0].aliasFormat;
- } else {
- baseCols[0].title = col.alias;
- }
- } else {
- baseCols.forEach(x => {
- if (x.aliasFormat) {
- x.title = x.aliasFormat.replace('{%s}', col.alias);
- delete x.aliasFormat;
- }
- });
- }
- }
- spreadSetting.cols.push(...baseCols);
- }
- return spreadSetting;
- }
- updateSpreadWithSpec(spreadSetting, specSetting) {
- if (!specSetting) return;
- for (const s of specSetting) {
- for (const c of spreadSetting.cols) {
- if (s.condition.value.indexOf(c[s.condition.key]) >= 0) {
- this._.assignIn(c, s.update);
- }
- }
- }
- }
- generateRelaSpread(colSetType, colSet) {
- const baseSetCol = colSetType.indexOf('stage') > 0 ? SpreadConst.BaseSetCol.Stage : SpreadConst.BaseSetCol.Ledger;
- const baseSpreadColSetting = colSetType.indexOf('stage') > 0 ? SpreadConst.BaseSpreadColSetting.Stage : SpreadConst.BaseSpreadColSetting.Ledger;
- const billsSpread = this.generateSpreadSetting(colSet, SpreadConst.EmptySpreadSetting[colSetType].bills, baseSetCol, baseSpreadColSetting.bills);
- const posSpread = this.generateSpreadSetting(colSet, SpreadConst.EmptySpreadSetting[colSetType].pos, baseSetCol, baseSpreadColSetting.pos, 'pos');
- const spreadSpec = SpreadConst.SpreadSpec[colSetType];
- if (spreadSpec) {
- this.updateSpreadWithSpec(billsSpread, spreadSpec.bills);
- this.updateSpreadWithSpec(posSpread, spreadSpec.pos);
- }
- return [billsSpread, posSpread];
- }
- async initProjectSpread(id) {
- for (const SpreadTemplate of SpreadConst.ProjectSpreadTemplate) {
- const data = JSON.parse(JSON.stringify(SpreadTemplate.template));
- [data.tz_ledger_bills_spread, data.tz_ledger_pos_spread] = this.generateRelaSpread('tz_ledger_set', data.tz_ledger_set);
- [data.tz_stage_bills_spread, data.tz_stage_pos_spread] = this.generateRelaSpread('tz_stage_set', data.tz_stage_set);
- [data.gcl_ledger_bills_spread, data.gcl_ledger_pos_spread] = this.generateRelaSpread('gcl_ledger_set', data.gcl_ledger_set);
- [data.gcl_stage_bills_spread, data.gcl_stage_pos_spread] = this.generateRelaSpread('gcl_stage_set', data.gcl_stage_set);
- const updateData = { pid: id, code: SpreadTemplate.code, name: SpreadTemplate.name, is_default: SpreadTemplate.isDefault };
- JsonFields.forEach(jf => { if (data[jf]) updateData[jf] = JSON.stringify(data[jf]) });
- await this.db.insert(this.tableName, updateData);
- }
- }
- async getProjectSpread(id, code) {
- const curSet = await this.loadProjectSpread(id, code);
- if (curSet) return curSet;
- await this.initProjectSpread(id);
- return await this.loadProjectSpread(id, code);
- }
- async getProjectSpreadType(id) {
- const result = await this.getAllDataByCondition({
- columns: ['code', 'name', 'is_default'],
- where: { pid: id },
- });
- if (result.length > 0) return result;
- await this.initProjectSpread(id);
- return await this.getAllDataByCondition({
- columns: ['code', 'name', 'is_default'],
- where: { pid: id },
- });
- }
- async updateProjectSet(id, code, colSetType, colSet) {
- const updateData = {};
- const [billsSpread, posSpread] = this.generateRelaSpread(colSetType, colSet);
- updateData[colSetType.replace('_set', '_bills_spread')] = JSON.stringify(billsSpread);
- updateData[colSetType.replace('_set', '_pos_spread')] = JSON.stringify(posSpread);
- updateData[colSetType] = JSON.stringify(colSet);
- await this.defaultUpdate(updateData, { where: { pid: id, code } });
- }
- async resetProjectSet(id, code, colSetType, resetCode) {
- const SpreadTemplate = SpreadConst.ProjectSpreadTemplate.find(x => { return x.code === resetCode; });
- if (!SpreadTemplate) throw '选择模板不存在';
- const colSet = SpreadTemplate.template[colSetType];
- const updateData = {};
- const [billsSpread, posSpread] = this.generateRelaSpread(colSetType, colSet);
- updateData[colSetType.replace('_set', '_bills_spread')] = JSON.stringify(billsSpread);
- updateData[colSetType.replace('_set', '_pos_spread')] = JSON.stringify(posSpread);
- updateData[colSetType] = JSON.stringify(colSet);
- await this.defaultUpdate(updateData, { where: { pid: id, code } });
- }
- }
- return ProjectSpread;
- };
|