using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using MySqlX.XDevAPI.Relational; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Transactions; using ZKLT.Hadoop.Interface; using ZKLT.Hadoop.Model; namespace ZKLT.Hadoop { /// /// 云计算扩展 /// public static class HadoopServiceExtend { /// /// 注入云计算服务 /// /// 服务集合 /// public static IServiceCollection AddHadoop(this IServiceCollection services) { services.AddSingleton(); services.AddSingleton(); return services; } /// /// 应用云计算 /// /// 应用 /// 源配置 /// public static IApplicationBuilder UseHadoop(this IApplicationBuilder app, Action config) { var _HadoopService = app.ApplicationServices.GetRequiredService(); _HadoopService.Init(config); return app; } } /// /// 云计算服务 /// public class HadoopService : IHadoopService { public HadoopService(ITableService tableService) { _TableService = tableService; _Source = new HDP_Source(); _Tables = new List(); } private ITableService _TableService; private HDP_Source _Source; private List _Tables; /// /// 初始化云计算 /// /// 配置 public void Init(Action config) { if (config == null) { throw new ArgumentNullException("配置无效"); } config(_Source); //参数校验 if (string.IsNullOrEmpty(_Source.Host)) { throw new ArgumentException("主机无效"); } if (string.IsNullOrEmpty(_Source.Key)) { throw new ArgumentException("源无效"); } if (string.IsNullOrEmpty(_Source.Account)) { throw new ArgumentException("用户名无效"); } if (string.IsNullOrEmpty(_Source.PassWord)) { throw new ArgumentException("密码无效"); } //初始化 if (_Source.Port == null) { _Source.Port = 3306; } if (string.IsNullOrEmpty(_Source.Id)) { _Source.Id = ""; } if (string.IsNullOrEmpty(_Source.Description)) { _Source.Description = "云计算系统"; } var _source = HDP_Table.Class2Table(); _Tables.Add(_source); if (!_TableService.InitStruct(_Source, _source)) { throw new Exception("初始化数据源失败"); } var _table = HDP_Table.Class2Table(); _Tables.Add(_table); if (!_TableService.InitStruct(_Source, _table)) { throw new Exception("初始化数据表失败"); } var _column = HDP_Table.Class2Table(); _Tables.Add(_column); if (!_TableService.InitStruct(_Source, _column)) { throw new Exception("初始化数据列失败"); } } /// /// 获取源 /// /// 数据源编号 /// 结果 public HDP_Source? GetSource(string sourceid) { if (string.IsNullOrEmpty(sourceid) || _Source.Id == sourceid) { return _Source; } var _result = _TableService.QuerySingle(_Source, GetTable("HDP_Source")!, new JObject { { "Id","=" } }, new JObject { { "Id",sourceid} }, null); return _result; } /// /// 创建源 /// /// 源 /// 是否成功 public bool InsertSource(HDP_Source source) { //校验 if (string.IsNullOrEmpty(source.Id)) { throw new ArgumentNullException("编号无效"); } if (string.IsNullOrEmpty(source.Host)) { throw new ArgumentNullException("主机无效"); } if (source.Port == null) { throw new ArgumentNullException("端口无效"); } if (string.IsNullOrEmpty(source.Key)) { throw new ArgumentNullException("数据源键无效"); } if (string.IsNullOrEmpty(source.Account)) { throw new ArgumentNullException("用户名无效"); } if (string.IsNullOrEmpty(source.PassWord)) { throw new ArgumentNullException("密码无效"); } if (GetSource(source.Id) != null) { throw new ArgumentException("编号已存在"); } return _TableService.Insert(_Source, GetTable("HDP_Source")!, HDP_Table.Class2JObject(source)); } /// /// 更新源 /// /// 源 /// 是否成功 public bool UpdateSource(HDP_Source source) { //校验 if (string.IsNullOrEmpty(source.Id)) { throw new ArgumentNullException("编号无效"); } if (string.IsNullOrEmpty(source.Host)) { throw new ArgumentNullException("主机无效"); } if (source.Port == null) { throw new ArgumentNullException("端口无效"); } if (string.IsNullOrEmpty(source.Key)) { throw new ArgumentNullException("数据源键无效"); } if (string.IsNullOrEmpty(source.Account)) { throw new ArgumentNullException("用户名无效"); } if (string.IsNullOrEmpty(source.PassWord)) { throw new ArgumentNullException("密码无效"); } if (GetSource(source.Id) == null) { throw new ArgumentException("编号不存在"); } return _TableService.Update(_Source, GetTable("HDP_Source")!, new JObject { {"Id","=" } }, HDP_Table.Class2JObject(source)); } /// /// 删除源 /// /// 源 /// 是否成功 public bool DeleteSource(string sourceid) { //校验 if (string.IsNullOrEmpty(sourceid)) { throw new ArgumentNullException("编号无效"); } if (GetSource(sourceid) == null) { throw new ArgumentException("编号不存在"); } return _TableService.Delete(_Source, GetTable("HDP_Source")!, new JObject{ {"Id","=" } }, new JObject{ {"Id",sourceid } }); } /// /// 查询源 /// /// 命令 /// 结果 public HDP_Source[] QuerySource(HDP_Command command) { return _TableService.Query(_Source, GetTable("HDP_Source")!, command.Where!, command.Data!, command.Order!, command.Col); } /// /// 获取表 /// /// 表编号 /// 结果 public HDP_Table? GetTable(string tableid) { if (string.IsNullOrEmpty(tableid)) { throw new ArgumentNullException("数据表编号无效"); } if (_Tables.Any(x => x.Id == tableid)) { return _Tables.First(x => x.Id == tableid); } var _result = _TableService.QuerySingle(_Source, GetTable("HDP_Table")!, new JObject { { "Id","=" } }, new JObject{ { "Id",tableid} }, null); if (_result != null) { _result.Columns = _TableService.Query(_Source, GetTable("HDP_Column")!, new JObject { { "TableId","="} }, new JObject{ {"TableId",_result.Id! } }, null, null); } return _result; } /// /// 创建表 /// /// 表 /// 是否成功 public bool InsertTable(HDP_Table table) { if (string.IsNullOrEmpty(table.Id)) { throw new ArgumentNullException("表编号无效"); } if (GetTable(table.Id) != null) { throw new ArgumentNullException("表编号已存在"); } if (_TableService.InitStruct(_Source, table)) { using (TransactionScope _scope = new TransactionScope()) { if (!_TableService.Insert(_Source, GetTable("HDP_Table")!, HDP_Table.Class2JObject(table))) { return false; } for (var i = 0; i < table.Columns!.Length; i++) { var _column = table.Columns![i]; _column.TableId = table.Id; if (!_TableService.Insert(_Source, GetTable("HDP_Column")!, HDP_Table.Class2JObject(_column))) { return false; } } _scope.Complete(); return true; } } else { return false; } } /// /// 更新表 /// /// 表 /// 是否成功 public bool UpdateTable(HDP_Table table) { if (string.IsNullOrEmpty(table.Id)) { throw new ArgumentNullException("表编号无效"); } if (GetTable(table.Id) == null) { throw new ArgumentNullException("表编号不存在"); } if (_TableService.InitStruct(_Source, table)) { using (TransactionScope _scope = new TransactionScope()) { if (!_TableService.Update(_Source, GetTable("HDP_Table")!, new JObject{ { "Id","="} }, HDP_Table.Class2JObject(table))) { return false; } for (var i = 0; i < table.Columns!.Length; i++) { var _column = table.Columns![i]; _column.TableId = table.Id; if (_TableService.QuerySingle(_Source, GetTable("HDP_Column")!, new JObject { {"Id","=" } }, HDP_Table.Class2JObject(_column), null) == null) { if (!_TableService.Insert(_Source, GetTable("HDP_Column")!, HDP_Table.Class2JObject(_column))) { return false; } } else { if (!_TableService.Update(_Source, GetTable("HDP_Column")!, new JObject { {"Id","=" } }, HDP_Table.Class2JObject(_column))) { return false; } } } _scope.Complete(); return true; } } else { return false; } } /// /// 删除表 /// /// 表编号 /// 是否成功 public bool DeleteTable(string tableId) { //校验 if (string.IsNullOrEmpty(tableId)) { throw new ArgumentNullException("编号无效"); } if (GetTable(tableId) == null) { throw new ArgumentException("编号不存在"); } using (TransactionScope _scope = new TransactionScope()) { if (!_TableService.Delete(_Source, GetTable("HDP_Table")!, new JObject{ {"Id","=" } }, new JObject { {"Id",tableId } })) { return false; } if (!_TableService.Delete(_Source, GetTable("HDP_Column")!, new JObject{ { "TableId","="} }, new JObject { {"TableId",tableId } })) { return false; } _scope.Complete(); return true; } } /// /// 查询表 /// /// 命令 /// 结果 public HDP_Table[] QueryTable(HDP_Command command) { return _TableService.Query(_Source, GetTable("HDP_Table")!, command.Where!, command.Data!, command.Order!, command.Col!); } /// /// 插入数据 /// /// 命令 /// 是否成功 public bool Insert(HDP_Command command) { if (string.IsNullOrEmpty(command.TableId)) { throw new ArgumentNullException("表无效"); } var _table = GetTable(command.TableId); if (_table == null) { throw new ArgumentException("表不存在"); } var _source = GetSource(_table.SourceId!); return _TableService.Insert(_source!, _table, command.Data!); } /// /// 更新数据 /// /// /// 是否成功 public bool Update(HDP_Command command) { if (string.IsNullOrEmpty(command.TableId)) { throw new ArgumentNullException("表无效"); } var _table = GetTable(command.TableId); if (_table == null) { throw new ArgumentException("表不存在"); } var _source = GetSource(_table.SourceId!); return _TableService.Update(_source!, _table, command.Where!, command.Data!); } /// /// 删除数据 /// /// 命令 /// 是否成功 public bool Delete(HDP_Command command) { if (string.IsNullOrEmpty(command.TableId)) { throw new ArgumentNullException("表无效"); } var _table = GetTable(command.TableId); if (_table == null) { throw new ArgumentException("表不存在"); } var _source = GetSource(_table.SourceId!); return _TableService.Delete(_source!, _table, command.Where!, command.Data!); } /// /// 查询单条 /// /// 返回类型 /// 命令 /// 结果 public T? QuerySingle(HDP_Command command) { if (string.IsNullOrEmpty(command.TableId)) { throw new ArgumentNullException("表无效"); } var _table = GetTable(command.TableId); if (_table == null) { throw new ArgumentException("表不存在"); } var _source = GetSource(_table.SourceId!); return _TableService.QuerySingle(_source!, _table, command.Where!, command.Data!, command.Col); } /// /// 查询列表 /// /// 返回类型 /// 命令 /// 结果 public T[] Query(HDP_Command command) { if (string.IsNullOrEmpty(command.TableId)) { throw new ArgumentNullException("表无效"); } var _table = GetTable(command.TableId); if (_table == null) { throw new ArgumentException("表不存在"); } var _source = GetSource(_table.SourceId!); return _TableService.Query(_source!, _table, command.Where!, command.Data!, command.Order!, command.Col); } /// /// 分页查询 /// /// 返回类型 /// 命令 /// 结果 public HDP_Page Page(HDP_Command command) { if (string.IsNullOrEmpty(command.TableId)) { throw new ArgumentNullException("表无效"); } if (command.PageIndex == null || command.PageIndex <= 0) { throw new ArgumentNullException("分页下标无效"); } if (command.PageSize == null || command.PageSize <= 0) { throw new ArgumentNullException("分页大小无效"); } var _table = GetTable(command.TableId); if (_table == null) { throw new ArgumentException("表不存在"); } var _source = GetSource(_table.SourceId!); return _TableService.QueryPage(_source!, _table, (int)command.PageIndex, (int)command.PageSize, command.Where!, command.Data!, command.Order!, command.Col!); } /// /// 批量执行任务 /// /// 指令 /// public object?[] PatchCommand(HDP_Command[] command) { object?[] _result = new object?[command.Length]; using (TransactionScope _scope = new TransactionScope()) { var _isComplete = true; for (var i = 0; i < command.Length; i++) { try { if (command[i].Type == HDP_CommandType.INSERT) { var _temp = Insert(command[i]); if (!_temp) { _isComplete = false; } _result[i] = _temp; } else if (command[i].Type == HDP_CommandType.UPDATE) { var _temp = Update(command[i]); if (!_temp) { _isComplete = false; } _result[i] = _temp; } else if (command[i].Type == HDP_CommandType.DELETE) { var _temp = Delete(command[i]); if (!_temp) { _isComplete = false; } _result[i] = _temp; } else if (command[i].Type == HDP_CommandType.QUERYSINGLE || command[i].Type == HDP_CommandType.QUERY || command[i].Type == HDP_CommandType.PAGE) { continue; } else { _result[i] = "不支持该命令"; _isComplete = false; } } catch (Exception ex) { _result[i] = ex.Message; _isComplete = false; } } if (_isComplete) { _scope.Complete(); } } for (var i = 0; i < command.Length; i++) { try { if (command[i].Type == HDP_CommandType.INSERT || command[i].Type == HDP_CommandType.UPDATE || command[i].Type == HDP_CommandType.DELETE) { continue; } else if (command[i].Type == HDP_CommandType.QUERYSINGLE) { var _temp = QuerySingle(command[i]); _result[i] = _temp; } else if (command[i].Type == HDP_CommandType.QUERY) { var _temp = Query(command[i]); _result[i] = _temp; } else if (command[i].Type == HDP_CommandType.PAGE) { var _temp = Page(command[i]); _result[i] = _temp; } else { _result[i] = "不支持该命令"; } } catch (Exception ex) { _result[i] = ex.Message; } } return _result.ToArray(); } } }