import {DataTable} from "./DataTable"; /** * DataSource 提供了设计器的数据来源, * 一个 DataSource 中可以包含多个 DataTable, * DataTable直接可以存在 RelationShip。 * DataTable的 name 在 DataSource 中是唯一的。 */ export interface IDataSource { name: string; displayText: string; description?: string; tables?: Array; entryTableNames?: Array; } export class DataSource { private _name: string; private _displayText: string; private _description: string; private _entryTableNames: Array; private _dataTables: Array; constructor(config: IDataSource) { const {name, displayText, description, tables, entryTableNames} = config; this.name = name; this.displayText = displayText; this.description = description || ''; this._dataTables = []; //包含的的DataTable tables && (tables instanceof Array) && tables.forEach(table => { this.addTable(table); }); //入口DataTable的名字 entryTableNames && (entryTableNames instanceof Array) && (this.entryTableNames = entryTableNames); } get name(): string { return this._name; } set name(value: string) { this._name = value; } get displayText(): string { return this._displayText; } set displayText(value: string) { this._displayText = value; } get description(): string { return this._description; } set description(value: string) { this._description = value; } get entryTableNames(): Array { return this._entryTableNames; } set entryTableNames(value: Array) { this._entryTableNames = value; } getTable(name: string): DataTable { if (!name) { return null; } return this._dataTables.find(table => table.name === name); } get dataTables() { return this._dataTables; } addTable(table: DataTable): DataSource { if (!table) { throw new Error(`parameter table is null!`); } if (!table.name) { throw new Error(`table'name is null!`); } if (this.getTable(table.name)) { throw new Error(`${table.name} DataTable exists!`); } if (table.dataSource) { throw new Error(`${table.name} owned by another dataSource!`); } table.dataSource = this; this._dataTables.push(table); return this; } removeTable(table: DataTable | string) { if (table instanceof DataTable) { let start = -1; this._dataTables.find((item, index) => { if (table === item) { start = index; return true; } }); if (start >= 0) { this._dataTables.splice(start, 1); } } else if (typeof table === 'string') { let start = -1; this._dataTables.find((item, index) => { if (item.name === table) { start = index; return true; } }); if (start >= 0) { this._dataTables.splice(start, 1); } } return this; } /** * 根据英文字段名转换成中文 * @param fieldPathStr * @returns {string} */ convertEngToCn(fieldPathStr: string) { //spec: 处理 field 里有带 "." 的携带字段, 对于表名不兼容带有 "." 的多级表名 let fieldsPathArray = []; const findTable = (tableStr: string) => { const index = tableStr.indexOf('.'); if(index > -1){ const tableName = tableStr.substring(0, index); const fieldName = tableStr.substring(index+1); const matchedTable = this.getTable(tableName); if(matchedTable) { fieldsPathArray.push(matchedTable.displayText); findField(fieldName, matchedTable); } else { fieldsPathArray.push(tableStr); } } else { fieldsPathArray.push(tableStr); } }; const findField = (pathStr: string, dataTable: DataTable) => { //如果有dataTable, 则先进行 field 的匹配 const matchedField = dataTable.fields.find(field => { return field.name === pathStr; }); if(matchedField) { fieldsPathArray.push(matchedField.displayText); } else { findTable(pathStr); } }; findTable(fieldPathStr); return fieldsPathArray.join('.'); // const fieldPath = fieldPathStr.split('.'); // // 字段 // const fieldName = fieldPath.pop(); // // 表名 // let cnFieldsPathArray = []; // fieldPath.forEach((dataTableName, index)=> { // const matchedTable = this.getTable(dataTableName); // // 字段处理 // if (index === fieldPath.length - 1 && matchedTable) { // cnFieldsPathArray.push(matchedTable.displayText); // const matchedField = matchedTable.fields.find((field)=> { // return field.name === fieldName; // }); // if (matchedField) { // cnFieldsPathArray.push(matchedField.displayText) // } else { // cnFieldsPathArray = []; // } // } else { // if (matchedTable) { // cnFieldsPathArray.push(matchedTable.displayText); // } else { // cnFieldsPathArray = []; // } // } // }); // return cnFieldsPathArray.join('.'); } /** * 根据中文字段转换成英文 * @param fieldPathStr * @returns {string} */ convertCnToEng(fieldPathStr: string): {fieldPath: string, errorMsg: string} { let errorMsg = '', enFieldsPathArray = []; const fieldPathArray: Array = fieldPathStr.split('.'); // 字段 const fieldCnName = fieldPathArray.length > 1 ? fieldPathArray[fieldPathArray.length - 1] : null; if (!fieldCnName) { return { fieldPath: '', errorMsg: `请选择字段` }; } // 表名 fieldPathArray.forEach((dataTableName, index)=> { let dataTable; // 过滤掉最后一个字段 if (index === fieldPathArray.length - 1) { const lastTableCnName = fieldPathArray[fieldPathArray.length - 2]; dataTable = this._getTableByDisplayText(lastTableCnName); if (dataTable) { const fields = dataTable.fields; const matchedField = fields.find((field)=> { return field.displayText === fieldCnName; }); if (matchedField) { enFieldsPathArray.push(matchedField.name); } else { enFieldsPathArray = []; errorMsg = `数据表"${lastTableCnName}"中不存在字段"${fieldCnName}"`; } } else { enFieldsPathArray = []; errorMsg = `数据源中不存在数据表"${lastTableCnName}"`; } } else { dataTable = this._getTableByDisplayText(dataTableName); if (dataTable) { enFieldsPathArray.push(dataTable.name); } else { enFieldsPathArray = []; errorMsg = `数据源中不存在数据表"${dataTableName}"`; } } }); return { fieldPath: enFieldsPathArray.join('.'), errorMsg: errorMsg }; } _getTableByDisplayText(tableDisplayText) { return this.dataTables.find((dataTable)=> { return dataTable.displayText === tableDisplayText; }); } }