using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using MySqlX.XDevAPI.Relational;
using Newtonsoft.Json.Linq;
using Quartz.Impl;
using Quartz;
using Quartz.Spi;
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;
using ZKLT.Quartz;
using ZKLT.Quartz.Interface;
namespace ZKLT.Hadoop
{
///
/// 云计算扩展
///
public static class HadoopServiceExtend
{
///
/// 注入云计算服务
///
/// 服务集合
///
public static IServiceCollection AddHadoop(this IServiceCollection services)
{
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
return services;
}
///
/// 应用云计算
///
/// 应用
/// 源配置
///
public static IApplicationBuilder UseHadoop(this IApplicationBuilder app, Action config)
{
var _TableService = app.ApplicationServices.GetRequiredService();
_TableService.Init(config);
return app;
}
}
///
/// 云计算服务
///
public class HadoopService : IHadoopService
{
public HadoopService(ITableService tableService)
{
_TableService = tableService;
}
private ITableService _TableService;
///
/// 创建源
///
/// 源
/// 是否成功
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 (_TableService.GetSource(source.Id) != null)
{
throw new ArgumentException("编号已存在");
}
return _TableService.Insert("", "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("密码无效");
}
return _TableService.Update(source.Id, "HDP_Source", new JObject {
{"Id","=" }
}, HDP_Table.Class2JObject(source));
}
///
/// 删除源
///
/// 源
/// 是否成功
public bool DeleteSource(string sourceid)
{
return _TableService.Delete(sourceid, "HDP_Source", new JObject{
{"Id","=" }
}, new JObject{
{"Id",sourceid }
});
}
///
/// 查询源
///
/// 命令
/// 结果
public HDP_Source[] QuerySource(HDP_Command command)
{
return _TableService.Query(command.SourceId!, command.TableId!, command.Where!, command.Data!, command.Order!, command.Col);
}
///
/// 创建表
///
/// 表
/// 是否成功
public bool InsertTable(HDP_Table table)
{
try
{
if (_TableService.GetTable(table.Id) != null)
{
throw new ArgumentNullException("表编号已存在");
}
}
catch (ArgumentNullException e)
{
if (!e.Message.Contains("数据表不存在"))
{
throw;
}
}
if (_TableService.InitStruct("", table))
{
using (TransactionScope _scope = new TransactionScope())
{
if (!_TableService.Insert("", "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("", "HDP_Column", HDP_Table.Class2JObject(_column)))
{
return false;
}
}
_scope.Complete();
return true;
}
}
else
{
return false;
}
}
///
/// 更新表
///
/// 表
/// 是否成功
public bool UpdateTable(HDP_Table table)
{
if (_TableService.InitStruct("", table))
{
using (TransactionScope _scope = new TransactionScope())
{
if (!_TableService.Update("", "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("", "HDP_Column", new JObject
{
{"Id","=" }
}, HDP_Table.Class2JObject(_column), null) == null)
{
if (!_TableService.Insert("", "HDP_Column", HDP_Table.Class2JObject(_column)))
{
return false;
}
}
else
{
if (!_TableService.Update("", "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("编号无效");
}
using (TransactionScope _scope = new TransactionScope())
{
if (!_TableService.Delete("", "HDP_Table", new JObject{
{"Id","=" }
}, new JObject {
{"Id",tableId }
}))
{
return false;
}
if (!_TableService.Delete("", "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("", "HDP_Table", command.Where!, command.Data!, command.Order!, command.Col!);
}
///
/// 插入数据
///
/// 命令
/// 是否成功
public bool Insert(HDP_Command command)
{
return _TableService.Insert(command.SourceId!, command.TableId!, command.Data!);
}
///
/// 更新数据
///
///
/// 是否成功
public bool Update(HDP_Command command)
{
return _TableService.Update(command.SourceId!, command.TableId!, command.Where!, command.Data!);
}
///
/// 删除数据
///
/// 命令
/// 是否成功
public bool Delete(HDP_Command command)
{
return _TableService.Delete(command.SourceId!, command.TableId!, command.Where!, command.Data!);
}
///
/// 查询单条
///
/// 返回类型
/// 命令
/// 结果
public T? QuerySingle(HDP_Command command)
{
return _TableService.QuerySingle(command.SourceId!, command.TableId!, command.Where!, command.Data!, command.Col);
}
///
/// 查询列表
///
/// 返回类型
/// 命令
/// 结果
public T[] Query(HDP_Command command)
{
return _TableService.Query(command.SourceId!, command.TableId!, command.Where!, command.Data!, command.Order!, command.Col);
}
///
/// 分页查询
///
/// 返回类型
/// 命令
/// 结果
public HDP_Page Page(HDP_Command command)
{
if (command.PageIndex == null || command.PageIndex <= 0)
{
throw new ArgumentNullException("分页下标无效");
}
if (command.PageSize == null || command.PageSize <= 0)
{
throw new ArgumentNullException("分页大小无效");
}
return _TableService.QueryPage(command.SourceId!, command.TableId!, (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();
}
public T? StoredProcedure(HDP_Command command)
{
return _TableService.StoredProcedure(command.SourceId!, command.StoredProcedureName!, command.Data!);
}
}
}