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
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using SqlSugar;
 
namespace MES.Service.service.BasicData.Material;
 
/// <summary>
/// 模具号逻辑处理类
/// </summary>
public class MesMouldManager : Repository<MesMould>
{
    /// <summary>
    /// 单个保存(Type: 0启用,1禁用,2审核/新增或更新,3反审核/删除)
    /// </summary>
    public bool Save(ErpMould mould)
    {
        var entity = ConvertToEntity(mould);
 
        return UseTransaction(db =>
        {
            switch (mould.Type)
            {
                case "0":
                    if (UpdateStatus(db, entity.Code, "A")) return 1;
                    break;
                case "1":
                    if (UpdateStatus(db, entity.Code, "B")) return 1;
                    break;
                case "3":
                    if (DeleteMould(db, entity.ErpId)) return 1;
                    break;
                case "2":
                    if (InsertOrUpdate(db, entity)) return 1;
                    break;
                default:
                    throw new ArgumentNullException($"Type参数错误:{mould.Type}");
            }
            throw new NotImplementedException("操作失败");
        }) > 0;
    }
 
    /// <summary>
    /// 批量保存(带Type判断)
    /// </summary>
    public bool SaveList(List<ErpMould> moulds)
    {
        var list = moulds.Select(ConvertToEntity).ToList();
        var groupByType = list.GroupBy(s => s.Type).ToDictionary(g => g.Key, g => g.ToList());
 
        return UseTransaction(db =>
        {
            foreach (var group in groupByType)
            {
                switch (group.Key)
                {
                    case "0":
                        if (!UpdateStatusBatch(db, group.Value, "A"))
                            throw new NotImplementedException("启用失败");
                        break;
                    case "1":
                        if (!UpdateStatusBatch(db, group.Value, "B"))
                            throw new NotImplementedException("禁用失败");
                        break;
                    case "3":
                        if (!DeleteMouldBatch(db, group.Value))
                            throw new NotImplementedException("删除失败");
                        break;
                    case "2":
                        if (!InsertOrUpdateBatch(db, group.Value))
                            throw new NotImplementedException("同步失败");
                        break;
                    default:
                        throw new ArgumentNullException($"Type参数错误:{group.Key}");
                }
            }
            return 1;
        }) > 0;
    }
 
    private bool UpdateStatus(SqlSugarScope db, string code, string status)
    {
        var result = db.Updateable<MesMould>()
            .SetColumns(s => s.FForbidStatus == status)
            .Where(s => s.Code == code)
            .ExecuteCommand();
        if (result > 0) return true;
        throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败");
    }
 
    private bool UpdateStatusBatch(SqlSugarScope db, List<MesMould> moulds, string status)
    {
        var codes = moulds.Select(s => s.Code).ToArray();
        var result = db.Updateable<MesMould>()
            .SetColumns(s => s.FForbidStatus == status)
            .Where(s => codes.Contains(s.Code))
            .ExecuteCommand();
        return result > 0;
    }
 
    /// <summary>
    /// 删除单条记录(按 ErpId)
    /// </summary>
    private bool DeleteMould(SqlSugarScope db, string erpId)
    {
        var result = db.Deleteable<MesMould>().Where(s => s.ErpId == erpId).ExecuteCommand();
        return true;
    }
 
    /// <summary>
    /// 批量删除(按 ErpId)
    /// </summary>
    private bool DeleteMouldBatch(SqlSugarScope db, List<MesMould> moulds)
    {
        var erpIds = moulds.Select(s => s.ErpId).ToArray();
        var result = db.Deleteable<MesMould>().Where(s => erpIds.Contains(s.ErpId)).ExecuteCommand();
        return result > 0;
    }
 
 
    private bool InsertOrUpdate(SqlSugarScope db, MesMould entity)
    {
        //db.Deleteable<MesMould>().Where(s => s.Code == entity.Code).ExecuteCommand();
        db.Deleteable<MesMould>().Where(s => s.ErpId == entity.ErpId).ExecuteCommand();
        var insert = db.Insertable(entity).ExecuteCommand();
        return insert > 0;
    }
 
    private bool InsertOrUpdateBatch(SqlSugarScope db, List<MesMould> moulds)
    {
        foreach (var entity in moulds)
            if (!InsertOrUpdate(db, entity))
                return false;
        return true;
    }
 
    private MesMould ConvertToEntity(ErpMould m) => new MesMould
    {
        Code = m.Code,                 // 编码
        Name = m.Name,                 // 名称
        DataStatus = m.DataStatus,     // 数据状态
        OldMouldNo = m.OldMouldNo,     // 旧模具号
        Cavity = m.Cavity,             // 穴数
        Cycle = m.Cycle,               // 周期
        Capacity = m.Capacity,         // 产能
        MachineType = m.MachineType,   // 机型
        FForbidStatus = m.FForbidStatus, // 禁用状态
        ProductWeight = m.ProductWeight, // 产品单重
        MaterialHead = m.MaterialHead, // 料头
        StandardLabor = m.StandardLabor, // 标准人力
        LaborHoursPerThousand = m.LaborHoursPerThousand, // 千片人力工时
        Remark = m.Remark,             // 备注
        CreateTime = DateTime.Now,     // 时间
        ErpId = m.ErpId,               // ERP编号
        Type = m.Type                  // 操作类型
    };
}