using System; using Newtonsoft.Json; using System.Collections.Generic; namespace Typhoon.Excel2Json.Export { /// /// 所有表格 /// [Serializable] public class AllTables { $CODE //忽略 [JsonIgnore] public Dictionary Database = new Dictionary(); #region 静态方法 //实例 private static AllTables _instance = null; /// /// json构建AllTables /// public static AllTables Build(string json) { var all = JsonConvert.DeserializeObject(json); all.Initialize(); return all; } /// /// 获取link项 /// public static ITableValue GetLink(string table, string column, string key) { if (_instance == null) { throw new Exception("未构建AllTable实例"); } return _instance.GetLinkTableValue(table, column, key); } /// /// 获取link项 /// public static ITableValue GetLink(string table, string column, int key) { if (_instance == null) { throw new Exception("未构建AllTable实例"); } return _instance.GetLinkTableValue(table, column, key); } /// /// 获取link项 /// public static ITableValue[] GetLink(string table, string column, string[] keys) { if (_instance == null) { throw new Exception("未构建AllTable实例"); } return _instance.GetLinkTableValue(table, column, keys); } /// /// 获取link项 /// public static ITableValue[] GetLink(string table, string column, int[] keys) { if (_instance == null) { throw new Exception("未构建AllTable实例"); } return _instance.GetLinkTableValue(table, column, keys); } /// /// 获取link项 /// public static T[] GetLink(string table, string column, int[] keys) where T : class, ITableValue { if (_instance == null) { throw new Exception("未构建AllTable实例"); } return _instance.GetLinkTableValue(table, column, keys); } /// /// 获取link项 /// public static T[] GetLink(string table, string column, string[] keys) where T : class, ITableValue { if (_instance == null) { throw new Exception("未构建AllTable实例"); } return _instance.GetLinkTableValue(table, column, keys); } #endregion /// /// 初始化 /// public void Initialize() { _instance = this; $INITIALIZE_METHOD } #region 泛用方法 /// /// 获取表 /// /// /// public ITable GetTable(string table) { return Database[table]; } /// /// 是否有表格 /// /// /// public bool HasTable(string table) { return Database.ContainsKey(table); } /// /// 是否有表格条目 /// /// 表名 /// 列名 /// key /// public bool HasTableValue(string table, string column, string key) { if (!HasTable(table)) { return false; } return GetTable(table).Has(column, key); } /// /// 获取表条目,只有#key标签才支持查询 /// /// 表名 /// 列名 /// 匹配名 /// public ITableValue GetTableValue(string table, string column, string key) { return Database[table].Get(column, key); } /// /// 获取表条目,只有#key标签才支持查询 /// /// 表名 /// 列名 /// 匹配名 /// public ITableValue GetTableValue(string table, string column, int key) { return Database[table].Get(column, key); } /// /// 获取link表条目 /// public ITableValue[] GetLinkTableValue(string table, string column, string[] keys) { var result = new ITableValue[keys.Length]; for (int i = 0; i < keys.Length; i++) { var key = keys[i]; if (string.IsNullOrWhiteSpace(key)) { result[i] = null; continue; } result[i] = Database[table].Get(column, key); } return result; } /// /// 获取link表条目 /// public ITableValue[] GetLinkTableValue(string table, string column, int[] keys) { var result = new ITableValue[keys.Length]; for (int i = 0; i < keys.Length; i++) { var key = keys[i]; result[i] = Database[table].Get(column, key); } return result; } /// /// 获取link表条目 /// public T[] GetLinkTableValue(string table, string column, string[] keys) where T : class, ITableValue { var result = new T[keys.Length]; for (int i = 0; i < keys.Length; i++) { var key = keys[i]; if (string.IsNullOrWhiteSpace(key)) { result[i] = null; continue; } result[i] = Database[table].Get(column, key) as T; } return result; } /// /// 获取link表条目 /// public T[] GetLinkTableValue(string table, string column, int[] keys) where T : class, ITableValue { var result = new T[keys.Length]; for (int i = 0; i < keys.Length; i++) { var key = keys[i]; result[i] = Database[table].Get(column, key) as T; } return result; } /// /// 获取link表条目 /// public ITableValue GetLinkTableValue(string table, string column, string key) { return string.IsNullOrWhiteSpace(key) ? null : Database[table].Get(column, key); } /// /// 获取link表条目 /// public ITableValue GetLinkTableValue(string table, string column, int key) { return Database[table].Get(column, key); } #endregion #region 快捷查询API $SEARCH_METHOD #endregion } }