1
hutongqing
2025-01-08 413f243b73ef073f4ac83889fda2f5bf6d1e727c
´úÂë¹ÜÀí/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderService.cs
@@ -1,4 +1,5 @@
using AutoMapper;
using MailKit.Search;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
@@ -43,7 +44,7 @@
        private readonly IInvokeERPService _invokeERPService;
        private readonly IOutStockLockInfoService _outStockLockInfoService;
        public OutboundOrderService(IOutboundOrderRepository BaseDal, IMapper mapper,IUnitOfWorkManage unitOfWorkManage, IBasicService basicService, IOutboundOrderDetailRepository outboundOrderDetailRepository, IStockInfoService stockInfoService,
        public OutboundOrderService(IOutboundOrderRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, IBasicService basicService, IOutboundOrderDetailRepository outboundOrderDetailRepository, IStockInfoService stockInfoService,
          IStockInfoRepository stockInfoRepository, IInvokeERPService invokeERPService, IOutStockLockInfoService outStockLockInfoService) : base(BaseDal)
        {
            _mapper = mapper;
@@ -98,30 +99,7 @@
                                OrderQuantity = model.Qty,
                                OrderId = oldOutboundOrder.Id
                            };
                            List<Dt_StockInfo>? stockInfos = new List<Dt_StockInfo>();
                            //ERP上传测试仓领料单更新对应库存状态
                            if (warehouse.WarehouseCode == WarehouseEnum.HA64.ToString())
                            {
                                WebResponseContent responseContent = TestOutStocksUpdate(warehouse, model);
                                if (!responseContent.Status)
                                {
                                    return responseContent;
                                }
                                stockInfos = responseContent.Data as List<Dt_StockInfo>;
                            }
                            _unitOfWorkManage.BeginTran();
                            if (stockInfos.Count>0)
                            {
                                _stockInfoRepository.UpdateData(stockInfos);
                                //测试架库给ERP上报出库完成
                                if (!TestOutBack(stockInfos, warehouse, model).Status)
                                {
                                    _unitOfWorkManage.RollbackTran();
                                    return WebResponseContent.Instance.Error($"测试架出库上报失败");
                                }
                            }
                            _outboundOrderDetailRepository.AddData(outboundOrderDetail);
                            _unitOfWorkManage.CommitTran();
                        }
                    }
                    else
@@ -135,41 +113,19 @@
                            OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(),
                            OrderQuantity = model.Qty,
                        };
                        Dt_OutboundOrder outboundOrder = new Dt_OutboundOrder()
                        {
                            OrderNo = model.OrderNo,
                            UpperOrderNo = model.OrderNo,
                            OrderStatus = OutOrderStatusEnum.未开始.ObjToInt(),
                            OrderType = OutOrderTypeEnum.Issue.ObjToInt(),
                            CreateType = OrderCreateTypeEnum.UpperSystemPush.ObjToInt(),
                            WarehouseId = warehouse.WarehouseId,
                            DepartmentCode = model.DepartmentCode,
                            DepartmentName = model.DepartmentName,
                            Details = new List<Dt_OutboundOrderDetail> { outboundOrderDetail }
                        };
                        List<Dt_StockInfo>? stockInfos = new List<Dt_StockInfo>();
                        //ERP上传测试仓领料单更新对应库存状态
                        if (warehouse.WarehouseCode == WarehouseEnum.HA64.ToString())
                        {
                            WebResponseContent responseContent = TestOutStocksUpdate(warehouse, model);
                            if (!responseContent.Status)
                            {
                                return responseContent;
                            }
                            stockInfos = responseContent.Data as List<Dt_StockInfo>;
                        }
                        _unitOfWorkManage.BeginTran();
                        if (stockInfos.Count > 0)
                        {
                            _stockInfoRepository.UpdateData(stockInfos);
                            //测试架库给ERP上报出库完成
                            if (!TestOutBack(stockInfos, warehouse, model).Status)
                            {
                                _unitOfWorkManage.RollbackTran();
                                return WebResponseContent.Instance.Error($"测试架出库上报失败");
                            }
                        }
                        Db.InsertNav(outboundOrder).Include(x => x.Details).ExecuteCommand();
                        _unitOfWorkManage.CommitTran();
                    }
                }
                else if (model.Way == 2)
@@ -215,21 +171,135 @@
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        //上报出库完成
        public WebResponseContent TestOutUpload(int id, List<Dt_OutStockLockInfo>? outStockLockInfos)
        {
            try
            {
                Dt_OutboundOrder outboundOrder = Db.Queryable<Dt_OutboundOrder>().Where(x => x.Id == id).Includes(x => x.Details).First();
                Dt_Warehouse warehouse = _basicService.WarehouseService.Repository.QueryFirst(x => x.WarehouseId == outboundOrder.WarehouseId);
                ERPIssueModel issueModel = new ERPIssueModel();
                List<Dt_StockInfo> stockInfos = new List<Dt_StockInfo>();
                List<Dt_OutStockLockInfo> _OutStockLockInfos = new List<Dt_OutStockLockInfo>();
                //测试架库给ERP上报出库完成
                if (outStockLockInfos.Count <= 0 || outStockLockInfos == null)
                {
                    stockInfos = TestOutStocksUpdate(warehouse).Data as List<Dt_StockInfo> ?? new List<Dt_StockInfo>();
                    List<ERPPickItemModel> eRPOutPick = new List<ERPPickItemModel>();
                    for (int i = 0; i < stockInfos.Count; i++)
                    {
                        ERPPickItemModel pickItemModel = new ERPPickItemModel()
                        {
                            Lotno = stockInfos[i].Details[0].BatchNo,
                            Qty = stockInfos[i].Details[0].StockQuantity.ObjToInt().ToString(),
                            Location = warehouse.WarehouseCode
                        };
                        eRPOutPick.Add(pickItemModel);
                    }
                    ERPPickModel pickModel = new ERPPickModel()
                    {
                        Rowindex = outboundOrder.Details[0].RowNo,
                        Material = outboundOrder.Details[0].MaterielCode,
                        Qty = (outboundOrder.Details[0].OrderQuantity.ObjToInt()).ToString(),
                        Dataitem = eRPOutPick
                    };
                    ERPIssueItemModel issueItemModel = new ERPIssueItemModel()
                    {
                        Pickcode = outboundOrder.UpperOrderNo,
                        PickList = new List<ERPPickModel>() { pickModel }
                    };
                    issueModel = new ERPIssueModel()
                    {
                        UniqueTag = id.ToString(),
                        Code = outboundOrder.OrderNo,//测试
                        WarehouseCode = warehouse.WarehouseCode,
                        Docremark = "",
                        Deptno = outboundOrder.DepartmentCode,
                        Deptname = outboundOrder.DepartmentName,
                        Createtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        Createuser = outboundOrder.Creater,
                        Issitem = new List<ERPIssueItemModel>() { issueItemModel },
                    };
                }
                else
                {
                    List<ERPPickModel> eRPPickModels = new List<ERPPickModel>();
                    //获取出库详情单
                    foreach (var item in outStockLockInfos)
                    {
                        ERPPickItemModel pickItemModel = new ERPPickItemModel()
                        {
                            Lotno = item.BatchNo,
                            Qty = item.AssignQuantity.ObjToInt().ToString(),
                            Location = item.LocationCode
                        };
                        //获取出库单明细
                        Dt_OutboundOrderDetail outboundOrderDetail = outboundOrder.Details.FirstOrDefault(x => x.Id == item.OrderDetailId);
                        ERPPickModel pickModel = new ERPPickModel()
                        {
                            Rowindex = outboundOrderDetail.RowNo,
                            Material = outboundOrderDetail.MaterielCode,
                            Qty = pickItemModel.Qty,
                            Dataitem = new List<ERPPickItemModel> { pickItemModel }
                        };
                        item.Status = OutLockStockStatusEnum.出库完成.ObjToInt();
                        eRPPickModels.Add(pickModel);
                        _OutStockLockInfos.Add(item);
                    }
                    ERPIssueItemModel issueItemModel = new ERPIssueItemModel()
                    {
                        Pickcode = outboundOrder.UpperOrderNo,
                        PickList = eRPPickModels
                    };
                    issueModel = new ERPIssueModel()
                    {
                        UniqueTag = id.ToString(),
                        Code = outboundOrder.OrderNo,//测试
                        WarehouseCode = warehouse.WarehouseCode,
                        Docremark = "",
                        Deptno = outboundOrder.DepartmentCode,
                        Deptname = outboundOrder.DepartmentName,
                        Createtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        Createuser = outboundOrder.Creater,
                        Issitem = new List<ERPIssueItemModel>() { issueItemModel },
                    };
                }
                if (stockInfos.Count > 0)
                {
                    _stockInfoRepository.UpdateData(stockInfos);
                }
                else
                {
                    _outStockLockInfoService.Repository.UpdateData(_OutStockLockInfos);
                }
                //出库完成上报ERP æµ‹è¯•注释
                _invokeERPService.InvokeOutStandardsApi(issueModel);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// æ›´æ”¹å‡ºåº“单库存状态
        /// </summary>
        /// <param name="warehouse"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public WebResponseContent TestOutStocksUpdate(Dt_Warehouse warehouse, ErpOutOrderDTO model)
        public WebResponseContent TestOutStocksUpdate(Dt_Warehouse warehouse)
        {
            try
            {
                //获取出库单库存
                List<Dt_StockInfo> stockInfos = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId).OrderBy(x => x.CreateDate).Includes(x => x.Details).Where(x => x.Details.Any(x => x.MaterielCode == model.MCode)).Take((int)(model.Qty)).ToList();
                if (model.Qty > stockInfos.Count)
                List<Dt_StockInfo> stockInfos = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId).Includes(x => x.Details).ToList();
                if (stockInfos.Count <= 0)
                {
                    return WebResponseContent.Instance.Error($"物料:{model.MCode}库存不足");
                    return WebResponseContent.Instance.Error($"库存不足");
                }
                //更改状态
                stockInfos.ForEach(x =>
@@ -243,58 +313,5 @@
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        ///推送测试架出库
        public WebResponseContent TestOutBack(List<Dt_StockInfo> stockInfos,Dt_Warehouse warehouse, ErpOutOrderDTO model)
        {
            try
            {
                //测试架库给ERP上报出库完成
                List<ERPOutPickDetail> pickDetails = stockInfos.Select(x => new ERPOutPickDetail()
                {
                    Lotno = x.Details[0].BatchNo,
                    Qty = x.Details[0].StockQuantity
                }).ToList();
                ERPOutPick outPick = new ERPOutPick()
                {
                    Rowindex = model.RowNo,
                    Material = model.MCode,
                    Qty = model.Qty,
                    Dataitem = pickDetails
                };
                ERPOutboundModel inboundModel = new ERPOutboundModel()
                {
                    Way = 1,
                    UniqueTag = model.OrderNo,
                    Code = model.OrderNo,
                    WarehouseCode = warehouse.WarehouseCode,
                    Docremark = "",
                    Deptno = model.DepartmentCode,
                    DeptName = model.DepartmentName,
                    Createtime = DateTime.Now,
                    Createuser = "WMS",
                    Picklist = new List<ERPOutPick>() { outPick },
                };
                string response = _invokeERPService.InvokeOutboundOrderApi(inboundModel);
                ErpRequestContent requestContent=response.DeserializeObject<ErpRequestContent>();
                if (requestContent.res==1)
                {
                    return WebResponseContent.Instance.OK(requestContent.Data);
                }
                else
                {
                    return WebResponseContent.Instance.Error(requestContent.Data);
                }
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        //public WebResponseContent FeedbackOutbondIssue(List<Dt_OutStockLockInfo> outStockLockInfos)
        //{
        //    ERPPickItemModel
        //}
    }
}