如洲 陈
2 天以前 21eaaa8218e1389babb60d75e6870659abed992e
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using AngleSharp.Dom;
using MES.Service.DB;
using MES.Service.Dto.webApi;
using MES.Service.Modes;
using SqlSugar;
using System.Data;
using System.Globalization;
using DbType = System.Data.DbType;
 
namespace MES.Service.service;
 
public class WomcaaManager : Repository<Womcaa>
{
    //当前类已经继承了 Repository 增、删、查、改的方法
 
    //这里面写的代码不会给覆盖,如果要重新生成请删除 WomcaaManager.cs
 
    private readonly WomcabManager _womcabManager = new();
 
    public bool SaveList(List<ErpWOM> rohIns)
    {
        var result = rohIns.Select(Save).ToList();
        return result.All(b => b);
    }
 
    public bool Save(ErpWOM wom)
    {
        var womErpCaa = wom. ErpCaa;
        var mesWomcaa = MapErpCAAtoWomcaa(womErpCaa);
        var mesWomcabs =  MapErpCABtoWomcab(wom.ErpCabs);
 
        return UseTransaction(db =>
        {
            switch (womErpCaa.Type)
            {
                case "3":
                    return UpdateData(db, mesWomcaa, mesWomcabs) ? 1 : 0;
                case "2":
                case "4":
                    return SaveOrUpdateData(db, mesWomcaa, mesWomcabs)
                        ? 1
                        : 0;
                default:
                    throw new NotImplementedException(
                        $"type没有{womErpCaa.Type}这个类型");
            }
        }) > 0;
    }
 
    private bool SaveOrUpdateData(SqlSugarScope db, Womcaa mesWomcaa,
        List<Womcab> mesWomcabs)
    {
 
        
                if (mesWomcaa.Caa001 != null && mesWomcaa.SrcBillType != null)
                {
 
                    db.Deleteable<Womcaa>()
                   .Where(it => it.Caa001 == mesWomcaa.Caa001 &&
                                it.SrcBillType == mesWomcaa.SrcBillType)
                   .ExecuteCommand();
 
                };
 
 
            if (mesWomcabs.Count > 0)
            {
 
           // var mesWomcab = mesWomcabs.Select(s => new { CAB001 = s.Cab001, CAB002 = s.Cab002, CAB003 = s.Cab003 }).ToList();
            var mesWomcab = mesWomcabs.Select(s => new { CAB001 = s.Cab001, CAB002 = s.Cab002}).ToList();
 
 
            /*db.Deleteable<Womcab>()
                .Where(it => mesWomcab
                .Any(p => p.CAB001 == it.Cab001 
                && p.CAB002 == it.Cab002 
                && p.CAB003 == it.Cab003))
                .ExecuteCommand();*/
            db.Deleteable<Womcab>()
                .Where(it => mesWomcab
                .Any(p => p.CAB001 == it.Cab001
                && p.CAB002 == it.Cab002))
                .ExecuteCommand();
 
        };
           
 
            var orUpdate = db.Insertable(mesWomcaa).ExecuteCommand();
            var baOrUpdate = db.Insertable(mesWomcabs).ExecuteCommand();
 
    
            //定义输入参数
            var inputParam1 = new SugarParameter("P_WORK_NO", mesWomcaa.Caa001);
            var inputParam2 = new SugarParameter("P_WORK_TYPE", mesWomcaa.SrcBillType);
            // 定义输出参数
            var outParam1 = new SugarParameter("c_Result",null,true);
            var outParam2 = new SugarParameter("C_MSG",null,true);
 
        
 
            // 使用 SqlSugar 执行存储过程
            Db.Ado.ExecuteCommand("BEGIN PRC_UPDATE_DAA(:P_WORK_NO,:P_WORK_TYPE,:c_Result,:C_MSG); END;", inputParam1, inputParam2, outParam1, outParam2);
 
 
 
            // 获取输出参数的值
       
              int     result = int.Parse((string)outParam1.Value);
              string  message = outParam2.Value == DBNull.Value ? string.Empty : (string)outParam2.Value;
 
 
        if (result == 1)
            {
                //存储过程失败则事务进行回滚
                db.Ado.RollbackTran();
                throw new Exception(message);   
              
            }
 
            // 提交事务
            db.Ado.CommitTran();
            return true;
        
 
            throw new NotImplementedException("插入或更新失败");
 
 
    }
    
    private bool UpdateData(SqlSugarScope db, Womcaa mesWomcaa,
        List<Womcab> mesWomcabs)
    {
        //根据单别和单号进行删除
        var update = db.Deleteable<Womcaa>()
        .Where(it => it.Caa001 == mesWomcaa.Caa001 &&
                     it.SrcBillType == mesWomcaa.SrcBillType)
        .ExecuteCommand() > 0;
 
 
        //var mesWomcab = mesWomcabs.Select(s => new { CAB001 = s.Cab001, CAB002 = s.Cab002, CAB003 = s.Cab003 }).ToList();
        var mesWomcab = mesWomcabs.Select(s => new { CAB001 = s.Cab001, CAB002 = s.Cab002}).ToList();
 
 
 
        //var insertOrUpdate = db.Deleteable<Womcab>().Where(it => mesWomcab.Any(p => p.CAB001 == it.Cab001 && p.CAB002 == it.Cab002 && p.CAB003 == it.Cab003)).ExecuteCommand() > 0;
        var insertOrUpdate = db.Deleteable<Womcab>().Where(it => mesWomcab.Any(p => p.CAB001 == it.Cab001 && p.CAB002 == it.Cab002)).ExecuteCommand() > 0;
 
 
        if (update && insertOrUpdate) return true;
        throw new NotImplementedException("更新失败");
    }
 
    private Womcaa MapErpCAAtoWomcaa(ErpCAA dto)
    {
 
 
        string DemandDate = !string.IsNullOrEmpty(dto.DemandDate) ?  DateTime.ParseExact(dto.DemandDate, "yyyymmdd", CultureInfo.InvariantCulture).ToString("yyyy-mm-dd") : null;
 
        var entity = new Womcaa
        {
            SrcBillType = dto.FSrcBillType,
            Caa001 = dto.FBillNo,
            Caa021 = dto.FWorkShopID0,
          
            DepotCode = dto.FStockId,
            CreateDate = dto.FDate,
            Caa004 = dto.FBillType,
          
            Caa006 = dto.FMaterialId,
            Caa009 = dto.FUnitId,
            Caa012 = !string.IsNullOrEmpty(dto.FQty)
                ? Convert.ToDecimal(dto.FQty)
                : null,
          
            Caa010 = dto.FPlanStartDate,
            Caa011 = dto.FPlanFinishDate,
            Caa005 = dto.FConveyDate,
         
            Lot = dto.FLot,
          
            Caa015 = dto.F_UNW_Text_xsddh,
            CAA015_Head = dto.xsddh_type,
 
 
            Caa016 = dto.FMemoItem,
            Caa023 = dto.FStatus,
            Suppno = dto.FSuppno,
            Cust_no = dto.FCustno,
            Cust_item_no = dto.FCustitemno,
            DemandDate   = DemandDate,
            CustNumber   = dto.CustNumber,
            ModelType    = dto.ModelType
 
 
 
        };
 
       
 
        return entity;
    }
 
    private List<Womcab> MapErpCABtoWomcab(List<ErpCAB> dtoList)
    {
        var womcabList = new List<Womcab>();
 
        foreach (var dto in dtoList)
        {
            var womcab = new Womcab
            {
               
                Cab001 = dto.FBillNo,
                Cab002 = dto.FBillNoType,
                Cab003 = dto.FMaterialID2,
                Cab006 = !string.IsNullOrEmpty(dto.FNeedQty2)
                    ? Convert.ToDecimal(dto.FNeedQty2)
                    : null,
                Cab007 = !string.IsNullOrEmpty(dto.FPickedQty)
                    ? Convert.ToDecimal(dto.FPickedQty)
                    : null,
                PositionNo = dto.FPositionNO,
                DepotCode = dto.FStockID.Trim(),
                IssueType = dto.FIssueType,
                Cab008 = dto.F_UNW_Text_tpgy,
                Cab009 = dto.FUnitID,
                SupplyType = dto.FSupplyType,
                Cab014 = dto.FIsKeyItem,
                Numerator = dto.FNumerator,
                Denominator = dto.FDenominator
              
            };
 
          
 
            womcabList.Add(womcab);
        }
 
        return womcabList;
    }
 
 
    public bool Delete(YFDelete data)
    {
        if (data == null)
            throw new ArgumentNullException(nameof(data));
 
        if (string.IsNullOrWhiteSpace(data.FBillNo))
            throw new ArgumentException("FBillNo 不能为空", nameof(data.FBillNo));
 
        if (string.IsNullOrWhiteSpace(data.FBillTypeID))
            throw new ArgumentException("FBillTypeID 不能为空", nameof(data.FBillTypeID));
 
        return UseTransaction(db =>
        {
            // 删除主表数据
            var deleteMain = db.Deleteable<Womcaa>()
                .Where(it => it.Caa001 == data.FBillNo && it.SrcBillType == data.FBillTypeID)
                .ExecuteCommand() > 0;
 
            // 删除子表数据
            var deleteDetail = db.Deleteable<Womcab>()
                .Where(it => it.Cab001 == data.FBillNo && it.Cab002 == data.FBillTypeID)
                .ExecuteCommand() > 0;
 
            if (!deleteMain || !deleteDetail)
                throw new Exception("删除失败:主表或子表记录不存在");
 
            // 调用存储过程进行后续处理
            var inputParam1 = new SugarParameter("P_WORK_NO", data.FBillNo);
            var inputParam2 = new SugarParameter("P_WORK_TYPE", data.FBillTypeID);
            var outParam1 = new SugarParameter("C_RESULT", null, true); // 输出参数
            var outParam2 = new SugarParameter("C_MSG", null, true);   // 输出参数
 
            db.Ado.ExecuteCommand(
                "BEGIN PRC_DELETE_DAA(:P_WORK_NO, :P_WORK_TYPE, :C_RESULT, :C_MSG); END;",
                inputParam1, inputParam2, outParam1, outParam2);
 
            int result = int.Parse((string)outParam1.Value);
            string message = outParam2.Value == DBNull.Value ? string.Empty : (string)outParam2.Value;
 
            if (result == 1)
            {
                //存储过程失败则事务进行回滚
                db.Ado.RollbackTran();
                throw new Exception(message);
 
            }
 
            return 1;
        }) > 0;
    }
 
 
}