| | |
| | | using Magicodes.ExporterAndImporter.Core.Models; |
| | | using Magicodes.ExporterAndImporter.Excel; |
| | | using Microsoft.AspNetCore.Http; |
| | | using Microsoft.AspNetCore.Mvc.RazorPages; |
| | | using Microsoft.Extensions.Options; |
| | | using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; |
| | | using SqlSugar; |
| | |
| | | using WIDESEAWCS_Core.Enums; |
| | | using WIDESEAWCS_Core.Helper; |
| | | using WIDESEAWCS_Core.Utilities; |
| | | using static OfficeOpenXml.ExcelErrorValue; |
| | | |
| | | namespace WIDESEAWCS_Core.BaseServices |
| | | { |
| | |
| | | |
| | | if (property == null) continue; |
| | | |
| | | (bool, string, object) result = property.ValidationVal(searchParametersList[i].Value.Split(',')); |
| | | if (!result.Item1) |
| | | 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 += "("; |
| | | } |
| | | 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}'"; |
| | | } |
| | | |
| | | LinqExpressionType expressionType = searchParametersList[i].DisplayType.GetLinqCondition(); |
| | | if (expressionType == LinqExpressionType.Equal) |
| | | { |
| | | if (string.IsNullOrEmpty(where)) |
| | | where += $"{searchParametersList[i].Name} = '{searchParametersList[i].Value}'"; |
| | | if (j == results.Count() - 1) |
| | | { |
| | | where += ")"; |
| | | } |
| | | else |
| | | where += $" and {searchParametersList[i].Name} = '{searchParametersList[i].Value}'"; |
| | | { |
| | | where += " or "; |
| | | } |
| | | } |
| | | 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}'"; |
| | | } |
| | | if (i < searchParametersList.Count - 1) |
| | | where += " and "; |
| | | } |
| | | return where; |
| | | } |
| | |
| | | } |
| | | return sortDic; |
| | | } |
| | | else if (propertyInfo.Any(x => x.Name == pageData.Sort)) |
| | | else if (propertyInfo.Any(x => x.Name == pageData.Sort.FirstLetterToLower() || x.Name == pageData.Sort.FirstLetterToUpper())) |
| | | { |
| | | return new Dictionary<string, OrderByType> { |
| | | { |
| | |
| | | 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> |
| | |
| | | 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>(); |
| | | |
| | |
| | | |
| | | 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); |
| | | } |
| | | } |