using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Dapper;
using ZKLT.Hadoop.Interface;
using ZKLT.Hadoop.Model;
using MySqlX.XDevAPI.Relational;
using Mysqlx.Crud;
using Newtonsoft.Json;
using Mysqlx.Resultset;
using Newtonsoft.Json.Linq;
using System.Transactions;
namespace ZKLT.Hadoop
{
public class TableService : ITableService
{
///
/// 合并条件
///
/// 表
/// 条件
/// 数据
/// 参数
private string MergeWhere(HDP_Table table, JContainer? where, JContainer? data, Dictionary param)
{
StringBuilder _wherestr = new StringBuilder();
string _guid = "";
if (where != null && data != null)
{
_wherestr.AppendLine("WHERE");
var _wheres = new List();
var _datas = new List();
if (where.Type == JTokenType.Object)
{
_wheres.Add((JObject)where);
_datas.Add((JObject)data);
}
else if (where.Type == JTokenType.Array)
{
for(var i = 0;i < where.Count;i++)
{
_wheres.Add((JObject)where.Children().ToArray()[i]);
_datas.Add((JObject)data.Children().ToArray()[i]);
}
}
for (var j = 0; j < _wheres.Count; j++) {
if (j > 0) {
_wherestr.AppendLine("OR");
}
_wherestr.AppendLine("(");
_wherestr.AppendLine("1 = 1");
var _where = _wheres[j];
var _data = _datas[j];
if (_where.Count > 0)
{
var _fileds = _where.Children().ToArray();
for (var i = 0; i < _fileds.Length; i++)
{
var _item = (JProperty)_fileds[i];
if (table.Columns!.Any(x => x.Key == _item.Name) && _data.ContainsKey(_item.Name))
{
if (_item.Value.Type == JTokenType.String)
{
_guid = Guid.NewGuid().ToString("N");
_wherestr.AppendLine(@$"AND `{_item.Name}` {_item.Value.ToString()} @{_guid}");
if (_data[_item.Name]!.Type == JTokenType.Object || _data[_item.Name]!.Type == JTokenType.Array)
{
param.Add(_guid, _data[_item.Name]!);
}
else if (_item.Value.ToString() == HDP_WhereType.LIKE)
{
param.Add(_guid,$@"%{((JValue)_data[_item.Name]!).Value!}%" );
}
else {
param.Add(_guid, ((JValue)_data[_item.Name]!).Value!);
}
}
else if (_item.Value.Type == JTokenType.Array)
{
string[] _itemv = _item.Value.ToObject()!;
object[] _colv = _data[_item.Name]!.ToObject