From 9753fb2756f6b4e30ff79d901a7bb86145517c8b Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期四, 18 十二月 2025 11:29:13 +0800
Subject: [PATCH] 1

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_InboundService/InboundService.cs |  215 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 213 insertions(+), 2 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_InboundService/InboundService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_InboundService/InboundService.cs"
index 97a2766..75a8640 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_InboundService/InboundService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_InboundService/InboundService.cs"
@@ -1,22 +1,233 @@
-锘縰sing System;
+锘縰sing LogLibrary.Log;
+using MailKit;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using WIDESEA_Common.CommonEnum;
+using WIDESEA_Common.OrderEnum;
+using WIDESEA_Common.StockEnum;
+using WIDESEA_Core;
+using WIDESEA_Core.BaseRepository;
+using WIDESEA_Core.Helper;
+using WIDESEA_Core.Utilities;
+using WIDESEA_DTO.Inbound;
 using WIDESEA_IInboundService;
+using WIDESEA_IStockService;
+using WIDESEA_Model.Models;
+using WIDESEA_Model.Models.Basic;
 
 namespace WIDESEA_InboundService
 {
     public class InboundService : IInboundService
     {
+        LogFactory LogFactory = new LogFactory();
+        private readonly IUnitOfWorkManage _unitOfWorkManage;
         public IInboundOrderDetailService InboundOrderDetailService { get; }
 
         public IInboundOrderService InbounOrderService { get; }
+        private readonly IRepository<Dt_InboundOrder> _inboundOrderRepository;
+        private readonly IRepository<Dt_WarehouseArea> _warehouseAreaRepository;
+        private readonly IRepository<Dt_LocationType> _locationTypeRepository;
+        private readonly IRepository<Dt_StockInfo> _stockInfoRepository;
+        private readonly IRepository<Dt_InboundOrderDetail> _inboundOrderDetailRepository;
+        private readonly IRepository<Dt_Task> _taskRepository;
+        private IStockService _stockService;
 
-        public InboundService(IInboundOrderDetailService inboundOrderDetailService, IInboundOrderService inbounOrderService)
+        public InboundService(IUnitOfWorkManage unitOfWorkManage, IInboundOrderDetailService inboundOrderDetailService, IInboundOrderService inbounOrderService, IRepository<Dt_InboundOrder> inboundOrderRepository, IRepository<Dt_WarehouseArea> warehouseAreaRepository, IRepository<Dt_LocationType> locationTypeRepository, IRepository<Dt_StockInfo> stockInfoRepository, IRepository<Dt_InboundOrderDetail> inboundOrderDetailRepository, IStockService stockService, IRepository<Dt_Task> taskRepository)
         {
+            _unitOfWorkManage = unitOfWorkManage;
             InboundOrderDetailService = inboundOrderDetailService;
             InbounOrderService = inbounOrderService;
+            _inboundOrderRepository = inboundOrderRepository;
+            _warehouseAreaRepository = warehouseAreaRepository;
+            _locationTypeRepository = locationTypeRepository;
+            _stockInfoRepository = stockInfoRepository;
+            _inboundOrderDetailRepository = inboundOrderDetailRepository;
+            _stockService = stockService;
+            _taskRepository = taskRepository;
+        }
+
+        public async Task<WebResponseContent> GroupPallet(GroupPalletDto palletDto)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                (bool, string, object?) result2 = ModelValidate.ValidateModelData(palletDto);
+                if (!result2.Item1) return content.Error(result2.Item2);
+
+                var code = _warehouseAreaRepository.Db.Queryable<Dt_WarehouseArea>().Where(x => x.Code == palletDto.WarehouseType).Select(x => x.Code).First();
+                if (string.IsNullOrEmpty(code))
+                {
+                    return content.Error($"浠撳簱涓病鏈夎{palletDto.WarehouseType}缂栧彿銆�");
+                }
+
+                Dt_InboundOrder inboundOrder = new Dt_InboundOrder();
+                var details = _inboundOrderDetailRepository.QueryData(x => (x.OutBoxbarcodes == palletDto.Barcode|| x.Barcode == palletDto.Barcode) && x.OrderDetailStatus == (int)InOrderStatusEnum.鏈紑濮�);
+
+                if (details.Count() <= 0)
+                {
+                    return content.Error("鏈壘鍒拌鏉$爜鍗曟嵁淇℃伅璇风‘璁ゆ槸鍚﹀凡缁忕粍鐩樺畬鎴�");
+                }
+                inboundOrder = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>().Includes(x=>x.Details).Where(x => x.Id == details.First().OrderId).First();
+
+                if (inboundOrder == null)
+                {
+                    return content.Error("鏈壘鍒拌鏉$爜涓诲崟淇℃伅");
+                }
+                Dt_StockInfo? stockInfo = await _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == palletDto.PalletCode).FirstAsync();
+
+                List<string?> materielCodes = details.GroupBy(x => x.Barcode).Select(x => x.Key).ToList();
+
+                (bool, string, object?) result = CheckMaterielGroupParam(palletDto, materielCodes, inboundOrder, stockInfo);
+                if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
+
+
+                if (stockInfo == null)
+                {
+                    stockInfo = new Dt_StockInfo() { PalletType = (int)PalletTypeEnum.None, LocationType = Convert.ToInt32(palletDto.locationType) };
+                    stockInfo.Details = new List<Dt_StockInfoDetail>();
+                }
+
+                if (stockInfo != null && stockInfo.Details.Count>0 && stockInfo.Details.FirstOrDefault()?.WarehouseCode != palletDto.WarehouseType)
+                {
+                    return content.Error($"璇ユ墭鐩樼粍鐩樹粨搴撲负{stockInfo.Details.FirstOrDefault()?.WarehouseCode}涓庡綋鍓嶄粨搴搟palletDto.WarehouseType}涓嶄竴鑷达紝涓嶅厑璁哥粍鐩�");
+                }
+
+                foreach (var item in details)
+                {
+                    stockInfo.Details.Add(new Dt_StockInfoDetail
+                    {
+                        StockId = stockInfo == null ? 0 : stockInfo.Id,
+                        Barcode = item.Barcode,
+                        MaterielCode = item.MaterielCode,
+                        BatchNo = item.BatchNo,
+                        Unit = item.Unit,
+                        InboundOrderRowNo = item.lineNo,
+                        SupplyCode = item.SupplyCode,
+                        WarehouseCode = palletDto.WarehouseType,
+                        StockQuantity = item.OrderQuantity,
+                        BarcodeQty = item.BarcodeQty,
+                        BarcodeUnit = item.BarcodeUnit,
+                        FactoryArea = inboundOrder.FactoryArea,
+                        Status = 0,
+                        OrderNo = inboundOrder.InboundOrderNo,
+                        BusinessType = inboundOrder.BusinessType,
+
+                    });
+
+                    item.ReceiptQuantity = item.BarcodeQty;
+                    item.OrderDetailStatus = (int)OrderDetailStatusEnum.GroupAndInbound;
+                    item.WarehouseCode = palletDto.WarehouseType;
+                    item.ReturnToMESStatus = 0;
+                }
+
+                if (stockInfo.Id == 0)
+                {
+                    stockInfo.PalletCode = palletDto.PalletCode;
+                    stockInfo.StockStatus = (int)StockStatusEmun.缁勭洏鏆傚瓨;
+                }
+                stockInfo.PalletType = (int)PalletTypeEnum.None;
+
+                List<int> updateDetailIds = details.Select(x => x.Id).ToList();
+                if (inboundOrder.OrderStatus == (int)InOrderStatusEnum.鏈紑濮�)
+                {
+                    inboundOrder.OrderStatus = (int)InOrderStatusEnum.鍏ュ簱涓�;
+                }
+                inboundOrder.Operator = App.User.UserName;
+                content = MaterielGroupUpdateData(inboundOrder, details, stockInfo).Result;
+                if (content.Status)
+                {
+                    Dt_StockInfo? NewstockInfo = await _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == palletDto.PalletCode).FirstAsync();
+
+                    return WebResponseContent.Instance.OK(data: NewstockInfo.Details.OrderByDescending(x => x.Id));
+                }
+                else
+                {
+                    content = WebResponseContent.Instance.Error(content.Message);
+                }
+            }
+            catch (Exception ex)
+            {
+                LogFactory.GetLog($"缁勭洏淇℃伅").Info(true, $"銆愬紓甯搞�戯細銆恵ex.Message}銆憑Environment.NewLine}銆恵ex.StackTrace}銆憑Environment.NewLine}{Environment.NewLine}");
+                return content.Error(ex.Message);
+            }
+            return content;
+        }
+
+        /// <summary>
+        /// 楠岃瘉缁勭洏鏁版嵁
+        /// </summary>
+        /// <param name="materielGroupDTO">鐗╂枡缁勭洏DTO</param>
+        /// <param name="matSerialNumberDTOs">鎵爜搴忓垪鍙�</param>
+        /// <param name="materielInfos">鐗╂枡淇℃伅</param>
+        /// <param name="materielCodes">鐗╂枡缂栧彿</param>
+        /// <param name="inboundOrder">鍏ュ簱鍗曟嵁</param>
+        /// <param name="stockInfo">缁勭洏淇℃伅</param>
+        /// <returns></returns>
+        public (bool, string, object?) CheckMaterielGroupParam(GroupPalletDto materielGroupDTO, List<string> barcodeCodes, Dt_InboundOrder inboundOrder, Dt_StockInfo stockInfo)
+        {
+            (bool, string, object?) result = ModelValidate.ValidateModelData(materielGroupDTO);
+            if (!result.Item1) return result;
+
+            if (_taskRepository.QueryFirst(x => x.PalletCode == materielGroupDTO.PalletCode) != null)
+            {
+                return (false, "璇ユ墭鐩樺彿宸叉湁浠诲姟", materielGroupDTO);
+            }
+
+            if (stockInfo != null && !string.IsNullOrEmpty(stockInfo.LocationCode) && stockInfo.StockStatus != (int)StockStatusEmun.缁勭洏鏆傚瓨)
+            {
+                return (false, "宸蹭笂鏋剁殑鎵樼洏涓嶈兘鍐嶆缁勭洏", materielGroupDTO);
+            }
+
+            if (_stockService.StockInfoDetailService.ExistBarcodes(barcodeCodes))
+            {
+                return (false, $"{barcodeCodes[0]} 鏉$爜鍦ㄥ簱瀛樹腑宸插瓨鍦�", materielGroupDTO);
+            }     
+
+            if (inboundOrder == null)
+            {
+                return (false, "鍗曟嵁涓嶅瓨鍦�", materielGroupDTO);
+            }
+
+            if (inboundOrder.Details == null || inboundOrder.Details.Count == 0)
+            {
+                return (false, "鏃犲崟鎹槑缁嗕俊鎭�", materielGroupDTO);
+            }
+
+            if (inboundOrder.OrderStatus != (int)InOrderStatusEnum.鏈紑濮� && inboundOrder.OrderStatus != (int)InOrderStatusEnum.鍏ュ簱涓�)
+            {
+                return (false, "璇ュ崟鎹笉鍙啀缁勭洏", materielGroupDTO);
+            }
+
+            List<Dt_InboundOrderDetail> inboundOrderDetails = inboundOrder.Details.Where(x => barcodeCodes.Contains(x.Barcode)).ToList();
+
+            if (inboundOrderDetails.GroupBy(x => x.Barcode).Count() != barcodeCodes.Count)
+            {
+                return (false, "鏈夌墿鏂欎笉鍦ㄥ崟鎹唴", materielGroupDTO);
+            }
+
+            return (true, "鎴愬姛", materielGroupDTO);
+        }
+
+        public async Task<WebResponseContent> MaterielGroupUpdateData(Dt_InboundOrder inboundOrder, List<Dt_InboundOrderDetail> inboundOrderDetails, Dt_StockInfo stockInfo)
+        {
+            try
+            {
+                _unitOfWorkManage.BeginTran();
+                //await _inboundOrderRepository.Db.UpdateNav(inboundOrder).Include(x=>x.Details).ExecuteCommandAsync();
+                _inboundOrderRepository.UpdateData(inboundOrder);
+                _inboundOrderDetailRepository.UpdateData(inboundOrderDetails);
+                _stockService.StockInfoService.AddMaterielGroup(stockInfo);
+                _unitOfWorkManage.CommitTran();
+                return WebResponseContent.Instance.OK();
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
         }
     }
 }

--
Gitblit v1.9.3