sjz
22 小时以前 74ab9465837f8a6f9eae854059e9783eff24e173
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
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using SqlSugar;
 
namespace MES.Service.service.BasicData;
 
public class MesStaffManager : Repository<MesStaff>
{
    // Save 方法用于保存单个员工记录,根据类型执行不同的操作
    public bool Save(ErpStaff unit)
    {
        var entity = GetMesStaff(unit); // 将 ErpStaff 转换为 MesStaff
 
        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, entity)) // 插入或更新员工
                        return 1;
                    break;
                case "3":
                    if (UpdateStaffStatus(db, entity.Id, "B")) // 禁用员工
                        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();
        return true;
    }
 
    // 插入或更新员工的方法
    private bool InsertOrUpdateStaff(SqlSugarScope db, MesStaff entity)
    {
        var exists = db.Queryable<MesStaff>().Any(e => e.Id == entity.Id);
        if (exists)
        {
            var update = db.Updateable(entity).ExecuteCommand();
            return true;
        }
        else
        {
            var insert = db.Insertable(entity).ExecuteCommand();
            if (insert > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
 
    // 将 ErpStaff 对象转换为 MesStaff 对象的方法
    private MesStaff GetMesStaff(ErpStaff staff)
    {
        var entity = new MesStaff
        {
            Id = Convert.ToDecimal(staff.Id),
            StaffNo = staff.FStaffNumber,
            StaffName = staff.FName,
            DepartmentNo = staff.FPostDept,
            PositionCode = staff.FPost,
            PhoneNumber = staff.FMobile,
            CreateOrg = Convert.ToDecimal(staff.FCreateOrgId),
            UseOrg = Convert.ToDecimal(staff.FUseOrgId),
            CreateDate = DateTime.Now,
            Remark = staff.FDescription,
            FforbidStatus = staff.FForbidStatus
        };
 
        if (staff.FStaffStartDate != null)
            entity.StartDate = DateTime.ParseExact(staff.FStaffStartDate,
                "yyyy-MM-dd HH:mm:ss", null);
 
        return entity;
    }
 
    // SaveList 方法用于保存多个员工记录,根据类型批量执行不同的操作
    public bool SaveList(List<ErpStaff> staffs)
    {
        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)
                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 "2":
                        if (!InsertStaffBatch(db, staffGroup.Value)) // 批量插入员工
                            throw new NotImplementedException("插入失败");
                        break;
                    case "3":
                        if (!UpdateStaffStatusBatch(db, staffGroup.Value, "B")) // 批量禁用员工
                            throw new NotImplementedException("删除失败");
                        break;
                    case "4":
                        if (!InsertOrUpdateBatch(db,
                                staffGroup.Value)) // 批量插入或更新员工
                            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();
        return true;
        
    }
 
    // 批量插入员工的方法
    private bool InsertStaffBatch(SqlSugarScope db, List<MesStaff> staffList)
    {
        foreach (var entity in staffList)
        {
            if (!InsertOrUpdateStaff(db, entity))
            {
                return false;
            }
        }
        return true;
    }
 
    // 批量插入或更新员工的方法
    private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesStaff> staffList)
    {
        foreach (var entity in staffList)
        {
            if (!InsertOrUpdateStaff(db, entity))
            {
                return false;
            }
        }
        return true;
    }
}