啊鑫
2025-01-24 bdbfc1045c94e23c74cf9c6f98b0b4f9b0ca0d00
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
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using SqlSugar;
 
namespace MES.Service.service.BasicData;
 
public class SysDepartmentManager : Repository<SysDepartment>
{
    // 当前类已经继承了 Repository 增、删、查、改的方法
 
    // Save 方法用于保存单个部门记录,根据类型执行不同的操作
    public bool Save(ErpDepartment department)
    {
        var entity =
            GetSysDepartment(department); // 将 ErpDepartment 转换为 SysDepartment
 
        return UseTransaction(db =>
        {
            switch (department.Type)
            {
                case "0":
                    if (UpdateDepartmentStatus(db, entity.Id, "A")) // 启用部门
                        return 1;
                    break;
                case "1":
                    if (UpdateDepartmentStatus(db, entity.Id, "B")) // 禁用部门
                        return 1;
                    break;
                //case "2":
                //    if (InsertDepartment(db, entity)) // 插入新部门
                //        return 1;
                //    break;
                case "3":
                    if (DeleteDepartment(db, entity.Id)) // 删除部门
                        return 1;
                    break;
                case "2":
                case "4":
                    if (InsertOrUpdateDepartment(db, entity)) // 插入或更新部门
                        return 1;
                    break;
                default:
                    throw new ArgumentNullException(
                        $"type没有{department.Type}这个类型的参数");
            }
 
            throw new NotImplementedException("操作失败");
        }) > 0;
    }
 
    // 更新部门状态的方法
    private bool UpdateDepartmentStatus(SqlSugarScope db, decimal departmentId,
        string status)
    {
        var result = db.Updateable<SysDepartment>()
            .SetColumns(s => s.Depextr4 == status)
            .Where(s => s.Id == departmentId).ExecuteCommand();
 
        if (result > 0)
            return true;
 
        throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败");
    }
 
    // 插入新部门的方法
    private bool InsertDepartment(SqlSugarScope db, SysDepartment entity)
    {
        var insert = db.Insertable(entity).ExecuteCommand();
        if (insert > 0)
            return true;
 
        throw new NotImplementedException("插入失败");
    }
 
    // 删除部门的方法
    private bool DeleteDepartment(SqlSugarScope db, decimal departmentId)
    {
        var deleteById = db.Deleteable<SysDepartment>().In(departmentId)
            .ExecuteCommand();
        if (deleteById > 0)
            return true;
 
        throw new NotImplementedException("删除失败");
    }
    
    ///新代码
    // 插入或更新部门的方法
    private bool InsertOrUpdateDepartment(SqlSugarScope db,
        SysDepartment entity)
    {
        db.Deleteable<SysDepartment>()
            .Where(s => s.Id == entity.Id)
            .ExecuteCommand();
 
        var insert = db.Insertable(entity).ExecuteCommand();
        return insert > 0;
    }
 
    private SysDepartment GetSysDepartment(ErpDepartment department)
    {
        return new SysDepartment
        {
            Departmentcode = department.FNumber,
            Departmentname = department.FName,
            Departmentid = Convert.ToDecimal(department.Id),
            Id = Convert.ToDecimal(department.Id),
            Depextr1 = department.FDeptProperty,
            Depextr2 = department.FGroup,
            Depextr3 = department.FWIPStockID,
            Depextr4 = department.FForbidStatus,
            CreateDate = DateTime.Now,
            CreateOrg = Convert.ToDecimal(department.FCreateOrgId),
            UseOrg = Convert.ToDecimal(department.FUseOrgId),
            ParentId = department.FParentID,
            Company = "1000",
            Factory = "1000"
        };
    }
 
    // SaveList 方法用于保存多个部门记录,根据类型批量执行不同的操作
    public bool SaveList(List<ErpDepartment> departments)
    {
        var result = departments.Select(Save).ToList();
        return result.All(b => b);
    }
}