From 17e4c7e3e7b3ef60d9da6de3b2a39a14a53c38a0 Mon Sep 17 00:00:00 2001 From: z8018 <1282578289@qq.com> Date: 星期三, 12 三月 2025 14:11:33 +0800 Subject: [PATCH] 1 --- WIDESEAWCS_Server/WIDESEAWCS_Core/BaseServices/ServiceBase.cs | 145 ++++++++++++++++++++++++++++++------------------ 1 files changed, 90 insertions(+), 55 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseServices/ServiceBase.cs b/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseServices/ServiceBase.cs index 9518cfe..7872822 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseServices/ServiceBase.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_Core/BaseServices/ServiceBase.cs @@ -12,10 +12,10 @@ using System.Linq.Expressions; using System.Reflection; using WIDESEAWCS_Core.BaseRepository; +using WIDESEAWCS_Core.Const; using WIDESEAWCS_Core.Enums; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_Core.Utilities; -using static OfficeOpenXml.ExcelErrorValue; namespace WIDESEAWCS_Core.BaseServices { @@ -49,18 +49,23 @@ public virtual PageGridData<TEntity> GetPageData(PageDataOptions options) { - string wheres = ValidatePageOptions(options); + ISugarQueryable<TEntity> sugarQueryable = Db.Queryable<TEntity>(); + + string wheres = ValidatePageOptions(options, ref sugarQueryable); //鑾峰彇鎺掑簭瀛楁 Dictionary<string, OrderByType> orderbyDic = GetPageDataSort(options, TProperties); - + int total = 0; PageGridData<TEntity> pageGridData = new PageGridData<TEntity>(); - pageGridData = BaseDal.QueryPage(wheres, options.Page, options.Rows, orderbyDic); + + pageGridData.Rows = sugarQueryable.ToPageList(options.Page, options.Rows, ref total); + pageGridData.Total = total; + + //pageGridData = BaseDal.QueryPage(wheres, options.Page, options.Rows, orderbyDic); return pageGridData; } - - protected string ValidatePageOptions(PageDataOptions options) + protected string ValidatePageOptions(PageDataOptions options, ref ISugarQueryable<TEntity> sugarQueryable) { options = options ?? new PageDataOptions(); string where = ""; @@ -87,7 +92,7 @@ continue; } - PropertyInfo property = TProperties.Where(c => c.Name.ToUpper() == searchParametersList[i].Name.ToUpper()).FirstOrDefault(); + PropertyInfo? property = TProperties.Where(c => c.Name.ToUpper() == searchParametersList[i].Name.ToUpper()).FirstOrDefault(); if (property == null) continue; @@ -98,33 +103,79 @@ } for (int j = 0; j < results.Count(); j++) { - if (j == 0) - { - where += "("; - } LinqExpressionType expressionType = searchParametersList[i].DisplayType.GetLinqCondition(); - if (expressionType == LinqExpressionType.Equal) - { - where += $"{searchParametersList[i].Name} = '{results[j].Item3}'"; - } - else - { - where += $"{searchParametersList[i].Name} {searchParametersList[i].DisplayType} '{results[j].Item3}'"; - } - - if (j == results.Count() - 1) - { - where += ")"; - } - else - { - where += " or "; - } + Expression<Func<TEntity, bool>> expression = GetWhereExpression(property.Name, results[j].Item3, null, expressionType); + sugarQueryable = sugarQueryable.Where(expression); } - if (i < searchParametersList.Count - 1) - where += " and "; } return where; + } + + private Expression<Func<TEntity, bool>> GetWhereExpression(string propertyName, object propertyValue, ParameterExpression parameter, LinqExpressionType expressionType) + { + Type? proType = typeof(TEntity).GetProperty(propertyName)?.PropertyType; + if (proType == null) return p => true; + if (propertyValue == null) return p => true; + string? value = propertyValue?.ToString(); + if (value == null) return p => true; + ConstantExpression constant = proType.ToString() == "System.String" + ? Expression.Constant(propertyValue) : Expression.Constant(value.ChangeType(proType)); + + // DateTime鍙�夋嫨浜嗘棩鏈熺殑鏃跺�欒嚜鍔ㄥ湪缁撴潫鏃ユ湡鍔犱竴澶╋紝淇DateTime绫诲瀷浣跨敤鏃ユ湡鍖洪棿鏌ヨ鏃犳硶鏌ヨ鍒扮粨鏉熸棩鏈熺殑闂 + if ((proType == typeof(DateTime) || proType == typeof(DateTime?)) && expressionType == LinqExpressionType.LessThanOrEqual && value.Length == 10) + { + constant = Expression.Constant(Convert.ToDateTime(value).AddDays(1)); + } + parameter = parameter ?? Expression.Parameter(typeof(TEntity), "x"); + //鍒涘缓鑺傜偣鐨勫睘鎬=>p.name 灞炴�ame + MemberExpression memberProperty = Expression.PropertyOrField(parameter, propertyName); + UnaryExpression member = Expression.Convert(memberProperty, constant.Type); + Expression<Func<TEntity, bool>> expression = p => false; + switch (expressionType) + { + case LinqExpressionType.Equal: + expression = Expression.Lambda<Func<TEntity, bool>>(Expression.Equal(member, constant), parameter); + break; + case LinqExpressionType.NotEqual: + expression = Expression.Lambda<Func<TEntity, bool>>(Expression.NotEqual(member, constant), parameter); + break; + case LinqExpressionType.GreaterThan: + expression = Expression.Lambda<Func<TEntity, bool>>(Expression.GreaterThan(member, constant), parameter); + break; + case LinqExpressionType.LessThan: + expression = Expression.Lambda<Func<TEntity, bool>>(Expression.LessThan(member, constant), parameter); + break; + case LinqExpressionType.ThanOrEqual: + expression = Expression.Lambda<Func<TEntity, bool>>(Expression.GreaterThanOrEqual(member, constant), parameter); + break; + case LinqExpressionType.LessThanOrEqual: + expression = Expression.Lambda<Func<TEntity, bool>>(Expression.LessThanOrEqual(member, constant), parameter); + break; + case LinqExpressionType.Contains: + case LinqExpressionType.NotContains: + MethodInfo? method = typeof(string).GetMethod("Contains", new[] { typeof(string) }); + if (method != null) + { + constant = Expression.Constant(propertyValue, typeof(string)); + if (expressionType == LinqExpressionType.Contains) + { + expression = Expression.Lambda<Func<TEntity, bool>>(Expression.Call(member, method, constant), parameter); + } + else + { + expression = Expression.Lambda<Func<TEntity, bool>>(Expression.Not(Expression.Call(member, method, constant)), parameter); + } + } + else + { + expression = p => true; + } + break; + default: + expression = p => false; + break; + } + return expression; } /// <summary> @@ -133,7 +184,7 @@ /// <param name="pageData"></param> /// <param name="propertyInfo"></param> /// <returns></returns> - private Dictionary<string, OrderByType> GetPageDataSort(PageDataOptions pageData, PropertyInfo[] propertyInfo) + protected Dictionary<string, OrderByType> GetPageDataSort(PageDataOptions pageData, PropertyInfo[] propertyInfo) { if (!string.IsNullOrEmpty(pageData.Sort)) { @@ -151,7 +202,7 @@ { return new Dictionary<string, OrderByType> { { - pageData.Sort,pageData.Order?.ToLower() == OrderByType.Asc.ToString() ? OrderByType.Asc : OrderByType.Desc + pageData.Sort,pageData.Order?.ToLower() == OrderByType.Asc.ToString().ToLower() ? OrderByType.Asc : OrderByType.Desc } }; } } @@ -263,7 +314,7 @@ } Type detailType = typeof(TEntity).GetDetailType(); - MethodInfo? methodInfo = GetType().GetMethod("AddDataIncludesDetail"); + MethodInfo? methodInfo = GetType().GetMethod(nameof(AddDataIncludesDetail)); methodInfo = methodInfo?.MakeGenericMethod(new Type[] { detailType }); object? obj = methodInfo?.Invoke(this, new object[] { entity, detailType, saveModel.DetailData }); return obj as WebResponseContent; @@ -373,27 +424,11 @@ TEntity entity = saveModel.MainData.DicToModel<TEntity>(); - List<string> listCol = new List<string>(); - foreach (var item in saveModel.MainData) - { - PropertyInfo propertyInfo = typeof(TEntity).GetProperty(item.Key); - if (propertyInfo == null) - { - propertyInfo = typeof(TEntity).GetProperty(item.Key.FirstLetterToLower()); - if (propertyInfo == null) - { - propertyInfo = typeof(TEntity).GetProperty(item.Key.FirstLetterToUpper()); - } - } - - listCol.Add(propertyInfo?.Name); - } - if (saveModel.DetailData == null || saveModel.DetailData.Count == 0) { - if (list != null) - listCol = listCol.Where(x => !list.Contains(x)).ToList(); - bool result = BaseDal.UpdateData(entity, listCol, list); + //if (list != null) + // listCol = listCol.Where(x => !list.Contains(x)).ToList(); + bool result = BaseDal.UpdateData(entity); return WebResponseContent.Instance.OK(); } @@ -403,7 +438,7 @@ } Type detailType = typeof(TEntity).GetDetailType(); - MethodInfo? methodInfo = GetType().GetMethod("UpdateDataInculdesDetail"); + MethodInfo? methodInfo = GetType().GetMethod(nameof(UpdateDataInculdesDetail)); methodInfo = methodInfo?.MakeGenericMethod(new Type[] { detailType }); object? obj = methodInfo?.Invoke(this, new object[] { entity, detailType, saveModel.DetailData, saveModel.DelKeys }); return obj as WebResponseContent; @@ -601,10 +636,10 @@ try { Type t = typeof(TEntity); - + ISugarQueryable<TEntity> sugarQueryable = Db.Queryable<TEntity>(); string savePath = AppDomain.CurrentDomain.BaseDirectory + $"ExcelExport"; IExporter exporter = new ExcelExporter(); - string wheres = ValidatePageOptions(options); + string wheres = ValidatePageOptions(options, ref sugarQueryable); //鑾峰彇鎺掑簭瀛楁 Dictionary<string, OrderByType> orderbyDic = GetPageDataSort(options, TProperties); -- Gitblit v1.9.3