yanjinhui
昨天 5d756f53f07880dc8b7ba0bc9f060b9e94d4b25e
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
using HslCommunication;
using Newtonsoft.Json;
using SqlSugar;
using WIDESEA_Common;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.SquareCabin;
using WIDESEA_ISquareCabinServices;
using WIDESEA_IWMsInfoServices;
using WIDESEA_Model.Models;
using static WIDESEA_DTO.SquareCabin.OrderDto;
using static WIDESEA_DTO.SquareCabin.TowcsDto;
 
 
namespace WIDESEA_SquareCabinServices
{
    public class InventoryServices : ServiceBase<Dt_Inventory, IRepository<Dt_Inventory>>, IInventoryServices
    {
        private readonly ICabinOrderServices _cabinOrderServices;
        private readonly IDeliveryOrderServices _deliveryOrderServices;
        private readonly ISupplyTaskService _supplyTaskService;
        private readonly IInventory_BatchServices _inventoryBatchServices;
        private readonly IMessageInfoService _infoService;
        public InventoryServices(IRepository<Dt_Inventory> BaseDal, ICabinOrderServices cabinOrderServices, IDeliveryOrderServices deliveryOrderServices, ISupplyTaskService supplyTaskService, IInventory_BatchServices inventoryBatchServices, IMessageInfoService infoService) : base(BaseDal)
        {
            _cabinOrderServices = cabinOrderServices;
            _deliveryOrderServices = deliveryOrderServices;
            _supplyTaskService = supplyTaskService;
            _inventoryBatchServices = inventoryBatchServices;
            _infoService = infoService;
        }
 
        /// <summary>
        /// 获取上游库存信息
        /// </summary>
        /// <param name="goods_no">商品编码</param>
        /// <param name="batch_num">批号</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public WebResponseContent GetInventoryList(int[] keys)
        {
            var responseContent = new WebResponseContent();
            try
            {
                //根据id查询库存批次信息表,获取到他们的商品编码和批次号
                var inventoryBatches = _inventoryBatchServices.Repository.QueryData(x => keys.Contains(x.Id)); //查全部
 
                var url = "http://121.37.118.63:80/GYZ2/95fck/repositoryInfo";
                foreach (var item in inventoryBatches)
                {
                    var result = HttpHelper.Post(url, new { goods_no = item.MaterielCode, batch_num = item.BatchNo }.ToJsonString());
 
                    var response = JsonConvert.DeserializeObject<UpstreamResponse<InventoryInfo>>(result);
                    if (response == null) continue;
                    var data = response.data.FirstOrDefault();
                    if (data != null)
                        item.ERPStockQuantity = data.business_qty;
                }
                _inventoryBatchServices.UpdateData(inventoryBatches);
                return responseContent.OK("库存信息同步完成");
 
            }
            catch (Exception ex)
            {
                return responseContent.Error("同步失败: " + ex.Message);
            }
        }
 
 
        /// <summary>
        /// wcs回传给我调用我的方法 不管是入库 出库 盘点都会调用这个接口(现在用这个)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
 
        public ApiResponse<Dt_InventoryInfo> OrderFeedback(EdiOrderCallbackRequest request)
        {
            try
            {
                if (request == null || request.details == null || request.details.Count < 1) throw new Exception("请求参数无效");
                var Warecode = WarehouseEnum.立库.ObjToInt().ToString("000");
                switch (request.orderType)
                {
                    case "1": //入库
                        {
                            Dt_CabinOrder cabinOrder = _cabinOrderServices.Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == request.externalOrderNo && x.Warehouse_no == Warecode).Includes(x => x.Details).First();
                            if (cabinOrder == null) throw new Exception($"未找到入库单号【{request.externalOrderNo}】的入库单信息");
                            if (cabinOrder.OdrderStatus == "已完成")
                                return new ApiResponse<Dt_InventoryInfo> { code = "0", msg = $"成功" };
                            WebResponseContent content = _cabinOrderServices.CompleteLKInOrder(cabinOrder, request);
                            if (!content.Status) throw new Exception(content.Message);
                        }
                        break;
                    case "2":
                        {
                            Dt_DeliveryOrder deliveryOrder = _deliveryOrderServices.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == request.externalOrderNo && x.Warehouse_no == Warecode).Includes(x => x.Details).First();
                            if (deliveryOrder == null) throw new Exception($"未找到出库单号【{request.externalOrderNo}】的出库单信息");
                            if (deliveryOrder.OutStatus == "已完成")
                                return new ApiResponse<Dt_InventoryInfo> { code = "0", msg = $"成功" };
                            WebResponseContent content = _deliveryOrderServices.CompleteLKOutOrder(deliveryOrder, request);
                            if (!content.Status) throw new Exception(content.Message);
                        }
                        break;
                    case "3":
                        {
                            #region 找盘点任务
                            List<Dt_SupplyTask> supplyTasks = _supplyTaskService.Repository.QueryData(x => x.OrderNo == request.externalOrderNo && x.WarehouseCode == Warecode && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt());
                            foreach (var detail in request.details)
                            {
                                Dt_SupplyTask? supplyTask = supplyTasks.Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo).FirstOrDefault();
                                if (supplyTask == null) throw new Exception($"未找到订单号【{request.externalOrderNo}】物料编号【{detail.productCode}】的盘点任务");
                                if (detail.isLossOrProfit == 1) // 盘亏
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity - detail.ea;
                                }
                                else if (detail.isLossOrProfit == 2) // 盘盈
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity + detail.ea;
                                }
                                else // 盘中
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity;
                                }
                                var content = _deliveryOrderServices.CheckTaskFinish(supplyTask);
                                if (!content.Status) throw new Exception(content.Message);
                            }
                            #endregion
                        }
                        break;
                    default:
                        throw new Exception($"未定义的类型【{request.orderType}】");
                }
                return new ApiResponse<Dt_InventoryInfo>
                {
                    code = "0",
                    msg = $"成功"
                };
            }
            catch (Exception ex)
            {
                return new ApiResponse<Dt_InventoryInfo>
                {
                    code = "500",
                    msg = $"处理失败: {ex.Message}"
                };
            }
        }
 
        /// <summary>
        /// wcs回传给我调用我的方法 不管是入库 出库 盘点都会调用这个接口
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ApiResponse<Dt_InventoryInfo> OrderFeedback1(EdiOrderCallbackRequest request)
        {
            try
            {
                // 1️⃣ 校验请求
                if (request == null || request.details == null || !request.details.Any())
                {
                    return new ApiResponse<Dt_InventoryInfo> { code = "500", msg = "请求参数无效" };
                }
                var reslut = WarehouseEnum.立库.ObjToInt().ToString("000");
                // 2️⃣ 开启事务
                //BaseDal.Db.Ado.BeginTran();
                foreach (var detail in request.details)
                {
                    #region 查询物料信息、计算总和
                    //查询物料表
                    var goods = Db.Queryable<Dt_MaterielInfo>().Where(x => x.MaterielCode == detail.productCode).First();
                    if (goods == null) throw new Exception($"未找到物料编码【{detail.productCode}】的物料信息");
                    // 3️ 计算入库数量(取正)遍历 orderDetails 中的每个 EdiOrderBoxDto 对象
                    decimal orderQty = detail.orderDetails?
                        .Sum(x => decimal.TryParse(x.quantity, out var q) ? Math.Abs(q) : 0)
                        ?? 0;
                    #endregion
 
                    // 7️⃣ 按订单类型执行不同逻辑
                    switch (request.orderType)
                    {
                        case "1": //入库
                            {
                                #region 处理入库订单
 
                                Dt_CabinOrder cabinOrder = Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == request.externalOrderNo && x.Warehouse_no == reslut).Includes(x => x.Details).First();
                                if (cabinOrder == null) throw new Exception($"未找到入库单号【{request.externalOrderNo}】的入库单信息");
                                #region 判断是否为盘盈入库单
                                if (cabinOrder.Order_type == "5")
                                {
                                    foreach (var item in cabinOrder.Details)
                                    {
                                        Dt_InventoryInfo inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                                        .First(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num && x.LocationCode == reslut);
                                        var qty = inventoryInfo.SupplyQuantity - inventoryInfo.StockQuantity;
                                        if (orderQty != qty)
                                            throw new Exception($"盘盈入库数量不一致,上传数量【{orderQty}】,库存盘盈数量【{qty}】");
                                        inventoryInfo.StockQuantity = inventoryInfo.SupplyQuantity;
                                        inventoryInfo.SupplyQuantity = 0;
                                        inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                                        #region 添加盘盈入库任务
                                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                        {
                                            WarehouseCode = inventoryInfo.WarehouseCode,
                                            OperateType = OperateTypeEnum.自动完成.ToString(),
                                            InsertTime = DateTime.Now,
                                            TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
                                            BatchNo = inventoryInfo.BatchNo,
                                            MaterielName = inventoryInfo.MaterielName,
                                            MaterielCode = inventoryInfo.MaterielCode,
                                            MaterielSpec = inventoryInfo.MaterielSpec,
                                            TaskType = TaskTypeEnum.ChenckIn.ObjToInt(),
                                            CreateDate = DateTime.Now,
                                            Creater = App.User.UserName,
                                            LocationCode = inventoryInfo.LocationCode,
                                            OrderNo = cabinOrder.Order_no,
                                            StockQuantity = orderQty,
                                            SupplyQuantity = 0,
                                            Remark = "盘盈入库"
                                        };
                                        #endregion
                                        var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                                        .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
                                        inventory_Batch.StockQuantity = inventory_Batch.SupplyQuantity;
                                        inventory_Batch.SupplyQuantity = 0;
                                        item.OrderDetailStatus = "已完成";
                                        BaseDal.Db.Updateable(item).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
                                        BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
                                    }
                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_CabinOrder>().Where(x => x.Id == cabinOrder.Id).Includes(x => x.Details).First();
                                    if (!cabinOrder1.Details.Where(x => x.OrderDetailStatus != "已完成").Any())
                                        cabinOrder.OdrderStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
                                }
                                #endregion
                                else
                                {
                                    if (cabinOrder.OdrderStatus == "已完成")
                                        return new ApiResponse<Dt_InventoryInfo>
                                        {
                                            code = "0",
                                            msg = "成功"
                                        };
                                    Dt_CabinOrderDetail cabinOrderDetail = cabinOrder.Details.Where(x => x.Goods_no == detail.productCode && x.Batch_num == detail.batchNo && x.Status == 1).First();
                                    if (cabinOrderDetail == null || cabinOrderDetail.OrderDetailStatus == "已完成")
                                        throw new Exception($"入库单明细已完成");
                                    cabinOrderDetail.Order_Inqty += orderQty;
                                    if (cabinOrderDetail.Order_Inqty > cabinOrderDetail.Order_qty)
                                        throw new Exception($"入库数量不可超出单据数量");
                                    cabinOrder.OdrderStatus = "开始";
                                    cabinOrderDetail.OrderDetailStatus = "开始";
                                    if (cabinOrderDetail.Order_Inqty == cabinOrderDetail.Order_qty)
                                        cabinOrderDetail.OrderDetailStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrderDetail).ExecuteCommand();
                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_CabinOrder>().Where(x => x.Order_no == cabinOrder.Order_no && x.Warehouse_no == reslut).Includes(x => x.Details).First();
                                    if (!cabinOrder1.Details.Where(x => x.OrderDetailStatus != "已完成").Any())
                                        cabinOrder.OdrderStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
                                    #endregion
 
                                    #region 创建库存信息
                                    // 5️⃣ 查询库存详情
                                    var inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                                        .First(x => x.MaterielCode == cabinOrderDetail.Goods_no && x.BatchNo == cabinOrderDetail.Batch_num && x.LocationCode == "立库");
                                    if (inventoryInfo != null)
                                    {
                                        inventoryInfo.StockQuantity += orderQty;
                                        inventoryInfo.AvailableQuantity += orderQty;
                                        inventoryInfo.InDate = DateTime.Now;
                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
                                    }
                                    else
                                    {
                                        inventoryInfo = new Dt_InventoryInfo
                                        {
                                            PalletCode = detail.orderDetails?.FirstOrDefault()?.palletCode ?? "",
                                            WarehouseCode = "001",
                                            LocationCode = "立库",
                                            StockStatus = StockStatusEmun.入库完成.ObjToInt(),
                                            MaterielCode = goods.MaterielCode,
                                            MaterielName = goods.MaterielName,
                                            MaterielSpec = goods.MaterielSpec,
                                            BatchNo = detail.batchNo,
                                            StockQuantity = orderQty,
                                            OutboundQuantity = 0,
                                            SupplyQuantity = 0,
                                            AvailableQuantity = orderQty,
                                            InDate = DateTime.Now,
                                            ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                                            ShelfLife = 0,
                                            ValidityPeriod = cabinOrderDetail.Exp_date,
                                            Remark = "智能立库"
                                        };
                                        BaseDal.Db.Insertable(inventoryInfo).ExecuteCommand();
                                    }
                                    #endregion
 
                                    #region 创建库存批次信息  只有不是调拨入库单(115)才对批次信息进行修改
                                    if (cabinOrder.Order_type != InOrderTypeEnum.Allocat.ObjToInt().ToString())
                                    {
                                        // 6️⃣ 查询库存批次信息
                                        var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                                            .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
 
                                        if (inventory_Batch != null)
                                        {
                                            inventory_Batch.StockQuantity += orderQty;
                                            inventory_Batch.AvailableQuantity += orderQty;
                                            BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
                                        }
                                        else
                                        {
                                            inventory_Batch = new Dt_Inventory_Batch
                                            {
                                                MaterielCode = inventoryInfo.MaterielCode,
                                                MaterielName = inventoryInfo.MaterielName,
                                                MaterielSpec = inventoryInfo.MaterielSpec,
                                                BatchNo = inventoryInfo.BatchNo,
                                                StockQuantity = inventoryInfo.StockQuantity,
                                                OutboundQuantity = inventoryInfo.OutboundQuantity,
                                                AvailableQuantity = inventoryInfo.AvailableQuantity,
                                                SupplyQuantity = inventoryInfo.SupplyQuantity,
                                                ERPStockQuantity = 0,
                                                Status = false,
                                                ProductionDate = detail.finishDate.ToString("yyyy-MM-dd"),
                                                ValidityPeriod = inventoryInfo.ValidityPeriod.ObjToDate(),
                                                Remark = "自动创建"
                                            };
                                            BaseDal.Db.Insertable(inventory_Batch).ExecuteCommand();
                                        }
                                    }
                                    #endregion
 
                                    #region 添加入库任务历史
                                    Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                    {
                                        WarehouseCode = inventoryInfo.WarehouseCode,
                                        TaskNum = cabinOrderDetail.Id,
                                        OperateType = OperateTypeEnum.自动完成.ToString(),
                                        InsertTime = DateTime.Now,
                                        TaskStatus = SupplyStatusEnum.InFinish.ObjToInt(),
                                        BatchNo = inventoryInfo.BatchNo,
                                        MaterielName = inventoryInfo.MaterielName,
                                        MaterielCode = inventoryInfo.MaterielCode,
                                        MaterielSpec = inventoryInfo.MaterielSpec,
                                        TaskType = cabinOrder.Order_type == "1" ? TaskTypeEnum.In.ObjToInt() : TaskTypeEnum.OutReturn.ObjToInt(),
                                        CreateDate = DateTime.Now,
                                        Creater = App.User.UserName,
                                        LocationCode = inventoryInfo.LocationCode,
                                        OrderNo = cabinOrder.Order_no,
                                        StockQuantity = orderQty,
                                        SupplyQuantity = 0,
                                        Remark = "入库"
                                    };
                                    //这里这里的入库调拨单状态是115,就将调拨任务状态改为7
                                    if (cabinOrder.Order_type == InOrderTypeEnum.Allocat.ObjToInt().ToString()) supplyTask_Hty.TaskType = TaskTypeEnum.AllocatIn.ObjToInt();
                                    BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
                                    #endregion
                                }
                            }
                            break;
                        case "2":
                            {
                                Dt_DeliveryOrder cabinOrder = Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Out_no == request.externalOrderNo && x.Warehouse_no == reslut).Includes(x => x.Details).First();
                                if (cabinOrder == null) throw new Exception($"未找到出库单号【{request.externalOrderNo}】的出库单信息");
 
                                if (cabinOrder.Out_type == "6")
                                {
                                    foreach (var item in cabinOrder.Details)
                                    {
                                        Dt_InventoryInfo inventoryInfo = BaseDal.Db.Queryable<Dt_InventoryInfo>()
                                        .First(x => x.MaterielCode == item.Goods_no && x.BatchNo == item.Batch_num && x.LocationCode == reslut);
                                        var qty = inventoryInfo.StockQuantity - inventoryInfo.SupplyQuantity;
                                        if (orderQty != qty)
                                            throw new Exception($"盘亏出库数量不一致,上传数量【{orderQty}】,库存盘亏数量【{qty}】");
                                        inventoryInfo.StockQuantity = inventoryInfo.SupplyQuantity;
                                        inventoryInfo.SupplyQuantity = 0;
                                        inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                                        #region 添加盘亏出库任务
                                        Dt_SupplyTask_Hty supplyTask_Hty = new Dt_SupplyTask_Hty()
                                        {
                                            WarehouseCode = inventoryInfo.WarehouseCode,
                                            OperateType = OperateTypeEnum.自动完成.ToString(),
                                            InsertTime = DateTime.Now,
                                            TaskStatus = SupplyStatusEnum.OutFinish.ObjToInt(),
                                            BatchNo = inventoryInfo.BatchNo,
                                            MaterielName = inventoryInfo.MaterielName,
                                            MaterielCode = inventoryInfo.MaterielCode,
                                            MaterielSpec = inventoryInfo.MaterielSpec,
                                            TaskType = TaskTypeEnum.ChenckOut.ObjToInt(),
                                            CreateDate = DateTime.Now,
                                            Creater = App.User.UserName,
                                            LocationCode = inventoryInfo.LocationCode,
                                            OrderNo = cabinOrder.Out_no,
                                            StockQuantity = orderQty,
                                            SupplyQuantity = 0,
                                            Remark = "盘亏出库"
                                        };
                                        #endregion
                                        var inventory_Batch = BaseDal.Db.Queryable<Dt_Inventory_Batch>()
                                        .First(x => x.MaterielCode == inventoryInfo.MaterielCode && x.BatchNo == inventoryInfo.BatchNo);
                                        inventory_Batch.StockQuantity = inventory_Batch.SupplyQuantity;
                                        inventory_Batch.SupplyQuantity = 0;
                                        item.OotDetailStatus = "已完成";
                                        BaseDal.Db.Updateable(item).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventoryInfo).ExecuteCommand();
                                        BaseDal.Db.Updateable(inventory_Batch).ExecuteCommand();
                                        BaseDal.Db.Insertable(supplyTask_Hty).ExecuteCommand();
                                    }
                                    var cabinOrder1 = BaseDal.Db.Queryable<Dt_DeliveryOrder>().Where(x => x.Id == cabinOrder.Id).Includes(x => x.Details).First();
                                    if (!cabinOrder1.Details.Where(x => x.OotDetailStatus != "已完成").Any())
                                        cabinOrder.OutStatus = "已完成";
                                    BaseDal.Db.Updateable(cabinOrder).ExecuteCommand();
                                }
                                else
                                {
                                    #region 找出库任务
                                    Dt_SupplyTask supplyTask = Db.Queryable<Dt_SupplyTask>().Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo && x.OrderNo == request.externalOrderNo && x.WarehouseCode == reslut && x.TaskStatus == SupplyStatusEnum.NewOut.ObjToInt()).First();
                                    if (supplyTask == null) throw new Exception($"未找到订单号【{request.externalOrderNo}】物料编号【{detail.productCode}】的出库任务");
                                    if (supplyTask.StockQuantity != orderQty) throw new Exception($"订单号【{request.orderNo}】物料编号【{detail.productCode}】的出库数量与出库任务数量不相等");
                                    #endregion
 
                                    #region 处理出库单、库存信息、出库任务、调拨任务
                                    var content = _deliveryOrderServices.OutTaskFinish(supplyTask);
                                    if (!content.Status) throw new Exception(content.Message);
                                    #endregion
                                }
                            }
                            break;
                        case "3": // 盘点
                            {
                                var WarehouseLK = WarehouseEnum.立库.ObjToInt().ToString("000");
                                #region 找盘点任务
                                Dt_SupplyTask supplyTask = Db.Queryable<Dt_SupplyTask>().Where(x => x.MaterielCode == detail.productCode && x.BatchNo == detail.batchNo && x.OrderNo == request.externalOrderNo && x.WarehouseCode == WarehouseLK && x.TaskStatus == SupplyStatusEnum.NewCheck.ObjToInt()).First();
                                if (supplyTask == null) throw new Exception($"未找到订单号【{request.externalOrderNo}】物料编号【{detail.productCode}】的盘点任务");
                                #endregion
                                if (detail.isLossOrProfit == 1) // 盘亏
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity - detail.ea;
                                }
                                else if (detail.isLossOrProfit == 2) // 盘盈
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity + detail.ea;
                                }
                                else // 盘中
                                {
                                    supplyTask.SupplyQuantity = supplyTask.StockQuantity;
                                }
                                var content = _deliveryOrderServices.CheckTaskFinish(supplyTask);
                                if (!content.Status) throw new Exception(content.Message);
                            }
                            break;
                    }
                }
 
                // 9️⃣ 提交事务
                //BaseDal.Db.Ado.CommitTran();
 
                return new ApiResponse<Dt_InventoryInfo>
                {
                    code = "0",
                    msg = "成功"
                };
            }
            catch (Exception ex)
            {
                // 🔟 回滚事务
                //BaseDal.Db.Ado.RollbackTran();
 
                return new ApiResponse<Dt_InventoryInfo>
                {
                    code = "500",
                    msg = $"处理失败: {ex.Message}"
                };
 
                //Console.WriteLine(ex.Message);
            }
        }
 
 
        /// <summary>
        /// 更新入库单详细
        /// </summary>
        /// <param name="batchNo">批次号</param>
        /// <param name="productCode">物料名编码/param>
        public void UpdateInboundOrderDetailStatus(string orderNo, string batchNo, string productCode)
        {
            try
            {
                //查询详情
                var detail = BaseDal.Db.Queryable<Dt_CabinOrderDetail>()
                .First(x => x.Goods_no == productCode && x.Batch_num == batchNo && x.OrderDetailStatus == "开始" && x.Status == 1);
                //查找到后根据物料状态(OrderDetailStatus==开始) 该状态Status=1修改该条详情的OrderDetailStatus==已完成
                detail.OrderDetailStatus = "已完成";
                BaseDal.Db.Updateable(detail).ExecuteCommand();
            }
            catch (Exception ex)
            {
 
                throw;
            }
 
        }
 
        /// <summary>
        ///  //查找到后根据物料状态(OrderDetailStatus==开始) 该状态Status=1修改该条详情的OrderDetailStatus==已完成
        /// </summary>
        /// <param name="batchNo"></param>
        /// <param name="productCode"></param>
        public void UpdateOutboundOrderDetailStatus(string batchNo, string productCode)
        {
            try
            {
                //查询详情
                var detail = BaseDal.Db.Queryable<Dt_DeliveryOrderDetail>()
                .First(x => x.Goods_no == productCode && x.Batch_num == batchNo && x.OotDetailStatus == "开始" && x.Status == 1);
                detail.OotDetailStatus = "已完成";
                BaseDal.Db.Updateable(detail).ExecuteCommand();
            }
            catch (Exception ex)
            {
 
                throw;
            }
 
 
        }
 
 
        /// <summary>
        /// 推送异常信息给上游系统1.入库单接口;2.入库单报完成接口;3.出库单接口;4.出库报完成接口;5.药品基础信息同步接口;6.供应商信息接口;7.客户信息接口;8.库存
        /// </summary>
        public void SendErrorToUpstream(int type, string code, string message, string remark)
        {
            try
            {
                var url = "http://121.37.118.63:80/GYZ2/95fck/exceptionLog";
 
                var requestData = new
                {
                    type = type.ToString(),
                    code = code,
                    message = message,
                    remark = remark
                };
 
                var result = HttpHelper.Post(url, requestData.ToJsonString());
                // 可以反序列化检查 resultCode 是否为0
            }
            catch (Exception e)
            {
                // 这里不要再抛异常了,避免死循环
                Console.WriteLine("异常接口推送失败:" + e.Message);
            }
        }
 
 
    }
}