647556386
2025-10-18 d01658c63cd541fe4ea5cec5c4bd7f23b9408cdb
WMS/WIDESEA_WMSServer/WIDESEA_InboundService/Base/InboundOrderService.cs
@@ -1,22 +1,29 @@
using AutoMapper;
using MailKit.Search;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_BasicRepository;
using WIDESEA_Common;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.CodeConfigEnum;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
using WIDESEA_DTO;
using WIDESEA_DTO.Basic;
using WIDESEA_DTO.ERP;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Outbound;
using WIDESEA_IBasicRepository;
@@ -24,9 +31,11 @@
using WIDESEA_IInboundRepository;
using WIDESEA_IInboundService;
using WIDESEA_InboundRepository;
using WIDESEA_IStockRepository;
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoRepository;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Inbound;
using static WIDESEA_Common.HouseInventoryIn;
namespace WIDESEA_InboundService
@@ -43,10 +52,14 @@
        private IInboundOrderDetailService _inboundOrderDetailService;
        private IInboundOrder_HtyService _inboundOrderHtyService;
        private IInboundOrderDetail_HtyService _inboundOrderDetail_HtyService;
        private IWarehouseService _warehouseService;
        private readonly IStockRepository _stockRepository;
        private IPalletTypeInfoRepository _palletTypeInfoRepository;
        private readonly IReturnOrderRepository _returnOrderRepository;
        public IInboundOrderRepository Repository => BaseDal;
        public InboundOrderService(IInboundOrderRepository BaseDal, IMapper mapper, IBasicService basicService, IBasicRepository basicRepository, IInboundRepository inboundRepository, IUnitOfWorkManage unitOfWorkManage, ITaskRepository taskRepository, IStockService stockService, IInboundOrderDetailService inboundOrderDetailService, IInboundOrder_HtyService inboundOrderHtyService, IInboundOrderDetail_HtyService inboundOrderDetail_HtyService) : base(BaseDal)
        public InboundOrderService(IInboundOrderRepository BaseDal, IMapper mapper, IBasicService basicService, IBasicRepository basicRepository, IInboundRepository inboundRepository, IUnitOfWorkManage unitOfWorkManage, ITaskRepository taskRepository, IStockService stockService, IInboundOrderDetailService inboundOrderDetailService, IInboundOrder_HtyService inboundOrderHtyService, IInboundOrderDetail_HtyService inboundOrderDetail_HtyService, IWarehouseService warehouseService, IStockRepository stockRepository, IPalletTypeInfoRepository palletTypeInfoRepository,IReturnOrderRepository returnOrderRepository) : base(BaseDal)
        {
            _mapper = mapper;
            _unitOfWorkManage = unitOfWorkManage;
@@ -58,6 +71,339 @@
            _inboundOrderHtyService = inboundOrderHtyService;
            _inboundOrderDetail_HtyService = inboundOrderDetail_HtyService;
            _basicService = basicService;
            _warehouseService = warehouseService;
            _stockRepository = stockRepository;
            _palletTypeInfoRepository = palletTypeInfoRepository;
            _returnOrderRepository = returnOrderRepository;
        }
        /// <summary>
        /// 组盘
        /// </summary>
        /// <param name="materielGroupDTO"></param>
        /// <returns></returns>
        public WebResponseContent MaterielGroup(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var orderNo = saveModel.MainData["orderNo"].ToString();
                var palletCode = saveModel.MainData["palletCode"].ToString();
                var warehouseId = saveModel.MainData["warehouseId"].ObjToInt();
                var Initiallife = saveModel.MainData["initiallife"].ObjToInt();
                List<string> serialNumbers = new List<string>();
                List<decimal> quantitys = new List<decimal>();
                foreach (var item in saveModel.DelKeys)
                {
                    string json = JsonConvert.SerializeObject(item);
                    Dictionary<string, object> delKeyDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
                    if (delKeyDict.TryGetValue("serialNumber", out object serialNumberObj))
                    {
                        string serialNumber = serialNumberObj?.ToString();
                        if (!string.IsNullOrEmpty(serialNumber))
                        {
                            serialNumbers.Add(serialNumber);
                        }
                        else
                        {
                            return WebResponseContent.Instance.Error("serialNumber的值为空");
                        }
                    }
                    if (delKeyDict.TryGetValue("quantity", out object quantityObj))
                    {
                        if (quantityObj == null)
                        {
                            return WebResponseContent.Instance.Error("物料数量的值为空");
                        }
                        decimal quantity;
                        try
                        {
                            quantity = Convert.ToDecimal(quantityObj);
                        }
                        catch
                        {
                            return WebResponseContent.Instance.Error($"物料数量{quantityObj}无法转换为数字");
                        }
                        if (quantity <= 0)
                        {
                            return WebResponseContent.Instance.Error("物料数量必须大于0");
                        }
                        quantitys.Add(quantity);
                    }
                    else
                    {
                        return WebResponseContent.Instance.Error("缺少物料数量字段");
                    }
                }
                Dt_Warehouse warehouse = _warehouseService.Repository.QueryFirst(x => x.WarehouseId == warehouseId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该仓库信息");
                }
                Dt_InboundOrder inboundOrder = BaseDal.Db.Queryable<Dt_InboundOrder>().Where(x => x.OrderNo == orderNo && x.WarehouseId == warehouse.WarehouseId).Includes(x => x.Details).First();
                if (inboundOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单信息");
                }
                if (inboundOrder.Details == null || inboundOrder.Details.Count <= 0)
                {
                    return WebResponseContent.Instance.Error($"未找到入库单明细信息");
                }
                List<MatSerNumAnalysisModel> models = CodeAnalysisHelper.CodeAnalysis<MatSerNumAnalysisModel>(AnalysisCodeEnum.MatSerNumAnalysis, serialNumbers);
                //验证判断时间格式
                WebResponseContent IsValidContent = IsValidMCDates(models);
                if (!IsValidContent.Status)
                {
                    return content.Error(IsValidContent.Message);
                }
                string materielCode = models.FirstOrDefault()?.MaterielCode ?? "";
                Dt_MaterielInfo materielInfo = _basicRepository.MaterielInfoRepository.QueryFirst(x => x.MaterielCode == materielCode);
                if (materielInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该物料的信息");
                }
                ///找数量匹配的
                List<Dt_InboundOrderDetail> inboundOrderDetails = inboundOrder.Details.Where(x => x.MaterielCode == materielCode && x.SupplierBatch == (models.FirstOrDefault()?.LotNo ?? "")&& x.OrderQuantity >x.ReceiptQuantity && x.OrderQuantity == (quantitys.FirstOrDefault())).ToList();
                if (inboundOrderDetails == null || inboundOrderDetails.Count <= 0)
                {
                     return WebResponseContent.Instance.Error($"未在入库单明细中找到该物料信息或数量不匹配");
                }
                var inboundOrderDet = inboundOrderDetails.FirstOrDefault();
                //Dt_StockInfo? stockInfo = _stockService.StockInfoService.GetStockByPalletCode(palletCode);
                decimal beforeQuantity = 0;
                Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == palletCode).Includes(x => x.Details).First();
                if (stockInfo == null)
                {
                    stockInfo = new Dt_StockInfo()
                    {
                        BatchNo = inboundOrderDet.BatchNo,
                        PalletCode = palletCode,
                        PalletType = GetPalletType(warehouse, palletCode),//GetPalletType(warehouse, palletCode)
                        IsFull = true,
                        StockStatus = (int)StockStatusEmun.组盘暂存,
                        Creater = "WMS",
                        CreateDate = DateTime.Now,
                        MaterialType = (int)InventoryMaterialType.原材料,
                        Materialweight = 0,
                        Wlstatus = (int)InventoryMaterialStatus.合格,
                        Mgeneratetime = DateTime.Now,
                        WarehouseId = warehouse.WarehouseId,
                        Details = new List<Dt_StockInfoDetail>()
                    };
                }
                else
                {
                    if (stockInfo.StockStatus != StockStatusEmun.组盘暂存.ObjToInt())
                    {
                        return WebResponseContent.Instance.Error($"托盘号重复");
                    }
                    beforeQuantity = stockInfo.Details.Sum(x => x.StockQuantity);
                }
                if (warehouse.WarehouseCode == WarehouseEnum.SC01_BC.ToString())
                {
                    stockInfo.Remark = Initiallife.ToString();
                    string batchNo = models.FirstOrDefault()?.LotNo ?? "";
                    //Dt_StockInfoDetail existDetail = _stockRepository.StockInfoDetailRepository.QueryFirst(x => x.BatchNo == batchNo);
                    //if (existDetail != null)
                    //{
                    //    return WebResponseContent.Instance.Error($"{batchNo}测试架已存在");
                    //}
                    if (models.Count >= 2)
                    {
                        return WebResponseContent.Instance.Error($"组盘明细不唯一");
                    }
                    //if (palletCode.Substring(0, 1) == "6")
                    //{
                    //    stockInfo.PalletType = PalletTypeEnum.MediumPallet.ObjToInt();
                    //}
                    //else
                    //{
                    //    stockInfo.PalletType = PalletTypeEnum.LargestPallet.ObjToInt();
                    //}
                }
                else if (warehouse.WarehouseCode == WarehouseEnum.SC01_BC.ToString())
                {
                    if (models.Count >= 2)
                    {
                        return WebResponseContent.Instance.Error($"组盘明细不唯一");
                    }
                }
                List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
                foreach (var model in models)
                {
                    Dt_InboundOrderDetail? notGroupDetail = inboundOrderDetails.Where(x => x.OrderDetailStatus <= OrderDetailStatusEnum.Inbounding.ObjToInt() && x.MaterielCode == model.MaterielCode && x.SupplierBatch== model.LotNo && x.OrderQuantity > x.ReceiptQuantity).FirstOrDefault();
                    if (notGroupDetail == null)
                    {
                        return WebResponseContent.Instance.Error($"该物料在该入库单中已全部组盘完成");
                    }
                    Dt_StockInfoDetail stockInfoDetail = new Dt_StockInfoDetail()
                    {
                        MaterielCode = inboundOrderDet.MaterielCode,
                        MaterielName = inboundOrderDet.MaterielName,
                        OrderNo = inboundOrder.OrderNo,
                        BatchNo = inboundOrderDet.BatchNo,
                        LinId = inboundOrderDet.LinId,
                        StockQuantity = (quantitys.FirstOrDefault()),
                        Status = (int)StockStatusEmun.组盘暂存,
                        Creater = "WMS",
                        CreateDate = DateTime.Now,
                        Id = inboundOrderDet.LinId.ObjToInt(),
                        DeliveryNote = model.DeliveryNote,
                        SupplierBatch = inboundOrderDet.SupplierBatch,
                        MaterieSpec = inboundOrderDet.MaterieSpec
                    };
                    if (stockInfo.Id > 0)
                    {
                        stockInfoDetail.StockId = stockInfo.Id;
                    }
                    stockInfo.Details.Add(stockInfoDetail);
                    stockInfoDetails.Add(stockInfoDetail);
                    decimal decimalReceiptQuantity = Convert.ToDecimal(notGroupDetail.ReceiptQuantity);
                    decimal decimalModelQuantity = Convert.ToDecimal((quantitys.FirstOrDefault()));
                    decimal decimalOrderQuantity = Convert.ToDecimal(notGroupDetail.OrderQuantity);
                    decimalReceiptQuantity += decimalModelQuantity;
                    // 检查是否超出订单数量
                    if (decimalReceiptQuantity > decimalOrderQuantity)
                    {
                        return WebResponseContent.Instance.Error($"组盘数量溢出{decimalReceiptQuantity - decimalOrderQuantity}");
                    }
                    // 转回float类型存储,但比较和计算都使用decimal完成
                    notGroupDetail.ReceiptQuantity = Convert.ToDecimal(decimalReceiptQuantity);
                    if (notGroupDetail.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt())
                    {
                        notGroupDetail.OrderDetailStatus = OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
                    }
                }
                decimal totalQuantity = stockInfo.Details.Sum(x => x.StockQuantity);
                inboundOrder.OrderStatus = InOrderStatusEnum.入库中.ObjToInt();
                _unitOfWorkManage.BeginTran();
                if (stockInfo.Id == 0)
                {
                    _stockRepository.StockInfoRepository.Db.InsertNav(stockInfo).Include(x => x.Details).ExecuteCommand();
                }
                else
                {
                    _stockRepository.StockInfoRepository.Db.UpdateNav(stockInfo).Include(x => x.Details, new UpdateNavOptions() { OneToManyInsertOrUpdate = true }).ExecuteCommand();
                }
                _inboundRepository.InboundOrderDetailRepository.UpdateData(inboundOrderDetails);
                _inboundRepository.InboundOrderRepository.UpdateData(inboundOrder);
                _unitOfWorkManage.CommitTran();
                content.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
            }
            return content;
        }
        public int GetPalletType(Dt_Warehouse warehouse, string palletCode)
        {
            if (warehouse.WarehouseCode == WarehouseEnum.SC01_BC.ObjToString())
            {
                Dt_PalletTypeInfo palletTypeInfo = _palletTypeInfoRepository.QueryFirst(x => x.CodeStartStr == palletCode.Substring(0, 3));
                if (palletTypeInfo == null)
                {
                    throw new Exception($"托盘号错误");
                }
                return palletTypeInfo.PalletType;
            }
            //else if (warehouse.WarehouseCode == WarehouseEnum.HA152.ObjToString())
            //{
            //    Dt_PalletTypeInfo palletTypeInfo = _palletTypeInfoRepository.QueryFirst(x => x.CodeStartStr == palletCode.Substring(0, 2));
            //    if (palletTypeInfo == null)
            //    {
            //        throw new Exception($"托盘号错误");
            //    }
            //    return palletTypeInfo.PalletType;
            //}
            //else if (warehouse.WarehouseCode == WarehouseEnum.HA57.ObjToString())
            //{
            //    Dt_PalletTypeInfo palletTypeInfo = _palletTypeInfoRepository.QueryFirst(x => x.CodeStartStr == palletCode.Substring(0, 3));
            //    if (palletTypeInfo == null)
            //    {
            //        throw new Exception($"托盘号错误");
            //    }
            //    return palletTypeInfo.PalletType;
            //}
            //else if (warehouse.WarehouseCode == WarehouseEnum.HA58.ObjToString())
            //{
            //    Dt_PalletTypeInfo palletTypeInfo = _palletTypeInfoRepository.QueryFirst(x => x.CodeStartStr == palletCode.Substring(0, 2));
            //    if (palletTypeInfo == null)
            //    {
            //        throw new Exception($"托盘号错误");
            //    }
            //    return palletTypeInfo.PalletType;
            //}
            return -1;
        }
        /// <summary>
        /// 判断正确时间格式
        /// </summary>
        public WebResponseContent IsValidMCDates(List<MatSerNumAnalysisModel> analysisModels)
        {
            string[] effDates = analysisModels.Select(x => x.EffectiveDate).Distinct().ToArray();
            string[] ProDates = analysisModels.Select(x => x.ProductionDate).Distinct().ToArray();
            foreach (string effDate in effDates)
            {
                string format = "yyyy-MM-dd"; // 目标格式
                DateTime parsedDate;
                // 解析并验证格式
                bool isValid = DateTime.TryParseExact(
                    effDate,
                    format,
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None,
                    out parsedDate
                );
                if (!isValid)
                {
                    return WebResponseContent.Instance.Error("格式无效或日期不合法");
                }
            }
            foreach (string ProDate in ProDates)
            {
                string format = "yyyy-MM-dd"; // 目标格式
                DateTime parsedDate;
                // 解析并验证格式
                bool isValid = DateTime.TryParseExact(
                    ProDate,
                    format,
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None,
                    out parsedDate
                );
                if (!isValid)
                {
                    return WebResponseContent.Instance.Error("格式无效或日期不合法");
                }
            }
            return WebResponseContent.Instance.OK();
        }
        /// <summary>
@@ -70,6 +416,7 @@
            WebResponseContent content = new WebResponseContent();
            try
            {
                _unitOfWorkManage.BeginTran();
                foreach (var item in model.DetailList)
                {
                    //获取物料信息
@@ -83,13 +430,19 @@
                    {
                        return content.Error($"未找到仓库信息");
                    }
                    if (item.SupplierBatch == null)
                    {
                        return content.Error("供应商批次不可为空");
                    }
                    Dt_InboundOrder inboundOrderOld = BaseDal.Db.Queryable<Dt_InboundOrder>().Where(x => x.UpperOrderNo == model.AsnNo).Includes(x => x.Details).First();
                    if (inboundOrderOld != null)
                    {
                        if (inboundOrderOld.OrderStatus != OrderDetailStatusEnum.New.ObjToInt())
                        {
                            return content.Error($"{model.AsnNo}单据已开始!");
                        }
                        Dt_InboundOrderDetail orderDetail1 = BaseDal.Db.Queryable<Dt_InboundOrderDetail>().Where(x => x.OrderId == inboundOrderOld.Id).First();
                        Dt_InboundOrderDetail? inboundOrderDetailOld = inboundOrderOld.Details?.FirstOrDefault(x => x.LinId == item.LinId && x.MaterielCode == item.MaterielCode);
                        if (inboundOrderDetailOld != null)
                        {
@@ -100,16 +453,19 @@
                        {
                            Dt_InboundOrderDetail orderDetail = new Dt_InboundOrderDetail()
                            {
                                OrderId = inboundOrderOld.Id,
                                OrderId = orderDetail1.OrderId,
                                MaterielCode = item.MaterielCode,
                                BatchNo = "",
                                BatchNo = item.BatchNo,
                                OrderQuantity = item.OrderQuantity,
                                ReceiptQuantity = 0,
                                OverInQuantity = 0,
                                OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(),
                                LinId = item.LinId,
                                MaterielName = materielInfo.MaterielName,
                                MaterieSpec = materielInfo.MaterieSpec
                                LPNNo = item.LPN_No,
                                MaterielName = item.MaterielName,
                                MaterieSpec = item.MaterieSpec,
                                Creater = "上游WMS",
                                SupplierBatch = item.SupplierBatch
                            };
                            _inboundRepository.InboundOrderDetailRepository.AddData(orderDetail);
                        }
@@ -128,7 +484,8 @@
                            LPNNo = item.LPN_No,
                            MaterielName = item.MaterielName,
                            MaterieSpec = item.MaterieSpec,
                            Creater = "上游WMS"
                            Creater = "上游WMS",
                            SupplierBatch = item.SupplierBatch
                        };
                        Dt_InboundOrder inboundOrder = new Dt_InboundOrder()
@@ -144,6 +501,7 @@
                            InoutType = model.OrderType,
                            OrderType = model.InoutType.ObjToInt(),
                            Creater = "上游WMS",
                            System = model.System,
                            Details = new List<Dt_InboundOrderDetail> { orderDetail }
                        };
                        //switch (model.OrderType)//单据类型
@@ -161,13 +519,11 @@
                        //        break;
                        //};
                        Db.InsertNav(inboundOrder).Include(x => x.Details).ExecuteCommand();
                    }
                }
                _unitOfWorkManage.CommitTran();
                content = WebResponseContent.Instance.OK();
                //InboundOrderAddDTO orderAddDTO1 = new InboundOrderAddDTO();
                //orderAddDTO1.OrderNo = orderAddDTO.AsnNo;
                //orderAddDTO1.UpperOrderNo = orderAddDTO.AsnNo;
@@ -190,7 +546,9 @@
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
@@ -313,6 +671,328 @@
            return content;
        }
        public WebResponseContent GetInboundOrders(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                int pageNo = saveModel.MainData["pageNo"].ObjToInt();
                string? orderNo = saveModel.MainData["orderNo"].ToString();
                int warehouseId = saveModel.MainData["warehouseId"].ObjToInt();
                List<Dt_InboundOrder> dt_ReceiveOrders = new List<Dt_InboundOrder>();
                if (string.IsNullOrEmpty(orderNo))
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_InboundOrder>().Where(x => x.OrderStatus < InboundStatusEnum.入库完成.ObjToInt() && x.WarehouseId == warehouseId).Includes(x => x.Details).OrderByDescending(x => x.CreateDate).ToPageList(pageNo, 10);
                }
                else
                {
                    dt_ReceiveOrders = Db.Queryable<Dt_InboundOrder>().Where(x => (x.OrderNo.Contains(orderNo)) && x.OrderStatus < InboundStatusEnum.入库完成.ObjToInt() && x.WarehouseId == warehouseId).Includes(x => x.Details).OrderByDescending(x => x.CreateDate).ToPageList(pageNo, 10);
                }
                content.OK(data: dt_ReceiveOrders);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        public WebResponseContent Save(AddInboundOrderModel addInboundOrder)
        {
            try
            {
                List<Dt_InboundOrderDetail> inboundOrderDetails = new List<Dt_InboundOrderDetail>();
                Random random = new Random();
                if (addInboundOrder.Details != null)
                {
                    foreach (var model in addInboundOrder.Details)
                    {
                        // 生成3位随机数(1-999之间)
                        int randomNum = random.Next(1, 1000);
                        // 格式化当前日期为年月日(例如:20230820)
                        string datePart = DateTime.Now.ToString("yyyyMMdd");
                        Dt_InboundOrderDetail inboundOrderDetail = new Dt_InboundOrderDetail()
                        {
                            MaterielCode = model.MaterielCode,
                            MaterielName = model.MaterielName,
                            SupplierBatch = model.SupplierBatch,
                            OrderQuantity = model.OrderQuantity,
                            BatchNo ="PC"+$"{datePart}{randomNum}",
                            OrderDetailStatus = InOrderStatusEnum.未开始.ObjToInt(),
                            Creater = "WMS",
                            CreateDate = DateTime.Now,
                        };
                        inboundOrderDetails.Add(inboundOrderDetail);
                    }
                }
                int randomNum2 = random.Next(1, 1000);
                string datePart2 = DateTime.Now.ToString("yyyyMMdd");
                Dt_InboundOrder erpProScrapSheet = new Dt_InboundOrder()
                {
                    OrderNo = "WMSIN" + $"{datePart2}{randomNum2}",
                    UpperOrderNo = "WMSIN" + $"{datePart2}{randomNum2}",
                    WarehouseId = addInboundOrder.WarehouseId,
                    OrderType = OrderTypeEnum.生产入库单.ObjToInt(),
                    InoutType = InoutTypeEnum.OtherIn.ToString(),
                    OrderStatus = InOrderStatusEnum.未开始.ObjToInt(),
                    CreateType = OrderCreateTypeEnum.CreateInSystem.ObjToInt(),
                    System = "WMS",
                    Details = inboundOrderDetails,
                    Creater = "WMS",
                    CreateDate = DateTime.Now,
                };
                _unitOfWorkManage.BeginTran();
                Db.InsertNav(erpProScrapSheet).Include(x => x.Details).ExecuteCommand();
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public string CancelInMaterialWarehousing = WIDESEA_Core.Helper.AppSettings.Configuration["CancelInMaterialWarehousing"];
        /// <summary>
        /// 入库单据取消推送ERP
        /// </summary>
        /// <param name="orderNo"></param>
        /// <returns></returns>
        public WebResponseContent FeedbackInboundCanceERP(int[] keys)
        {
            try
            {
                List<Dt_InboundOrderDetail> inboundOrderDetails = _inboundRepository.InboundOrderDetailRepository.QueryData(x => keys.Contains(x.Id));
                if (inboundOrderDetails == null || inboundOrderDetails.Count == 0)
                {
                    return WebResponseContent.Instance.Error("未找到入库单明细信息");
                }
                if (inboundOrderDetails.FirstOrDefault(x => x.OrderDetailStatus > OrderDetailStatusEnum.New.ObjToInt() && x.OrderDetailStatus != OrderDetailStatusEnum.AssignOverPartial.ObjToInt()) != null)
                {
                    return WebResponseContent.Instance.Error("所选入库单明细存在入库中或已完成");
                }
                Dt_InboundOrder inboundOrder = _inboundRepository.InboundOrderRepository.QueryFirst(x => x.Id == inboundOrderDetails[0].OrderId);
                if (inboundOrder == null)
                {
                    return WebResponseContent.Instance.Error("未通过该明细找到入库单信息");
                }
                if (!inboundOrder.System.Equals("ERP"))
                {
                    return WebResponseContent.Instance.Error("该入库单据非ERP推送,无法取消");
                }
                List<FeedbackInboundOrderERP> feedbackInboundOrders = new List<FeedbackInboundOrderERP>();
                foreach (var item in inboundOrderDetails)
                {
                    FeedbackInboundOrderERP feedbackInbound = new FeedbackInboundOrderERP()
                    {
                        code = inboundOrder.UpperOrderNo,
                        itemId = item.LinId,
                    };
                    feedbackInboundOrders.Add(feedbackInbound);
                }
                var response = HttpHelper.Post<WebResponseContent>(CancelInMaterialWarehousing, feedbackInboundOrders, "入库明细取消回传ERP");
                _unitOfWorkManage.BeginTran();
                if (response.Code == 0)
                {
                    _inboundRepository.InboundOrderDetailRepository.DeleteAndMoveIntoHty(inboundOrderDetails, OperateType.人工取消);
                    //检查该主订单是否还有剩余明细
                    int remainingDetailsCount = _inboundRepository.InboundOrderDetailRepository
                        .Db.Queryable<Dt_InboundOrderDetail>()
                        .Where(d => d.OrderId == inboundOrder.Id)
                        .Count();
                    // 如果没有剩余明细,再删除主订单
                    if (remainingDetailsCount == 0)
                    {
                        _inboundRepository.InboundOrderRepository.DeleteAndMoveIntoHty(inboundOrder, OperateType.人工取消);
                    }
                    _unitOfWorkManage.CommitTran();
                    return WebResponseContent.Instance.OK();
                }
                else
                {
                    throw new Exception($"操作失败: {response.Message ?? "未提供错误信息"}");
                }
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent ReceiveReturnOrder(List<HouseReturnOrder> houseReturnOrder)
        {
            try
            {
                Random random = new Random();
                List<Dt_ReturnOrder> returnOrders = new List<Dt_ReturnOrder>();
                _unitOfWorkManage.BeginTran();
                foreach(var item in houseReturnOrder)
                {
                    int randomNum = random.Next(1, 1000);
                    string datePart = DateTime.Now.ToString("yyyyMMdd");
                    Dt_ReturnOrder returnOrder = _returnOrderRepository.QueryFirst(x => x.LPNNo == item.LPNNo && x.OrderStatus == InOrderStatusEnum.未开始.ObjToInt());
                    if(returnOrder == null)
                    {
                        Dt_ReturnOrder newReturnOrder = new Dt_ReturnOrder
                        {
                            OrderType = OrderTypeEnum.反拣回库单.ObjToInt(),
                            MaterielCode = item.MaterielCode,
                            MaterielName = item.MaterielName,
                            MaterieSpec = item.MaterieSpec,
                            BatchNo = item.BatchNo,
                            OrderQuantity = item.OrderQuantity,
                            Remark = item.Remark,
                            LinId = item.LinId,
                            LPNNo = item.LPNNo,
                            LocationCode = item.LocationCode,
                            WarehouseCode = item.WarehouseCode,
                            System = item.System,
                            OrderStatus = InOrderStatusEnum.未开始.ObjToInt(),
                            Creater = "SMOM",
                            CreateDate = DateTime.Now
                        };
                        _returnOrderRepository.AddData(newReturnOrder);
                        Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo, Dt_StockInfoDetail>((stock, detail) => stock.Id == detail.StockId).Where((stock, detail) => stock.PalletCode == item.LPNNo && stock.LocationCode == item.LocationCode).First();
                        Dt_OutboundOrderDetail outboundOrderDetail = new Dt_OutboundOrderDetail();
                        Dt_OutboundOrder outboundOrder = new Dt_OutboundOrder();
                        if (stockInfo == null)
                        {
                            Dt_Warehouse warehouse = _warehouseService.Repository.QueryData(x => x.WarehouseCode == item.WarehouseCode).FirstOrDefault();
                            if(warehouse == null)
                            {
                                return WebResponseContent.Instance.Error($"该仓库编号{item.WarehouseCode}未配置");
                            }
                            Dt_StockInfoDetail stockInfoDetail = new Dt_StockInfoDetail
                            {
                                MaterielCode = item.MaterielCode,
                                MaterielName = item.MaterielName,
                                MaterieSpec = item.MaterieSpec,
                                BatchNo = item.BatchNo,
                                LinId = item.LinId,
                                Status = StockStatusEmun.组盘暂存.ObjToInt(),
                                Creater = "SMOM",
                                CreateDate = DateTime.Now,
                                OrderNo = newReturnOrder.OrderNo,
                                StockQuantity = item.OrderQuantity,
                            };
                            Dt_StockInfo stockInfo1 = new Dt_StockInfo
                            {
                                PalletCode = item.LPNNo,
                                WarehouseId = warehouse.WarehouseId,
                                BatchNo = item.BatchNo,
                                PalletType = GetPalletType(warehouse, item.LPNNo),
                                IsFull = true,
                                StockStatus = (int)StockStatusEmun.反拣入库,
                                Creater = "WMS",
                                CreateDate = DateTime.Now,
                                MaterialType = (int)InventoryMaterialType.原材料,
                                Materialweight = 0,
                                Wlstatus = (int)InventoryMaterialStatus.合格,
                                Mgeneratetime = DateTime.Now,
                                Details = new List<Dt_StockInfoDetail> { stockInfoDetail}
                            };
                            Db.InsertNav(stockInfo1).Include(x => x.Details).ExecuteCommand();
                        }
                        else
                        {
                             outboundOrderDetail = new Dt_OutboundOrderDetail
                            {
                                MaterielCode = item.MaterielCode,
                                MaterielName = item.MaterielName,
                                MaterieSpec = item.MaterieSpec,
                                BatchNo = item.BatchNo,
                                OrderQuantity = stockInfo.Details.Sum(x=>x.StockQuantity),
                                Remark = item.Remark,
                                LinId = item.LinId,
                                LPNNo = item.LPNNo,
                                Creater = "SMOM",
                                CreateDate = DateTime.Now,
                                LocationName = item.LocationCode
                            };
                             outboundOrder = new Dt_OutboundOrder
                            {
                                OrderNo = "FJCK" + $"{datePart}{randomNum}",
                                UpperOrderNo = "WMSFJCK" + $"{datePart}{randomNum}",
                                OrderStatus = OutOrderStatusEnum.未开始.ObjToInt(),
                                OrderType = OrderTypeEnum.反拣出库单.ObjToInt(),
                                InoutType = InoutTypeEnum.OtherOut.ToString(),
                                System = item.System,
                                Creater = "SMOM",
                                CreateDate = DateTime.Now,
                                CreateType = OrderCreateTypeEnum.UpperSystemPush.ObjToInt(),
                                Details = new List<Dt_OutboundOrderDetail> { outboundOrderDetail }
                             };
                            Db.InsertNav(outboundOrder).Include(x => x.Details).ExecuteCommand();
                        }
                    }
                    else
                    {
                        Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.Db.Queryable<Dt_StockInfo, Dt_StockInfoDetail>((stock, detail) => stock.Id == detail.StockId).Where((stock, detail) => stock.PalletCode == item.LPNNo && stock.StockStatus == StockStatusEmun.余料退库.ObjToInt()).First();
                        stockInfo.StockStatus = StockStatusEmun.反拣入库.ObjToInt();
                        returnOrder.LinId = item.LinId;
                        returnOrder.System = item.System;
                        returnOrder.WarehouseCode = item.WarehouseCode;
                        returnOrder.LocationCode = item.LocationCode;
                        returnOrder.OrderType = OrderTypeEnum.反拣回库单.ObjToInt();
                        returnOrder.OrderQuantity += item.OrderQuantity;
                        returnOrder.ReceiptQuantity += item.OrderQuantity;
                        _returnOrderRepository.UpdateData(returnOrder);
                        _stockRepository.StockInfoRepository.UpdateData(stockInfo);
                    }
                }
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch(Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent GetMaterielCode(int warehouseId)
        {
            try
            {
                List<Dt_MaterielInfo> materielInfos = _basicRepository.MaterielInfoRepository.QueryData(x => x.WarehouseId == warehouseId).ToList();
                List<string> materielCodes = materielInfos.Select(x => x.MaterielCode).Distinct().ToList();
                return WebResponseContent.Instance.OK("成功", data: materielCodes);
            }
            catch(Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent GetMaterielName(int warehouseId, string materielCode)
        {
            try
            {
                List<Dt_MaterielInfo> materielInfos = _basicRepository.MaterielInfoRepository.QueryData(x => x.WarehouseId == warehouseId && x.MaterielCode == materielCode).ToList();
                List<string> materielNames = materielInfos.Select(x => x.MaterielName).Distinct().ToList();
                return WebResponseContent.Instance.OK("成功", data: materielNames);
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// 验证单据添加DTO对象
        /// </summary>