自定义排序完成

pull/1/head
潘建东 8 months ago
parent b367d83eaf
commit 2733c0f7a7

@ -73,7 +73,8 @@ namespace ZKLT.Hadoop.Interface
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>结果集</returns>
public T[] Query<T>(HDP_Source source, HDP_Table table, Dictionary<string, string>? where, Dictionary<string, object>? row, Dictionary<string, string>? order, string[]? col);
public T[] Query<T>(HDP_Source source, HDP_Table table, Dictionary<string, string>? where, Dictionary<string, object>? row,
Dictionary<string, object>? order, string[]? col);
/// <summary>
/// 查询列表
@ -84,7 +85,7 @@ namespace ZKLT.Hadoop.Interface
/// <param name="where">条件</param>
/// <param name="row">数据</param>
/// <returns>结果集</returns>
public HDP_Page<T> QueryPage<T>(HDP_Source source, HDP_Table table, int pageIndex, int pageSize, Dictionary<string, string>? where, Dictionary<string, object>? row, Dictionary<string, string>? order, string[]? col);
public HDP_Page<T> QueryPage<T>(HDP_Source source, HDP_Table table, int pageIndex, int pageSize, Dictionary<string, string>? where, Dictionary<string, object>? row, Dictionary<string, object>? order, string[]? col);
/// <summary>

@ -27,7 +27,7 @@ namespace ZKLT.Hadoop.Model
private Dictionary<string, object>? _Data;
private Dictionary<string, string>? _Order;
private Dictionary<string, object>? _Order;
/// <summary>
/// 源
@ -62,7 +62,7 @@ namespace ZKLT.Hadoop.Model
/// <summary>
/// 排序
/// </summary>
public Dictionary<string, string>? Order { get => _Order; set => _Order = value; }
public Dictionary<string, object>? Order { get => _Order; set => _Order = value; }
/// <summary>
/// 命令类型

@ -11,6 +11,7 @@ using ZKLT.Hadoop.Model;
using MySqlX.XDevAPI.Relational;
using Mysqlx.Crud;
using Newtonsoft.Json;
using Mysqlx.Resultset;
namespace ZKLT.Hadoop
{
@ -43,9 +44,11 @@ namespace ZKLT.Hadoop
param.Add(_column.Key!, row![_column.Key!]);
break;
case HDP_WhereType.BETWEEN:
if (row![_column.Key!] != null) {
if (row![_column.Key!] != null)
{
var _betweendata = JsonConvert.DeserializeObject<object[]>(JsonConvert.SerializeObject(row![_column.Key!]));
if (_betweendata != null) {
if (_betweendata != null)
{
if (_betweendata[0] != null && _betweendata[0].ToString() != "")
{
_wherestr.Append($@" AND `{_column.Key!}` >= @{_column.Key!}0");
@ -97,6 +100,56 @@ namespace ZKLT.Hadoop
command.AppendLine(_colstr.ToString());
}
/// <summary>
/// 合并排序
/// </summary>
/// <param name="table">表</param>
/// <param name="order">排序</param>
/// <returns></returns>
private string MergeOrder(HDP_Table table, Dictionary<string, object>? order, Dictionary<string, object> param)
{
StringBuilder _orderstr = new StringBuilder();
if (order != null && order.Count > 0)
{
_orderstr.Append("ORDER BY ");
foreach (var key in order.Keys)
{
if (table.Columns!.Any(x => x.Key == key))
{
var _column = table.Columns!.First(x => x.Key == key);
if (order[key] is string)
{
switch (order[key])
{
case "DESC":
_orderstr.Append($@"`{_column.Key!}` DESC,");
break;
default:
_orderstr.Append($@"`{_column.Key!}` ASC,");
break;
}
}
else if (order[key] is Newtonsoft.Json.Linq.JArray)
{
var _orderTemp = JsonConvert.DeserializeObject<object[]>(JsonConvert.SerializeObject(order[key]));
_orderstr.Append(@$"CASE `{_column.Key!}`");
for (var i = 0; i < _orderTemp!.Length; i++)
{
_orderstr.Append(@$" WHEN @OR{_column.Key}{i} THEN {i}");
param.Add($@"@OR{_column.Key}{i}", _orderTemp[i]);
}
_orderstr.Append($@" ELSE {_orderTemp.Length} END,");
}
}
}
if (_orderstr.Length > 0 && _orderstr[_orderstr.Length - 1] == ',')
{
_orderstr.Remove(_orderstr.Length - 1, 1);
}
}
return _orderstr.ToString();
}
/// <summary>
/// 同步结构
/// </summary>
@ -621,7 +674,7 @@ namespace ZKLT.Hadoop
/// <param name="row">数据</param>
/// <returns>结果集</returns>
public T[] Query<T>(HDP_Source source, HDP_Table table, Dictionary<string, string>? where, Dictionary<string, object>? row,
Dictionary<string, string>? order, string[]? col)
Dictionary<string, object>? order, string[]? col)
{
//数据校验
if (string.IsNullOrEmpty(table.Key))
@ -661,32 +714,8 @@ namespace ZKLT.Hadoop
MergeWhere(table, where, row, _command, _params);
//执行排序
StringBuilder _orderstr = new StringBuilder();
_orderstr.Append("ORDER BY ");
if (order != null && order.Count > 0)
{
for (var i = 0; i < table.Columns.Length; i++)
{
var _column = table.Columns[i];
if (order.ContainsKey(_column.Key!))
{
switch (order[_column.Key!])
{
case "DESC":
_orderstr.Append($@"`{_column.Key!}` DESC,");
break;
default:
_orderstr.Append($@"`{_column.Key!}` ASC,");
break;
}
}
}
if (_orderstr[_orderstr.Length - 1] == ',')
{
_orderstr.Remove(_orderstr.Length - 1, 1);
}
_command.AppendLine(_orderstr.ToString());
}
_command.AppendLine(MergeOrder(table, order, _params));
var _result = _connection.Query<T>(_command.ToString(), _params);
_connection.Close();
return _result.ToArray();
@ -703,7 +732,7 @@ namespace ZKLT.Hadoop
/// <param name="row">数据</param>
/// <returns>结果集</returns>
public HDP_Page<T> QueryPage<T>(HDP_Source source, HDP_Table table, int pageIndex, int pageSize, Dictionary<string, string>? where,
Dictionary<string, object>? row, Dictionary<string, string>? order, string[]? col)
Dictionary<string, object>? row, Dictionary<string, object>? order, string[]? col)
{
//数据校验
if (string.IsNullOrEmpty(table.Key))

Loading…
Cancel
Save