| | |
| | | using Magicodes.ExporterAndImporter.Core.Models; |
| | | using Magicodes.ExporterAndImporter.Excel; |
| | | 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 WIDESEA_Core.Enums; |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_Core.Utilities; |
| | | using static OfficeOpenXml.ExcelErrorValue; |
| | | |
| | | namespace WIDESEA_Core.BaseServices |
| | | { |
| | |
| | | public ISqlSugarClient Db => BaseDal.Db; |
| | | |
| | | private PropertyInfo[] _propertyInfo { get; set; } = null; |
| | | private PropertyInfo[] TProperties |
| | | public PropertyInfo[] TProperties |
| | | { |
| | | get |
| | | { |
| | |
| | | |
| | | public virtual PageGridData<TEntity> GetPageData(PageDataOptions options) |
| | | { |
| | | string wheres = ValidatePageOptions(options); |
| | | string wheres = options.ValidatePageOptions(TProperties); |
| | | //è·åæåºå段 |
| | | Dictionary<string, OrderByType> orderbyDic = GetPageDataSort(options, TProperties); |
| | | Dictionary<string, OrderByType> orderbyDic = options.GetPageDataSort(TProperties); |
| | | |
| | | 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); |
| | | |
| | | return pageGridData; |
| | | } |
| | | |
| | | |
| | | protected string ValidatePageOptions(PageDataOptions options) |
| | | public string ValidatePageOptions(PageDataOptions options) |
| | | { |
| | | options = options ?? new PageDataOptions(); |
| | | string where = ""; |
| | | string text = ""; |
| | | List<SearchParameters> searchParametersList = new List<SearchParameters>(); |
| | | if (options.Filter != null && options.Filter.Count > 0) |
| | | { |
| | |
| | | searchParametersList = options.Wheres.DeserializeObject<List<SearchParameters>>(); |
| | | options.Filter = searchParametersList; |
| | | } |
| | | catch { } |
| | | catch |
| | | { |
| | | } |
| | | } |
| | | QueryRelativeList?.Invoke(searchParametersList); |
| | | |
| | | for (int i = 0; i < searchParametersList.Count; i++) |
| | | base.QueryRelativeList?.Invoke(searchParametersList); |
| | | int i; |
| | | for (i = 0; i < searchParametersList.Count; i++) |
| | | { |
| | | if (string.IsNullOrEmpty(searchParametersList[i].Value)) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | PropertyInfo property = TProperties.Where(c => c.Name.ToUpper() == searchParametersList[i].Name.ToUpper()).FirstOrDefault(); |
| | | |
| | | if (property == null) continue; |
| | | |
| | | (bool, string, object) result = property.ValidationVal(searchParametersList[i].Value.Split(',')); |
| | | if (result.Item1) |
| | | PropertyInfo propertyInfo = TProperties.Where((PropertyInfo c) => c.Name.ToUpper() == searchParametersList[i].Name.ToUpper()).FirstOrDefault(); |
| | | if (propertyInfo == null) |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | LinqExpressionType expressionType = searchParametersList[i].DisplayType.GetLinqCondition(); |
| | | if (expressionType == LinqExpressionType.Equal) |
| | | object[] values = searchParametersList[i].Value.Split(','); |
| | | List<(bool, string, object)> list = propertyInfo.ValidationValueForDbType(values).ToList(); |
| | | if (list == null || list.Count() == 0) |
| | | { |
| | | if (string.IsNullOrEmpty(where)) |
| | | where += $"{searchParametersList[i].Name} = '{searchParametersList[i].Value}'"; |
| | | else |
| | | where += $" and {searchParametersList[i].Name} = '{searchParametersList[i].Value}'"; |
| | | continue; |
| | | } |
| | | else |
| | | { |
| | | if (string.IsNullOrEmpty(where)) |
| | | where += $"{searchParametersList[i].Name} {searchParametersList[i].DisplayType} '{searchParametersList[i].Value}'"; |
| | | else |
| | | where += $" and {searchParametersList[i].Name} {searchParametersList[i].DisplayType} '{searchParametersList[i].Value}'"; |
| | | } |
| | | } |
| | | return where; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// è·åæåºå段 |
| | | /// </summary> |
| | | /// <param name="pageData"></param> |
| | | /// <param name="propertyInfo"></param> |
| | | /// <returns></returns> |
| | | private Dictionary<string, OrderByType> GetPageDataSort(PageDataOptions pageData, PropertyInfo[] propertyInfo) |
| | | { |
| | | if (!string.IsNullOrEmpty(pageData.Sort)) |
| | | { |
| | | if (pageData.Sort.Contains(",")) |
| | | for (int j = 0; j < list.Count(); j++) |
| | | { |
| | | List<string> sortArr = pageData.Sort.Split(",").Where(x => propertyInfo.Any(p => p.Name == x)).ToList(); |
| | | Dictionary<string, OrderByType> sortDic = new Dictionary<string, OrderByType>(); |
| | | foreach (var item in sortArr) |
| | | if (j == 0) |
| | | { |
| | | sortDic[item] = pageData.Order?.ToLower() == OrderByType.Asc.ToString() ? OrderByType.Asc : OrderByType.Desc; |
| | | text += "("; |
| | | } |
| | | return sortDic; |
| | | |
| | | text = ((searchParametersList[i].DisplayType.GetLinqCondition() != 0) ? (text + $"{searchParametersList[i].Name} {searchParametersList[i].DisplayType} '{list[j].Item3}'") : (text + $"{searchParametersList[i].Name} = '{list[j].Item3}'")); |
| | | text = ((j != list.Count() - 1) ? (text + " or ") : (text + ")")); |
| | | } |
| | | else if (propertyInfo.Any(x => x.Name == pageData.Sort)) |
| | | |
| | | if (i < searchParametersList.Count - 1) |
| | | { |
| | | return new Dictionary<string, OrderByType> { |
| | | { |
| | | pageData.Sort,pageData.Order?.ToLower() == OrderByType.Asc.ToString() ? OrderByType.Asc : OrderByType.Desc |
| | | } }; |
| | | text += " and "; |
| | | } |
| | | } |
| | | return new Dictionary<string, OrderByType> { { "CreateDate", pageData.Order?.ToLower() == OrderByType.Asc.ToString() ? OrderByType.Asc : OrderByType.Desc } }; |
| | | |
| | | return text; |
| | | } |
| | | |
| | | public virtual object GetDetailPage(PageDataOptions pageData) |
| | |
| | | if (propertyInfo != null) |
| | | { |
| | | Type detailType = propertyInfo.PropertyType.GetGenericArguments()[0]; |
| | | List<ExpandoObject> list = BaseDal.Db.Queryable(detailType.Name, "detail").Where(keyName, "=", pageData.Value).ToPageList(pageData.Page, pageData.Rows, ref totalCount); |
| | | Navigate navigate = propertyInfo.GetCustomAttribute<Navigate>(); |
| | | List<ExpandoObject> list = BaseDal.Db.Queryable(detailType.Name, "detail").Where(navigate.GetName(), "=", pageData.Value).ToPageList(pageData.Page, pageData.Rows, ref totalCount); |
| | | return new PageGridData<ExpandoObject>(totalCount, list); |
| | | } |
| | | return new PageGridData<object>(total: 0, null); |
| | |
| | | { |
| | | return WebResponseContent.Instance.Error("ä¼ åé误,åæ°ä¸è½ä¸ºç©º"); |
| | | } |
| | | //string validResult = typeof(TEntity).ValidateDicInEntity(saveModel.MainData, true, TProperties); |
| | | string validResult = typeof(TEntity).ValidateDicInEntity(saveModel.MainData, true, TProperties); |
| | | |
| | | //if (!string.IsNullOrEmpty(validResult)) |
| | | //{ |
| | | // return WebResponseContent.Instance.Error(validResult); |
| | | //} |
| | | if (!string.IsNullOrEmpty(validResult)) |
| | | { |
| | | return WebResponseContent.Instance.Error(validResult); |
| | | } |
| | | |
| | | PropertyInfo keyPro = typeof(TEntity).GetKeyProperty(); |
| | | if (keyPro == null) |
| | |
| | | } |
| | | else if (keyPro.PropertyType == typeof(int) || keyPro.PropertyType == typeof(long)) |
| | | { |
| | | SugarColumn sugarColumn = keyPro.GetCustomAttribute<SugarColumn>(); |
| | | if (sugarColumn.IsIdentity) |
| | | SugarColumn? sugarColumn = keyPro.GetCustomAttribute<SugarColumn>(); |
| | | if (sugarColumn?.IsIdentity ?? true) |
| | | { |
| | | saveModel.MainData.Remove(keyPro.Name); |
| | | saveModel.MainData.Remove(keyPro.Name.FirstLetterToUpper()); |
| | | saveModel.MainData.Remove(keyPro.Name.FirstLetterToLower()); |
| | | } |
| | | } |
| | | |
| | | TEntity entity = saveModel.MainData.DicToModel<TEntity>(); |
| | | if (saveModel.DetailData == null || saveModel.DetailData.Count == 0) |
| | | { |
| | | BaseDal.AddData(entity); |
| | | return WebResponseContent.Instance.OK(); |
| | | } |
| | | |
| | | BaseDal.AddData(entity); |
| | | if (typeof(TEntity).GetNavigatePro() == null) |
| | | { |
| | | return WebResponseContent.Instance.Error("æªé
置导èªå±æ§"); |
| | | } |
| | | |
| | | return WebResponseContent.Instance.OK(); |
| | | Type detailType = typeof(TEntity).GetDetailType(); |
| | | MethodInfo? methodInfo = GetType().GetMethod("AddDataIncludesDetail"); |
| | | methodInfo = methodInfo?.MakeGenericMethod(new Type[] { detailType }); |
| | | object? obj = methodInfo?.Invoke(this, new object[] { entity, detailType, saveModel.DetailData }); |
| | | return obj as WebResponseContent; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | } |
| | | |
| | | public WebResponseContent AddDataIncludesDetail<TDetail>(TEntity entity, Type detailType, List<Dictionary<string, object>> detailDics) where TDetail : class, new() |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | try |
| | | { |
| | | string name = typeof(TEntity).GetMainIdByDetail(); |
| | | string reslut = detailType.ValidateDicInEntity(detailDics, true, new string[] { name }); |
| | | if (reslut != string.Empty) |
| | | return WebResponseContent.Instance.Error(reslut); |
| | | |
| | | List<TDetail> list = detailDics.DicToIEnumerable<TDetail>(); |
| | | |
| | | ((SqlSugarClient)BaseDal.Db).BeginTran(); |
| | | |
| | | int id = BaseDal.Db.Insertable(entity).ExecuteReturnIdentity(); |
| | | |
| | | for (int i = 0; i < list.Count; i++) |
| | | { |
| | | TDetail detail = list[i]; |
| | | typeof(TDetail).SetDetailId(detail, id, name); |
| | | } |
| | | |
| | | BaseDal.Db.Insertable(list).ExecuteCommand(); |
| | | |
| | | ((SqlSugarClient)BaseDal.Db).CommitTran(); |
| | | |
| | | content = WebResponseContent.Instance.OK(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | ((SqlSugarClient)BaseDal.Db).RollbackTran(); |
| | | content = WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | return content; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | try |
| | | { |
| | | List<string> list = UpdateIgnoreColOnExecute?.Invoke(saveModel); |
| | | List<string>? list = UpdateIgnoreColOnExecute?.Invoke(saveModel); |
| | | if (saveModel == null || saveModel.MainData == null || saveModel.MainData.Count == 0)//夿忰æ¯å¦ä¼ å
¥ |
| | | { |
| | | return WebResponseContent.Instance.Error("ä¼ åé误,åæ°ä¸è½ä¸ºç©º"); |
| | | } |
| | | //string validResult = typeof(TEntity).ValidateDicInEntity(saveModel.MainData, false, TProperties, list.ToArray()); |
| | | string validResult = typeof(TEntity).ValidateDicInEntity(saveModel.MainData, false, TProperties, list?.ToArray() ?? null); |
| | | |
| | | //if (!string.IsNullOrEmpty(validResult)) |
| | | //{ |
| | | // return WebResponseContent.Instance.Error(validResult); |
| | | //} |
| | | if (!string.IsNullOrEmpty(validResult)) |
| | | { |
| | | return WebResponseContent.Instance.Error(validResult); |
| | | } |
| | | |
| | | //PropertyInfo keyPro = typeof(TEntity).GetKeyProperty(); |
| | | //if (keyPro == null) |
| | | //{ |
| | | // return WebResponseContent.Instance.Error("请å
设置主é®"); |
| | | //} |
| | | PropertyInfo keyPro = typeof(TEntity).GetKeyProperty(); |
| | | if (keyPro == null) |
| | | { |
| | | return WebResponseContent.Instance.Error("请å
设置主é®"); |
| | | } |
| | | |
| | | 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); |
| | | PropertyInfo? propertyInfo = typeof(TEntity).GetProperty(item.Key); |
| | | if (propertyInfo == null) |
| | | { |
| | | propertyInfo = typeof(TEntity).GetProperty(item.Key.FirstLetterToLower()); |
| | |
| | | |
| | | listCol.Add(propertyInfo?.Name); |
| | | } |
| | | if (list != null) |
| | | listCol = listCol.Where(x => !list.Contains(x)).ToList(); |
| | | bool result = BaseDal.UpdateData(entity, listCol, list); |
| | | |
| | | return WebResponseContent.Instance.OK(); |
| | | 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*/); |
| | | return WebResponseContent.Instance.OK(); |
| | | } |
| | | |
| | | if (typeof(TEntity).GetNavigatePro() == null) |
| | | { |
| | | return WebResponseContent.Instance.Error("æªé
置导èªå±æ§"); |
| | | } |
| | | |
| | | Type detailType = typeof(TEntity).GetDetailType(); |
| | | MethodInfo? methodInfo = GetType().GetMethod("UpdateDataInculdesDetail"); |
| | | methodInfo = methodInfo?.MakeGenericMethod(new Type[] { detailType }); |
| | | object? obj = methodInfo?.Invoke(this, new object[] { entity, detailType, saveModel.DetailData, saveModel.DelKeys }); |
| | | return obj as WebResponseContent; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | return WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | } |
| | | |
| | | public WebResponseContent UpdateDataInculdesDetail<TDetail>(TEntity entity, Type detailType, List<Dictionary<string, object>> detailDics, List<object> delKeys) where TDetail : class, new() |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | try |
| | | { |
| | | string name = typeof(TEntity).GetMainIdByDetail(); |
| | | string reslut = detailType.ValidateDicInEntity(detailDics, true, new string[] { name }); |
| | | if (reslut != string.Empty) |
| | | return WebResponseContent.Instance.Error(reslut); |
| | | |
| | | List<TDetail> list = detailDics.DicToIEnumerable<TDetail>(); |
| | | |
| | | List<object> dynamicDelKeys = new List<object>(); |
| | | if (delKeys != null) |
| | | { |
| | | for (int i = 0; i < delKeys.Count; i++) |
| | | { |
| | | dynamicDelKeys.Add(delKeys[i]); |
| | | } |
| | | } |
| | | |
| | | List<TDetail> updateRows = new List<TDetail>(); |
| | | List<TDetail> addRows = new List<TDetail>(); |
| | | |
| | | for (int i = 0; i < list.Count; i++) |
| | | { |
| | | object detailId = typeof(TDetail).GetPropertyValue(list[i], typeof(TDetail).GetKeyName()); |
| | | if (detailId != null) |
| | | { |
| | | if (detailId.ToString() != "0") |
| | | { |
| | | updateRows.Add(list[i]); |
| | | } |
| | | else |
| | | { |
| | | addRows.Add(list[i]); |
| | | } |
| | | } |
| | | } |
| | | |
| | | object mainId = typeof(TEntity).GetPropertyValue(entity, typeof(TEntity).GetKeyName()); |
| | | if (mainId != null) |
| | | { |
| | | ((SqlSugarClient)BaseDal.Db).BeginTran(); |
| | | |
| | | if (dynamicDelKeys.Count > 0) |
| | | BaseDal.Db.Deleteable<object>().AS(detailType.Name).Where($"{detailType.GetKeyName()} in (@id)", new { id = dynamicDelKeys.ToArray() }).ExecuteCommandHasChange(); |
| | | |
| | | BaseDal.Db.Updateable(entity).ExecuteCommandHasChange(); |
| | | |
| | | BaseDal.Db.Updateable(updateRows).ExecuteCommand(); |
| | | |
| | | for (int i = 0; i < addRows.Count; i++) |
| | | { |
| | | TDetail detail = addRows[i]; |
| | | typeof(TDetail).SetDetailId(detail, mainId, name); |
| | | } |
| | | |
| | | BaseDal.Db.Insertable(addRows).ExecuteCommand(); |
| | | |
| | | ((SqlSugarClient)BaseDal.Db).CommitTran(); |
| | | |
| | | content = WebResponseContent.Instance.OK(); |
| | | } |
| | | else |
| | | { |
| | | content = WebResponseContent.Instance.Error("æªæ¾å°ä¸»è¡¨ä¸»é®å¼"); |
| | | } |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | ((SqlSugarClient)BaseDal.Db).RollbackTran(); |
| | | content = WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | return content; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | try |
| | | { |
| | | return BaseDal.DeleteDataByIds(keys) ? WebResponseContent.Instance.OK() : WebResponseContent.Instance.Error(); |
| | | if (typeof(TEntity).GetNavigatePro() == null) |
| | | return BaseDal.DeleteDataByIds(keys) ? WebResponseContent.Instance.OK() : WebResponseContent.Instance.Error(); |
| | | else |
| | | { |
| | | if (keys != null) |
| | | { |
| | | Type detailType = typeof(TEntity).GetDetailType(); |
| | | string name = typeof(TEntity).GetMainIdByDetail(); |
| | | List<object> dynamicDelKeys = new List<object>(); |
| | | |
| | | for (int i = 0; i < keys.Length; i++) |
| | | { |
| | | dynamicDelKeys.Add(keys[i]); |
| | | } |
| | | ((SqlSugarClient)BaseDal.Db).BeginTran(); |
| | | |
| | | if (dynamicDelKeys.Count > 0) |
| | | BaseDal.Db.Deleteable<object>().AS(detailType.Name).Where($"{name} in (@id)", new { id = dynamicDelKeys.ToArray() }).ExecuteCommandHasChange(); |
| | | |
| | | BaseDal.DeleteDataByIds(keys); |
| | | |
| | | ((SqlSugarClient)BaseDal.Db).CommitTran(); |
| | | |
| | | return WebResponseContent.Instance.OK(); |
| | | } |
| | | else |
| | | { |
| | | return WebResponseContent.Instance.Error("åæ°é误"); |
| | | } |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | ((SqlSugarClient)BaseDal.Db).RollbackTran(); |
| | | return WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | } |
| | |
| | | |
| | | string savePath = AppDomain.CurrentDomain.BaseDirectory + $"ExcelExport"; |
| | | IExporter exporter = new ExcelExporter(); |
| | | string wheres = ValidatePageOptions(options); |
| | | string wheres = options.ValidatePageOptions(TProperties); |
| | | //è·åæåºå段 |
| | | Dictionary<string, OrderByType> orderbyDic = GetPageDataSort(options, TProperties); |
| | | Dictionary<string, OrderByType> orderbyDic = options.GetPageDataSort(TProperties); |
| | | |
| | | List<TEntity> entities = BaseDal.QueryData(wheres, orderbyDic); |
| | | |
| | |
| | | content = WebResponseContent.Instance.OK(data: savePath + "\\" + fileName); |
| | | return content; |
| | | } |
| | | |
| | | public WebResponseContent ExportSeedData() |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | try |
| | | { |
| | | string seedDataFolder = $"WIDESEA_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; |
| | | } |
| | | } |
| | | } |