新框架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
using Masuit.Tools;
using MES.Service.Modes;
using NewPdaSqlServer.DB;
using NewPdaSqlServer.Dto.service;
using NewPdaSqlServer.entity;
using NewPdaSqlServer.entity.Base;
using NewPdaSqlServer.util;
using SqlSugar;
using SqlSugar.Extensions;
using System.Data;
using System.Data.SqlClient;
 
namespace NewPdaSqlServer.service.Warehouse;
 
public class MesXsckManager : Repository<MesItemBl>
{
    #region 生产补料
 
    /// <summary>
    ///     获取生产发货通知单号列表
    /// </summary>
    /// <returns>发货通知单号列表</returns>
    public List<string> GetFHTZBillNo(WarehouseQuery query)
    {
        var list = Db.Queryable<SalesDeliver>()
            .Where(s => (s.FApproverStatus ?? false) == true)
            .Select(s => s.BillNo)
            .ToList();
        return list;
    }
 
    /// <summary>
    ///     根据发货通知单获取对应代发货明细  prc_rf_pda_scan_zout_showbl
    /// </summary>
    /// <returns>发货通知单明细列表</returns>
    public List<ItemDetailModel> GetMesItemFHTZetailByBillNo(
            WarehouseQuery query)
    {
        if (string.IsNullOrEmpty(query.billNo))
            throw new Exception("请选单据号!");
 
        // 检查发货通知单是否存在且审核
        var mesItem = Db.Queryable<SalesDeliver>()
            .Where(a => a.BillNo == query.billNo && a.FApproverStatus == true)
            .First();
 
        if (mesItem == null)
            throw new Exception("单据号不存在或未审核!");
 
        if (mesItem.FinishStatus == true)
            throw new Exception("单据号已完结!");
 
        // 获取未完成的发货通知单明细
        var blDetails = Db.Queryable<SalesDeliver, SalesDeliverDetail, SalesOrderDetail,MesItems>((a, b, c, d) =>
                new JoinQueryInfos(JoinType.Left, a.Id == b.Pid,
                JoinType.Left, b.SalesDetailId == c.Id,
                JoinType.Left, c.MaterialId == d.ItemId.ToString()))
            .Where((a, b) => a.BillNo == query.billNo
                             && (b.FMustQty ?? 0) - (b.FRealQty ?? 0) > 0)
            .Select((a, b, c, d) => new ItemDetailModel
            {
                ItemNo = d.ItemNo,
                ItemName = d.ItemName,
                ItemModel = d.ItemModel,
                FQty = b.FMustQty,
                SQty = b.FRealQty,
                FMaterialId = d.ItemId.ToString(),
                Id = b.Id.ToString()
 
            })
            .ToList();
 
        return  blDetails;
    }
 
    /// <summary>
    /// 生产工单补料扫码
    ///     扫描条码  prc_rf_pda_scan_zout_barcode3
    /// </summary>
    /// <param name="query">查询参数</param>
    /// <returns>扫描结果</returns>
    /// <remarks>
    ///     参数说明:
    ///     - billNo: 单据号(必填)
    ///     - barcode: 条码(必填)
    ///     - userName: 用户名
    ///     - blNo: 发货通知单号(必填)
    /// </remarks>
    public ProductionPickDto XSCKScanBarcode( WarehouseQuery query)
    {
        var _strMsg = "";
        var _intSum = "";
        using (var conn = new SqlConnection(DbHelperSQL.strConn))
        {
            if (query.userName.IsNullOrEmpty()) throw new Exception("用户名不允许为空");
            if (query.daa001.IsNullOrEmpty()) throw new Exception("发货通知单号不允许为空");
            if (query.barcode.IsNullOrEmpty()) throw new Exception("条码不允许为空");
 
            using (var cmd = new SqlCommand("[prc_pda_XSCK]", conn))
            {
                try
                {
                    conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter[] parameters =
                    {
                        new("@outMsg", SqlDbType.NVarChar, 300),
                        new("@outSum", SqlDbType.NVarChar, 300),
                        new("@barcode_num", SqlDbType.NVarChar, 300),
                        new("@split_num", SqlDbType.NVarChar, 300),
                        new("@c_User", query.userName),
                        new("@p_biLL_no", query.daa001),
                        new("@p_item_barcode", query.barcode)
                    };
                    parameters[0].Direction = ParameterDirection.Output;
                    parameters[1].Direction = ParameterDirection.Output;
                    parameters[2].Direction = ParameterDirection.Output;
                    parameters[3].Direction = ParameterDirection.Output;
                    foreach (var parameter in parameters)
                        cmd.Parameters.Add(parameter);
                    cmd.ExecuteNonQuery();
                    _strMsg = parameters[0].Value.ToString();
                    _intSum = parameters[1].Value.ToString();
 
                    var barcodeNum = parameters[2].Value.ToString();
                    var splitNum = parameters[3].Value.ToString();
 
                    var result = Convert.ToInt32(_intSum);
                    if (result <= 0) throw new Exception(_strMsg);
 
                    var dto = new ProductionPickDto
                    {
                        daa001 = query.daa001,
                        barcodeNum = barcodeNum,
                        splitNum = splitNum,
                        barcode = query.barcode,
                        strMsg = _strMsg,
                        result = _intSum
                    };
 
                    return dto;
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }
 
    /// <summary>
    ///     生产发货通知单条码拆分 prc_rf_pda_prnt_zout_barcode2
    /// </summary>
    /// <param name="query">查询参数</param>
    /// <returns>(成功标志, 待处理列表)</returns>
    /// <remarks>
    ///     前台需要传入的参数:
    ///     - userName: 用户名(必填)
    ///     - billNo: 工单号(必填)
    ///     - barcode: 物料条码(必填)
    ///     - Num: 发料数量(必填,必须大于0)
    ///     - blNo: 发货通知单号(必填)
    /// </remarks>
    public (bool success, List<MesItemBlDetail> pendingList) SplitBarcode(
        WarehouseQuery query)
    {
        if (string.IsNullOrEmpty(query.userName))
            throw new Exception("用户名不能为空!");
 
        if (string.IsNullOrEmpty(query.billNo))
            throw new Exception("请选取单据号!");
 
        if (string.IsNullOrEmpty(query.barcode))
            throw new Exception("请扫描条码!");
 
        if ((query.Num ?? 0) <= 0)
            throw new Exception("请输入正确的发料数量!");
 
        if (string.IsNullOrEmpty(query.blNo))
            throw new Exception("发货通知单号不能为空!");
 
 
        // 检验是否重复扫描
        var exists = Db.Queryable<MesInvItemOutCDetails>()
            .Where(b => b.ItemBarcode == query.barcode)
            .Any();
 
        if (exists)
            throw new Exception("此条码已扫描,勿重复扫码!");
 
        // 查询条码库存信息
        var stockBarcode = Db.Queryable<MesInvItemStocks>()
            .Where(t => t.ItemBarcode == query.barcode && t.Quantity > 0)
            .First();
 
        if (stockBarcode == null)
            throw new Exception($"库存中无此条码,请核对!{query.barcode}");
 
        var totalQty = stockBarcode.Quantity;
        string newBarcode = null;
 
        var outNoType = "";
        if (query.Type == "生产补料")
        {
            outNoType = "SCBL(生产补料)";
        }
        else
        {
            outNoType = "SCCL(生产超领)";
        }
 
        // 开启事务处理
        var success = UseTransaction(db =>
        {
            var executeCommand = 0;
 
            // 拆分条码
            if (totalQty > query.Num)
            {
                var mesItems = db.Queryable<MesItems>()
                    .Where(s => s.Id == stockBarcode.ItemId).First();
 
                // 生成新条码号
                newBarcode = BillNo.GetBillNo("TM(条码)", mesItems.ItemNo);
 
                // 写入新条码
                executeCommand += db.Insertable(new MesInvItemBarcodes
                {
                    Guid = Guid.NewGuid(),
                    ItemBarcode = newBarcode,
                    CustNo = stockBarcode.CustomerNo,
                    // ProductCode = stockBarcode.ProductCode, 
                    // ItemBarcode2 = stockBarcode.ItemBarcode2,
                    // ItemCode = stockBarcode.ItemCode,
                    ItemNo = stockBarcode.ItemNo,
                    LotNo = stockBarcode.LotNo,
                    Quantity = query.Num,
                    EpFlag = true,
                    TaskNo = stockBarcode.TaskNo,
                    CreateBy = query.userName,
                    CreateDate = DateTime.Now,
                    LastupdateBy = query.userName,
                    LastupdateDate = DateTime.Now,
                    OldItemBarcode = query.barcode,
                    // Mblnr = stockBarcode.Mblnr,
                    // Zeile = stockBarcode.Zeile,
                    // RohInId = stockBarcode.RohInId,
                    Barcodestatus = false,
                    Oldqty = query.Num as long?,
                    // Unit = stockBarcode.Unit,
                    // WeightUnit = stockBarcode.WeightUnit,
                    Factory = stockBarcode.Factory,
                    Company = stockBarcode.Company,
                    BillNo = stockBarcode.BillNo,
                    BoardStyle = stockBarcode.BoardStyle,
                    // ColorName = stockBarcode.ColorName,
                    WorkNo = stockBarcode.WorkNo,
                    WorkLine = stockBarcode.WorkLine,
                    // MemoBad = stockBarcode.MemoBad,
                    ComeFlg = 5,
                    // Memo = stockBarcode.Memo,
                    SuppId = stockBarcode.SuppId,
                    SuppNo = stockBarcode.SuppNo,
                    InsDate = stockBarcode.IndepDate, // Added InsDate
                    ItemId = stockBarcode.ItemId
                    // ItemUnit = stockBarcode.ItemUnit // Added ItemUnit
                }).IgnoreColumns(true).ExecuteCommand();
 
                // 更新原条码数量
                executeCommand += db.Updateable<MesInvItemBarcodes>()
                    .SetColumns(it => it.Quantity == it.Quantity - query.Num)
                    .Where(it => it.ItemBarcode == query.barcode)
                    .ExecuteCommand();
 
                // 更新原条码数量
                executeCommand += db.Updateable<MesInvItemStocks>()
                    .SetColumns(it => it.Quantity == it.Quantity - query.Num)
                    .Where(it => it.ItemBarcode == query.barcode)
                    .ExecuteCommand();
 
                //// 删除原条码库存记录
                //executeCommand += db.Deleteable<MesInvItemStocks>()
                //    .Where(it => it.ItemBarcode == query.barcode)
                //    .ExecuteCommand();
 
                // 插入剩余条码数量的新库存记录
                executeCommand += db.Insertable(new MesInvItemStocks
                {
                    Guid = Guid.NewGuid(),
                    TaskNo = stockBarcode.TaskNo,
                    ItemBarcode = newBarcode,
                    ItemNo = stockBarcode.ItemNo,
                    LotNo = stockBarcode.LotNo,
                    Quantity = query.Num,
                    EpFlag = stockBarcode.EpFlag,
                    CustomerNo = stockBarcode.CustomerNo,
                    ItemWt = stockBarcode.ItemWt,
                    DepotsCode = stockBarcode.DepotsCode,
                    DepotsId = stockBarcode.DepotsId,
                    DepotSectionsCode = stockBarcode.DepotSectionsCode,
                    CheckDate = stockBarcode.CheckDate,
                    ItemType = stockBarcode.ItemType,
                    IndepDate = stockBarcode.IndepDate,
                    Factory = stockBarcode.Factory,
                    Company = stockBarcode.Company,
                    IqcStatus = stockBarcode.IqcStatus,
                    BoardStyle = stockBarcode.BoardStyle,
                    WorkNo = stockBarcode.WorkNo,
                    WorkLine = stockBarcode.WorkLine,
                    SuppNo = stockBarcode.SuppNo,
                    ItemId = stockBarcode.ItemId
                    // UnitId = stockBarcode.ItemUnit
                }).IgnoreColumns(true).ExecuteCommand();
 
                // 写入新条码的交易记录
                executeCommand += db.Insertable(new MesInvBusiness2
                {
                    Guid = Guid.NewGuid(),
                    Status = 1,
                    BillTypeId = 200, // p_bill_type_id
                    TransactionCode = "209", // p_transaction_no
                    BusinessType = 0,
                    ItemBarcode = newBarcode,
                    ItemNo = stockBarcode.ItemNo,
                    LotNo = stockBarcode.LotNo,
                    EpFlag = true,
                    Quantity = query.Num,
                    FromInvDepotsCode = null,
                    FromInvDepotSectionsCode = null,
                    ToInvDepotsCode = stockBarcode.DepotsCode,
                    ToInvDepotSectionsCode = stockBarcode.DepotSectionsCode,
                    Description = query.Type + "拆分生成",
                    CreateBy = query.userName,
                    CreateDate = DateTime.Now,
                    LastupdateBy = query.userName,
                    LastupdateDate = DateTime.Now,
                    Factory = stockBarcode.Factory,
                    Company = stockBarcode.Company,
                    TaskNo = stockBarcode.TaskNo,
                    BillNo = stockBarcode.BillNo,
                    WorkNo = stockBarcode.WorkNo,
                    WorkLine = stockBarcode.WorkLine,
                    SuppNo = stockBarcode.SuppNo,
                    SuppId = stockBarcode.SuppId,
                    ItemId = stockBarcode.ItemId
                    // CkDepot = stockBarcode.DepotsId
                }).IgnoreColumns(true).ExecuteCommand();
            }
            else if (totalQty < query.Num)
            {
                throw new Exception("发料数量大于条码数,请核对!");
            }
 
            //if (string.IsNullOrEmpty(newBarcode)) newBarcode = query.barcode;
 
            // 检查发货通知单状态
            var mesItemBl = Db.Queryable<MesItemBl>()
                .Where(a => a.BlNo == query.blNo && (a.Bl018 ?? false) == true)
                .First();
 
            if (mesItemBl == null)
                throw new Exception($"申请单 {query.blNo} 已撤回!");
 
            if (mesItemBl.Bl018 != true)
                throw new Exception($"申请单 {query.blNo} 未审核!");
 
            if (mesItemBl.Bl019 == true)
                throw new Exception($"申请单 {query.blNo} 已完结!");
 
            // 获取发货通知单明细并校验
            var blDetail = Db.Queryable<MesItemBlDetail>()
                .Where(b =>
                    b.Mid == mesItemBl.Id && b.Bld012 == stockBarcode.ItemId)
                .First();
 
            if (blDetail == null)
                throw new Exception($"申请单不存在此物料 {stockBarcode.ItemNo} 请确认!");
 
            var remainingQty = (blDetail.Bld007 ?? 0) - (blDetail.Bld008 ?? 0);
            if (remainingQty == 0)
                throw new Exception("物料已扫码完成,请核对!");
 
            if (query.Num > remainingQty)
                throw new Exception(
                    $"拆分数量:{query.Num} 大于待发料数量:{remainingQty},请核对!");
 
            // 检查工单信息
            var womdaa = Db.Queryable<Womdaa>()
                .Where(a => a.Daa001 == query.billNo)
                .First();
 
            if (womdaa == null)
                throw new Exception($"工单 {query.billNo} 不存在,请确认!");
 
            var womdab = Db.Queryable<Womdab>()
                .Where(b =>
                    b.Dab001 == query.billNo && b.Erpid == blDetail.Bld014)
                .First();
 
            if (womdab == null)
                throw new Exception($"备料明细不存在此物料 {stockBarcode.ItemNo} 请确认!");
 
            // 检查已发料数量是否超过待发料数量
            var sumQty = db.Queryable<MesInvItemOutCDetails>()
                .Where(it =>
                    it.TaskNo == query.blNo && it.ItemId == stockBarcode.ItemId)
                .Sum(it => it.Quantity);
 
            //if (sumQty > remainingQty)
            //    throw new Exception(
            //        $"拆分数量:{sumQty} 大于待发料数量:{remainingQty},请核对!");
 
 
            var depots = Db.Queryable<MesDepots>()
                .Where(t => t.DepotId == stockBarcode.DepotId)
                .First();
 
            // 获取或创建出库单
            var itemOut = db.Queryable<MesInvItemOuts>()
                .Where(a => a.TaskNo == query.blNo
                            && a.DepotId == stockBarcode.DepotId
                            && a.OutDate.Value.Date.ToString("yyyy-MM-dd") ==
                            DateTime.Now.Date.ToString("yyyy-MM-dd")
                            && a.BillTypeId == 200
                            && a.TransactionNo == 209
                            && a.Status == 0)
                .First();
 
            var outId = new Guid();
            var outNo = "";
            if (itemOut == null)
            {
                // 创建新的出库单
                outId = Guid.NewGuid();
                outNo = BillNo.GetBillNo(outNoType);
 
                // 插入出库单主表
                executeCommand += db.Insertable(new MesInvItemOuts
                {
                    Guid = outId,
                    ItemOutNo = outNo,
                    TaskNo = query.blNo,
                    CreateBy = query.userName,
                    CreateDate = DateTime.Now,
                    LastupdateBy = query.userName,
                    LastupdateDate = DateTime.Now,
                    BillTypeId = 200,
                    TransactionNo = 209,
                    Remark = mesItemBl.Bl007,
                    DepotCode = depots.DepotCode,
                    OutPart = womdaa.Daa013,
                    FType = 0,
                    Factory = stockBarcode.Factory,
                    Company = stockBarcode.Company,
                    WorkNo = womdaa.Daa021,
                    BoardItem = womdaa.Daa002,
                    PbillNo = womdaa.Daa001,
                    OutDate = DateTime.Now,
                    Status = 0,
                    DepotId = stockBarcode.DepotId,
                    THORGID = stockBarcode.StockOrgId,
                    OutType = query.Type,
                    //BbillNo = query.blNo
                }).IgnoreColumns(true).ExecuteCommand();
            }
            else
            {
                outId = itemOut.Guid;
                outNo = itemOut.ItemOutNo;
            }
 
            // 检查是否已存在出库单明细
            var itemOutItemCount = db.Queryable<MesInvItemOutItems>()
                .Where(it =>
                    it.ItemOutId == outId &&
                    it.ItemId == stockBarcode.ItemId &&
                    it.DepotId == stockBarcode.DepotId.ToString())
                .Count();
 
            if (itemOutItemCount == 0)
                // 插入新的出库单明细
                executeCommand += db.Insertable(new MesInvItemOutItems
                {
                    Guid = Guid.NewGuid(),
                    ItemOutId = outId,
                    ItemNo = blDetail.Bld002,
                    Quantity = query.Num,
                    TlQty = query.Num,
                    CreateBy = query.userName,
                    CreateDate = DateTime.Now,
                    LastupdateBy = query.userName,
                    LastupdateDate = DateTime.Now,
                    Factory = stockBarcode.Factory,
                    Company = stockBarcode.Company,
                    DepotCode = depots.DepotCode,
                    TaskNo = query.blNo,
                    WorkNo = womdaa.Daa021,
                    WorkLine = blDetail.Bld013,
                    ErpItemNo = womdab.Dab003,
                    ErpId = womdab.Eid,
                    ErpAutoid = womdab.Erpid,
                    PbillNo = query.billNo,
                    ItemId = blDetail.Bld012,
                    DepotId = stockBarcode.DepotId.ToString(),
                    ItemDabid = womdab.Guid,
                    // Unit = blDetail.Bld009,
                    // DepotId = (int)stockBarcode.DepotsId
                }).IgnoreColumns(true).ExecuteCommand();
            else
                // 更新已有出库单明细数量
                executeCommand += db.Updateable<MesInvItemOutItems>()
                    .SetColumns(it => it.TlQty == (it.TlQty ?? 0) + query.Num)
                    .Where(it =>
                    it.ItemOutId == outId &&
                    it.ItemId == stockBarcode.ItemId &&
                    it.DepotId == stockBarcode.DepotId.ToString())
                    .ExecuteCommand();
 
            // 插入出库条码明细
            executeCommand += db.Insertable(new MesInvItemOutCDetails
            {
                Guid = Guid.NewGuid(),
                ItemOutId = outId,
                ItemBarcode = newBarcode ?? query.barcode,
                ItemNo = stockBarcode.ItemNo,
                LotNo = stockBarcode.LotNo,
                Quantity = query.Num,
                ForceOutFlag = 0,
                CreateBy = query.userName,
                CreateDate = DateTime.Now,
                LastupdateBy = query.userName,
                LastupdateDate = DateTime.Now,
                DepotCode = depots.DepotCode,
                DepotSectionCode = stockBarcode.DepotSectionsCode,
                Remark = blDetail.Bld010,
                Factory = stockBarcode.Factory,
                Company = stockBarcode.Company,
                TaskNoy = mesItemBl.Bl013,
                BoardStyle = mesItemBl.Bl002,
                TaskNo = query.blNo,
                WorkNo = blDetail.Bld001,
                WorkLine = blDetail.Bld013,
                SuppNo = stockBarcode.SuppNo,
                PbillNo = query.billNo,
                ItemId = blDetail.Bld012,
                Unit = blDetail.Bld009,
                DepotId = (int)stockBarcode.DepotsId,
                Dabid = womdab.Guid,
            }).IgnoreColumns(true).ExecuteCommand();
 
            // 插入业务流水
            executeCommand += db.Insertable(new MesInvBusiness2
            {
                Guid = Guid.NewGuid(),
                Status = 1,
                BillTypeId = 200, // p_bill_type_id
                TransactionCode = "209", // p_transaction_no 
                BusinessType = 1,
                ItemBarcode = newBarcode ?? query.barcode,
                ItemNo = stockBarcode.ItemNo,
                LotNo = stockBarcode.LotNo,
                EpFlag = true,
                Quantity = query.Num,
                FromInvDepotsCode = stockBarcode.DepotsCode,
                FromInvDepotSectionsCode = stockBarcode.DepotSectionsCode,
                Description = query.Type,
                CreateBy = query.userName,
                CreateDate = DateTime.Now,
                LastupdateBy = query.userName,
                LastupdateDate = DateTime.Now,
                Factory = stockBarcode.Factory,
                Company = stockBarcode.Company,
                TaskNo = mesItemBl.Bl012,
                BillNo = query.blNo,
                WorkNo = blDetail.Bld001,
                WorkLine = blDetail.Bld013,
                SuppNo = stockBarcode.SuppNo,
                ItemId = stockBarcode.ItemId
                // CkDepot = stockBarcode.DepotsId
            }).IgnoreColumns(true).ExecuteCommand();
 
            // 更新工单表数量
            executeCommand += db.Updateable<Womdab>()
                .SetColumns(it => new Womdab
                {
                    Dab007 = (it.Dab007 ?? 0) + query.Num, // 工单数量
                    Dab020 = (it.Dab020 ?? 0) + query.Num, // 已发料数量
                    Dab021 = (it.Dab021 ?? 0) + query.Num // 已发料数量
                })
                .Where(it => it.Guid == womdab.Guid && it.Dab003 == womdab.Dab003)
                .ExecuteCommand();
 
            // 更新发货通知单明细已补数量
            executeCommand += db.Updateable<MesItemBlDetail>()
                .SetColumns(it => new MesItemBlDetail
                {
                    Bld008 = (it.Bld008 ?? 0) + (int)query.Num
                })
                .Where(it => it.Id == blDetail.Id)
                .ExecuteCommand();
 
            // 获取更新后的发货通知单明细数量
            var updatedDetail = db.Queryable<MesItemBlDetail>()
                .Where(it => it.Id == blDetail.Id)
                .Select(it => new { it.Bld007, it.Bld008 })
                .First();
 
            if ((updatedDetail.Bld007 ?? 0) <= (updatedDetail.Bld008 ?? 0))
                // 更新明细完成状态
                executeCommand += db.Updateable<MesItemBlDetail>()
                    .SetColumns(it => new MesItemBlDetail { Bld011 = 1 })
                    .Where(it => it.Id == blDetail.Id)
                    .ExecuteCommand();
 
            // 检查是否还有未完成的明细
 
            var unfinishedDetail = db.Queryable<MesItemBlDetail>()
                .Where(it => it.Mid == mesItemBl.Id && (it.Bld011 ?? 0) == 0)
                .First();
 
            if (unfinishedDetail == null)
                // 如果没有找到未完成明细,则更新发货通知单状态为已完成
                executeCommand += db.Updateable<MesItemBl>()
                    .SetColumns(it => new MesItemBl
                    {
                        Bl019 = true,
                        WcUser = query.userName,
                        WcTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                    })
                    .Where(it => it.Id == mesItemBl.Id)
                    .ExecuteCommand();
 
            if (executeCommand <= 1) throw new Exception("更新失败");
 
            return executeCommand;
        }) > 0;
 
        // 获取最终的待发料明细列表
        var pendingList = Db.Queryable<MesItemBl, MesItemBlDetail>((a, b) =>
                new JoinQueryInfos(JoinType.Left, a.Id == b.Mid))
            .Where((a, b) => a.BlNo == query.blNo
                             && (b.Bld007 ?? 0) - (b.Bld008 ?? 0) > 0)
            .Select((a, b) => new MesItemBlDetail
            {
                Bld012 = b.Bld012,
                Bld002 = b.Bld002,
                Bld003 = b.Bld003,
                Bld004 = b.Bld004,
                Bld007 = b.Bld007,
                Bld008 = b.Bld008
            })
            .ToList();
 
        return (success, pendingList);
    }
 
    #endregion
 
}