wangxinhui
2024-12-23 942e47f8987bd8c266e58863bed3895d9cb4ded0
´úÂë¹ÜÀí/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderService.cs
@@ -1,6 +1,7 @@
using AutoMapper;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -9,6 +10,7 @@
using System.Threading.Tasks;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
@@ -18,6 +20,7 @@
using WIDESEA_DTO.ERP;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Outbound;
using WIDESEA_External.ERPService;
using WIDESEA_External.Model;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundRepository;
@@ -37,16 +40,18 @@
        private readonly IStockInfoService _stockInfoService;
        private readonly IStockInfoRepository _stockInfoRepository;
        public IOutboundOrderRepository Repository => BaseDal;
        private readonly IInvokeERPService _invokeERPService;
        public OutboundOrderService(IOutboundOrderRepository BaseDal, IMapper mapper,IUnitOfWorkManage unitOfWorkManage, IBasicService basicService, IOutboundOrderDetailRepository outboundOrderDetailRepository, IStockInfoService stockInfoService,
          IStockInfoRepository stockInfoRepository) : base(BaseDal)
          IStockInfoRepository stockInfoRepository, IInvokeERPService invokeERPService) : base(BaseDal)
        {
            _mapper = mapper;
            _unitOfWorkManage=unitOfWorkManage;
            _basicService = basicService;
            _outboundOrderDetailRepository = outboundOrderDetailRepository;
            _stockInfoService = stockInfoService;
            _stockInfoRepository= stockInfoRepository;
            _stockInfoRepository = stockInfoRepository;
            _invokeERPService = invokeERPService;
        }
        public WebResponseContent ReceiveOutOrder(ErpOutOrderDTO model)
@@ -81,7 +86,6 @@
                        }
                        else
                        {
                            Dt_StockInfo? stockInfo = null;
                            Dt_OutboundOrderDetail outboundOrderDetail = new Dt_OutboundOrderDetail()
                            {
                                RowNo = Convert.ToInt32(model.RowNo),
@@ -92,17 +96,27 @@
                                OrderQuantity = model.Qty,
                                OrderId = oldOutboundOrder.Id
                            };
                            List<Dt_StockInfo>? stockInfos = new List<Dt_StockInfo>();
                            //ERP上传测试仓领料单更新对应库存状态
                            if (warehouse.WarehouseId == 1)
                            if (warehouse.WarehouseCode == WarehouseEnum.HA64.ToString())
                            {
                                //获取出库单库存
                                stockInfo = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId).Includes(x => x.Details).Where(x => x.Details.Any(x => x.MaterielCode == model.MCode && x.BatchNo == model.MLot && x.InboundOrderRowNo == Convert.ToInt32(model.RowNo))).First();
                                stockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                                WebResponseContent responseContent = TestOutStocksUpdate(warehouse, model);
                                if (!responseContent.Status)
                                {
                                    return responseContent;
                                }
                                stockInfos = responseContent.Data as List<Dt_StockInfo>;
                            }
                            _unitOfWorkManage.BeginTran();
                            if (stockInfo != null)
                            if (stockInfos.Count>0)
                            {
                                _stockInfoRepository.UpdateData(stockInfo);
                                _stockInfoRepository.UpdateData(stockInfos);
                                //测试架库给ERP上报出库完成
                                if (!TestOutBack(stockInfos, warehouse, model).Status)
                                {
                                    _unitOfWorkManage.RollbackTran();
                                    return WebResponseContent.Instance.Error($"测试架出库上报失败");
                                }
                            }
                            _outboundOrderDetailRepository.AddData(outboundOrderDetail);
                            _unitOfWorkManage.CommitTran();
@@ -129,21 +143,31 @@
                            WarehouseId = warehouse.WarehouseId,
                            Details = new List<Dt_OutboundOrderDetail> { outboundOrderDetail }
                        };
                        Dt_StockInfo? stockInfo = null;
                        //ERP上传测试仓领料单更新对应库存状态
                        if (warehouse.WarehouseId==1)
                        List<Dt_StockInfo>? stockInfos = new List<Dt_StockInfo>();
                        //ERP上传测试仓领料单更新对应库存状态
                        if (warehouse.WarehouseCode == WarehouseEnum.HA64.ToString())
                        {
                            //获取出库单库存
                            stockInfo = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId).Includes(x => x.Details).Where(x => x.Details.Any(x => x.MaterielCode == model.MCode && x.BatchNo == model.MLot && x.InboundOrderRowNo == Convert.ToInt32(model.RowNo))).First();
                            stockInfo.StockStatus=StockStatusEmun.入库完成.ObjToInt();
                            WebResponseContent responseContent = TestOutStocksUpdate(warehouse, model);
                            if (!responseContent.Status)
                            {
                                return responseContent;
                            }
                            stockInfos = responseContent.Data as List<Dt_StockInfo>;
                        }
                        _unitOfWorkManage.BeginTran();
                        if (stockInfo!=null)
                        if (stockInfos.Count > 0)
                        {
                            _stockInfoRepository.UpdateData(stockInfo);
                            _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)
@@ -189,7 +213,82 @@
                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)
        {
            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)
                {
                    return WebResponseContent.Instance.Error($"物料:{model.MCode}库存不足");
                }
                //更改状态
                stockInfos.ForEach(x =>
                {
                    x.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                });
                return WebResponseContent.Instance.OK("成功", stockInfos);
            }
            catch (Exception ex)
            {
                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)
        //{