From 966d1fe6077c885db064fcea98bb48cbccb464d6 Mon Sep 17 00:00:00 2001 From: dengjunjie <dengjunjie@hnkhzn.com> Date: 星期一, 14 十月 2024 17:23:02 +0800 Subject: [PATCH] WCS --- 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseServices/ServiceBase.cs | 229 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 202 insertions(+), 27 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseServices/ServiceBase.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseServices/ServiceBase.cs" index 88fae6b..29c88cd 100644 --- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseServices/ServiceBase.cs" +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseServices/ServiceBase.cs" @@ -120,7 +120,7 @@ if (property == null) continue; (bool, string, object) result = property.ValidationVal(searchParametersList[i].Value.Split(',')); - if (result.Item1) + if (!result.Item1) { continue; } @@ -188,7 +188,8 @@ 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); @@ -241,12 +242,12 @@ { 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) @@ -262,21 +263,71 @@ SugarColumn sugarColumn = keyPro.GetCustomAttribute<SugarColumn>(); if (sugarColumn.IsIdentity) { - 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) + if (saveModel.DetailData == null ) + { + 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> /// 淇敼鏁版嵁 @@ -321,23 +372,23 @@ { 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()); - //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>(); @@ -356,17 +407,111 @@ 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 (saveModel.DetailData == null) + { + 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> /// 鍒犻櫎鏁版嵁 @@ -394,10 +539,40 @@ { 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); } } -- Gitblit v1.9.3