啊鑫
2024-10-22 e08a2e8a24be0996dec3b681f3c4ab45333f331c
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
using Castle.Core.Resource;
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
 
namespace MES.Service.service.BasicData;
 
public class MesItemTypeManager : Repository<MesItemType>
{
    //当前类已经继承了 Repository 增、删、查、改的方法
    public bool Save(ErpItemType customer)
    {
        var entity = GetMesItemType(customer);
 
        try
        {
            switch (customer.Type)
            {
                case "0":
                    return InsertItemType(entity);
                case "1":
                    return DeleteItemType(entity.Id);
                default:
                    throw new ArgumentNullException(
                        $"type没有{customer.Type}这个类型的参数");
            }
        }
        catch (Exception ex)
        {
            throw new ApplicationException($"操作失败: {ex.Message}", ex);
        }
    }
 
    private bool InsertItemType(MesItemType entity)
    {
        var insert = base.Insert(entity);
        if (insert)
            return true;
 
        throw new NotImplementedException("插入失败");
    }
 
    private bool DeleteItemType(decimal id)
    {
        var deleteById = base.DeleteById(id);
        if (deleteById)
            return true;
 
        throw new NotImplementedException("删除失败");
    }
 
    private MesItemType GetMesItemType(ErpItemType department)
    {
        return new MesItemType
        {
            Id = Convert.ToDecimal(department.Id),
            Pgroup = department.FPARENTID,
            Tcode = department.FNumber,
            Tname = department.FName,
            Description = department.FDescription,
            //FSubsidiary = department.FSubsidiary,
            //Fumbrella = department.Fumbrella,
            CreateDate = DateTime.Now,
            LastupdateDate = DateTime.Now,
            Company = "1000",
            Factory = "1000"
        };
    }
 
    public bool SaveList(List<ErpItemType> departments)
    {
        var list = new List<MesItemType>();
        departments.ForEach(s =>
        {
            var entity = GetMesItemType(s);
            entity.Type = s.Type;
            list.Add(entity);
        });
 
        var groupBy = list.GroupBy(s => s.Type)
            .ToDictionary(g => g.Key, g => g.ToList());
        var result = new List<bool>();
 
        foreach (var itemTypeGroup in groupBy)
            try
            {
                switch (itemTypeGroup.Key)
                {
                    case "0":
                        result.Add(InsertItemTypeBatch(itemTypeGroup.Value));
                        break;
                    case "1":
                        result.Add(DeleteItemTypeBatch(itemTypeGroup.Value));
                        break;
                    default:
                        throw new ArgumentNullException(
                            $"type没有{itemTypeGroup.Key}这个类型的参数");
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException($"批量操作失败: {ex.Message}", ex);
            }
 
        if (result.All(b => b))
            return true;
 
        throw new NotImplementedException("接口执行失败");
    }
 
    private bool InsertItemTypeBatch(List<MesItemType> itemTypeList)
    {
        var insertRange = base.InsertRange(itemTypeList);
        if (insertRange)
            return true;
 
        throw new NotImplementedException("插入失败");
    }
 
    private bool DeleteItemTypeBatch(List<MesItemType> itemTypeList)
    {
        var ids = itemTypeList.Select(it => it.Id).ToArray();
 
        var deleteByIds = Db.Deleteable<MesItemType>()
            .Where(s => ids.Contains(s.Id)).ExecuteCommand();
 
        if (deleteByIds > 0)
            return true;
 
        throw new NotImplementedException("删除失败");
    }
}