zjh
2025-06-26 da3b2cfb3a34a7b1f83dcd9fc762b1d2459547dc
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using MES.Service.DB;
using MES.Service.Dto.service;
using MES.Service.Modes;
 
namespace MES.Service.service.Warehouse;
 
public class MesInvItemOutsManager : Repository<MesInvItemOuts>
{
    private readonly int BILL_TYPE_ID = 200;
    private readonly int TRANSACTION_NO = 203;
 
    public bool ReturnRequest(ItemOut oItemOut)
    {
        var itemOutFrom = oItemOut.from;
        var itemOutLists = oItemOut.items;
 
        // 根据AsnNo和MesNo对明细进行分组
        var groupedItems = itemOutLists
            .GroupBy(item => new {   item.MesNo, item.SqNo })
            .ToList();
 
        bool result = true;
        foreach (var group in groupedItems)
        {
            //if (group.Key.AsnNo == null)
            //{
            //    throw new NotImplementedException("AsnNo不能为空");
            //}
 
            if (group.Key.MesNo == null)
            {
                throw new NotImplementedException("入库单单号不能为空");
            }
 
            if (group.Key.SqNo == null)
            {
                throw new NotImplementedException("退货申请单行号不能为空");
            }
 
            // 创建一个临时的itemOutFrom对象,使用分组的Key作为主要属性
            var tempItemOutFrom = new ItemOutFrom
            {
                
                MesNo = group.Key.MesNo,
                SqNo = group.Key.SqNo,
                // 继承原始itemOutFrom的其他属性
                RtnNo = itemOutFrom.RtnNo,
                Type = itemOutFrom.Type,
                CreateBy = itemOutFrom.CreateBy,
                FMRMODE = itemOutFrom.FMRMODE,
                DepotId = itemOutFrom.DepotId,
                SupperId = itemOutFrom.SupperId
            };
 
            // 根据Type执行不同的逻辑
            switch (tempItemOutFrom.Type)
            {
                case "1":
                {
                    var mesInvItemOuts = Db.Queryable<MesInvItemOuts>()
                        .Where(s => s.BillTypeId == BILL_TYPE_ID
                                    && s.TransactionNo == TRANSACTION_NO
                                    && s.ItemOutNo == tempItemOutFrom.RtnNo
                                  
                        )
                        .Count();
 
                    if (mesInvItemOuts > 0)
                    {
                        throw new NotImplementedException(
                            tempItemOutFrom.RtnNo +
                            "的退料申请单已经存在");
                    }
 
                    // 为当前分组保存数据
                    var groupResult = Save(tempItemOutFrom, group.ToList());
                    if (!groupResult)
                    {
                        result = false;
                    }
 
                    break;
                }
                case "4":
                    var removeResult = Remove(tempItemOutFrom);
                    if (!removeResult)
                    {
                        result = false;
                    }
 
                    break;
                default:
                    result = false;
                    break;
            }
        }
 
        return result;
    }
 
    private bool Save(ItemOutFrom from, List<ItemOutList> items)
    {
        var mesInvItemIns = Db.Queryable<MesInvItemIns>()
            .Where(s => s.BillTypeId == 100
                        && s.TransctionNo == "101"
                        && s.BillNo == from.MesNo
            ).First();
 
        if (mesInvItemIns == null)
        {
            throw new NotImplementedException("采购入库不存在");
        }
 
        var mesDepots = Db.Queryable<MesDepots>()
            .Where(s => s.DepotId == Decimal.Parse(from.DepotId)).First();
 
        var mesLinkU9 = Db.Queryable<MesLinkU9>()
            .Where(s => s.TableType == "MES_SUPPLIER"
                        && s.U9Id == from.SupperId).First();
 
        if (mesLinkU9 == null)
        {
            throw new NotImplementedException("供应商ID不存在或未同步于U9");
        }
 
        var mesSupplier = Db.Queryable<MesSupplier>()
            .Where(s => s.Id == Decimal.Parse(mesLinkU9.MesId))
            .First();
 
        if (mesDepots == null)
        {
            throw new NotImplementedException("[" + from.DepotId +
                                              "]仓库不存在,请同步给MES");
        }
 
        if (mesSupplier == null)
        {
            throw new NotImplementedException("[" + from.SupperId +
                                              "]供应商不存在,请同步给MES");
        }
 
        var nextSequenceValue =
            Db.Ado.SqlQuery<decimal>("SELECT SEQ_OUT_ID.NEXTVAL FROM DUAL")
                .First();
 
        // var billCode =
        //     Db.Ado.SqlQuery<string>(
        //             "SELECT GETBILLCODE1('1000', '1000', 'TLSQ') FROM DUAL")
        //         .First();
        var billCode = from.RtnNo;
 
        // 创建采购退料单记录
        var mesInvItemOuts = new MesInvItemOuts
        {
            Id = nextSequenceValue,
            ItemOutNo = billCode,
            Status = 0,
            CreateBy = "PL017",
            CreateDate = DateTime.Now,
            BillTypeId = 200,
            TransactionNo = 203,
            DepotCode = mesDepots.DepotCode,
            FType = 0,
            OutStatus = 0,
            IsVisual = 1,
            Factory = "1000",
            Company = "1000",
            SuppNo = mesSupplier.SuppNo,
            ItemFlag = 0,
            BoardFlag = 0,
            OutType = "采购退料",
            Nflag = 0,
            Fmrmode = from.FMRMODE,
            Sapno = from.SqNo,
            Organizeid = "1002503270000079",
        };
 
        // 创建采购退料单记录
        var mesInvItemOutItems = new List<MesInvItemOutItems>();
 
        foreach (var itemOutList in items)
        {
            // 检查必要字段是否为空
            if (string.IsNullOrEmpty(itemOutList.SrcDocNo))
            {
                throw new NotImplementedException("采购订单号不能为空");
            }
 
            if (string.IsNullOrEmpty(itemOutList.SrcDocLineNo))
            {
                throw new NotImplementedException("采购订单行号不能为空");
            }
 
            //if (string.IsNullOrEmpty(itemOutList.AsnLineNo))
            //{
            //    throw new NotImplementedException("ASN行号不能为空");
            //}
 
            if (string.IsNullOrEmpty(itemOutList.itemId))
            {
                throw new NotImplementedException("物料ID不能为空");
            }
 
            var mesRohInData = Db.Queryable<MesRohInData>()
                .Where(s => s.BillNo == itemOutList.SrcDocNo
                            && s.OrderLineId == itemOutList.SrcDocLineNo)
                .First();
 
            if (mesRohInData == null)
            {
                throw new NotImplementedException("采购订单不存在");
            }
 
            //var deliveryDetail = Db.Queryable<DeliveryDetail>()
            //    .Where(a =>  Int32.Parse(a.ZzitemId) ==
            //                Int32.Parse(itemOutList.AsnLineNo))
            //    .Count();
 
            //if (deliveryDetail <= 0)
            //{
            //    throw new NotImplementedException("[" + from.AsnNo + "]的明细行[" +
            //                                      itemOutList.AsnLineNo +
            //                                      "]不存在");
            //}
 
            var itemIdLinkU9 = Db.Queryable<MesLinkU9>()
                .Where(s => s.TableType == "MES_ITEMS"
                            && s.U9Id == itemOutList.itemId).First();
 
            if (mesLinkU9 == null)
            {
                throw new NotImplementedException("供应商ID不存在或未同步于U9");
            }
 
            var mesItems = Db.Queryable<MesItems>()
                .Where(s => s.Id == Decimal.Parse(itemIdLinkU9.MesId))
                .First();
 
            if (mesItems == null)
            {
                throw new NotImplementedException("[" + itemOutList.itemId +
                                                  "]物料不存在,请同步给MES");
            }
 
            var mesInvItemInCItems = Db.Queryable<MesInvItemInCItems>()
                .Where(s => s.ItemInId == mesInvItemIns.Id
                            && s.ItemNo == mesItems.ItemNo
                            && s.Ebeln == itemOutList.SrcDocNo
                            && s.EbelnLineNo ==
                            Decimal.Parse(itemOutList.SrcDocLineNo)
                            && s.SuppNo == mesSupplier.SuppNo
                            ).First();
 
            if (mesInvItemInCItems == null)
            {
                throw new NotImplementedException("没有对应的入库明细");
            }
 
 
            // 确保CbillNo不为空
            if (string.IsNullOrEmpty(mesInvItemIns.CbillNo))
            {
                throw new NotImplementedException("入库单关联的采购单号不能为空");
            }
 
            mesInvItemOutItems.Add(new MesInvItemOutItems
            {
                ItemOutId = nextSequenceValue,
                ItemNo = mesItems.ItemNo,
                Quantity = Decimal.Parse(itemOutList.qty),
                CreateBy = "PL017",
                CreateDate = DateTime.Now,
                Factory = "1000",
                Company = "1000",
                DepotCode = mesDepots.DepotCode,
                WorkNo = itemOutList.SrcDocNo, // 确保WorkNo有值
                WorkLine =
                    Decimal.Parse(itemOutList.SrcDocLineNo), // 确保WorkLine有值
                EbelnK3id = Decimal.Parse(mesRohInData.ErpId),
                LineK3id = Decimal.Parse(mesRohInData.EbelnK3id),
                FType = 0,
                Status = 0,
                PbillNo = mesInvItemIns.CbillNo, // 确保PbillNo有值
                RkNo = from.MesNo, // 确保RkNo有值
                RkLine = mesInvItemInCItems.Id, // 确保RkLine有值
                RkQty = mesInvItemInCItems.Quantity,
                TlQty = 0,
                ItemId = Decimal.Parse(itemIdLinkU9.MesId), // 确保ItemId有值
                //SqNo = itemOutList.SqNo, // 确保ItemId有值
                //ZzitemId = itemOutList.AsnLineNo, // 确保ItemId有值
                // Unit = item.Unit,
            });
        }
 
        var outItemCommand = Db.Insertable(mesInvItemOutItems)
            .PageSize(1).IgnoreColumnsNull().ExecuteCommand();
        if (outItemCommand <= 0)
        {
            throw new Exception("创建采购退料单子表失败");
        }
 
        // 插入采购退料单记录
        var insertResult = Db.Insertable(mesInvItemOuts).IgnoreColumns(true)
            .ExecuteCommand();
        if (insertResult <= 0)
        {
            throw new Exception("创建采购退料单失败");
        }
 
        return outItemCommand + insertResult >= 2;
    }
 
    private bool Remove(ItemOutFrom from)
    {
        // 查找要删除的采购退料单
        var mesInvItemOuts = Db.Queryable<MesInvItemOuts>()
            .Where(s => s.BillTypeId == BILL_TYPE_ID
                        && s.TransactionNo == TRANSACTION_NO
                        && s.ItemOutNo == from.RtnNo).ToList();
 
        if (mesInvItemOuts == null || mesInvItemOuts.Count == 0)
        {
            throw new NotImplementedException("找不到对应的采购退料单: " + from.RtnNo);
        }
 
        // 检查是否有已审核的单据,如果存在已审核(Status=1)则不允许删除
        if (mesInvItemOuts.Any(item => item.Status == 1))
        {
            throw new NotImplementedException("存在已审核的采购退料单,不允许删除");
        }
 
        // 删除所有相关单据
        foreach (var itemOut in mesInvItemOuts)
        {
            // 查找要删除的采购退料单明细
            var mesInvItemOutItems = Db.Queryable<MesInvItemOutItems>()
                .Where(s => s.ItemOutId == itemOut.Id)
                .ToList();
 
            if (mesInvItemOutItems == null || mesInvItemOutItems.Count == 0)
            {
                throw new NotImplementedException(
                    $"找不到采购退料单[{itemOut.ItemOutNo}]对应的明细");
            }
 
            // 删除采购退料单明细
            var deleteItemsResult = Db.Deleteable<MesInvItemOutItems>()
                .Where(s => s.ItemOutId == itemOut.Id)
                .ExecuteCommand();
 
            if (deleteItemsResult <= 0)
            {
                throw new Exception($"删除采购退料单[{itemOut.ItemOutNo}]明细失败");
            }
 
            // 删除采购退料单
            var deleteResult = Db.Deleteable<MesInvItemOuts>()
                .Where(s => s.Id == itemOut.Id)
                .ExecuteCommand();
 
            if (deleteResult <= 0)
            {
                throw new Exception($"删除采购退料单[{itemOut.ItemOutNo}]失败");
            }
        }
 
        return true;
    }
}