zjh
4 天以前 661e24b717604dfc5c0f1c6385931943c2a9192e
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
using MES.Service.DB;
using MES.Service.Dto.service;
using MES.Service.Modes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace MES.Service.service.Warehouse
{
    public class MesItemQtManager:Repository<MesItemQt>
    {
        public bool MiscellaneousIssues(MesItemQtDto mesItemQtDto)
        {
            var itemQtFrom = mesItemQtDto.from;
            var itemQtLists = mesItemQtDto.items;
 
            // 根据AsnNo和MesNo对明细进行分组
            //var groupedItems = itemOutLists
            //    .GroupBy(item => new { item.AsnNo, item.MesNo, item.SqNo })
            //    .ToList();
 
            bool result = true;
            //foreach (var group in itemQtRKLists)
            //{
            //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
            //{
            //    AsnNo = group.Key.AsnNo,
            //    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 (itemQtFrom.Type)
            {
                case "1":
                    {
                        var mesInvItemOuts = Db.Queryable<MesItemQt>()
                            .Where(s => s.Qtck == itemQtFrom.ApplyNumber
 
                            )
                            .Count();
 
                        if (mesInvItemOuts > 0)
                        {
                            throw new NotImplementedException(
                                itemQtFrom.ApplyNumber +
                                "的杂发申请单已经存在");
                        }
 
                        // 为当前分组保存数据
                        var groupResult = Save(itemQtFrom, itemQtLists);
                        if (!groupResult)
                        {
                            result = false;
                        }
 
                        break;
                    }
                case "4":
                    var removeResult = Remove(itemQtFrom);
                    if (!removeResult)
                    {
                        result = false;
                    }
 
                    break;
                default:
                    result = false;
                    break;
            }
            //}
 
            return result;
        }
 
 
        private bool Save(MesItemQtFrom from, List<MesItemQtList> 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("采购入库不存在");
            //}
 
 
            string SuppNo = "";
            if (from.SupplierCode != "" && from.SupplierCode != null)
            {
                var mesLinkU9 = Db.Queryable<MesLinkU9>()
              .Where(s => s.TableType == "MES_SUPPLIER"
                          && s.U9Id == from.SupplierCode).First();
 
                if (mesLinkU9 == null)
                {
                    throw new NotImplementedException("供应商ID不存在或未同步于U9");
                }
 
                var mesSupplier = Db.Queryable<MesSupplier>()
                    .Where(s => s.Id == Decimal.Parse(mesLinkU9.MesId))
                    .First();
                if (mesSupplier == null)
                {
                    throw new NotImplementedException("[" + from.SupplierCode +
                                                      "]供应商不存在,请同步给MES");
                }
                SuppNo = mesSupplier.SuppNo;
            }
            var mesDepots = Db.Queryable<MesDepots>()
                .Where(s => s.DepotId == Decimal.Parse(from.FromWarehouse)).First();
 
            if (mesDepots == null)
            {
                throw new NotImplementedException("[" + from.FromWarehouse +
                                                  "]仓库不存在,请同步给MES");
            }
 
            var sysDepartment = Db.Queryable<SysDepartment>()
              .Where(s => s.Departmentid == Decimal.Parse(from.ApplyDept)).First();
 
            if (sysDepartment == null)
            {
                throw new NotImplementedException("[" + from.ApplyDept +
                                                  "]部门不存在,请同步给MES");
            }
 
 
            var nextSequenceValue =
                Db.Ado.SqlQuery<decimal>("SELECT SEQ_QTCK.NEXTVAL FROM DUAL")
                    .First();
 
            // var billCode =
            //     Db.Ado.SqlQuery<string>(
            //             "SELECT GETBILLCODE1('1000', '1000', 'TLSQ') FROM DUAL")
            //         .First();
            //var billCode = from.RtnNo;
 
            // 创建杂发申请单记录
            var MesItemQtrk = new MesItemQt
            {
                Id = nextSequenceValue,
                Qtck = from.ApplyNumber,
                Qt001 = from.Equals(null) ? DateTime.Now : from.ApplyDate,
                Qt002 = from.Equals(null) ? DateTime.Now : from.AuditDate,
                Qt003 = from.DocumentType,
                Qt004 = from.StockOutType,
                Qt005 = from.CreatedBy,
                Qt006 = from.WorkOrder,
                Qt009 = sysDepartment.Departmentname,
                Qt012 = sysDepartment.Departmentcode,
                Qt010 = from.Reason,
                Qt011 = mesDepots.DepotCode,
                Qt008 = mesDepots.DepotName,
 
                Qt015 = 1,
                Qt016 = from.Auditor,
                Qt017 = from.TaskNumber,
                Qt018 = SuppNo,
                Qt019 = from.Direction,
                Qt020 = from.OwnerType,
                CustomerCode = from.CustomerCode,
                Receiver = from.Receiver,
                Department = from.Department,
                StockKeeper = from.StockKeeper,
                SourceType = from.SourceType,
                SourceDocument = from.SourceDocument,
                ProductionRelated = from.ProductionRelated,
                ProductionLine = from.ProductionLine,
                ProductionActivity = from.ProductionActivity,
                FinishedGoods = from.FinishedGoods,
                ReversePosting = from.ReversePosting,
                AccountingPeriod = from.AccountingPeriod,
                Employee = from.Employee,
                FalseReturn = from.FalseReturn,
                ProjectCode = from.ProjectCode,
                TaskCode = from.TaskCode,
                Status = from.Status,
                OwnerOrganization = from.OwnerOrganization
 
 
            };
 
            // 创建杂发申请单行记录
            var mesItemQtDetail = new List<MesItemQtDatall>();
 
            foreach (var ItemQtRKList in items)
            {
                // 检查必要字段是否为空
                //if (string.IsNullOrEmpty(itemOutList.SrcDocNo))
                //{
                //    throw new NotImplementedException("采购订单号不能为空");
                //}
 
 
 
                //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 => a.Zzasn == from.AsnNo
                //                && 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 == ItemQtRKList.ItemId).First();
 
                if (itemIdLinkU9 == 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("[" + ItemQtRKList.ItemId +
                                                      "]物料不存在,请同步给MES");
                }
                var mesDepot = "";
                if (ItemQtRKList.StorageLocation != null && ItemQtRKList.StorageLocation != null)
                {
                    mesDepots = Db.Queryable<MesDepots>()
                   .Where(s => s.DepotId == Decimal.Parse(ItemQtRKList.StorageLocation)).First();
                    if (mesDepots == null)
                    {
                        throw new NotImplementedException("[" + ItemQtRKList.StorageLocation +
                                                          "]仓库不存在,请同步给MES");
                    }
                    mesDepot = mesDepots.DepotCode;
                }
                string supplierCode = "";
                if (ItemQtRKList.SupplierCode != null && ItemQtRKList.SupplierCode != "")
                {
                    var mesLinkU9 = Db.Queryable<MesLinkU9>()
   .Where(s => s.TableType == "MES_SUPPLIER"
               && s.U9Id == ItemQtRKList.SupplierCode).First();
 
                    if (mesLinkU9 == null)
                    {
                        throw new NotImplementedException("[" + ItemQtRKList.SupplierCode +
                                                          "]供应商不存在,请同步给MES");
                    }
                    var mesSupplier = Db.Queryable<MesSupplier>()
        .Where(s => s.Id == Decimal.Parse(mesLinkU9.MesId))
        .First();
                    if (mesSupplier == null)
                    {
                        throw new NotImplementedException("[" + ItemQtRKList.SupplierCode +
                                                          "]供应商不存在,请同步给MES");
                    }
                    supplierCode = mesSupplier.SuppNo;
                }
 
 
                //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
                //                && Convert.ToInt32(s.ZzitemId) ==
                //                Convert.ToInt32(itemOutList.AsnLineNo)).First();
 
                //if (mesInvItemInCItems == null)
                //{
                //    throw new NotImplementedException("没有对应的入库明细");
                //}
 
 
                // 确保CbillNo不为空
                if (ItemQtRKList.AppliedQuantity <= 0 || ItemQtRKList.AppliedQuantity == null)
                {
                    throw new NotImplementedException("申请数量不能为0或NULL值!");
                }
 
                mesItemQtDetail.Add(new MesItemQtDatall
                {
                    Mid = nextSequenceValue,
                    Qd002 = mesItems.ItemNo,
                    Qd003 = mesItems.ItemName,
                    Qd004 = mesItems.ItemModel,
                    Qd005 = mesItems.ColorName,
                    Qd006 = ItemQtRKList.PlannedQuantity,
                    Qd007 = ItemQtRKList.AppliedQuantity,
                    Qd008 = 0, // 已入数量默认为0
                    Qd009 = mesItems.ItemUnit,
                    Qd010 = ItemQtRKList.Remark,
                    Qd012 = mesItems.Id, // 物料内码
                    LineNumber = ItemQtRKList.LineNumber,
                    ProductionRelated = ItemQtRKList.ProductionRelated,
                    StorageType = ItemQtRKList.StorageType,
                    OwnerOrganization = ItemQtRKList.OwnerOrganization,
                    StorageLocation = mesDepot,
                    SupplierCode = supplierCode,
                    CustomerCode = ItemQtRKList.CustomerCode,
                    ProjectCode = ItemQtRKList.ProjectCode,
                    TaskCode = ItemQtRKList.TaskCode,
                    ProductionOrder = ItemQtRKList.ProductionOrder,
                    DailyPlan = ItemQtRKList.DailyPlan,
                    ProductionActivity = ItemQtRKList.ProductionActivity,
                    FinishedProduct = ItemQtRKList.FinishedProduct,
                    PickingUsage= ItemQtRKList.PickingUsage
 
 
                });
            }
 
            var outItemCommand = Db.Insertable(mesItemQtDetail)
                .PageSize(1).IgnoreColumnsNull().ExecuteCommand();
            if (outItemCommand <= 0)
            {
                throw new Exception("创建杂发单子表失败");
            }
 
            // 插入采购退料单记录
            var insertResult = Db.Insertable(MesItemQtrk).IgnoreColumns(true)
                .ExecuteCommand();
            if (insertResult <= 0)
            {
                throw new Exception("创建杂发单失败");
            }
 
            return outItemCommand + insertResult >= 2;
        }
 
        private bool Remove(MesItemQtFrom from)
        {
            // 查找要删除的杂发单
            var mesItemQtrk = Db.Queryable<MesItemQt>()
                .Where(s => s.Qtck == from.ApplyNumber
                           ).ToList();
 
            if (mesItemQtrk == null || mesItemQtrk.Count == 0)
            {
                throw new NotImplementedException("找不到对应的杂发单: " + from.ApplyNumber);
            }
 
            // 检查是否有已审核的单据,如果存在已审核(Status=1)则不允许删除
            //if (mesInvItemOuts.Any(item => item.Status == 1))
            //{
            //    throw new NotImplementedException("存在已审核的采购退料单,不允许删除");
            //}
 
            // 删除所有相关单据
            foreach (var itemRK in mesItemQtrk)
            {
                // 查找要删除的杂发单明细
                var mesInvItemOutItems = Db.Queryable<MesItemQtDatall>()
                    .Where(s => s.Mid == itemRK.Id)
                    .ToList();
 
                if (mesInvItemOutItems == null || mesInvItemOutItems.Count == 0)
                {
                    throw new NotImplementedException(
                        $"找不到杂发单[{from.ApplyNumber}]对应的明细");
                }
 
                // 删除杂发单明细
                var deleteItemsResult = Db.Deleteable<MesItemQtDatall>()
                    .Where(s => s.Mid == itemRK.Id)
                    .ExecuteCommand();
 
                if (deleteItemsResult <= 0)
                {
                    throw new Exception($"找不到杂发单[ {from.ApplyNumber}]明细,删除失败");
                }
 
                // 删除杂发单
                var deleteResult = Db.Deleteable<MesItemQt>()
                    .Where(s => s.Id == itemRK.Id)
                    .ExecuteCommand();
 
                if (deleteResult <= 0)
                {
                    throw new Exception($"删除杂发单[{from.ApplyNumber}]失败");
                }
            }
 
            return true;
        }
    }
}