From 8d771316d68f1523bcdf30c97d3a55107cb5ae56 Mon Sep 17 00:00:00 2001
From: 啊鑫 <t2856754968@163.com>
Date: 星期四, 04 九月 2025 10:53:31 +0800
Subject: [PATCH] 111

---
 MES.Service/service/BasicData/MesStaffManager.cs |   81 +++++++++++++++++++++++++++-------------
 1 files changed, 55 insertions(+), 26 deletions(-)

diff --git a/MES.Service/service/BasicData/MesStaffManager.cs b/MES.Service/service/BasicData/MesStaffManager.cs
index b9276bd..102faab 100644
--- a/MES.Service/service/BasicData/MesStaffManager.cs
+++ b/MES.Service/service/BasicData/MesStaffManager.cs
@@ -346,8 +346,6 @@
         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);
         List<MesStaffPositionLink> mesStaffPositionLink = null;
 
@@ -359,19 +357,54 @@
                 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;
+                    // return InsertOrUpdateStaff(db,
+                    //     new List<SysUser> { sysUser },
+                    //     new List<MesStaff> { entity }, mesStaffPositionLink)
+                    //     ? 1
+                    //     : 0;
+                    return InsertUser(db, entity) ? 1 : 0;
                 case "3":
-                    return DeleteStaff(db, new List<SysUser> { sysUser },
-                        new List<MesStaff> { entity })
-                        ? 1
-                        : 0;
+                    // return DeleteStaff(db, new List<SysUser> { sysUser },
+                    //     new List<MesStaff> { entity })
+                    //     ? 1
+                    //     : 0;
+                    return DeleteStaff(db, entity) ? 1 : 0;
                 default: throw new ArgumentException($"涓嶆敮鎸佺殑绫诲瀷: {unit.Type}");
             }
         }) > 0;
+    }
+
+    private bool InsertUser(SqlSugarScope db, MesStaff entity)
+    {
+        db.Deleteable<MesStaff>()
+            .Where(s => s.Id == entity.Id)
+            .ExecuteCommand();
+
+        db.Deleteable<SysUser>()
+            .Where(s => s.StaffId == entity.Id.ToString())
+            .ExecuteCommand();
+
+        var sysUser = GetUser(entity);
+        if (entity.Id == null)
+        {
+            var id = db.Insertable<MesStaff>(entity).ExecuteReturnIdentity();
+            sysUser.StaffId = id.ToString();
+        }
+
+        return db.Insertable(sysUser).ExecuteCommand() > 0;
+    }
+
+    private bool DeleteStaff(SqlSugarScope db, MesStaff entity)
+    {
+        var executeCommand = db.Deleteable<MesStaff>()
+            .Where(s => s.Id == entity.Id)
+            .ExecuteCommand();
+
+        db.Deleteable<SysUser>()
+            .Where(s => s.StaffId == entity.Id.ToString())
+            .ExecuteCommand();
+
+        return executeCommand > 0;
     }
 
     // 淇濆瓨澶氫釜鍛樺伐璁板綍
@@ -545,7 +578,7 @@
     }
 
     // 鏇存柊鍛樺伐鐘舵��
-    private bool UpdateStaffStatus(SqlSugarScope db, decimal staffId,
+    private bool UpdateStaffStatus(SqlSugarScope db, decimal? staffId,
         string status)
     {
         var result = db.Updateable<MesStaff>()
@@ -578,9 +611,9 @@
         {
             var entity = new MesStaff
             {
-                Id = string.IsNullOrEmpty(staff.Id)
-                    ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
-                    : Convert.ToDecimal(staff.Id),
+                // Id = string.IsNullOrEmpty(staff.Id)
+                //     ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
+                //     : Convert.ToDecimal(staff.Id),
                 StaffNo = staff.FStaffNumber,
                 StaffName = staff.FName,
                 DepartmentName = staff.FPostDept,
@@ -626,30 +659,26 @@
     }
 
     // 灏� ErpStaff 瀵硅薄杞崲涓� SysUser 瀵硅薄
-    private SysUser GetUser(ErpStaff staff)
+    private SysUser GetUser(MesStaff entity)
     {
-        if (staff == null) throw new ArgumentNullException(nameof(staff));
+        if (entity == null) throw new ArgumentNullException(nameof(entity));
 
         try
         {
             return new SysUser
             {
-                StaffId = staff.Id, // 纭繚Sid涓庡憳宸D涓�鑷�
+                StaffId = entity.Id.ToString(), // 纭繚Sid涓庡憳宸D涓�鑷�
                 IsStatus = true,
-                Account = staff.FStaffNumber,
-                UserName = staff.FName,
+                Account = entity.StaffNo,
+                UserName = entity.StaffName,
                 Password = "E1ADC3949BA59ABBE56E057F2F883E", // 鍒濆瀵嗙爜
-                DepartNo = staff.FPostDept,
-                Type = staff.Type,
+                DepartNo = entity.DepartmentName,
                 CreateTime = DateTime.Now
             };
         }
         catch (Exception ex)
         {
-            Console.WriteLine($"杞崲ErpStaff鍒癝ysUser澶辫触: {ex.Message}");
-            Console.WriteLine(
-                $"杈撳叆鏁版嵁: {staff.Id}, {staff.FStaffNumber}, {staff.FName}");
-            throw;
+            throw new NotImplementedException(ex.Message);
         }
     }
 

--
Gitblit v1.9.3