From eab13f60bbdc8ea275c6dd7b6424cdfc7769f6e1 Mon Sep 17 00:00:00 2001
From: 啊鑫 <t2856754968@163.com>
Date: 星期三, 27 八月 2025 16:53:15 +0800
Subject: [PATCH] 111111

---
 MES.Service/service/BasicData/MesStaffManager.cs |  223 ++++++++++++++++++++++++++++---------------------------
 1 files changed, 114 insertions(+), 109 deletions(-)

diff --git a/MES.Service/service/BasicData/MesStaffManager.cs b/MES.Service/service/BasicData/MesStaffManager.cs
index a0081d3..b9276bd 100644
--- a/MES.Service/service/BasicData/MesStaffManager.cs
+++ b/MES.Service/service/BasicData/MesStaffManager.cs
@@ -70,7 +70,7 @@
         db.Deleteable<MesStaff>()
             .Where(s => s.Id == entity.Id)
             .ExecuteCommand();
- 
+
         var insertStaff =
             db.Insertable(entity).IgnoreColumns(true).ExecuteCommand();
 
@@ -87,13 +87,13 @@
             insertPosition = db.Insertable(mesStaffPositionLink).PageSize(1)
                 .IgnoreColumnsNull().ExecuteCommand();
         }
- 
- 
+
+
         return insertStaff > 0 && insertPosition > 0;
     }
 
     // 鍒犻櫎鍛樺伐鐨勬柟娉�
-   
+
 
     private bool DeleteStaff(SqlSugarScope db, SysUser sysUser, MesStaff entity)
     {
@@ -107,16 +107,16 @@
                         s.Id == entity.Id &&
                         s.PositionCode == entity.PositionCode)
                     .ExecuteCommand();
- 
+
             //鍒犻櫎瀵瑰簲鍛樺伐宀椾綅淇℃伅鍏宠仈琛� 姹犲崡楠� 2025-01-02
             var insertPosition = db.Deleteable<MesStaffPositionLink>()
                 .Where(s => s.StaffId == entity.Id)
                 .ExecuteCommand();
- 
+
             if (deleteStaff > 0)
                 return true;
         }
- 
+
         throw new NotImplementedException("鍙嶅鏍稿け璐�");
     }
     // 灏� ErpStaff 瀵硅薄杞崲涓� MesStaff 瀵硅薄鐨勬柟娉�
@@ -174,7 +174,7 @@
     {
         // 鍒濆鍖栬繑鍥炵殑鑱屼綅鍏宠仈鍒楄〃
         List<MesStaffPositionLink> staffDetails = new List<MesStaffPositionLink>();
- 
+
         // 妫�鏌rpStaff鏄惁鍖呭惈鑱屼綅璇︽儏鏁版嵁
         if (staff.ErpStaffDetails != null && staff.ErpStaffDetails.Count > 0)
         {
@@ -184,19 +184,19 @@
                 {
                     // 鍛樺伐ID锛氫粠ErpStaff鐨処d灞炴�ц浆鎹负decimal绫诲瀷
                     StaffId = Convert.ToDecimal(staff.Id),
-                
+
                     // 鑱屼綅ID锛�
                     // 1. 妫�鏌PostId鏄惁涓虹┖鎴杗ull
                     // 2. 闈炵┖鏃惰浆鎹负decimal锛屽惁鍒欒涓簄ull
                     PositionId = string.IsNullOrEmpty(staffDetail.fPostId.ToString())
                         ? null
                         : Convert.ToDecimal(staffDetail.fPostId),
-                    
+
                     // 鑱屼綅閮ㄩ棬ID锛氶�昏緫涓庤亴浣岻D绫讳技
                     FPostDeptId = string.IsNullOrEmpty(staffDetail.fPostDeptid.ToString())
                         ? null
                         : Convert.ToDecimal(staffDetail.fPostDeptid),
-                    
+
                     // 鍛樺伐鍏ヨ亴鏃ユ湡锛�
                     // 1. 妫�鏌StaffStartDate鏄惁涓虹┖鎴杗ull
                     // 2. 闈炵┖鏃舵寜"yyyy-MM-dd HH:mm:ss"鏍煎紡瑙f瀽涓篋ateTime锛屽惁鍒欒涓簄ull
@@ -322,13 +322,13 @@
             if (!InsertOrUpdateStaff(db, sysUser, entity, mesStaffPositionLink))
                 return false;
         }
- 
+
         return true;
     }
 }
  */
- 
- namespace MES.Service.service.BasicData;
+
+namespace MES.Service.service.BasicData;
 
 using MES.Service.DB;
 using MES.Service.Dto.webApi;
@@ -346,8 +346,10 @@
         if (unit == null) throw new ArgumentNullException(nameof(unit));
 
         var entity = GetMesStaff(unit);
+        unit.Id = entity.Id.ToString();
         var sysUser = GetUser(unit);
-        var mesStaffPositionLink = GetMesStaffPositionLink(unit);
+        //var mesStaffPositionLink = GetMesStaffPositionLink(unit);
+        List<MesStaffPositionLink> mesStaffPositionLink = null;
 
         return UseTransaction(db =>
         {
@@ -356,8 +358,17 @@
                 case "0": return UpdateStaffStatus(db, entity.Id, "A") ? 1 : 0;
                 case "1": return UpdateStaffStatus(db, entity.Id, "B") ? 1 : 0;
                 case "2":
-                case "4": return InsertOrUpdateStaff(db, new List<SysUser> { sysUser }, new List<MesStaff> { entity }, mesStaffPositionLink) ? 1 : 0;
-                case "3": return DeleteStaff(db, new List<SysUser> { sysUser }, new List<MesStaff> { entity }) ? 1 : 0;
+                case "4":
+                    return InsertOrUpdateStaff(db,
+                        new List<SysUser> { sysUser },
+                        new List<MesStaff> { entity }, mesStaffPositionLink)
+                        ? 1
+                        : 0;
+                case "3":
+                    return DeleteStaff(db, new List<SysUser> { sysUser },
+                        new List<MesStaff> { entity })
+                        ? 1
+                        : 0;
                 default: throw new ArgumentException($"涓嶆敮鎸佺殑绫诲瀷: {unit.Type}");
             }
         }) > 0;
@@ -372,53 +383,14 @@
             return false;
         }
 
-        var mesStaffList = new List<MesStaff>();
-        var sysUserList = new List<SysUser>();
-        var positionLinkList = new List<MesStaffPositionLink>();
-
-        // 杞崲鏁版嵁
-        foreach (var erpStaff in departments)
-        {
-            mesStaffList.Add(GetMesStaff(erpStaff));
-            sysUserList.Add(GetUser(erpStaff));
-            positionLinkList.AddRange(GetMesStaffPositionLink(erpStaff));
-        }
-
-        // 鎸夌被鍨嬪垎缁勫鐞�
-        var typeGroups = mesStaffList.GroupBy(s => s.Type).ToList();
-
-        return UseTransaction(db =>
-        {
-            foreach (var group in typeGroups)
-            {
-                var groupType = group.Key;
-                var groupUsers = sysUserList.Where(u => u.Type == groupType).ToList();
-
-                switch (groupType)
-                {
-                    case "0":
-                        UpdateStaffStatusBatch(db, group.ToList(), "A");
-                        break;
-                    case "1":
-                        UpdateStaffStatusBatch(db, group.ToList(), "B");
-                        break;
-                    case "2":
-                    case "4":
-                        InsertOrUpdateBatch(db, groupUsers, group.ToList(), positionLinkList);
-                        break;
-                    case "3":
-                        DeleteStaffBatch(db, groupUsers, group.ToList());
-                        break;
-                    default:
-                        throw new ArgumentException($"涓嶆敮鎸佺殑绫诲瀷: {groupType}");
-                }
-            }
-            return 1;
-        }) > 0;
+        // 閫愭潯澶勭悊锛屽叏閮ㄦ垚鍔熸墠杩斿洖true锛堜簨鍔″唴鎵归噺澶勭悊鏇翠紭锛屾澶勪繚鎸佸師鏈夐�昏緫锛�
+        var result = departments.Select(Save).ToList();
+        return result.All(b => b);
     }
 
     // 鎻掑叆鎴栨洿鏂板憳宸ワ紙鎵归噺鐗堟湰锛�
-    private bool InsertOrUpdateBatch(SqlSugarScope db, List<SysUser> sysUsers, List<MesStaff> entities, List<MesStaffPositionLink> positionLinks)
+    private bool InsertOrUpdateBatch(SqlSugarScope db, List<SysUser> sysUsers,
+        List<MesStaff> entities, List<MesStaffPositionLink> positionLinks)
     {
         if (!sysUsers.Any() || !entities.Any())
         {
@@ -430,42 +402,52 @@
         {
             // 1. 鎵归噺鎻掑叆 SysUser锛堜粎鏂板锛�
             var newSysUsers = sysUsers
-                .Where(u => !db.Queryable<SysUser>().Any(e => e.Sid == u.Sid))
+                .Where(u =>
+                    !db.Queryable<SysUser>().Any(e => e.StaffId == u.StaffId))
                 .ToList();
 
             if (newSysUsers.Any())
             {
                 Console.WriteLine($"鍑嗗鎻掑叆 {newSysUsers.Count} 涓柊SysUser");
                 var insertCount = db.Insertable(newSysUsers).ExecuteCommand();
-                
+
                 if (insertCount != newSysUsers.Count)
                 {
-                    throw new InvalidOperationException($"SysUser鎻掑叆澶辫触锛屾湡鏈涙彃鍏� {newSysUsers.Count} 鏉★紝瀹為檯鎻掑叆 {insertCount} 鏉�");
+                    throw new InvalidOperationException(
+                        $"SysUser鎻掑叆澶辫触锛屾湡鏈涙彃鍏� {newSysUsers.Count} 鏉★紝瀹為檯鎻掑叆 {insertCount} 鏉�");
                 }
-                
+
                 Console.WriteLine($"鎴愬姛鎻掑叆 {insertCount} 涓猄ysUser");
             }
 
             // 2. 鎵归噺鍒犻櫎骞舵彃鍏� MesStaff
             var staffIds = entities.Select(e => e.Id).ToList();
-            db.Deleteable<MesStaff>().Where(s => staffIds.Contains(s.Id)).ExecuteCommand();
-            
-            var staffInsertCount = db.Insertable(entities).IgnoreColumns(true).ExecuteCommand();
+            db.Deleteable<MesStaff>().Where(s => staffIds.Contains(s.Id))
+                .ExecuteCommand();
+
+            var staffInsertCount = db.Insertable(entities).IgnoreColumns(true)
+                .ExecuteCommand();
             if (staffInsertCount != entities.Count)
             {
-                throw new InvalidOperationException($"MesStaff鎻掑叆澶辫触锛屾湡鏈涙彃鍏� {entities.Count} 鏉★紝瀹為檯鎻掑叆 {staffInsertCount} 鏉�");
+                throw new InvalidOperationException(
+                    $"MesStaff鎻掑叆澶辫触锛屾湡鏈涙彃鍏� {entities.Count} 鏉★紝瀹為檯鎻掑叆 {staffInsertCount} 鏉�");
             }
 
             // 3. 澶勭悊宀椾綅鍏宠仈锛堝鏋滄湁锛�
             if (positionLinks != null && positionLinks.Any())
             {
-                var positionStaffIds = positionLinks.Select(p => p.StaffId).Distinct().ToList();
-                db.Deleteable<MesStaffPositionLink>().Where(p => positionStaffIds.Contains(p.StaffId)).ExecuteCommand();
-                
-                var positionInsertCount = db.Insertable(positionLinks).PageSize(500).IgnoreColumnsNull().ExecuteCommand();
+                var positionStaffIds = positionLinks.Select(p => p.StaffId)
+                    .Distinct().ToList();
+                db.Deleteable<MesStaffPositionLink>()
+                    .Where(p => positionStaffIds.Contains(p.StaffId))
+                    .ExecuteCommand();
+
+                var positionInsertCount = db.Insertable(positionLinks)
+                    .PageSize(500).IgnoreColumnsNull().ExecuteCommand();
                 if (positionInsertCount != positionLinks.Count)
                 {
-                    throw new InvalidOperationException($"MesStaffPositionLink鎻掑叆澶辫触锛屾湡鏈涙彃鍏� {positionLinks.Count} 鏉★紝瀹為檯鎻掑叆 {positionInsertCount} 鏉�");
+                    throw new InvalidOperationException(
+                        $"MesStaffPositionLink鎻掑叆澶辫触锛屾湡鏈涙彃鍏� {positionLinks.Count} 鏉★紝瀹為檯鎻掑叆 {positionInsertCount} 鏉�");
                 }
             }
 
@@ -475,52 +457,57 @@
         {
             Console.WriteLine($"鎵归噺鎻掑叆/鏇存柊澶辫触: {ex.Message}");
             Console.WriteLine($"SQL: {db.Ado.SqlExecutionTime}");
-            
+
             // 璁板綍瀹屾暣鐨勯敊璇爢鏍�
             Console.WriteLine($"鍫嗘爤璺熻釜: {ex.StackTrace}");
-            
+
             // 璁板綍澶辫触鐨勬暟鎹�
             if (sysUsers != null && sysUsers.Any())
             {
-                Console.WriteLine($"澶辫触鐨凷ysUser鏁版嵁: {string.Join(", ", sysUsers.Select(u => u.Account))}");
+                Console.WriteLine(
+                    $"澶辫触鐨凷ysUser鏁版嵁: {string.Join(", ", sysUsers.Select(u => u.Account))}");
             }
-            
+
             throw;
         }
     }
 
     // 鎻掑叆鎴栨洿鏂板憳宸ワ紙鍏煎鏃х増鏈紝璋冪敤鎵归噺鏂规硶锛�
-    private bool InsertOrUpdateStaff(SqlSugarScope db, List<SysUser> sysUsers, List<MesStaff> entities, List<MesStaffPositionLink> positionLinks)
+    private bool InsertOrUpdateStaff(SqlSugarScope db, List<SysUser> sysUsers,
+        List<MesStaff> entities, List<MesStaffPositionLink> positionLinks)
     {
         return InsertOrUpdateBatch(db, sysUsers, entities, positionLinks);
     }
 
     // 鎵归噺鏇存柊鍛樺伐鐘舵��
-    private bool UpdateStaffStatusBatch(SqlSugarScope db, List<MesStaff> staffList, string status)
+    private bool UpdateStaffStatusBatch(SqlSugarScope db,
+        List<MesStaff> staffList, string status)
     {
         if (!staffList.Any())
         {
             Console.WriteLine("璀﹀憡: 鍛樺伐鍒楄〃涓虹┖锛岃烦杩囩姸鎬佹洿鏂�");
             return false;
         }
-        
+
         var ids = staffList.Select(s => s.Id).ToList();
         var updateCount = db.Updateable<MesStaff>()
             .SetColumns(s => s.FforbidStatus == status)
             .Where(s => ids.Contains(s.Id))
             .ExecuteCommand();
-            
+
         if (updateCount <= 0)
         {
-            throw new InvalidOperationException($"鏇存柊鍛樺伐鐘舵�佸け璐ワ紝鐘舵��: {status}锛屽奖鍝嶈鏁�: {updateCount}");
+            throw new InvalidOperationException(
+                $"鏇存柊鍛樺伐鐘舵�佸け璐ワ紝鐘舵��: {status}锛屽奖鍝嶈鏁�: {updateCount}");
         }
-        
+
         Console.WriteLine($"鎴愬姛鏇存柊 {updateCount} 涓憳宸ョ姸鎬佷负 {status}");
         return true;
     }
 
     // 鎵归噺鍒犻櫎鍛樺伐
-    private bool DeleteStaffBatch(SqlSugarScope db, List<SysUser> sysUsers, List<MesStaff> entities)
+    private bool DeleteStaffBatch(SqlSugarScope db, List<SysUser> sysUsers,
+        List<MesStaff> entities)
     {
         if (!sysUsers.Any() || !entities.Any())
         {
@@ -532,18 +519,21 @@
         {
             // 1. 鍒犻櫎 SysUser
             var userAccounts = sysUsers.Select(u => u.Account).ToList();
-            var userDeleteCount = db.Deleteable<SysUser>().Where(u => userAccounts.Contains(u.Account)).ExecuteCommand();
-            
+            var userDeleteCount = db.Deleteable<SysUser>()
+                .Where(u => userAccounts.Contains(u.Account)).ExecuteCommand();
+
             // 2. 鍒犻櫎 MesStaff
             var staffIds = entities.Select(e => e.Id).ToList();
-            var staffDeleteCount = db.Deleteable<MesStaff>().Where(s => staffIds.Contains(s.Id)).ExecuteCommand();
-            
+            var staffDeleteCount = db.Deleteable<MesStaff>()
+                .Where(s => staffIds.Contains(s.Id)).ExecuteCommand();
+
             // 3. 鍒犻櫎宀椾綅鍏宠仈 锛氫娇鐢ˋny鏂规硶锛堟帹鑽愶級
             db.Deleteable<MesStaffPositionLink>()
                 .Where(p => staffIds.Any(id => id == p.StaffId))
                 .ExecuteCommand();
 
-            Console.WriteLine($"鎴愬姛鍒犻櫎 {userDeleteCount} 涓猄ysUser鍜� {staffDeleteCount} 涓狹esStaff");
+            Console.WriteLine(
+                $"鎴愬姛鍒犻櫎 {userDeleteCount} 涓猄ysUser鍜� {staffDeleteCount} 涓狹esStaff");
             return userDeleteCount > 0 && staffDeleteCount > 0;
         }
         catch (Exception ex)
@@ -555,7 +545,8 @@
     }
 
     // 鏇存柊鍛樺伐鐘舵��
-    private bool UpdateStaffStatus(SqlSugarScope db, decimal staffId, string status)
+    private bool UpdateStaffStatus(SqlSugarScope db, decimal staffId,
+        string status)
     {
         var result = db.Updateable<MesStaff>()
             .SetColumns(s => s.FforbidStatus == status)
@@ -564,14 +555,16 @@
 
         if (result <= 0)
         {
-            throw new InvalidOperationException($"鏇存柊鍛樺伐鐘舵�佸け璐ワ紝鍛樺伐ID: {staffId}锛岀姸鎬�: {status}");
+            throw new InvalidOperationException(
+                $"鏇存柊鍛樺伐鐘舵�佸け璐ワ紝鍛樺伐ID: {staffId}锛岀姸鎬�: {status}");
         }
 
         return true;
     }
 
     // 鍒犻櫎鍛樺伐锛堣皟鐢ㄦ壒閲忔柟娉曪級
-    private bool DeleteStaff(SqlSugarScope db, List<SysUser> sysUsers, List<MesStaff> entities)
+    private bool DeleteStaff(SqlSugarScope db, List<SysUser> sysUsers,
+        List<MesStaff> entities)
     {
         return DeleteStaffBatch(db, sysUsers, entities);
     }
@@ -585,8 +578,8 @@
         {
             var entity = new MesStaff
             {
-                Id = string.IsNullOrEmpty(staff.Id) 
-                    ? DateTimeOffset.UtcNow.ToUnixTimeSeconds() 
+                Id = string.IsNullOrEmpty(staff.Id)
+                    ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
                     : Convert.ToDecimal(staff.Id),
                 StaffNo = staff.FStaffNumber,
                 StaffName = staff.FName,
@@ -608,7 +601,8 @@
 
             if (!string.IsNullOrEmpty(staff.FStaffStartDate))
             {
-                entity.StartDate = DateTime.ParseExact(staff.FStaffStartDate, "yyyy-MM-dd HH:mm:ss", null);
+                entity.StartDate = DateTime.ParseExact(staff.FStaffStartDate,
+                    "yyyy-MM-dd HH:mm:ss", null);
             }
 
             var mesStaff = Db.Queryable<MesStaff>()
@@ -625,7 +619,8 @@
         catch (Exception ex)
         {
             Console.WriteLine($"杞崲ErpStaff鍒癕esStaff澶辫触: {ex.Message}");
-            Console.WriteLine($"杈撳叆鏁版嵁: {staff.Id}, {staff.FStaffNumber}, {staff.FName}");
+            Console.WriteLine(
+                $"杈撳叆鏁版嵁: {staff.Id}, {staff.FStaffNumber}, {staff.FName}");
             throw;
         }
     }
@@ -639,7 +634,7 @@
         {
             return new SysUser
             {
-                Sid = Convert.ToDecimal(staff.Id), // 纭繚Sid涓庡憳宸D涓�鑷�
+                StaffId = staff.Id, // 纭繚Sid涓庡憳宸D涓�鑷�
                 IsStatus = true,
                 Account = staff.FStaffNumber,
                 UserName = staff.FName,
@@ -652,7 +647,8 @@
         catch (Exception ex)
         {
             Console.WriteLine($"杞崲ErpStaff鍒癝ysUser澶辫触: {ex.Message}");
-            Console.WriteLine($"杈撳叆鏁版嵁: {staff.Id}, {staff.FStaffNumber}, {staff.FName}");
+            Console.WriteLine(
+                $"杈撳叆鏁版嵁: {staff.Id}, {staff.FStaffNumber}, {staff.FName}");
             throw;
         }
     }
@@ -666,21 +662,30 @@
         {
             var staffDetails = new List<MesStaffPositionLink>();
 
-            if (staff.ErpStaffDetails != null && staff.ErpStaffDetails.Count > 0)
+            if (staff.ErpStaffDetails != null &&
+                staff.ErpStaffDetails.Count > 0)
             {
                 staffDetails = staff.ErpStaffDetails.Select(staffDetail =>
                     new MesStaffPositionLink
                     {
                         StaffId = Convert.ToDecimal(staff.Id),
-                        PositionId = string.IsNullOrEmpty(staffDetail.fPostId?.ToString()) 
-                            ? null 
-                            : Convert.ToDecimal(staffDetail.fPostId),
-                        FPostDeptId = string.IsNullOrEmpty(staffDetail.fPostDeptid?.ToString()) 
-                            ? null 
-                            : Convert.ToDecimal(staffDetail.fPostDeptid),
-                        FStaffStartDate = string.IsNullOrEmpty(staffDetail.fStaffStartDate?.ToString()) 
-                            ? null 
-                            : DateTime.ParseExact(staffDetail.fStaffStartDate, "yyyy-MM-dd HH:mm:ss", null)
+                        PositionId =
+                            string.IsNullOrEmpty(
+                                staffDetail.fPostId?.ToString())
+                                ? null
+                                : Convert.ToDecimal(staffDetail.fPostId),
+                        FPostDeptId =
+                            string.IsNullOrEmpty(staffDetail.fPostDeptid
+                                ?.ToString())
+                                ? null
+                                : Convert.ToDecimal(staffDetail.fPostDeptid),
+                        FStaffStartDate =
+                            string.IsNullOrEmpty(staffDetail.fStaffStartDate
+                                ?.ToString())
+                                ? null
+                                : DateTime.ParseExact(
+                                    staffDetail.fStaffStartDate,
+                                    "yyyy-MM-dd HH:mm:ss", null)
                     }).ToList();
             }
 

--
Gitblit v1.9.3