From 302054e253f1550ee64430882786ec9a3920ec92 Mon Sep 17 00:00:00 2001 From: sjz <1240968267@qq.com> Date: 星期六, 19 七月 2025 08:33:44 +0800 Subject: [PATCH] 优化多组织代码 --- MES.Service/service/BasicData/OrganizeManager.cs | 225 ++++++++++++++++++-------------------------------------- 1 files changed, 73 insertions(+), 152 deletions(-) diff --git a/MES.Service/service/BasicData/OrganizeManager.cs b/MES.Service/service/BasicData/OrganizeManager.cs index e83b43d..bec7482 100644 --- a/MES.Service/service/BasicData/OrganizeManager.cs +++ b/MES.Service/service/BasicData/OrganizeManager.cs @@ -7,9 +7,7 @@ public class OrganizeManager : Repository<Organize> { - //褰撳墠绫诲凡缁忕户鎵夸簡 Repository 澧炪�佸垹銆佹煡銆佹敼鐨勬柟娉� - //杩欓噷闈㈠啓鐨勪唬鐮佷笉浼氱粰瑕嗙洊,濡傛灉瑕侀噸鏂扮敓鎴愯鍒犻櫎 OrganizeManager.cs public bool Save(ErpOrganize item) { var entity = GetOrganize(item); @@ -19,28 +17,27 @@ switch (item.Type) { case "0": - if (UpdateOrganizetatus(db, entity.Id, "A")) + if (UpdateOrganizetatus(db, entity.Id, "A")) //鈭� return 1; break; case "1": - if (UpdateOrganizetatus(db, entity.Id, "B")) + if (UpdateOrganizetatus(db, entity.Id, "B")) //鈭� return 1; break; case "2": - if (InsertItem(db, entity)) + if (InsertItem(db, entity)) //鈭� return 1; break; case "3": - if (DeleteItem(db, Convert.ToDecimal(entity.Id))) + if (UpdateOrganizetatus(db, entity.Id, "B")) //鈭� return 1; break; case "4": - if (InsertOrUpdate(db, entity)) + if (InsertOrUpdate(db, entity)) //鈭� return 1; break; default: - throw new ArgumentNullException( - $"type娌℃湁{item.Type}杩欎釜绫诲瀷鐨勫弬鏁�"); + throw new ArgumentNullException($"type娌℃湁{item.Type}杩欎釜绫诲瀷鐨勫弬鏁�"); } throw new NotImplementedException("鎿嶄綔澶辫触"); @@ -50,8 +47,7 @@ public bool SaveList(List<ErpOrganize> Organize) { var list = Organize.Select(GetOrganize).ToList(); - var groupBy = list.GroupBy(s => s.Type) - .ToDictionary(g => g.Key, g => g.ToList()); + var groupBy = list.GroupBy(s => s.Type).ToDictionary(g => g.Key, g => g.ToList()); return UseTransaction(db => { @@ -71,7 +67,7 @@ throw new NotImplementedException("鎻掑叆澶辫触"); break; case "3": - if (!DeleteItemBatch(db, itemGroup.Value)) + if (!UpdateOrganizetatusBatch(db, itemGroup.Value, "B")) throw new NotImplementedException("鍒犻櫎澶辫触"); break; case "4": @@ -79,90 +75,39 @@ throw new NotImplementedException("鍚屾澶辫触"); break; default: - throw new ArgumentNullException( - $"type娌℃湁{itemGroup.Key}杩欎釜绫诲瀷鐨勫弬鏁�"); + throw new ArgumentNullException($"type娌℃湁{itemGroup.Key}杩欎釜绫诲瀷鐨勫弬鏁�"); } return 1; }) > 0; } - private bool UpdateOrganizetatus(SqlSugarScope db, decimal itemId, - string status) + private bool UpdateOrganizetatus(SqlSugarScope db, decimal itemId,string status) { - var result = db.Updateable<Organize>() - .SetColumns(s => s.Fforbidstatus == status) - .Where(s => s.Id == itemId).ExecuteCommand(); - - if (result > 0) - return true; - - throw new NotImplementedException(status == "A" ? "鍚敤澶辫触" : "绂佺敤澶辫触"); + var result = db.Updateable<Organize>().SetColumns(s => s.Fforbidstatus == status).Where(s => s.Id == itemId).ExecuteCommand(); + return true; } private bool InsertItem(SqlSugarScope db, Organize entity) { - var insert = db.Insertable(entity).ExecuteCommand(); - if (insert > 0) - return true; - - throw new NotImplementedException("鎻掑叆澶辫触"); - } - - private bool DeleteItem(SqlSugarScope db, decimal itemId) - { - var deleteById = db.Deleteable<Organize>().In(itemId).ExecuteCommand(); - if (deleteById > 0) - return true; - - throw new NotImplementedException("鍒犻櫎澶辫触"); - } - - private Organize GetOrganize(ErpOrganize Organize) - { - return new Organize + var exists = db.Queryable<Organize>().Any(e => e.Id == entity.Id); + if (exists) { - Id = Convert.ToDecimal(Organize.Id), - Fnumber = Organize.FNumber, - Fname = Organize.FName, - Fforbidstatus = Organize.FForbidStatus, - Type = Organize.Type, - Fparentid = Organize.FParentID, - Fcreatedate = DateTime.Now - }; - } - - private bool UpdateOrganizetatusBatch(SqlSugarScope db, - List<Organize> itemList, string status) - { - var ids = itemList.Select(it => it.Id).ToArray(); - var result = db.Updateable<Organize>() - .SetColumns(s => s.Fforbidstatus == status) - .Where(s => ids.Contains(s.Id)).ExecuteCommand(); - - if (result > 0) + var result = db.Updateable(entity).ExecuteCommand(); return true; - - throw new NotImplementedException(status == "A" ? "鍚敤澶辫触" : "绂佺敤澶辫触"); - } - - private bool InsertItemBatch(SqlSugarScope db, List<Organize> itemList) - { - var insertRange = db.Insertable(itemList).ExecuteCommand(); - if (insertRange > 0) - return true; - - throw new NotImplementedException("鎻掑叆澶辫触"); - } - - private bool DeleteItemBatch(SqlSugarScope db, List<Organize> itemList) - { - var ids = itemList.Select(it => it.Id).ToArray(); - var deleteByIds = db.Deleteable<Organize>().In(ids).ExecuteCommand(); - if (deleteByIds > 0) - return true; - - throw new NotImplementedException("鍒犻櫎澶辫触"); + } + else + { + var insert = db.Insertable(entity).ExecuteCommand(); + if (insert > 0) + { + return true; + } + else + { + throw new NotImplementedException("鎻掑叆澶辫触"); + } + } } private bool InsertOrUpdate(SqlSugarScope db, Organize entity) @@ -171,90 +116,66 @@ if (exists) { var update = db.Updateable(entity).ExecuteCommand(); - if (update > 0) - return true; + return true; } else { var insert = db.Insertable(entity).ExecuteCommand(); if (insert > 0) + { return true; + } + else + { + return false; + } } + } - return false; + private Organize GetOrganize(ErpOrganize Organize) + { + return new Organize + { + + Id = Convert.ToDecimal(Organize.Id), + Fnumber = Organize.FNumber, + Fname = Organize.FName, + Fforbidstatus = Organize.FForbidStatus, + Type = Organize.Type, + Fparentid=Organize.FParentID, + Fcreatedate=DateTime.Now + }; + } + + private bool UpdateOrganizetatusBatch(SqlSugarScope db,List<Organize> itemList, string status) + { + var ids = itemList.Select(it => it.Id).ToArray(); + var result = db.Updateable<Organize>().SetColumns(s => s.Fforbidstatus == status).Where(s => ids.Contains(s.Id)).ExecuteCommand(); + return true; + } + + private bool InsertItemBatch(SqlSugarScope db, List<Organize> itemList) + { + foreach (var entity in itemList) + { + if (!InsertItem(db, entity)) + { + return false; + } + } + return true; } private bool InsertOrUpdateBatch(SqlSugarScope db, List<Organize> itemList) { foreach (var entity in itemList) + { if (!InsertOrUpdate(db, entity)) + { return false; - + } + } return true; } - - #region 鏁欏鏂规硶 - - /// <summary> - /// 浠撳偍鏂规硶婊¤冻涓嶄簡澶嶆潅涓氬姟闇�姹傦紝涓氬姟浠g爜璇峰湪杩欓噷闈㈠畾涔夋柟娉� - /// </summary> - public void Study() - { - /*********鏌ヨ*********/ - - var data1 = base.GetById(1); //鏍规嵁ID鏌ヨ - var data2 = base.GetList(); //鏌ヨ鎵�鏈� - var data3 = base.GetList(it => 1 == 1); //鏍规嵁鏉′欢鏌ヨ - //var data4 = base.GetSingle(it => 1 == 1);//鏍规嵁鏉′欢鏌ヨ涓�鏉�,濡傛灉瓒呰繃涓�鏉′細鎶ラ敊 - - var p = new PageModel { PageIndex = 1, PageSize = 2 }; // 鍒嗛〉鏌ヨ - var data5 = base.GetPageList(it => 1 == 1, p); - Console.Write(p.TotalCount); //杩斿洖鎬绘暟 - - var data6 = - base.GetPageList(it => 1 == 1, p, - it => SqlFunc.GetRandom()); // 鍒嗛〉鏌ヨ鍔犳帓搴� - Console.Write(p.TotalCount); //杩斿洖鎬绘暟 - - var conModels = new List<IConditionalModel>(); //缁勮鏉′欢鏌ヨ浣滀负鏉′欢瀹炵幇 鍒嗛〉鏌ヨ鍔犳帓搴� - conModels.Add(new ConditionalModel - { - FieldName = typeof(Organize).GetProperties()[0].Name, - ConditionalType = ConditionalType.Equal, FieldValue = "1" - }); //id=1 - var data7 = base.GetPageList(conModels, p, it => SqlFunc.GetRandom()); - - AsQueryable().Where(x => 1 == 1) - .ToList(); //鏀寔浜嗚浆鎹㈡垚queryable,鎴戜滑鍙互鐢╭ueryable瀹炵幇澶嶆潅鍔熻兘 - - - /*********鎻掑叆*********/ - var insertData = new Organize(); //娴嬭瘯鍙傛暟 - var insertArray = new[] { insertData }; - base.Insert(insertData); //鎻掑叆 - base.InsertRange(insertArray); //鎵归噺鎻掑叆 - var id = base.InsertReturnIdentity(insertData); //鎻掑叆杩斿洖鑷鍒� - AsInsertable(insertData).ExecuteCommand(); //鎴戜滑鍙互杞垚 Insertable瀹炵幇澶嶆潅鎻掑叆 - - - /*********鏇存柊*********/ - var updateData = new Organize(); //娴嬭瘯鍙傛暟 - var updateArray = new[] { updateData }; //娴嬭瘯鍙傛暟 - base.Update(updateData); //鏍规嵁瀹炰綋鏇存柊 - base.UpdateRange(updateArray); //鎵归噺鏇存柊 - //base.Update(it => new Organize() { ClassName = "a", CreateTime = DateTime.Now }, it => it.id==1);// 鍙洿鏂癈lassName鍒楀拰CreateTime鍒楋紝鍏跺畠鍒椾笉鏇存柊锛屾潯浠秈d=1 - AsUpdateable(updateData).ExecuteCommand(); //杞垚Updateable鍙互瀹炵幇澶嶆潅鐨勬彃鍏� - - - /*********鍒犻櫎*********/ - var deldata = new Organize(); //娴嬭瘯鍙傛暟 - base.Delete(deldata); //鏍规嵁瀹炰綋鍒犻櫎 - base.DeleteById(1); //鏍规嵁涓婚敭鍒犻櫎 - base.DeleteById(new[] { 1, 2 }); //鏍规嵁涓婚敭鏁扮粍鍒犻櫎 - base.Delete(it => 1 == 2); //鏍规嵁鏉′欢鍒犻櫎 - AsDeleteable().Where(it => 1 == 2) - .ExecuteCommand(); //杞垚Deleteable瀹炵幇澶嶆潅鐨勬搷浣� - } - - #endregion + } \ No newline at end of file -- Gitblit v1.9.3