duyongjia
2024-12-29 9a0acba23c5b5095be2a89b8ca190fe5202c2ea6
ÏîÄ¿´úÂë/WMS/WIDESEA_WMSServer/WIDESEA_InboundService/Service/InboundOrderService.cs
@@ -1,7 +1,10 @@
using AutoMapper;
using LogLibrary.Log;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
@@ -17,9 +20,13 @@
using WIDESEA_IBasicService;
using WIDESEA_IInboundRepository;
using WIDESEA_IInboundService;
using WIDESEA_InboundRepository;
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoRepository;
using WIDESEA_Model;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Inbound;
using WIDESEA_Model.Models.System.Request;
namespace WIDESEA_InboundService
{
@@ -46,65 +53,264 @@
            WebResponseContent content = new WebResponseContent();
            try
            {
                List<MatSerialNumberDTO> matSerialNumberDTOs = CodeAnalysisHelper.CodeAnalysis<MatSerialNumberDTO>(AnalysisCode.InnerCode, materielGroupDTO.SerialNumbers);
                (bool, string, object?) result2 = ModelValidate.ValidateModelData(matSerialNumberDTOs);
                if (!result2.Item1) return content = WebResponseContent.Instance.Error(result2.Item2);
                List<string> materielCodes = matSerialNumberDTOs.GroupBy(x => x.MaterielCode).Select(x => x.Key).ToList();
                List<Dt_MaterielInfo> materielInfos = _basicService.MaterielInfoService.GetMaterielInfos(materielCodes);
                Dt_InboundOrder inboundOrder = GetInboundOrder(materielGroupDTO.OrderNo);
                materielGroupDTO.OrderNo = GetOrderNo();
                Dt_StockInfo? stockInfo = _stockService.StockInfoService.GetStockByPalletCode(materielGroupDTO.PalletCode);
                (bool, string, object?) result = CheckMaterielGroupParam(materielGroupDTO, matSerialNumberDTOs, materielInfos, materielCodes, inboundOrder, stockInfo);
                (bool, string, object?) result = CheckMaterielGroupParam(materielGroupDTO, stockInfo);
                if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
                if (stockInfo == null)
                {
                    stockInfo = new Dt_StockInfo();
                    stockInfo.Details = new List<Dt_StockInfoDetail>();
                }
                List<Dt_StockInfoDetail> stockInfoDetails = _mapper.Map<List<Dt_StockInfoDetail>>(matSerialNumberDTOs);
                stockInfoDetails.ForEach(x =>
                {
                    x.Status = 0;
                    x.OrderNo = inboundOrder.OrderNo;
                    x.MaterielName = materielInfos.FirstOrDefault(v => v.MaterielCode == x.MaterielCode)?.MaterielName ?? "";
                    x.StockId = stockInfo.Id != 0 ? stockInfo.Id : 0;
                });
                if (stockInfo.Id == 0)
                {
                    stockInfo.PalletCode = materielGroupDTO.PalletCode;
                    stockInfo.StockStatus = StockStatusEmun.组盘暂存.ObjToInt();
                    stockInfo.Creater = "立库WMS";
                    stockInfo.Details = new List<Dt_StockInfoDetail>();
                }
                stockInfo.Details.AddRange(stockInfoDetails);
                List<Dt_InboundOrderDetail> inboundOrderDetails = new List<Dt_InboundOrderDetail>();
                for (int i = 0; i < materielCodes.Count; i++)
                List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
                List<Dt_LabelMaster> notExistLabels = new List<Dt_LabelMaster>();
                foreach (var lablel in materielGroupDTO.SerialNumbers)
                {
                    decimal stockQuantity = stockInfoDetails.Where(x => x.MaterielCode == materielCodes[i]).Sum(x => x.StockQuantity);
                    inboundOrderDetails.AddRange(_inboundOrderDetailService.UpdateReceiptQuantity(inboundOrder.Details.Where(x => x.MaterielCode == materielCodes[i]).ToList(), stockQuantity));
                }
                List<int> updateDetailIds = inboundOrderDetails.Select(x => x.Id).ToList();
                if (inboundOrderDetails.FirstOrDefault(x => x.OrderDetailStatus != OrderDetailStatusEnum.Over.ObjToInt()) == null && inboundOrder.Details.FirstOrDefault(x => !updateDetailIds.Contains(x.Id) && x.OrderDetailStatus != OrderDetailStatusEnum.Over.ObjToInt()) == null)
                {
                    inboundOrder.OrderStatus = InboundStatusEnum.入库完成.ObjToInt();
                    BaseDal.DeleteAndMoveIntoHty(inboundOrder, App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                    for (int i = 0; i < inboundOrderDetails.Count; i++)
                    Dt_LabelMaster labmaster = _labelMasterRepository.QueryFirst(x => x.LABEL_NO == lablel);
                    if(labmaster == null)
                    {
                        _inboundOrderDetailService.Repository.DeleteAndMoveIntoHty(inboundOrderDetails[i], App.User.UserId == 0 ? OperateType.自动完成 : OperateType.人工完成);
                        //通过条码接口同步条码主数据
                        var res= _sys_JobService.GetLabMaster(lablel);
                        if (res != null && res.Status)
                        {
                            labmaster = _labelMasterRepository.QueryFirst(x => x.LABEL_NO == lablel);
                            if (labmaster == null)
                            {
                                notExistLabels.Add(labmaster);
                            }
                        }
                        else
                        {
                          return  content = WebResponseContent.Instance.Error("组盘条码在上游WMS系统中不存在!");
                        }
                    }
                    if(labmaster!=null)
                    {
                        //主数据条码状态允许组盘:01已收料待质检,02已收料无需质检,03待进仓已质检,09已下架;
                        //主数据条码状态不允许组盘:00创建,04已质检待退货,05收料房退货,06库房退货,07已进仓,08已上架,10已出库,11已冻结,12已锁定,20关闭的;
                        if (labmaster.LABEL_STATUS=="00"|| labmaster.LABEL_STATUS == "04" || labmaster.LABEL_STATUS == "05"|| labmaster.LABEL_STATUS == "06" || labmaster.LABEL_STATUS == "07" || labmaster.LABEL_STATUS == "08" || labmaster.LABEL_STATUS == "10" || labmaster.LABEL_STATUS == "11" || labmaster.LABEL_STATUS == "12" || labmaster.LABEL_STATUS == "20")
                        {
                            return content = WebResponseContent.Instance.Error("主数据条码状态不允许组盘!条码状态:"+ labmaster.LABEL_STATUS);
                        }
                    }
                }
                else if (inboundOrder.OrderStatus == InboundStatusEnum.未开始.ObjToInt())
                if (notExistLabels.Count == 0)
                {
                    inboundOrder.OrderStatus = InboundStatusEnum.入库中.ObjToInt();
                }
                    foreach (var item in materielGroupDTO.SerialNumbers)
                    {
                        Dt_LabelMaster labmaster = _labelMasterRepository.QueryFirst(x => x.LABEL_NO == item);
                        Dt_StockInfoDetail dt_StockInfoDetail = new Dt_StockInfoDetail();
                        dt_StockInfoDetail.Status = 0;
                        dt_StockInfoDetail.OrderNo = materielGroupDTO.OrderNo;
                        dt_StockInfoDetail.StockId = stockInfo.Id != 0 ? stockInfo.Id : 0;
                        dt_StockInfoDetail.MaterielCode = labmaster.MATNR;
                        dt_StockInfoDetail.MaterielName = labmaster.MAKTX;
                        dt_StockInfoDetail.BatchNo = labmaster.BATCH;
                        dt_StockInfoDetail.SerialNumber = labmaster.LABEL_NO;
                        dt_StockInfoDetail.StockQuantity = int.Parse(labmaster.BOX_QTY);
                        dt_StockInfoDetail.OutboundQuantity = 0;
                        dt_StockInfoDetail.Creater = "System";
                        stockInfoDetails.Add(dt_StockInfoDetail);
                        stockInfo.Details.AddRange(stockInfoDetails);
                    }
                    content = MaterielGroupUpdateData(stockInfo);
                content = MaterielGroupUpdateData(inboundOrder, inboundOrderDetails, stockInfo);
                }
                else
                {
                    content = WebResponseContent.Instance.Error("组盘条码在上游WMS系统中不存在!");
                }
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
            }
            return content;
        }
        /// <summary>
        /// ä½™æ–™é€€å›žç»„盘
        /// </summary>
        /// <param name="materielGroupDTO"></param>
        /// <returns></returns>
        public WebResponseContent ReturnMaterielGroup(MaterielGroupDTO materielGroupDTO, ReturnInventoryRequest inventoryRequest)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_StockInfo? stockInfo = _stockService.StockInfoService.GetStockByPalletCode(materielGroupDTO.PalletCode);
                (bool, string, object?) result = CheckMaterielGroupParam(materielGroupDTO, stockInfo);
                if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
                if (stockInfo == null)
                {
                    stockInfo = new Dt_StockInfo();
                    stockInfo.PalletCode = materielGroupDTO.PalletCode;
                    stockInfo.StockStatus = StockStatusEmun.组盘暂存.ObjToInt();
                    stockInfo.Creater = "WMS";
                    stockInfo.Details = new List<Dt_StockInfoDetail>();
                }
                List<Dt_StockInfoDetail> stockInfoDetails = new List<Dt_StockInfoDetail>();
                foreach (var item in inventoryRequest.DATA)
                {
                    Dt_StockInfoDetail dt_StockInfoDetail = new Dt_StockInfoDetail();
                    dt_StockInfoDetail.Status = 0;
                    dt_StockInfoDetail.OrderNo = materielGroupDTO.OrderNo;
                    dt_StockInfoDetail.StockId = stockInfo.Id != 0 ? stockInfo.Id : 0;
                    dt_StockInfoDetail.MaterielCode = item.MATNR;
                    dt_StockInfoDetail.MaterielName = "";
                    dt_StockInfoDetail.BatchNo = "";
                    dt_StockInfoDetail.SerialNumber = item.LABEL_NO;
                    dt_StockInfoDetail.StockQuantity = int.Parse(item.QTY);
                    dt_StockInfoDetail.OutboundQuantity = 0;
                    dt_StockInfoDetail.Creater = "WMS";
                    stockInfoDetails.Add(dt_StockInfoDetail);
                    stockInfo.Details.AddRange(stockInfoDetails);
                }
                content = MaterielGroupUpdateData(stockInfo);
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
            }
            return content;
        }
        /// <summary>
        /// æ ¹æ®BYDWMS组盘信息,再生成立库组盘信息
        /// </summary>
        /// <param name="inventoryRequest"></param>
        /// <returns></returns>
        public ReturnInventoryResponse returnInventory(string inventoryRequeststr)
        {
            new LogFactory().GetLog("WMS接口").InfoFormat(true, "returnInventory", "余料退回信息", $"{inventoryRequeststr}");
            ReturnInventoryRequest inventoryRequest = JsonConvert.DeserializeObject<ReturnInventoryRequest>(inventoryRequeststr);
            ReturnInventoryResponse response= new ReturnInventoryResponse();
            MaterielGroupDTO materielGroupDTO = new MaterielGroupDTO();
            materielGroupDTO.PalletCode = inventoryRequest.TPNUM;
            materielGroupDTO.OrderNo = inventoryRequest.IZLID; //退货入库指令
            List<string> SerialNumbers = new List<string>();
            Dt_MainReturnInventory dt_MainReturnInventoryOld =  _mainReturnInventoryRepository.QueryFirst(x => x.RETURN_NO == inventoryRequest.RETURN_NO && x.RETURN_ITEM_NO == inventoryRequest.RETURN_ITEM_NO);
            if (dt_MainReturnInventoryOld == null)
            {
                //保存回退数据到本地
                Dt_MainReturnInventory dt_MainReturnInventory = new Dt_MainReturnInventory();
                dt_MainReturnInventory.WH_NUMBER = inventoryRequest.WH_NUMBER;
                dt_MainReturnInventory.BUSINESS_CODE = inventoryRequest.BUSINESS_CODE;
                dt_MainReturnInventory.BUSINESS_NAME = inventoryRequest.BUSINESS_NAME;
                dt_MainReturnInventory.WERKS = inventoryRequest.WERKS;
                dt_MainReturnInventory.LGORT = inventoryRequest.LGORT;
                dt_MainReturnInventory.TOTAL_RETURN_QTY = inventoryRequest.TOTAL_RETURN_QTY;
                dt_MainReturnInventory.RETURN_NO = inventoryRequest.RETURN_NO;
                dt_MainReturnInventory.RETURN_ITEM_NO = inventoryRequest.RETURN_ITEM_NO;
                dt_MainReturnInventory.TPNUM = inventoryRequest.TPNUM;
                dt_MainReturnInventory.YLZD1 = inventoryRequest.YLZD1;
                dt_MainReturnInventory.YLZD2 = inventoryRequest.YLZD2;
                dt_MainReturnInventory.YLZD3 = inventoryRequest.YLZD3;
                dt_MainReturnInventory.YLZD4 = inventoryRequest.YLZD4;
                dt_MainReturnInventory.YLZD5 = inventoryRequest.YLZD5;
                dt_MainReturnInventory.IZLID = inventoryRequest.IZLID;
                dt_MainReturnInventory.SYSNOD = inventoryRequest.SYSNOD;
                dt_MainReturnInventory.MO_NO = inventoryRequest.MO_NO;
                foreach (ReturnInventory item in inventoryRequest.DATA)
                {
                    Dt_ReturnInventoryDetail dt_ReturnInventoryDetail = new Dt_ReturnInventoryDetail();
                    dt_ReturnInventoryDetail.LABEL_NO = item.LABEL_NO;
                    dt_ReturnInventoryDetail.SOBKZ = item.SOBKZ;
                    dt_ReturnInventoryDetail.UNIT = item.UNIT;
                    dt_ReturnInventoryDetail.LGORT = item.LGORT;
                    dt_ReturnInventoryDetail.QTY = item.QTY;
                    dt_ReturnInventoryDetail.LIFNR = item.LIFNR;
                    dt_ReturnInventoryDetail.F_LGORT = item.F_LGORT;
                    dt_ReturnInventoryDetail.MATNR = item.MATNR;
                    _ReturnInventoryDetailRepository.AddData(dt_ReturnInventoryDetail);
                    SerialNumbers.Add(item.LABEL_NO);
                }
                materielGroupDTO.SerialNumbers = SerialNumbers;
                _mainReturnInventoryRepository.AddData(dt_MainReturnInventory);
                WebResponseContent content = ReturnMaterielGroup(materielGroupDTO, inventoryRequest);
                if (content.Status)
                {
                    response.MSGTY = "S";
                    response.MSGTX = "";
                }
                else
                {
                    response.MSGTY = "E";
                    response.MSGTX = content.Message;
                }
            }
            else
            {
                response.MSGTY = "E";
                response.MSGTX = "不能重复申请!";
            }
            return response;
        }
        /// <summary>
        /// ç”Ÿæˆè®¢å•号
        /// </summary>
        /// <returns></returns>
        public string GetOrderNo()
        {
            DataTable dt = BaseDal.QueryTable("SELECT FORMAT(NEXT VALUE FOR dbo.seqOrderNum, '000000000');");
            return DateTime.Now.ToString("yyyyMMdd") + dt.Rows[0][0].ToString();
        }
        /// <summary>
        /// æ’¤é”€ç»„盘
        /// </summary>
        /// <param name="materielGroupDTO"></param>
        /// <returns></returns>
        public WebResponseContent MaterielGroupRevoke(string PalletCode)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_StockInfo? stockInfo = _stockService.StockInfoService.GetStockByPalletCode(PalletCode);
                if(stockInfo != null)
                {
                    if(stockInfo.StockStatus!= (int)StockStatusEmun.组盘暂存)
                    {
                        return content = WebResponseContent.Instance.Error("组盘暂存状态才可以撤销组盘!");
                    }
                    else
                    {
                        stockInfo.StockStatus = (int)StockStatusEmun.组盘撤销;
                        foreach (var item in stockInfo.Details)
                        {
                            item.Status = (int)OutStockStatus.撤销;
                        }
                    }
                    content = MaterielGroupUpdateData(stockInfo);
                }
                else
                {
                    return content = WebResponseContent.Instance.Error("组盘信息不存在!");
                }
            }
            catch (Exception ex)
            {
@@ -141,6 +347,30 @@
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// ç»„盘数据更新
        /// </summary>
        /// <param name="inboundOrder">入库单</param>
        /// <param name="inboundOrderDetails">入库单明细</param>
        /// <param name="stockInfo">组盘数据</param>
        /// <returns></returns>
        public WebResponseContent MaterielGroupUpdateData(Dt_StockInfo stockInfo)
        {
            try
            {
                _unitOfWorkManage.BeginTran();
                _stockService.StockInfoService.AddMaterielGroup(stockInfo);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        /// <summary>
        /// éªŒè¯ç»„盘数据
@@ -219,6 +449,38 @@
            return (true, "成功", materielGroupDTO);
        }
        /// <summary>
        /// éªŒè¯ç»„盘数据
        /// </summary>
        /// <param name="materielGroupDTO">物料组盘DTO</param>
        /// <param name="stockInfo">组盘信息</param>
        /// <returns></returns>
        public (bool, string, object?) CheckMaterielGroupParam(MaterielGroupDTO materielGroupDTO, 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 != StockStatusEmun.组盘暂存.ObjToInt())
            {
                return (false, "已上架的托盘不能再次组盘", materielGroupDTO);
            }
            if (_stockService.StockInfoDetailService.ExistSerialNumbers(materielGroupDTO.SerialNumbers))
            {
                return (false, "有序列号在库存中已存在", materielGroupDTO);
            }
            return (true, "成功", materielGroupDTO);
        }
        /// <summary>
        /// PDA分页查询数据
        /// </summary>