啊鑫
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
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 "2":
                case "4":
                    return InsertItemType(entity);
                case "3":
                    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)
    {
        // 先根据ID删除现有记录
        try
        {
            Db.Deleteable<MesItemType>()
                .Where(it => it.Id == entity.Id).ExecuteCommand();
        }
        catch (Exception)
        {
            // 删除失败可能是因为记录不存在,继续执行插入操作
        }
 
        var insert = base.Insert(entity);
        if (insert)
            return true;
 
        throw new NotImplementedException("插入失败");
    }
 
    private bool DeleteItemType(long? id)
    {
        var deleteById = Db.Deleteable<MesItemType>()
            .Where(it => it.Id == id).ExecuteCommand();
        if (deleteById > 0)
            return true;
 
        throw new NotImplementedException("删除失败");
    }
 
    private MesItemType GetMesItemType(ErpItemType department)
    {
        var entity = new MesItemType
        {
            Id = string.IsNullOrEmpty(department.Id)
                ? DateTimeOffset.UtcNow.ToUnixTimeSeconds()
                : long.Parse(department.Id),
            Pgroup = department.FParentId,
            Tcode = department.FNumber,
            Tname = department.FName,
            Description = department.FDescription,
            CreateDate = DateTime.Now,
            LastupdateDate = DateTime.Now,
            Company = "1000",
            Factory = "1000"
        };
 
        var mesItemType = Db.Queryable<MesItemType>()
            .Where(it => it.Tcode == entity.Tcode)
            .First();
 
        if (mesItemType != null)
        {
            entity.Id = mesItemType.Id;
        }
 
        return entity;
    }
 
    public bool SaveList(List<ErpItemType> departments)
    {
        if (departments == null || !departments.Any())
        {
            Console.WriteLine("警告: 传入的列表为空");
            return false;
        }
 
        // 逐条处理,全部成功才返回true(事务内批量处理更优,此处保持原有逻辑)
        var result = departments.Select(Save).ToList();
        return result.All(b => b);
    }
 
    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("删除失败");
    }
}