啊鑫
2025-08-27 eab13f60bbdc8ea275c6dd7b6424cdfc7769f6e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
namespace MES.Service.service.BasicData;
 
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using SqlSugar;
 
public class MesStaffManager : Repository<MesStaff>
{
    // Save 方法用于保存单个员工记录,根据类型执行不同的操作
    public bool Save(ErpStaff unit)
    {
        var entity = GetMesStaff(unit); // 将 ErpStaff 转换为 MesStaff
        var sysUser = GetUser(unit); // 获取 SysUser 实例
        var mesStaffPositionLink =  GetMesStaffPositionLink(unit); // 获取 MesStaffPositionLink 实例
        return UseTransaction(db =>
        {
            switch (unit.Type)
            {
                case "0":
                    if (UpdateStaffStatus(db, entity.Id, "A")) // 启用员工
                        return 1;
                    break;
                case "1":
                    if (UpdateStaffStatus(db, entity.Id, "B")) // 禁用员工
                        return 1;
                    break;
                case "2":
                case "4":
                    if (InsertOrUpdateStaff(db, sysUser, entity,mesStaffPositionLink)) // 插入或更新员工
                        return 1;
                    break;
                case "3":
                    if (DeleteStaff(db, sysUser, entity)) // 删除员工
                        return 1;
                    break;
                default:
                    throw new ArgumentNullException(
                        $"type没有{unit.Type}这个类型的参数");
            }
 
            throw new NotImplementedException("操作失败");
        }) > 0;
    }
 
    // 更新员工状态的方法
    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" ? "启用失败" : "禁用失败");
    }
 
    // 插入或更新员工的方法
    private bool InsertOrUpdateStaff(SqlSugarScope db, SysUser sysUser,
        MesStaff entity, List<MesStaffPositionLink> mesStaffPositionLink)
    {
        var exists = db.Queryable<SysUser>().Any(u => u.Sid == sysUser.Sid);
        if (!exists)
        {
            var insertUser = db.Insertable(sysUser).ExecuteCommand();
        }
        db.Deleteable<MesStaff>()
            .Where(s => s.Id == entity.Id)
            .ExecuteCommand();
 
        var insertStaff =
            db.Insertable(entity).IgnoreColumns(true).ExecuteCommand();
 
        var insertPosition = 1;
        if (mesStaffPositionLink != null && mesStaffPositionLink.Count > 0)
        {
            //先删除,再新增
            db.Deleteable<MesStaffPositionLink>()
                .Where(s =>
                    mesStaffPositionLink.Any(m => m.StaffId == s.StaffId))
                .ExecuteCommand();
 
            //批量插入时,忽略空字段
            insertPosition = db.Insertable(mesStaffPositionLink).PageSize(1)
                .IgnoreColumnsNull().ExecuteCommand();
        }
 
 
        return insertStaff > 0 && insertPosition > 0;
    }
 
    // 删除员工的方法
 
 
    private bool DeleteStaff(SqlSugarScope db, SysUser sysUser, MesStaff entity)
    {
        var deleteUser = db.Deleteable<SysUser>()
            .Where(s => s.Sid == sysUser.Sid).ExecuteCommand();
        if (deleteUser > 0)
        {
            var deleteStaff =
                db.Deleteable<MesStaff>()
                    .Where(s =>
                        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 对象的方法
    private MesStaff GetMesStaff(ErpStaff staff)
    {
        var entity = new MesStaff
        {
            Id = Convert.ToDecimal(staff.Id),
            StaffNo = staff.FStaffNumber,
            StaffName = staff.FName,
            DepartmentName = staff.FPostDept,
            PositionCode = staff.FPostId,
            PhoneNumber = staff.FMobile,
            Remark = staff.FDescription,
            FforbidStatus = staff.FForbidStatus,
            FSubsidiary = staff.FUseOrgId,
            Fumbrella = staff.FCreateOrgId,
            CreateDate = DateTime.Now,
            LastupdateDate = DateTime.Now,
            Type = staff.Type
        };
 
        if (staff.FStaffStartDate != null)
            entity.StartDate = DateTime.ParseExact(staff.FStaffStartDate,
                "yyyy-MM-dd HH:mm:ss", null);
 
        return entity;
    }
 
    // 将 ErpStaff 对象转换为 SysUser 对象的方法
    private SysUser GetUser(ErpStaff staff)
    {
        return new SysUser
        {
            IsStatus = true,
            Account = staff.FStaffNumber,
            UserName = staff.FName,
            Password = "E1ADC3949BA59ABBE56E057F2F883E", // 初始密码
            DepartNo = staff.FPostDept,
            Type = staff.Type,
            CreateTime = DateTime.Now
        };
    }
 
    /// <summary>
    ///     接口字段调整:根据json格式,在MesStaffPositionLink<List>中存入数据。
    /// </summary>
    /// <remarks>
    ///     修改人:池南骏
    ///     修改日期:2025-01-02 ——》 2025-01-05 二次修改
    ///     修改说明:
    ///     - 新增代码
    /// </remarks>
    private List<MesStaffPositionLink> GetMesStaffPositionLink(ErpStaff staff)
    {
        // 初始化返回的职位关联列表
        List<MesStaffPositionLink> staffDetails = new List<MesStaffPositionLink>();
 
        // 检查ErpStaff是否包含职位详情数据
        if (staff.ErpStaffDetails != null && staff.ErpStaffDetails.Count > 0)
        {
            // 使用LINQ将ErpStaffDetails转换为MesStaffPositionLink对象
            staffDetails = staff.ErpStaffDetails.Select(staffDetail =>
                new MesStaffPositionLink
                {
                    // 员工ID:从ErpStaff的Id属性转换为decimal类型
                    StaffId = Convert.ToDecimal(staff.Id),
 
                    // 职位ID:
                    // 1. 检查fPostId是否为空或null
                    // 2. 非空时转换为decimal,否则设为null
                    PositionId = string.IsNullOrEmpty(staffDetail.fPostId.ToString())
                        ? null
                        : Convert.ToDecimal(staffDetail.fPostId),
 
                    // 职位部门ID:逻辑与职位ID类似
                    FPostDeptId = string.IsNullOrEmpty(staffDetail.fPostDeptid.ToString())
                        ? null
                        : Convert.ToDecimal(staffDetail.fPostDeptid),
 
                    // 员工入职日期:
                    // 1. 检查fStaffStartDate是否为空或null
                    // 2. 非空时按"yyyy-MM-dd HH:mm:ss"格式解析为DateTime,否则设为null
                    FStaffStartDate = string.IsNullOrEmpty(staffDetail.fStaffStartDate.ToString())
                        ? null
                        : DateTime.ParseExact(staffDetail.fStaffStartDate, "yyyy-MM-dd HH:mm:ss", null)
                }).ToList();
        }
        // 返回转换后的职位关联列表
        return staffDetails;
    }
    // SaveList 方法用于保存多个员工记录,根据类型批量执行不同的操作
    public bool SaveList(List<ErpStaff> departments)
    {
        var list = new List<MesStaff>();
        var userList = new List<SysUser>();
        var mesStaffPositionLinkList = new List<MesStaffPositionLink>();
        departments.ForEach(s =>
        {
            var entity = GetMesStaff(s);
            entity.Type = s.Type;
            list.Add(entity);
 
            var sysUser = GetUser(s);
            sysUser.Type = s.Type;
            userList.Add(sysUser);
            var mesStaffPositionLink =
                GetMesStaffPositionLink(s); // 获取 SysUser 实例
            mesStaffPositionLinkList.AddRange(mesStaffPositionLink);
        });
 
        var groupBy = list.GroupBy(s => s.Type)
            .ToDictionary(g => g.Key, g => g.ToList());
        return UseTransaction(db =>
        {
            foreach (var staffGroup in groupBy)
                switch (staffGroup.Key)
                {
                    case "0":
                        if (!UpdateStaffStatusBatch(db, staffGroup.Value,
                                "A")) // 批量启用员工
                            throw new NotImplementedException("启用失败");
                        break;
                    case "1":
                        if (!UpdateStaffStatusBatch(db, staffGroup.Value,
                                "B")) // 批量禁用员工
                            throw new NotImplementedException("禁用失败");
                        break;
                    case "3":
                        if (!DeleteStaffBatch(db, userList,
                                staffGroup.Value)) // 批量删除员工
                            throw new NotImplementedException("删除失败");
                        break;
                    case "2":
                    case "4":
                        if (!InsertOrUpdateBatch(db, userList,
                                staffGroup.Value,mesStaffPositionLinkList)) // 批量插入或更新员工
                            throw new NotImplementedException("同步失败");
                        break;
                    default:
                        throw new ArgumentNullException(
                            $"type没有{staffGroup.Key}这个类型的参数");
                }
 
            return 1;
        }) > 0;
    }
 
    // 批量更新员工状态的方法
    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" ? "启用失败" : "禁用失败");
    }
 
    // 批量插入员工的方法
    private bool InsertStaffBatch(SqlSugarScope db, List<SysUser> userList,
        List<MesStaff> staffList)
    {
        var userInsert = userList.FindAll(s => s.Type == "2");
        var executeCommand = db.Insertable(userInsert).ExecuteCommand();
        if (executeCommand <= 0) throw new ArgumentNullException("审核失败");
        if (db.Insertable(staffList).ExecuteCommand() > 0)
            return true;
 
        throw new ArgumentNullException("审核失败");
    }
 
    // 批量删除员工的方法
    private bool DeleteStaffBatch(SqlSugarScope db, List<SysUser> userList,
        List<MesStaff> staffList)
    {
        var sid = userList.FindAll(s => s.Type == "3").Select(s => s.Account)
            .ToArray();
        var result = db.Deleteable<SysUser>()
            .Where(s => sid.Contains(s.Account)).ExecuteCommand();
 
        if (result <= 0) throw new ArgumentNullException("反审核失败");
        var ids = staffList.Select(it => it.Id).ToArray();
        if (db.Deleteable<MesStaff>()
                .Where(s => ids.Contains(s.Id)).ExecuteCommand() > 0)
            return true;
 
        throw new ArgumentNullException("反审核失败");
    }
 
    // 批量插入或更新员工的方法
    private bool InsertOrUpdateBatch(SqlSugarScope db, List<SysUser> userList,
        List<MesStaff> staffList,
        List<MesStaffPositionLink> mesStaffPositionLink)
    {
        foreach (var sysUser in userList)
        {
            var entity = staffList.First(s => s.Id == sysUser.Sid);
            if (!InsertOrUpdateStaff(db, sysUser, entity, mesStaffPositionLink))
                return false;
        }
 
        return true;
    }
}
 */
 
namespace MES.Service.service.BasicData;
 
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
 
public class MesStaffManager : Repository<MesStaff>
{
    // 保存单个员工记录
    public bool Save(ErpStaff unit)
    {
        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;
 
        return UseTransaction(db =>
        {
            switch (unit.Type)
            {
                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;
                default: throw new ArgumentException($"不支持的类型: {unit.Type}");
            }
        }) > 0;
    }
 
    // 保存多个员工记录
    public bool SaveList(List<ErpStaff> departments)
    {
        if (departments == null || !departments.Any())
        {
            Console.WriteLine("警告: 传入的员工列表为空");
            return false;
        }
 
        // 逐条处理,全部成功才返回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)
    {
        if (!sysUsers.Any() || !entities.Any())
        {
            Console.WriteLine("警告: SysUser或MesStaff列表为空,跳过插入操作");
            return false;
        }
 
        try
        {
            // 1. 批量插入 SysUser(仅新增)
            var newSysUsers = sysUsers
                .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} 条");
                }
 
                Console.WriteLine($"成功插入 {insertCount} 个SysUser");
            }
 
            // 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();
            if (staffInsertCount != entities.Count)
            {
                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();
                if (positionInsertCount != positionLinks.Count)
                {
                    throw new InvalidOperationException(
                        $"MesStaffPositionLink插入失败,期望插入 {positionLinks.Count} 条,实际插入 {positionInsertCount} 条");
                }
            }
 
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"批量插入/更新失败: {ex.Message}");
            Console.WriteLine($"SQL: {db.Ado.SqlExecutionTime}");
 
            // 记录完整的错误堆栈
            Console.WriteLine($"堆栈跟踪: {ex.StackTrace}");
 
            // 记录失败的数据
            if (sysUsers != null && sysUsers.Any())
            {
                Console.WriteLine(
                    $"失败的SysUser数据: {string.Join(", ", sysUsers.Select(u => u.Account))}");
            }
 
            throw;
        }
    }
 
    // 插入或更新员工(兼容旧版本,调用批量方法)
    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)
    {
        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}");
        }
 
        Console.WriteLine($"成功更新 {updateCount} 个员工状态为 {status}");
        return true;
    }
 
    // 批量删除员工
    private bool DeleteStaffBatch(SqlSugarScope db, List<SysUser> sysUsers,
        List<MesStaff> entities)
    {
        if (!sysUsers.Any() || !entities.Any())
        {
            Console.WriteLine("警告: SysUser或MesStaff列表为空,跳过删除操作");
            return false;
        }
 
        try
        {
            // 1. 删除 SysUser
            var userAccounts = sysUsers.Select(u => u.Account).ToList();
            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();
 
            // 3. 删除岗位关联 :使用Any方法(推荐)
            db.Deleteable<MesStaffPositionLink>()
                .Where(p => staffIds.Any(id => id == p.StaffId))
                .ExecuteCommand();
 
            Console.WriteLine(
                $"成功删除 {userDeleteCount} 个SysUser和 {staffDeleteCount} 个MesStaff");
            return userDeleteCount > 0 && staffDeleteCount > 0;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"批量删除失败: {ex.Message}");
            Console.WriteLine($"SQL: {db.Ado.SqlExecutionTime}");
            throw;
        }
    }
 
    // 更新员工状态
    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)
        {
            throw new InvalidOperationException(
                $"更新员工状态失败,员工ID: {staffId},状态: {status}");
        }
 
        return true;
    }
 
    // 删除员工(调用批量方法)
    private bool DeleteStaff(SqlSugarScope db, List<SysUser> sysUsers,
        List<MesStaff> entities)
    {
        return DeleteStaffBatch(db, sysUsers, entities);
    }
 
    // 将 ErpStaff 对象转换为 MesStaff 对象
    private MesStaff GetMesStaff(ErpStaff staff)
    {
        if (staff == null) throw new ArgumentNullException(nameof(staff));
 
        try
        {
            var entity = new MesStaff
            {
                Id = string.IsNullOrEmpty(staff.Id)
                    ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
                    : Convert.ToDecimal(staff.Id),
                StaffNo = staff.FStaffNumber,
                StaffName = staff.FName,
                DepartmentName = staff.FPostDept,
                PositionCode = staff.FPostId,
                PhoneNumber = staff.FMobile,
                Remark = staff.FDescription,
                FforbidStatus = staff.FForbidStatus,
                FSubsidiary = string.IsNullOrEmpty(staff.FUseOrgId)
                    ? "1"
                    : staff.FUseOrgId,
                Fumbrella = string.IsNullOrEmpty(staff.FCreateOrgId)
                    ? "1"
                    : staff.FCreateOrgId,
                CreateDate = DateTime.Now,
                LastupdateDate = DateTime.Now,
                Type = staff.Type
            };
 
            if (!string.IsNullOrEmpty(staff.FStaffStartDate))
            {
                entity.StartDate = DateTime.ParseExact(staff.FStaffStartDate,
                    "yyyy-MM-dd HH:mm:ss", null);
            }
 
            var mesStaff = Db.Queryable<MesStaff>()
                .Where(s => s.StaffNo == entity.StaffNo)
                .First();
 
            if (mesStaff != null)
            {
                entity.Id = mesStaff.Id;
            }
 
            return entity;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"转换ErpStaff到MesStaff失败: {ex.Message}");
            Console.WriteLine(
                $"输入数据: {staff.Id}, {staff.FStaffNumber}, {staff.FName}");
            throw;
        }
    }
 
    // 将 ErpStaff 对象转换为 SysUser 对象
    private SysUser GetUser(ErpStaff staff)
    {
        if (staff == null) throw new ArgumentNullException(nameof(staff));
 
        try
        {
            return new SysUser
            {
                StaffId = staff.Id, // 确保Sid与员工ID一致
                IsStatus = true,
                Account = staff.FStaffNumber,
                UserName = staff.FName,
                Password = "E1ADC3949BA59ABBE56E057F2F883E", // 初始密码
                DepartNo = staff.FPostDept,
                Type = staff.Type,
                CreateTime = DateTime.Now
            };
        }
        catch (Exception ex)
        {
            Console.WriteLine($"转换ErpStaff到SysUser失败: {ex.Message}");
            Console.WriteLine(
                $"输入数据: {staff.Id}, {staff.FStaffNumber}, {staff.FName}");
            throw;
        }
    }
 
    // 获取员工岗位关联列表
    private List<MesStaffPositionLink> GetMesStaffPositionLink(ErpStaff staff)
    {
        if (staff == null) return new List<MesStaffPositionLink>();
 
        try
        {
            var staffDetails = new List<MesStaffPositionLink>();
 
            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)
                    }).ToList();
            }
 
            return staffDetails;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"转换岗位关联数据失败: {ex.Message}");
            Console.WriteLine($"员工ID: {staff.Id}");
            throw;
        }
    }
}