新框架PDA后端(祈禧6月初版本)
南骏 池
3 天以前 fca0719af6948fe8fa1e4f094f8e7dba339c7428
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
using NewPdaSqlServer.DB;
using NewPdaSqlServer.Dto.service;
using NewPdaSqlServer.entity;
using NewPdaSqlServer.entity.Base;
using NewPdaSqlServer.util;
using SqlSugar;
 
namespace NewPdaSqlServer.service.Warehouse;
 
public class MesItemQtManager : Repository<MesItemQt>
{
    //当前类已经继承了 Repository 增、删、查、改的方法
    /// <summary>
    ///     获取未完成的退料单号列表
    /// </summary>
    /// <returns>退料单号列表</returns>
    public List<string> GetPendingQtList()
    {
        const string sql = @"SELECT qtck 
                        FROM MES_ITEM_QT 
                        WHERE qt015 = 1 
                          AND qt026 = 1 
                          AND QT029 = 1 
                          AND qt032 = 1
                          AND QT014 = 0  ";
 
        return Db.Ado.SqlQuery<string>(sql);
    }
 
    /// <summary>
    ///     获取待处理的退料单明细列表
    /// </summary>
    /// <param name="query">仓库查询参数,包含用户名和单据号</param>
    /// <returns>待处理的退料单明细列表</returns>
    public ProductionPickDto GetPendingQtList(WarehouseQuery query)
    {
        // 从查询参数中获取用户名和单据号
        var c_User = query.userName;
        var p_bill_no = query.billNo;
 
        // 根据单据号查询退料单主表信息
        var mesItemQt = Db.Queryable<MesItemQt>()
            .Where(it => it.Qtck == p_bill_no)
            .First();
 
        // 如果未找到退料单,抛出异常
        if (mesItemQt == null) throw new Exception($"未查询到此其他入库申请单 {p_bill_no}");
 
        //// 检查退料单的审核状态(Qt015),未审核则抛出异常
        //if (mesItemQt.Qt015 == false)
        //    throw new Exception($"其他出库申请单 {p_bill_no} 未审核,请确认!");
 
        // 检查退料单的完结状态(Qt014),已完结则抛出异常
        if (mesItemQt.Qt014 == true)
            throw new Exception($"其他出库申请单 {p_bill_no} 已完结,请确认!");
 
        var sql = @"SELECT c.item_no ItemNo,c.item_name ItemName,c.item_model ItemModel,
                    ISNULL(A.qd007,0)  FQty,ISNULL(A.qd008,0) SQty,ISNULL(A.qd007,0) - ISNULL(A.qd008,0) DSQty,
                    dbo.F_QX_GETRECODEPOTSE(A.itemId,'','','') as RecoKw
                    FROM MES_ITEM_QT_DATALL A
                    LEFT JOIN MES_ITEM_QT B ON A.qtGuid = B.guid
                    LEFT JOIN MES_ITEMS C ON A.itemId = C.item_id
                    WHERE B.qtck = @billNo AND (qt015 = 1 AND qt026 =1 AND QT029 = 1 AND qt032 = 1)";
 
        var sqlParams = new List<SugarParameter> {
            new("@billNo", query.billNo)
        };
 
        var womdabs = Db.Ado.SqlQuery<ItemDetailModel>(sql, sqlParams);
 
        // 检查退料单的完结状态(Qt014),已完结则抛出异常
        if (womdabs.Count < 1)
            throw new Exception($"该其他出库申请单 {p_bill_no} 未全部审核,请确认!");
 
        var DS_list = womdabs.Where(s => s.DSQty > 0).ToList();
 
        var YS_list = womdabs.Where(s => s.SQty > 0).ToList();
 
        var dto = new ProductionPickDto
        {
            //daa001 = womdaa.Daa001,
            //PlanNo = womcaa.Caa020,
            items = DS_list,
            Ysitems = YS_list
        };
 
        return dto;
    }
 
    public (WarehouseQuery form, List<MesItemQtDatall> item, string message)
        OutScanBarcode(
            WarehouseQuery query)
    {
        var p_bill_no = query.billNo;
        var p_item_barcode = query.barcode;
        var c_user = query.userName;
        var p_bill_type_id = 200;
        var p_transaction_no = 202;
 
        // 检查单据号是否为空
        if (string.IsNullOrEmpty(p_bill_no)) throw new Exception("请选取单据号!");
 
        // 检查是否重复扫描
        var existingBarcode = Db.Queryable<MesInvItemOutCDetails>()
            .Where(it =>
                it.ItemBarcode == p_item_barcode && it.PbillNo == p_bill_no)
            .Any();
 
        if (existingBarcode) throw new Exception("此条码已经扫码出库,勿重复扫描!");
 
        // 获取条码信息
        var barcode = Db.Queryable<MesInvItemBarcodes>()
            .Where(it => it.ItemBarcode == p_item_barcode)
            .First();
 
        if (barcode == null)
            throw new Exception($"mes中不存在此条码,请核对!{p_item_barcode}");
 
        // 获取库存信息
        var stock = Db.Queryable<MesInvItemStocks>()
            .Where(it => it.ItemBarcode == p_item_barcode && it.Quantity > 0)
            .First();
 
        if (stock == null)
            throw new Exception($"库存中无此条码,请检查条码是否未入库或已出库!{p_item_barcode}");
 
 
        if (string.IsNullOrEmpty(stock.DepotsCode))
            throw new Exception($"调拨中的条码不可发料,请先完成调拨单据{p_item_barcode}");
 
        if (stock.DepotsCode is "S006" or "S005")
            throw new Exception($"条码在不良品仓下 不可发料{p_item_barcode}");
 
        // 检查是否在寄存仓位
        // var isDepotSection = Db.Queryable<MesJcDepot>()
        //     .Where(it => it.DepotSectionsCode == stock.DepotSectionsCode)
        //     .Any();
        //
        // if (isDepotSection)
        // {
        //     throw new Exception($"在寄存仓位下的条码不可直接扫码发料{p_item_barcode}");
        // }
 
        // 获取其他出库单信息
        var mesItemQt = Db.Queryable<MesItemQt>()
            .Where(it => it.Qtck == p_bill_no && it.Qt015 == true && it.Qt026 == true && it.Qt029 == true && it.Qt032 == true)
            .First();
 
        if (mesItemQt == null)
            throw new Exception($"其他出库申请单 {p_bill_no} 不存在,请确认!");
 
        // 检查仓库是否一致
        if (Convert.ToInt32(mesItemQt.Qt008)  != stock.DepotId)
            throw new Exception(
                $"扫码出货仓库id{mesItemQt.Qt008}与其他出库申请仓库{stock.DepotId}不一致,请核对!");
 
        // 获取出库单明细
        var qtDetail = Db.Queryable<MesItemQtDatall>()
            .Where(it =>
                it.QtGuid == mesItemQt.Guid &&
                it.ItemId == stock.ItemId.ToString())
            .First();
 
        if (qtDetail == null) throw new Exception("扫码物料非本次出库申请物料,请核对!");
 
        // 检查剩余数量
        var remainingQty = (qtDetail.Qd007 ?? 0) - (qtDetail.Qd008 ?? 0);
        if (remainingQty <= 0)
            throw new Exception($"申请物料 {barcode.ItemNo} 已出库完成!");
 
        // 检查数量是否超出
        if (stock.Quantity > remainingQty)
        {
            var pendingList = Db
                .Queryable<MesItemQt, MesItemQtDatall, MesItems>(
                    (a, b, c) => new JoinQueryInfos(
                        JoinType.Left, a.Guid == b.QtGuid,
                        JoinType.Left, b.ItemId == c.Id.ToString()))
                .Where((a, b, c) => a.Qtck == p_bill_no &&
                                    (b.Qd007 ?? 0) - (b.Qd008 ?? 0) > 0)
                .OrderBy((a, b, c) => c.ItemNo)
                .Select((a, b, c) => new MesItemQtDatall
                {
                    Qd002 = b.Qd002, // 明细行号
                    Qt011 = a.Qt011, // 退料原因
                    ItemNo = c.ItemNo, // 物料编号
                    Qd007 = b.Qd007 ?? 0, // 计划数量
                    Qd008 = b.Qd008 ?? 0 // 已完成数量
                })
                .ToList();
 
            var message =
                $"请确认发料数量!!已带出满足其他出库单的发料数量 {remainingQty} 确认后请点击条码拆分";
 
            query.itemNo = stock.ItemNo;
            query.Num = stock.Quantity;
            query.Fum = remainingQty;
 
            return (query, pendingList, message);
        }
 
        var mesItemQtDatalls = new List<MesItemQtDatall>();
        var mess = "";
        UseTransaction(db =>
        {
            var commit = 0;
 
            // 查找当天是否已存在出库单
            var existingOut = Db.Queryable<MesInvItemOuts>()
                .Where(it => it.TaskNo == p_bill_no
                             // && it.DepotCode == stock.DepotsCode
                             && it.DepotId == stock.DepotId
                             && it.OutDate.Value.Date.ToString("yyyy-MM-dd") ==
                             DateTime.Now.Date.ToString("yyyy-MM-dd")
                             && it.BillTypeId == p_bill_type_id
                             && it.TransactionNo == p_transaction_no
                             && it.Status == 0)
                .First();
 
            Guid outId;
            string outNo;
 
            if (existingOut == null)
            {
                // 创建新的出库单
                outId = Guid.NewGuid();
                outNo = BillNo.GetBillNo("QTCK(其他出库)");
 
                var mesItemQt = Db.Queryable<MesItemQt>()
                    .Where(it => it.Qtck == p_bill_no)
                    .First();
 
                var newOut = new MesInvItemOuts
                {
                    Guid = outId,
                    ItemOutNo = outNo,
                    TaskNo = p_bill_no,
                    Status = 0,
                    CreateBy = c_user,
                    CreateDate = DateTime.Now,
                    LastupdateBy = c_user,
                    LastupdateDate = DateTime.Now,
                    BillTypeId = p_bill_type_id,
                    TransactionNo = p_transaction_no,
                    DepotCode = stock.DepotsCode,
                    DepotId = stock.DepotId.HasValue
                        ? (int)stock.DepotId
                        : null,
                    OutPart = mesItemQt.Qt012,
                    FType = 0,
                    //WorkNo = p_bill_no,
                    OutType = "其他出库",
                    //PbillNo = p_bill_no,
                    OutDate = DateTime.Now,
                    Nflag = 0,
                    Reason = mesItemQt.Qt010,
                    Remark = mesItemQt.Qt010
                };
 
                commit += db.Insertable(newOut).IgnoreColumns(true)
                    .ExecuteCommand();
            }
            else
            {
                outId = existingOut.Guid;
                outNo = existingOut.ItemOutNo;
            }
 
            // 获取退料单明细信息
            qtDetail = Db.Queryable<MesItemQtDatall>()
                .Where(it =>
                    it.QtGuid == mesItemQt.Guid &&
                    it.ItemId == stock.ItemId.ToString())
                .First();
 
            if (qtDetail == null) throw new Exception("未找到对应的退料单明细信息");
 
            // 检查是否已存在出库物料记录
            var existingOutItem = Db.Queryable<MesInvItemOutItems>()
                .Where(it =>
                    it.ItemOutId == outId && it.ItemId == barcode.ItemId && it.ItemDabid == qtDetail.Guid)
                .First();
 
            if (existingOutItem == null)
            {
                // 创建新的出库物料记录
                var newOutItem = new MesInvItemOutItems
                {
                    Guid = Guid.NewGuid(),
                    ItemOutId = outId,
                    ItemNo = qtDetail.Qd002,
                    Quantity = qtDetail.Qd007,
                    TlQty = stock.Quantity,
                    CreateBy = c_user,
                    CreateDate = DateTime.Now,
                    LastupdateBy = c_user,
                    LastupdateDate = DateTime.Now,
                    DepotCode = stock.DepotsCode,
                    TaskNo = p_bill_no,
                    WorkNo = p_bill_no,
                    QtOutId = qtDetail.Guid,
                    // WorkLine = qtDetail.Id,
                    // ErpItemNo = qtDetail.Qd012,
                    PbillNo = p_bill_no,
                    ItemId = !string.IsNullOrEmpty(qtDetail.ItemId)
                        ? long.Parse(qtDetail.ItemId)
                        : null,
                    FType = 0,
                    AboutGuid = qtDetail.Guid,
                    ItemDabid = qtDetail.Guid
                    // Unit = qtDetail.Qd009
                };
 
                commit += db.Insertable(newOutItem).IgnoreColumns(true)
                    .ExecuteCommand();
            }
            else
            {
                // 更新已存在的出库物料记录数量
                commit += db.Updateable<MesInvItemOutItems>()
                    .SetColumns(it =>
                        it.TlQty == (it.TlQty ?? 0) + stock.Quantity)
                    .Where(it => it.ItemOutId == outId && it.ItemId == barcode.ItemId && it.ItemDabid == qtDetail.Guid)
                    .ExecuteCommand();
            }
 
            // 创建出库条码明细记录
            var outCDetail = new MesInvItemOutCDetails
            {
                ItemOutId = outId,
                ItemBarcode = stock.ItemBarcode,
                ItemNo = stock.ItemNo,
                LotNo = stock.LotNo,
                Quantity = stock.Quantity,
                ForceOutFlag = 0,
                CreateBy = c_user,
                CreateDate = DateTime.Now,
                LastupdateBy = c_user,
                LastupdateDate = DateTime.Now,
                DepotCode = stock.DepotsCode,
                DepotId = stock.DepotId.HasValue
                    ? (int)stock.DepotId
                    : null,
                // DepotsCode = stock.DepotsCode,
                // DepotSectionsCode = stock.DepotSectionsCode,
                DepotSectionCode = stock.DepotSectionsCode,
                BoardStyle = stock.BoardStyle,
                TaskNo = p_bill_no,
                WorkNo = p_bill_no,
                // WorkLine = qtDetail.Id,
                SuppNo = stock.SuppNo,
                PbillNo = p_bill_no,
                ItemId = !string.IsNullOrEmpty(qtDetail.ItemId)
                    ? long.Parse(qtDetail.ItemId)
                    : null
            };
 
            commit += db.Insertable(outCDetail).IgnoreColumns(true)
                .ExecuteCommand();
 
 
            // 插入业务流水记录
            var business = new MesInvBusiness2
            {
                Status = 1,
                BillTypeId = p_bill_type_id,
                TransactionCode = p_transaction_no.ToString(),
                BusinessType = 1,
                ItemBarcode = stock.ItemBarcode,
                ItemNo = stock.ItemNo,
                LotNo = stock.LotNo,
                EpFlag = true,
                Quantity = stock.Quantity,
                FromInvDepotsCode = stock.DepotsCode,
                FromInvDepotSectionsCode = stock.DepotSectionsCode,
                Description = "其他出库",
                CreateBy = c_user,
                CreateDate = DateTime.Now,
                LastupdateBy = c_user,
                LastupdateDate = DateTime.Now,
                TaskNo = p_bill_no,
                BillNo = outNo,
                WorkNo = p_bill_no,
                // WorkLine = qtDetail.Id,
                SuppId = stock.SuppId,
                SuppNo = stock.SuppNo,
                ItemId = !string.IsNullOrEmpty(qtDetail.ItemId)
                    ? long.Parse(qtDetail.ItemId)
                    : null
            };
 
            commit += db.Insertable(business).IgnoreColumns(true)
                .ExecuteCommand();
 
            // 删除库存记录
            commit += db.Deleteable<MesInvItemStocks>()
                .Where(it => it.ItemBarcode == stock.ItemBarcode)
                .ExecuteCommand();
 
            // 更新退料单明细已完成数量
            commit += db.Updateable<MesItemQtDatall>()
                .SetColumns(it => it.Qd008 == (it.Qd008 ?? 0) + stock.Quantity)
                .Where(it => it.Guid == qtDetail.Guid)
                .ExecuteCommand();
 
            // 检查明细是否完成,如果完成则更新状态
            if ((qtDetail.Qd007 ?? 0) - (qtDetail.Qd008 ?? 0) == stock.Quantity)
                commit += db.Updateable<MesItemQtDatall>()
                    .SetColumns(it => it.Qd011 == 1)
                    .Where(it => it.Guid == qtDetail.Guid)
                    .ExecuteCommand();
 
            mesItemQtDatalls = Db
                .Queryable<MesItemQt, MesItemQtDatall, MesItems>(
                    (a, b, c) => new JoinQueryInfos(
                        JoinType.Left, a.Guid == b.QtGuid,
                        JoinType.Left, b.ItemId == c.Id.ToString()))
                .Where((a, b, c) => a.Qtck == p_bill_no &&
                                    (b.Qd007 ?? 0) - (b.Qd008 ?? 0) > 0)
                .OrderBy((a, b, c) => c.ItemNo)
                .Select((a, b, c) => new MesItemQtDatall
                {
                    Qd002 = b.Qd002, // 明细行号
                    Qt011 = a.Qt011, // 退料原因
                    ItemNo = c.ItemNo, // 物料编号
                    Qd007 = b.Qd007 ?? 0, // 计划数量
                    Qd008 = b.Qd008 ?? 0 // 已完成数量
                })
                .ToList();
 
            // 如果没有待处理明细,更新退料单状态为已完成
            if (CollectionUtil.IsNullOrEmpty(mesItemQtDatalls))
                db.Updateable<MesItemQt>()
                    .SetColumns(it => it.Qt014 == true)
                    .Where(it => it.Qtck == p_bill_no)
                    .ExecuteCommand();
 
            // 构建返回消息
            mess = $"扫码成功!条码 {p_item_barcode} 数量 {stock.Quantity} 已出库";
 
            query.barcode = null;
            query.itemNo = stock.ItemNo;
            query.Num = stock.Quantity;
            query.Fum = null;
 
            // 创建 插入日志
            var logService = new LogService();
            var LogMsg = "【PDA】其他出库。条码【" + query.barcode + "】数量【"+ stock.Quantity.ToString() + "】 出库单号【" + outNo + "】";
            logService.CreateLog(db, query.userName, mesItemQt.Guid.ToString(), "MES_ITEM_QT", LogMsg, mesItemQt.Qtck);
 
            if (commit < 5) throw new Exception("更新失败");
 
            return commit;
        });
 
        return (query, mesItemQtDatalls, mess);
    }
 
    public (WarehouseQuery form, List<MesItemQtDatall> item, string message)
        PrintQtckBarcode(
            WarehouseQuery query)
    {
        var p_bill_no = query.billNo;
        var p_old_barcode = query.barcode;
        var c_user = query.userName;
        var p_qty = query.Fum;
        var p_bill_type_id = 200;
        var p_transaction_no = 202;
 
        // 检查单据号是否为空
        if (string.IsNullOrEmpty(p_bill_no)) throw new Exception("请选取单据号!");
 
        // 检查数量是否有效
        if (p_qty <= 0) throw new Exception("请输入正确的发料数量!");
 
        // 获取库存条码信息
        var stock = Db.Queryable<MesInvItemStocks>()
            .Where(it => it.ItemBarcode == p_old_barcode && it.Quantity > 0)
            .First();
 
        if (stock == null)
            throw new Exception($"库存中无此条码,请检查条码是否未入库或已出库!{p_old_barcode}");
 
        if (string.IsNullOrEmpty(stock.DepotsCode))
            throw new Exception($"调拨中的条码不可发料,请先完成调拨单据{p_old_barcode}");
 
        if (stock.DepotsCode is "S006" or "S005")
            throw new Exception($"条码在不良品仓下 不可发料{p_old_barcode}");
 
        // 获取条码信息
        var barcode = Db.Queryable<MesInvItemBarcodes>()
            .Where(it => it.ItemBarcode == p_old_barcode)
            .First();
 
        if (barcode == null)
            throw new Exception($"mes中不存在此条码,请核对!{p_old_barcode}");
 
        // 获取退料单信息
        var mesItemQt = Db.Queryable<MesItemQt>()
            .Where(it => it.Qtck == p_bill_no)
            .First();
 
        if (mesItemQt == null)
            throw new Exception($"其他出库申请单 {p_bill_no} 不存在,请确认!");
 
        if (mesItemQt.Qt011 != stock.DepotsCode)
            throw new Exception(
                $"扫码出货仓库{stock.DepotsCode}与其他出库申请仓库{mesItemQt.Qt011}不一致,请核对!");
 
        // 获取退料单明细
        var qtDetail = Db.Queryable<MesItemQtDatall>()
            .Where(it =>
                it.QtGuid == mesItemQt.Guid &&
                it.ItemId == stock.ItemId.ToString())
            .First();
 
        if (qtDetail == null) throw new Exception("扫码物料非本次出库申请物料,请核对!");
 
        var remainingQty = (qtDetail.Qd007 ?? 0) - (qtDetail.Qd008 ?? 0);
 
        if (remainingQty <= 0)
            throw new Exception($"申请物料 {barcode.ItemNo} 已出库完成!");
 
        if (p_qty > remainingQty)
            throw new Exception(
                $"输入的拆分数量 {p_qty} 不可大于剩余需发数量 {remainingQty} 请修改");
 
        var totalQty = Db.Queryable<MesInvItemStocks>()
            .Where(it => it.ItemBarcode == p_old_barcode && it.Quantity > 0)
            .Sum(it => it.Quantity);
 
        if (totalQty < p_qty)
            throw new Exception($"输入的发料数量 {p_qty} 不可大于条码数量 {totalQty} 请修改");
 
        var message = string.Empty;
        var mesItemQtDatalls = new List<MesItemQtDatall>();
        var newBarcode = string.Empty;
 
        UseTransaction(db =>
        {
            var commit = 0;
 
            // 如果需要拆分条码
            if (totalQty > p_qty)
            {
                var mesItems = db.Queryable<MesItems>()
                    .Where(s => s.Id == stock.ItemId).First();
                // 生成新条码号
                newBarcode = BillNo.GetBillNo("TMBH(条码编号)", mesItems.ItemNo);
 
                // 插入新条码记录
                commit += db.Insertable(new MesInvItemBarcodes
                {
                    Guid = Guid.NewGuid(),
                    ItemBarcode = newBarcode,
                    ItemNo = barcode.ItemNo,
                    LotNo = barcode.LotNo,
                    Quantity = p_qty,
                    TaskNo = barcode.TaskNo,
                    CreateBy = c_user,
                    CreateDate = DateTime.Now,
                    LastupdateBy = c_user,
                    LastupdateDate = DateTime.Now,
                    OldItemBarcode = p_old_barcode,
                    Mblnr = barcode.Mblnr,
                    Zeile = barcode.Zeile,
                    Barcodestatus = false,
                    Oldqty = Convert.ToInt64(p_qty.Value),
                    Unit = barcode.Unit,
                    LotDate = barcode.LotDate,
                    Memo = "其他出库拆分",
                    SuppNo = barcode.SuppNo,
                    ItemSname = barcode.ItemSname,
                    TrLotno = barcode.TrLotno,
                    BillNo = barcode.BillNo,
                    UrgentFlag = barcode.UrgentFlag,
                    InsDate = barcode.InsDate,
                    WorkNo = barcode.WorkNo,
                    WorkLine = barcode.WorkLine,
                    ComeFlg = 5,
                    EbelnK3id = barcode.EbelnK3id,
                    LineK3id = barcode.LineK3id,
                    ItemId = barcode.ItemId
                }).IgnoreColumns(true).ExecuteCommand();
 
                // 更新原条码数量
                commit += db.Updateable<MesInvItemBarcodes>()
                    .SetColumns(it => it.Quantity == it.Quantity - p_qty)
                    .Where(it => it.ItemBarcode == p_old_barcode)
                    .ExecuteCommand();
 
                // 更新原库存数量
                commit += db.Updateable<MesInvItemStocks>()
                    .SetColumns(it => it.Quantity == it.Quantity - p_qty)
                    .Where(it => it.ItemBarcode == p_old_barcode)
                    .ExecuteCommand();
 
                // 插入新库存记录
                commit += db.Insertable(new MesInvItemStocks
                {
                    Guid = Guid.NewGuid(),
                    ItemBarcode = newBarcode,
                    ItemNo = stock.ItemNo,
                    LotNo = stock.LotNo,
                    Quantity = p_qty,
                    DepotsCode = stock.DepotsCode,
                    DepotSectionsCode = stock.DepotSectionsCode,
                    CheckDate = stock.CheckDate,
                    IndepDate = stock.IndepDate,
                    IqcStatus = stock.IqcStatus,
                    WorkNo = stock.WorkNo,
                    WorkLine = stock.WorkLine,
                    SuppNo = stock.SuppNo,
                    BillNo = stock.BillNo,
                    EbelnK3id = stock.EbelnK3id,
                    LineK3id = stock.LineK3id,
                    ItemId = stock.ItemId
                }).IgnoreColumns(true).ExecuteCommand();
 
 
                // 插入业务流水记录
                commit += db.Insertable(new MesInvBusiness2
                {
                    Status = 1,
                    BillTypeId = p_bill_type_id,
                    TransactionCode = p_transaction_no.ToString(),
                    BusinessType = 1,
                    TaskNo = barcode.TaskNo,
                    BillNo = barcode.BillNo,
                    ItemBarcode = newBarcode,
                    ItemNo = barcode.ItemNo,
                    LotNo = barcode.LotNo,
                    EpFlag = true,
                    Quantity = p_qty,
                    FromInvDepotsCode = null,
                    FromInvDepotSectionsCode = null,
                    ToInvDepotsCode = stock.DepotsCode,
                    ToInvDepotSectionsCode = stock.DepotSectionsCode,
                    Description = "其他出库条码拆分",
                    CreateBy = c_user,
                    CreateDate = DateTime.Now,
                    LastupdateBy = c_user,
                    LastupdateDate = DateTime.Now,
                    Factory = "1000",
                    Company = "1000",
                    WorkNo = stock.WorkNo,
                    WorkLine = stock.WorkLine,
                    EbelnK3id = stock.EbelnK3id,
                    LineK3id = stock.LineK3id,
                    SuppId = stock.SuppId,
                    SuppNo = stock.SuppNo,
                    ItemId = stock.ItemId
                }).IgnoreColumns(true).ExecuteCommand();
            }
            else
            {
                newBarcode = p_old_barcode;
            }
 
 
            // 获取或创建出库单
            var outRecord = db.Queryable<MesInvItemOuts>()
                .Where(it => it.PbillNo == p_bill_no
                             && it.DepotCode == stock.DepotsCode
                             && it.OutDate.Value.Date.ToString("yyyy-MM-dd") ==
                             DateTime.Now.Date.ToString("yyyy-MM-dd")
                             && it.BillTypeId == p_bill_type_id
                             && it.TransactionNo == p_transaction_no
                             && it.Status == 0)
                .First();
 
            var outId = Guid.Empty;
            var outNo = string.Empty;
 
            if (outRecord == null)
            {
                outId = Guid.NewGuid();
                outNo = BillNo.GetBillNo("INV_OUT_OTHER");
                // 创建新出库单
                commit += db.Insertable(new MesInvItemOuts
                {
                    Guid = outId,
                    ItemOutNo = outNo,
                    TaskNo = p_bill_no,
                    Status = 0,
                    CreateBy = c_user,
                    CreateDate = DateTime.Now,
                    LastupdateBy = c_user,
                    LastupdateDate = DateTime.Now,
                    BillTypeId = p_bill_type_id,
                    TransactionNo = p_transaction_no,
                    DepotCode = stock.DepotsCode,
                    OutPart = mesItemQt.Qt012,
                    FType = 0,
                    WorkNo = p_bill_no,
                    OutType = "其他出库",
                    BusinessType = "",
                    PbillNo = p_bill_no,
                    OutDate = DateTime.Now,
                    Nflag = 0,
                    Reason = mesItemQt.Qt010
                }).ExecuteReturnIdentity();
            }
            else
            {
                outId = outRecord.Guid;
                outNo = outRecord.ItemOutNo;
            }
 
            // 检查并更新出库单明细
            var outItem = db.Queryable<MesInvItemOutItems>()
                .Where(it =>
                    it.ItemOutId == outId && it.ItemId == barcode.ItemId)
                .First();
 
            if (outItem == null)
                // 插入新明细
                db.Insertable(new MesInvItemOutItems
                {
                    ItemOutId = outId,
                    ItemNo = qtDetail.Qd002,
                    Quantity = p_qty,
                    CreateBy = c_user,
                    CreateDate = DateTime.Now,
                    LastupdateBy = c_user,
                    LastupdateDate = DateTime.Now,
                    DepotCode = stock.DepotsCode,
                    TaskNo = p_bill_no,
                    WorkNo = p_bill_no,
                    QtOutId = qtDetail.Guid,
                    // WorkLine = qtDetail.Id.ToString(),
                    ErpItemNo = qtDetail.ItemNo,
                    PbillNo = p_bill_no,
                    ItemId = !string.IsNullOrEmpty(qtDetail.ItemId)
                        ? long.Parse(qtDetail.ItemId)
                        : null,
                    FType = 0
                    // Unit = qtDetail.Qd009
                }).ExecuteCommand();
            else
                // 更新明细数量
                db.Updateable<MesInvItemOutItems>()
                    .SetColumns(it =>
                        it.Quantity == (it.Quantity ?? 0) + p_qty)
                    .Where(it =>
                        it.ItemOutId == outId &&
                        it.ItemId == stock.ItemId &&
                        it.QtOutId == qtDetail.Guid)
                    .ExecuteCommand();
 
            // 插入出库明细
            db.Insertable(new MesInvItemOutCDetails
            {
                ItemOutId = outId,
                ItemBarcode = newBarcode,
                ItemNo = stock.ItemNo,
                LotNo = stock.LotNo,
                Quantity = p_qty,
                ForceOutFlag = 0,
                CreateBy = c_user,
                CreateDate = DateTime.Now,
                LastupdateBy = c_user,
                LastupdateDate = DateTime.Now,
                DepotCode = stock.DepotsCode,
                DepotSectionCode = stock.DepotSectionsCode,
                Factory = "1000",
                Company = "1000",
                BoardStyle = stock.BoardStyle,
                TaskNo = p_bill_no,
                WorkNo = p_bill_no,
                // WorkLine = qtDetail.Id.ToString(),
                SuppNo = stock.SuppNo,
                PbillNo = p_bill_no,
                ItemId = !string.IsNullOrEmpty(qtDetail.ItemId)
                    ? long.Parse(qtDetail.ItemId)
                    : null
            }).IgnoreColumns(true).ExecuteCommand();
 
            // 插入业务记录
            db.Insertable(new MesInvBusiness2
            {
                Status = 1,
                BillTypeId = p_bill_type_id,
                TransactionCode = p_transaction_no.ToString(),
                BusinessType = 1,
                ItemBarcode = newBarcode,
                ItemNo = stock.ItemNo,
                LotNo = stock.LotNo,
                EpFlag = true,
                Quantity = p_qty,
                FromInvDepotsCode = stock.DepotsCode,
                FromInvDepotSectionsCode = stock.DepotSectionsCode,
                Description = "其他出库",
                CreateBy = c_user,
                CreateDate = DateTime.Now,
                LastupdateBy = c_user,
                LastupdateDate = DateTime.Now,
                Factory = "1000",
                Company = "1000",
                TaskNo = p_bill_no,
                BillNo = outNo,
                WorkNo = p_bill_no,
                // WorkLine = qtDetail.Id.ToString(),
                SuppId = stock.SuppId,
                SuppNo = stock.SuppNo,
                ItemId = !string.IsNullOrEmpty(qtDetail.ItemId)
                    ? long.Parse(qtDetail.ItemId)
                    : null
            }).IgnoreColumns(true).ExecuteCommand();
 
            // 删除库存记录
            db.Deleteable<MesInvItemStocks>()
                .Where(it => it.ItemBarcode == newBarcode)
                .ExecuteCommand();
 
            // 更新退料单明细
            db.Updateable<MesItemQtDatall>()
                .SetColumns(it => it.Qd008 == (it.Qd008 ?? 0) + (int)p_qty)
                .SetColumnsIF(
                    (qtDetail.Qd007 ?? 0) - (qtDetail.Qd008 ?? 0) == p_qty,
                    it => it.Qd011 == 1)
                .Where(it => it.Guid == qtDetail.Guid)
                .ExecuteCommand();
 
            // 检查是否所有明细都已完成
            var hasUnfinished = db.Queryable<MesItemQtDatall>()
                .Where(it => it.Qd011 == 0)
                .Any();
 
            // 如果所有明细完成,更新退料单状态
            if (!hasUnfinished)
                db.Updateable<MesItemQt>()
                    .SetColumns(it => it.Qt014 == true)
                    .Where(it => it.Qtck == p_bill_no)
                    .ExecuteCommand();
 
            // 获取剩余待处理明细
            mesItemQtDatalls = db
                .Queryable<MesItemQt, MesItemQtDatall, MesItems>(
                    (a, b, c) => new JoinQueryInfos(
                        JoinType.Left, a.Guid == b.QtGuid,
                        JoinType.Left, b.ItemId == c.Id.ToString()))
                .Where((a, b, c) => a.Qtck == p_bill_no &&
                                    (b.Qd007 ?? 0) - (b.Qd008 ?? 0) > 0)
                .OrderBy((a, b, c) => c.ItemNo)
                .Select((a, b, c) => new MesItemQtDatall
                {
                    Qd002 = b.Qd002,
                    Qt011 = a.Qt011,
                    ItemNo = c.ItemNo,
                    Qd007 = b.Qd007 ?? 0,
                    Qd008 = b.Qd008 ?? 0
                })
                .ToList();
 
            message = $"操作成功!拆分条码:{newBarcode} 数量:{p_qty}已出库";
 
            return commit;
        });
        return (query, mesItemQtDatalls, message);
    }
}