'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 typeValid = spreadType + '_valid'; if (col[typeValid] !== undefined && !col[typeValid]) continue; const dc = BaseSetCol.find(x => { return x.key === col.key; }); if (!dc || !dc[spreadType]) 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 = SpreadConst.BaseSetCol[colSetType]; const baseSpreadColSetting = SpreadConst.BaseSpreadColSetting[colSetType]; 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 initProjectSpreadByTemplate(id, spreadTemplate) { 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 ? 1 : 0 }; JsonFields.forEach(jf => { if (data[jf]) updateData[jf] = JSON.stringify(data[jf]) }); await this.db.insert(this.tableName, updateData); } async initProjectSpread(id) { for (const template of SpreadConst.ProjectSpreadTemplate) { await this.initProjectSpreadByTemplate(id, template); } } 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 === SpreadConst.ProjectSpreadTemplate.length) return result; if (result.length === 0) { await this.initProjectSpread(id); } else { for (const spreadTemplate of SpreadConst.ProjectSpreadTemplate) { const SpreadType = result.find(x => { return x.code === spreadTemplate.code; }); if (!SpreadType) await this.initProjectSpreadByTemplate(id, spreadTemplate); } } 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; };