| | |
| | | using Microsoft.AspNetCore.Http; |
| | | using Microsoft.AspNetCore.Mvc.RazorPages; |
| | | using Microsoft.Extensions.Options; |
| | | using Newtonsoft.Json; |
| | | using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; |
| | | using SqlSugar; |
| | | using System.Drawing.Drawing2D; |
| | |
| | | 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 |
| | | { |
| | |
| | | |
| | | 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>(); |
| | | //if (QueryRelativeExpression != null) |
| | | //{ |
| | | // queryable = QueryRelativeExpression.Invoke(queryable); |
| | | //} |
| | | //if (options.Export) |
| | | //{ |
| | | // queryable = queryable.GetIQueryableOrderBy(orderbyDic); |
| | | // if (Limit > 0) |
| | | // { |
| | | // queryable = queryable.Take(Limit); |
| | | // } |
| | | // pageGridData.rows = queryable.ToList(); |
| | | //} |
| | | //else |
| | | //{ |
| | | // pageGridData.rows = repository.IQueryablePage(queryable, |
| | | // options.Page, |
| | | // options.Rows, |
| | | // out int rowCount, |
| | | // orderbyDic).ToList(); |
| | | // pageGridData.total = rowCount; |
| | | // //查询界面统计求等字段 |
| | | // if (SummaryExpress != null) |
| | | // { |
| | | // pageGridData.summary = SummaryExpress.Invoke(queryable); |
| | | // //Func<T, T> groupExpress = x =>x; |
| | | // //pageGridData.summary = queryable.GroupBy(groupExpress).Select(SummaryExpress).FirstOrDefault(); |
| | | // } |
| | | //} |
| | | //GetPageDataOnExecuted?.Invoke(pageGridData); |
| | | |
| | | 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 = ""; |
| | |
| | | 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; |
| | | |
| | |
| | | } |
| | | 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}'"; |
| | | } |
| | | Expression<Func<TEntity, bool>> expression = GetWhereExpression(property.Name, results[j].Item3, null, expressionType); |
| | | sugarQueryable = sugarQueryable.Where(expression); |
| | | |
| | | if (j == results.Count() - 1) |
| | | { |
| | | where += ")"; |
| | | } |
| | | else |
| | | { |
| | | where += " or "; |
| | | } |
| | | //if (j == 0) |
| | | //{ |
| | | // where += "("; |
| | | //} |
| | | //LinqExpressionType expressionType = searchParametersList[i].DisplayType.GetLinqCondition(); |
| | | //if (expressionType == LinqExpressionType.Equal) |
| | | //{ |
| | | // where += $"{searchParametersList[i].Name} {HtmlElementType.Equal} '{results[j].Item3}'"; |
| | | //} |
| | | //else if (expressionType == LinqExpressionType.ThanOrEqual) |
| | | //{ |
| | | // where += $"{searchParametersList[i].Name} {HtmlElementType.ThanOrEqual} '{searchParametersList[i].Value}'"; |
| | | //} |
| | | //else if (expressionType == LinqExpressionType.LessThanOrEqual) |
| | | //{ |
| | | // where += $"{searchParametersList[i].Name} {HtmlElementType.LessOrEqual} '{searchParametersList[i].Value}'"; |
| | | //} |
| | | //else if (expressionType == LinqExpressionType.GreaterThan) |
| | | //{ |
| | | // where += $"{searchParametersList[i].Name} {HtmlElementType.GT} '{searchParametersList[i].Value}'"; |
| | | //} |
| | | //else if (expressionType == LinqExpressionType.LessThan) |
| | | //{ |
| | | // where += $"{searchParametersList[i].Name} {HtmlElementType.LT} '{searchParametersList[i].Value}'"; |
| | | //} |
| | | //else if (expressionType == LinqExpressionType.Contains) |
| | | //{ |
| | | // where += $"{searchParametersList[i].Name} {HtmlElementType.like} '%{searchParametersList[i].Value}%'"; |
| | | //} |
| | | //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 "; |
| | | //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=>p.name 属性name |
| | | 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> |
| | |
| | | /// <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)) |
| | | { |
| | |
| | | { |
| | | 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 |
| | | } }; |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | TEntity entity = saveModel.MainData.DicToModel<TEntity>(); |
| | | if (saveModel.DetailData == null && saveModel.DetailData.Count == 0) |
| | | if (saveModel.DetailData == null || saveModel.DetailData.Count == 0) |
| | | { |
| | | BaseDal.AddData(entity); |
| | | return WebResponseContent.Instance.OK(); |
| | |
| | | } |
| | | |
| | | 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; |
| | |
| | | |
| | | 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()); |
| | | } |
| | | } |
| | | //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); |
| | | } |
| | | // listCol.Add(propertyInfo?.Name); |
| | | //} |
| | | |
| | | if (saveModel.DetailData == null && saveModel.DetailData.Count == 0) |
| | | 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(); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | 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; |
| | |
| | | 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); |
| | | |
| | |
| | | content = WebResponseContent.Instance.OK(data: savePath + "\\" + fileName); |
| | | return content; |
| | | } |
| | | |
| | | public WebResponseContent ExportSeedData() |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | try |
| | | { |
| | | string seedDataFolder = $"WIDESEAWCS_DB.DBSeed.Json/{typeof(TEntity).Name}.tsv"; |
| | | List<TEntity> deviceInfos = BaseDal.QueryData(); |
| | | string str = JsonConvert.SerializeObject(deviceInfos, Formatting.Indented); |
| | | List<Dictionary<string, object>> keyValuePairs = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(str); |
| | | FileHelper.WriteFileAndDelOldFile($"{AppDomain.CurrentDomain.BaseDirectory}wwwroot/{seedDataFolder}", str); |
| | | content = WebResponseContent.Instance.OK(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | content = WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | return content; |
| | | } |
| | | } |
| | | } |