yanjinhui
2025-11-12 b790305873e20a7f26d63c71623210964105014e
н¨Îļþ¼Ð/WIDESEA_WMSServer/WIDESEA_SquareCabinServices/CabinOrderServices.cs
@@ -9,6 +9,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using WIDESEA_Common;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.OrderEnum;
@@ -502,6 +503,338 @@
        #endregion
        #region åˆ›å»ºç›˜ç›ˆå…¥åº“单
        //public WebResponseContent CreateCheckInOrder(UpstreamOrderInfo order)
        //{
        //    WebResponseContent content = new WebResponseContent();
        //    try
        //    {
        //        string WareCodeMJ = WarehouseEnum.麻精库.ObjToInt().ToString("000");
        //        string WareCodeLD = WarehouseEnum.冷冻库.ObjToInt().ToString("000");
        //        List<Dt_SupplyTask_Hty> supplyTask_Hties = new List<Dt_SupplyTask_Hty>();
        //        List<Dt_Inventory_Batch> batchesUp = new List<Dt_Inventory_Batch>();
        //        List<Dt_InventoryInfo> infosUp = new List<Dt_InventoryInfo>();
        //        var codes = order.details.Select(x => x.goods_no).ToList();
        //        #region ç‰¹æ®Šåº“房
        //        if (order.warehouse_no == WareCodeMJ || order.warehouse_no == WareCodeLD)
        //        {
        //            List<Dt_Inventory_Batch> inventory_Batchs = _inventory_BatchServices.Repository.QueryData(x => codes.Contains(x.MaterielCode));
        //            List<Dt_InventoryInfo> _InventoryInfos = _inventoryInfoService.Repository.QueryData(x => codes.Contains(x.MaterielCode));
        //            #region åº“存、库存批次平账
        //            foreach (var item in order.details)
        //            {
        //                //找库存批次信息
        //                Dt_Inventory_Batch inventory_Batch = inventory_Batchs.Where(x => x.MaterielCode == item.goods_no && x.BatchNo == item.batch_num).First();
        //                if (inventory_Batch.SupplyQuantity != item.order_qty)
        //                {
        //                    _messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "ERP报报溢入库错误", $"盘盈入库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘盈数量有误");
        //                    throw new Exception($"盘盈入库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘盈数量有误");
        //                }
        //                //找所有库存
        //                List<Dt_InventoryInfo> inventoryInfos = _InventoryInfos.Where(x => x.MaterielCode == inventory_Batch.MaterielCode && x.BatchNo == inventory_Batch.BatchNo).ToList();
        //                foreach (var inventoryInfo in inventoryInfos)
        //                {
        //                    #region æ·»åŠ ç›˜ç›ˆå…¥åº“ä»»åŠ¡
        //                    if (inventoryInfo.SupplyQuantity != 0)
        //                    {
        //                        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 = order.order_no,
        //                            StockQuantity = inventoryInfo.SupplyQuantity,
        //                            SupplyQuantity = 0,
        //                            Remark = "盘盈入库"
        //                        };
        //                        //_supplyTaskHtyService.AddData(supplyTask_Hty);
        //                        supplyTask_Hties.Add(supplyTask_Hty);
        //                    }
        //                    #endregion
        //                    inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
        //                    inventoryInfo.AvailableQuantity = inventoryInfo.StockQuantity;
        //                    inventoryInfo.SupplyQuantity = 0;
        //                    inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
        //                }
        //                infosUp.AddRange(inventoryInfos);
        //                inventory_Batch.StockQuantity += inventory_Batch.SupplyQuantity;
        //                inventory_Batch.AvailableQuantity = inventory_Batch.StockQuantity;
        //                inventory_Batch.SupplyQuantity = 0;
        //                batchesUp.Add(inventory_Batch);
        //                //_inventoryInfoService.UpdateData(inventoryInfos);
        //                //_inventory_BatchServices.UpdateData(inventory_Batch);
        //            }
        //            #endregion
        //            #region åˆ›å»ºç›˜ç‚¹å•
        //            var entityOrder = new Dt_CabinOrder
        //            {
        //                Order_no = order.order_no,
        //                Order_type = order.order_type,
        //                Supplier_no = order.supplier_no,
        //                Account_tiem = order.account_time,
        //                OdrderStatus = "已完成",
        //                Supplier_name = order.supplier_name,
        //                Warehouse_no = order.warehouse_no,
        //                Details = order.details.Select(d => new Dt_CabinOrderDetail
        //                {
        //                    Goods_no = d.goods_no,
        //                    Order_qty = Math.Abs(d.order_qty),
        //                    Order_Inqty = Math.Abs(d.order_qty),
        //                    Batch_num = d.batch_num,
        //                    Exp_date = d.exp_date,
        //                    Reservoirarea = order.warehouse_no,
        //                    OrderDetailStatus = "已完成",
        //                    Status = 2,
        //                }).ToList()
        //            };
        //            _unitOfWorkManage.BeginTran();
        //            if (supplyTask_Hties.Count > 0) _supplyTaskHtyService.AddData(supplyTask_Hties);
        //            if (infosUp.Count > 0) _inventoryInfoService.UpdateData(infosUp);
        //            if (batchesUp.Count > 0) _inventory_BatchServices.UpdateData(batchesUp);
        //            BaseDal.Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
        //            _unitOfWorkManage.CommitTran();
        //            //Repository.AddData(entityOrder);
        //            #endregion
        //        }
        //        #endregion
        //        else
        //        {
        //            List<Dt_CabinOrder> cabinOrdersAdd = new List<Dt_CabinOrder>();
        //            string WareCodeLK = WarehouseEnum.立库.ObjToInt().ToString("000");
        //            string WareCodeDJ = WarehouseEnum.大件库.ObjToInt().ToString("000");
        //            List<Dt_Inventory_Batch> inventory_Batchs = _inventory_BatchServices.Repository.QueryData(x => codes.Contains(x.MaterielCode)).ToList();
        //            List<Dt_InventoryInfo> _InventoryInfos = _inventoryInfoService.Repository.QueryData(x => codes.Contains(x.MaterielCode)).ToList();
        //            foreach (var item in order.details)
        //            {
        //                //找库存批次信息
        //                Dt_Inventory_Batch inventory_Batch = inventory_Batchs.Where(x => x.MaterielCode == item.goods_no && x.BatchNo == item.batch_num).First();
        //                var Qty = Math.Abs(inventory_Batch.SupplyQuantity);
        //                if (Qty != item.order_qty)
        //                {
        //                    _messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "ERP报报溢入库错误", $"盘盈入库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘盈数量有误");
        //                    throw new Exception($"盘盈入库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘盈数量有误");
        //                }
        //                //找所有库存
        //                List<Dt_InventoryInfo> inventoryInfos = _InventoryInfos.Where(x => x.MaterielCode == inventory_Batch.MaterielCode && x.BatchNo == inventory_Batch.BatchNo).ToList();
        //                //获取立库盘点差异数
        //                var inventoryLK = inventoryInfos.Where(x => x.WarehouseCode == WareCodeLK).FirstOrDefault(); //修改这里有可能只有大件库才会存在差异
        //                //var LkQty = Math.Abs(inventoryLK.SupplyQuantity);
        //                var LkQty = inventoryLK?.SupplyQuantity ?? 0;
        //                //获取大件库盘点差异数
        //                var inventoryDJ = inventoryInfos.Where(x => x.WarehouseCode == WareCodeDJ).ToList();
        //                //var DJQty = Math.Abs(inventoryDJ.Sum(x => x.SupplyQuantity));
        //                var DJQty = inventoryDJ.Sum(x => x.SupplyQuantity);
        //                if (LkQty + DJQty != Qty)
        //                {
        //                    _messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "ERP报报溢入库错误", $"盘盈入库单【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的盘盈数量有误");
        //                    throw new Exception($"【{order.order_no}】物料编号【{item.goods_no}】物料批次【{item.batch_num}】的物料信息与物料批次信息盘盈数量不符");
        //                }
        //                if (LkQty == 0)//立库无差异
        //                {
        //                    #region åº“存、库存批次平账
        //                    foreach (var inventoryInfo in inventoryInfos)
        //                    {
        //                        #region æ·»åŠ ç›˜ç›ˆå…¥åº“ä»»åŠ¡
        //                        if (inventoryInfo.StockQuantity != inventoryInfo.SupplyQuantity)
        //                        {
        //                            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 = order.order_no,
        //                                StockQuantity = Math.Abs(inventoryInfo.SupplyQuantity),
        //                                SupplyQuantity = 0,
        //                                Remark = "盘盈入库"
        //                            };
        //                            //_supplyTaskHtyService.AddData(supplyTask_Hty);
        //                            supplyTask_Hties.Add(supplyTask_Hty);
        //                        }
        //                        #endregion
        //                        inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
        //                        inventoryInfo.AvailableQuantity = inventoryInfo.StockQuantity;
        //                        inventoryInfo.SupplyQuantity = 0;
        //                        inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
        //                    }
        //                    //_inventoryInfoService.UpdateData(inventoryInfos);
        //                    infosUp.AddRange(inventoryInfos);
        //                    inventory_Batch.StockQuantity += inventory_Batch.SupplyQuantity;
        //                    inventory_Batch.AvailableQuantity = inventory_Batch.StockQuantity;
        //                    inventory_Batch.SupplyQuantity = 0;
        //                    //_inventory_BatchServices.UpdateData(inventory_Batch);
        //                    batchesUp.Add(inventory_Batch);
        //                    #endregion
        //                    #region åˆ›å»ºå¤§ä»¶åº“盘点单
        //                    var entityOrder = new Dt_CabinOrder
        //                    {
        //                        Order_no = order.order_no,
        //                        Order_type = order.order_type,
        //                        Supplier_no = order.supplier_no,
        //                        Account_tiem = order.account_time,
        //                        OdrderStatus = "已完成",
        //                        Supplier_name = order.supplier_name,
        //                        Warehouse_no = WareCodeDJ,
        //                        Details = order.details.Select(d => new Dt_CabinOrderDetail
        //                        {
        //                            Goods_no = d.goods_no,
        //                            Order_qty = Math.Abs(d.order_qty),
        //                            Order_Inqty = Math.Abs(d.order_qty),
        //                            Batch_num = d.batch_num,
        //                            Exp_date = d.exp_date,
        //                            Reservoirarea = WareCodeDJ,
        //                            OrderDetailStatus = "已完成",
        //                            Status = 2,
        //                        }).ToList()
        //                    };
        //                    //Repository.AddData(entityOrder);
        //                    //Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
        //                    cabinOrdersAdd.Add(entityOrder);
        //                    #endregion
        //                }
        //                else // LkQty != 0 è¡¨ç¤ºç«‹åº“有差异
        //                {
        //                    #region å¤§ä»¶åº“库存平账
        //                    inventoryInfos = inventoryInfos.Where(x => x.WarehouseCode == WareCodeDJ).ToList();
        //                    foreach (var inventoryInfo in inventoryInfos)
        //                    {
        //                        #region æ·»åŠ ç›˜ç›ˆå…¥åº“ä»»åŠ¡
        //                        if (inventoryInfo.StockQuantity != inventoryInfo.SupplyQuantity)
        //                        {
        //                            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 = order.order_no,
        //                                StockQuantity = Math.Abs(inventoryInfo.SupplyQuantity),
        //                                SupplyQuantity = 0,
        //                                Remark = "盘盈入库"
        //                            };
        //                            //_supplyTaskHtyService.AddData(supplyTask_Hty);
        //                            supplyTask_Hties.Add(supplyTask_Hty);
        //                        }
        //                        #endregion
        //                        inventoryInfo.StockQuantity += inventoryInfo.SupplyQuantity;
        //                        inventoryInfo.AvailableQuantity = inventoryInfo.StockQuantity;
        //                        inventoryInfo.SupplyQuantity = 0;
        //                        inventoryInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
        //                    }
        //                    //_inventoryInfoService.UpdateData(inventoryInfos);
        //                    infosUp.AddRange(inventoryInfos);
        //                    #endregion
        //                    #region åˆ›å»ºå¤§ä»¶åº“盘点单
        //                    if (DJQty != 0)
        //                    {
        //                        var cabinOrder = new Dt_CabinOrder
        //                        {
        //                            Order_no = order.order_no,
        //                            Order_type = order.order_type,
        //                            Supplier_no = order.supplier_no,
        //                            Account_tiem = order.account_time,
        //                            OdrderStatus = "已完成",
        //                            Supplier_name = order.supplier_name,
        //                            Warehouse_no = WareCodeDJ,
        //                            Details = order.details.Select(d => new Dt_CabinOrderDetail
        //                            {
        //                                Goods_no = d.goods_no,
        //                                Order_qty = DJQty,
        //                                Order_Inqty = DJQty,
        //                                Batch_num = d.batch_num,
        //                                Exp_date = d.exp_date,
        //                                Reservoirarea = WareCodeDJ,
        //                                OrderDetailStatus = "已完成",
        //                                Status = 2,
        //                            }).ToList()
        //                        };
        //                        //Repository.AddData(cabinOrder);
        //                        //Db.InsertNav(cabinOrder).Include(it => it.Details).ExecuteCommand();
        //                        cabinOrdersAdd.Add(cabinOrder);
        //                    }
        //                    #endregion
        //                    #region åˆ›å»ºç«‹åº“盘点单
        //                    var entityOrder = new Dt_CabinOrder
        //                    {
        //                        Order_no = order.order_no,
        //                        Order_type = order.order_type,
        //                        Supplier_no = order.supplier_no,
        //                        Account_tiem = order.account_time,
        //                        OdrderStatus = "新建",
        //                        Supplier_name = order.supplier_name,
        //                        Warehouse_no = WareCodeLK,
        //                        Details = order.details.Select(d => new Dt_CabinOrderDetail
        //                        {
        //                            Goods_no = d.goods_no,
        //                            Order_qty = Math.Abs(LkQty),
        //                            Batch_num = d.batch_num,
        //                            Exp_date = d.exp_date,
        //                            Reservoirarea = WareCodeLK,
        //                            OrderDetailStatus = "新建",
        //                            Status = 0,
        //                        }).ToList()
        //                    };
        //                    //Repository.AddData(entityOrder);
        //                    //Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
        //                    cabinOrdersAdd.Add(entityOrder);
        //                    #endregion
        //                }
        //            }
        //            _unitOfWorkManage.BeginTran();
        //            if (supplyTask_Hties.Count > 1) _supplyTaskHtyService.AddData(supplyTask_Hties);
        //            if (infosUp.Count > 0) _inventoryInfoService.UpdateData(infosUp);
        //            if (batchesUp.Count > 0) _inventory_BatchServices.UpdateData(batchesUp);
        //            if (cabinOrdersAdd.Count > 0) BaseDal.Db.InsertNav(cabinOrdersAdd).Include(it => it.Details).ExecuteCommand();
        //            _unitOfWorkManage.CommitTran();
        //            // æ·»åŠ äº‹åŠ¡æäº¤åŽçš„æ£€æŸ¥
        //            Console.WriteLine($"事务提交完成,准备返回。cabinOrdersAdd数量: {cabinOrdersAdd.Count}");
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        _unitOfWorkManage.RollbackTran();
        //        //_messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "创建盘盈入库单", ex.Message);
        //        //return content.Error(ex.Message);
        //        throw ex;
        //    }
        //    //return content;
        //    return content.OK($"盘盈入库单 {order.order_no} åˆ›å»ºæˆåŠŸ");
        //}
        #endregion
        #region åˆ›å»ºç›˜ç›ˆå…¥åº“单(修改原版因为前面订单创建失败导致后面也创建不了问题)
        public WebResponseContent CreateCheckInOrder(UpstreamOrderInfo order)
        {
            WebResponseContent content = new WebResponseContent();
@@ -573,7 +906,6 @@
                        //_inventory_BatchServices.UpdateData(inventory_Batch);
                    }
                    #endregion
                    #region åˆ›å»ºç›˜ç‚¹å•
                    var entityOrder = new Dt_CabinOrder
                    {
@@ -596,13 +928,14 @@
                            Status = 2,
                        }).ToList()
                    };
                    _unitOfWorkManage.BeginTran();
                    _supplyTaskHtyService.AddData(supplyTask_Hties);
                    _inventoryInfoService.UpdateData(infosUp);
                    _inventory_BatchServices.UpdateData(batchesUp);
                    BaseDal.Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
                    _unitOfWorkManage.CommitTran();
                    //Repository.AddData(entityOrder);
                    using (var scop = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        if (supplyTask_Hties.Count > 0) _supplyTaskHtyService.AddData(supplyTask_Hties);
                        if (infosUp.Count > 0) _inventoryInfoService.UpdateData(infosUp);
                        if (batchesUp.Count > 0) _inventory_BatchServices.UpdateData(batchesUp);
                        BaseDal.Db.InsertNav(entityOrder).Include(it => it.Details).ExecuteCommand();
                        scop.Complete();
                    }
                    #endregion
                }
                #endregion
@@ -627,9 +960,9 @@
                        //找所有库存
                        List<Dt_InventoryInfo> inventoryInfos = _InventoryInfos.Where(x => x.MaterielCode == inventory_Batch.MaterielCode && x.BatchNo == inventory_Batch.BatchNo).ToList();
                        //获取立库盘点差异数
                        var inventoryLK = inventoryInfos.Where(x => x.WarehouseCode == WareCodeLK).First();
                        var inventoryLK = inventoryInfos.Where(x => x.WarehouseCode == WareCodeLK).FirstOrDefault(); //修改这里有可能只有大件库才会存在差异
                        //var LkQty = Math.Abs(inventoryLK.SupplyQuantity);
                        var LkQty = inventoryLK.SupplyQuantity;
                        var LkQty = inventoryLK?.SupplyQuantity ?? 0;
                        //获取大件库盘点差异数
                        var inventoryDJ = inventoryInfos.Where(x => x.WarehouseCode == WareCodeDJ).ToList();
@@ -797,7 +1130,7 @@
                                Details = order.details.Select(d => new Dt_CabinOrderDetail
                                {
                                    Goods_no = d.goods_no,
                                    Order_qty =Math.Abs( LkQty),
                                    Order_qty = Math.Abs(LkQty),
                                    Batch_num = d.batch_num,
                                    Exp_date = d.exp_date,
                                    Reservoirarea = WareCodeLK,
@@ -811,25 +1144,27 @@
                            #endregion
                        }
                    }
                    _unitOfWorkManage.BeginTran();
                    _supplyTaskHtyService.AddData(supplyTask_Hties);
                    _inventoryInfoService.UpdateData(infosUp);
                    _inventory_BatchServices.UpdateData(batchesUp);
                    BaseDal.Db.InsertNav(cabinOrdersAdd).Include(it => it.Details).ExecuteCommand();
                    _unitOfWorkManage.CommitTran();
                    using (var scop = new TransactionScope(TransactionScopeOption.RequiresNew))
                    {
                        if (supplyTask_Hties.Count > 0) _supplyTaskHtyService.AddData(supplyTask_Hties);
                        if (infosUp.Count > 0) _inventoryInfoService.UpdateData(infosUp);
                        if (batchesUp.Count > 0) _inventory_BatchServices.UpdateData(batchesUp);
                        if (cabinOrdersAdd.Count > 0) BaseDal.Db.InsertNav(cabinOrdersAdd).Include(it => it.Details).ExecuteCommand();
                        scop.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                //_messageInfoService.AddMessageInfo(MessageGroupByEnum.InOrderAlarm, "创建盘盈入库单", ex.Message);
                content.Error(ex.Message);
                throw ex;
            }
            return content;
            //return content;
            return content.OK($"盘盈入库单创建成功");
        }
        #endregion
        /// <summary>
        /// äººå·¥å…¥åº“完成
        /// </summary>