using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
namespace WIDESEA_Core
{
public class PageDataOptions
{
public int Page { get; set; }
public int Rows { get; set; }
public int Total { get; set; }
public string TableName { get; set; }
public string Sort { get; set; }
///
/// 排序方式
///
public string Order { get; set; }
public string Wheres { get; set; }
public bool Export { get; set; }
public object Value { get; set; }
///
/// 查询条件
///
public List Filter { get; set; }
public string ValidatePageOptions(PropertyInfo[] entityProperties)
{
string where = string.Empty;
List searchParametersList = new List();
if (this.Filter != null && this.Filter.Count > 0)
{
searchParametersList.AddRange(Filter);
}
else if (!string.IsNullOrEmpty(Wheres))
{
try
{
searchParametersList = Wheres.DeserializeObject>();
Filter = searchParametersList;
}
catch { }
}
for (int i = 0; i < searchParametersList.Count; i++)
{
if (string.IsNullOrEmpty(searchParametersList[i].Value))
{
continue;
}
PropertyInfo? property = entityProperties.Where(c => c.Name.ToUpper() == searchParametersList[i].Name.ToUpper()).FirstOrDefault();
if (property == null) continue;
List<(bool, string, object)> results = property.ValidationValueForDbType(searchParametersList[i].Value.Split(',')).ToList();
if (results == null || results.Count() == 0)
{
continue;
}
for (int j = 0; j < results.Count(); j++)
{
if (j == 0)
{
where += "(";
}
if (searchParametersList[i].Name == "column")
{
searchParametersList[i].Name = '"' + searchParametersList[i].Name + '"';
}
LinqExpressionType expressionType = searchParametersList[i].DisplayType.GetLinqCondition();
if (expressionType == LinqExpressionType.Equal)
{
where += $"{searchParametersList[i].Name} = '{results[j].Item3}'";
}
else if (expressionType == LinqExpressionType.Contains)
{
where += $"{searchParametersList[i].Name} {searchParametersList[i].DisplayType} '%{results[j].Item3}%'";
}
else
{
where += $"{searchParametersList[i].Name} {searchParametersList[i].DisplayType} '{results[j].Item3}'";
}
if (j == results.Count() - 1)
{
where += ")";
}
else
{
where += " or ";
}
}
if (i < searchParametersList.Count - 1)
where += " and ";
}
return where;
}
public Dictionary GetPageDataSort(PropertyInfo[] propertyInfo)
{
if (!string.IsNullOrEmpty(Sort))
{
if (Sort.Contains(","))
{
List sortArr = Sort.Split(",").Where(x => propertyInfo.Any(p => p.Name == x)).ToList();
Dictionary sortDic = new Dictionary();
foreach (var item in sortArr)
{
sortDic[item] = Order?.ToLower() == OrderByType.Asc.ToString().ToLower() ? OrderByType.Asc : OrderByType.Desc;
}
return sortDic;
}
else if (propertyInfo.Any(x => x.Name == Sort.FirstLetterToLower() || x.Name == Sort.FirstLetterToUpper()))
{
return new Dictionary {
{
Sort,Order?.ToLower() == OrderByType.Asc.ToString().ToLower() ? OrderByType.Asc : OrderByType.Desc
} };
}
}
return new Dictionary { { "CreateDate", Order?.ToLower() == OrderByType.Asc.ToString().ToLower() ? OrderByType.Asc : OrderByType.Desc } };
}
}
public class SearchParameters
{
public string Name { get; set; }
public string Value { get; set; }
//查询类型:LinqExpressionType
public string DisplayType { get; set; }
}
}