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/MesStaffManager.cs |  106 ++++++++++++++++++----------------------------------
 1 files changed, 37 insertions(+), 69 deletions(-)

diff --git a/MES.Service/service/BasicData/MesStaffManager.cs b/MES.Service/service/BasicData/MesStaffManager.cs
index 7f37ec4..52a57c9 100644
--- a/MES.Service/service/BasicData/MesStaffManager.cs
+++ b/MES.Service/service/BasicData/MesStaffManager.cs
@@ -30,12 +30,11 @@
                         return 1;
                     break;
                 case "3":
-                    if (DeleteStaff(db, entity.Id)) // 鍒犻櫎鍛樺伐
+                    if (UpdateStaffStatus(db, entity.Id, "B")) // 绂佺敤鍛樺伐
                         return 1;
                     break;
                 default:
-                    throw new ArgumentNullException(
-                        $"type娌℃湁{unit.Type}杩欎釜绫诲瀷鐨勫弬鏁�");
+                    throw new ArgumentNullException($"type娌℃湁{unit.Type}杩欎釜绫诲瀷鐨勫弬鏁�");
             }
 
             throw new NotImplementedException("鎿嶄綔澶辫触");
@@ -46,41 +45,31 @@
     private bool UpdateStaffStatus(SqlSugarScope db, decimal staffId,
         string status)
     {
-        var result = db.Updateable<MesStaff>()
-            .SetColumns(s => s.FforbidStatus == status)
-            .Where(s => s.Id == staffId).ExecuteCommand();
-
-        if (result > 0) return true;
-        throw new NotImplementedException(status == "A" ? "鍚敤澶辫触" : "绂佺敤澶辫触");
+        var result = db.Updateable<MesStaff>().SetColumns(s => s.FforbidStatus == status).Where(s => s.Id == staffId).ExecuteCommand();
+        return true;
     }
 
     // 鎻掑叆鎴栨洿鏂板憳宸ョ殑鏂规硶
     private bool InsertOrUpdateStaff(SqlSugarScope db, MesStaff entity)
     {
-        var exists = db.Queryable<MesStaff>()
-            .Any(u => u.StaffNo == entity.StaffNo);
+        var exists = db.Queryable<MesStaff>().Any(e => e.Id == entity.Id);
         if (exists)
         {
-            var updateStaff = db.Updateable(entity).ExecuteCommand();
-            if (updateStaff > 0) return true;
+            var update = db.Updateable(entity).ExecuteCommand();
+            return true;
         }
         else
         {
-            var insertStaff = db.Insertable(entity).ExecuteCommand();
-            if (insertStaff > 0) return true;
+            var insert = db.Insertable(entity).ExecuteCommand();
+            if (insert > 0)
+            {
+                return true;
+            }
+            else
+            {
+                return false;
+            }
         }
-
-        return false;
-    }
-
-    // 鍒犻櫎鍛樺伐鐨勬柟娉�
-    private bool DeleteStaff(SqlSugarScope db, decimal staffId)
-    {
-        var deleteStaff =
-            db.Deleteable<MesStaff>().In(staffId).ExecuteCommand();
-        if (deleteStaff > 0) return true;
-
-        throw new NotImplementedException("鍙嶅鏍稿け璐�");
     }
 
     // 灏� ErpStaff 瀵硅薄杞崲涓� MesStaff 瀵硅薄鐨勬柟娉�
@@ -109,19 +98,10 @@
     }
 
     // SaveList 鏂规硶鐢ㄤ簬淇濆瓨澶氫釜鍛樺伐璁板綍锛屾牴鎹被鍨嬫壒閲忔墽琛屼笉鍚岀殑鎿嶄綔
-    public bool SaveList(List<ErpStaff> departments)
+    public bool SaveList(List<ErpStaff> staffs)
     {
-        var list = new List<MesStaff>();
-
-        departments.ForEach(s =>
-        {
-            var entity = GetMesStaff(s);
-            entity.Type = s.Type;
-            list.Add(entity);
-        });
-
-        var groupBy = list.GroupBy(s => s.Type)
-            .ToDictionary(g => g.Key, g => g.ToList());
+        var list = staffs.Select(GetMesStaff).ToList();
+        var groupBy = list.GroupBy(s => s.Type).ToDictionary(g => g.Key, g => g.ToList());
         return UseTransaction(db =>
         {
             foreach (var staffGroup in groupBy)
@@ -142,7 +122,7 @@
                             throw new NotImplementedException("鎻掑叆澶辫触");
                         break;
                     case "3":
-                        if (!DeleteStaffBatch(db, staffGroup.Value)) // 鎵归噺鍒犻櫎鍛樺伐
+                        if (!UpdateStaffStatusBatch(db, staffGroup.Value, "B")) // 鎵归噺绂佺敤鍛樺伐
                             throw new NotImplementedException("鍒犻櫎澶辫触");
                         break;
                     case "4":
@@ -160,49 +140,37 @@
     }
 
     // 鎵归噺鏇存柊鍛樺伐鐘舵�佺殑鏂规硶
-    private bool UpdateStaffStatusBatch(SqlSugarScope db,
-        List<MesStaff> staffList, string status)
+    private bool UpdateStaffStatusBatch(SqlSugarScope db,List<MesStaff> staffList, string status)
     {
         var ids = staffList.Select(it => it.Id).ToArray();
-        var result = db.Updateable<MesStaff>()
-            .SetColumns(s => s.FforbidStatus == status)
-            .Where(s => ids.Contains(s.Id)).ExecuteCommand();
-
-        if (result > 0) return true;
-
-        throw new NotImplementedException(status == "A" ? "鍚敤澶辫触" : "绂佺敤澶辫触");
+        var result = db.Updateable<MesStaff>().SetColumns(s => s.FforbidStatus == status).Where(s => ids.Contains(s.Id)).ExecuteCommand();
+        return true;
+        
     }
 
     // 鎵归噺鎻掑叆鍛樺伐鐨勬柟娉�
     private bool InsertStaffBatch(SqlSugarScope db, List<MesStaff> staffList)
     {
-        var userInsert = staffList.FindAll(s => s.Type == "INSERT");
-        var executeCommand = db.Insertable(userInsert).ExecuteCommand();
-        if (executeCommand > 0)
-            if (db.Insertable(staffList).ExecuteCommand() > 0)
-                return true;
-
-        throw new ArgumentNullException("瀹℃牳澶辫触");
-    }
-
-    // 鎵归噺鍒犻櫎鍛樺伐鐨勬柟娉�
-    private bool DeleteStaffBatch(SqlSugarScope db, List<MesStaff> staffList)
-    {
-        var ids = staffList.Select(it => it.Id).ToArray();
-        if (db.Deleteable<MesStaff>().In(ids).ExecuteCommand() > 0) return true;
-
-        throw new ArgumentNullException("鍙嶅鏍稿け璐�");
+        foreach (var entity in staffList)
+        {
+            if (!InsertOrUpdateStaff(db, entity))
+            {
+                return false;
+            }
+        }
+        return true;
     }
 
     // 鎵归噺鎻掑叆鎴栨洿鏂板憳宸ョ殑鏂规硶
     private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesStaff> staffList)
     {
-        foreach (var staff in staffList)
+        foreach (var entity in staffList)
         {
-            var entity = staffList.First(s => s.StaffNo == staff.StaffNo);
-            if (!InsertOrUpdateStaff(db, entity)) return false;
+            if (!InsertOrUpdateStaff(db, entity))
+            {
+                return false;
+            }
         }
-
         return true;
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3