1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- namespace DataService;
- public class DBProcessData<T_Hierarchy, T_PrimaryKey, T_Value>(Func<object, T_PrimaryKey?> KeyConverter, char spilter, string keyName, int skip)
- where T_PrimaryKey : unmanaged
- where T_Hierarchy : IDictionary<string, object>, new()
- where T_Value : IDictionary<T_PrimaryKey, object>, new()
- {
- public bool ToDictionary(IEnumerable<dynamic> inputs, out T_Hierarchy? output)
- {
- output = default;
- if (inputs is null)
- return false;
- T_Hierarchy cache = [];
- foreach (dynamic input in inputs)
- {
- if (input is null)
- return false;
- if (input is not IDictionary<string, object> inputPair)
- return false;
- if (!inputPair.TryGetValue(keyName, out object? primaryKeyValue) || primaryKeyValue is null)
- return false;
- if (KeyConverter?.Invoke(primaryKeyValue) is not T_PrimaryKey primaryKey)
- return false;
- if (!inputPair.Remove(keyName))
- return false;
- foreach (KeyValuePair<string, object> rawData in input)
- ColumAnalizer(cache, rawData.Key.Split(spilter).AsSpan()[skip..], primaryKey, rawData.Value);
- }
- output = cache;
- return true;
- }
- private static void ColumAnalizer(/*ref*/ T_Hierarchy cache, Span<string> seprated, T_PrimaryKey key, object value)
- {
- if (seprated.Length == 1)
- {
- TryGetValueElseCreateNew<T_Value>(cache, seprated[0]).TryAdd(key, value);
- Console.Write($"{seprated[0]},{value}");
- Console.WriteLine();
- return;
- }
- cache = TryGetValueElseCreateNew<T_Hierarchy>(cache, seprated[0]);
- Console.Write($"{seprated[0]},");
- ColumAnalizer(cache, seprated[1..], key, value);
- }
- private static T TryGetValueElseCreateNew<T>(T_Hierarchy cache, string key) where T : new()
- {
- if (!cache.TryGetValue(key, out object? output) || output is not T dic)
- {
- dic = new();
- cache[key] = dic;
- }
- return dic;
- }
- }
- public class GeneralHeaderAnalizer
- {
- public static DateTime? LongToDateTime(object o)
- {
- if (o is not long l)
- return null;
- return new(l);
- }
- }
- public class GeneralProcessData(int skip = 0)
- : DBProcessData<Dictionary<string, object>, DateTime, Dictionary<DateTime, object>>(GeneralHeaderAnalizer.LongToDateTime, '.', "time", skip)
- {
- }
|