chenyong
4 天以前 867793e0bd4d8e84b8ae98e31d5d722fdb73cee9
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/Task/Partial/Dt_TaskService.cs
@@ -1,29 +1,18 @@

using log4net.Core;
using Mapster;
using Masuit.Tools;
using Masuit.Tools.Models;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using SixLabors.Fonts.Tables.AdvancedTypographic;
using System.Diagnostics;
using WIDESEA_Common;
using Masuit.Tools;
using SqlSugar;
using WIDESEA_Common.CustomModels;
using WIDESEA_Core.Const;
using WIDESEA_DTO;
using WIDESEA_DTO.MOM;
using WIDESEA_DTO.WMS;
using WIDESEA_IStorageBasicRepository;
using WIDESEA_IStoragIntegrationServices;
using WIDESEA_Model.Models;
using WIDESEA_StorageBasicRepository;
using WIDESEA_StorageTaskRepository;
using WIDESEA_StoragIntegrationServices;
using WIDESEAWCS_Model.Models;
using WIDESEAWCS_QuartzJob.Models;
namespace WIDESEA_StorageTaskServices;
public partial class Dt_TaskService : ServiceBase<Dt_Task, IDt_TaskRepository>, IDt_TaskService
{
    #region 请求任务入库
    /// <summary>
    /// 请求入库
    /// </summary>
@@ -42,23 +31,23 @@
                //if (task.TaskState == (int)TaskInStatusEnum.InNew)
                {
                    // 创建WMS任务
                    WMSTaskDTO taskDTO = new WMSTaskDTO()
                    {
                        TaskNum = task.TaskNum.Value,
                        Grade = 1,
                        PalletCode = task.PalletCode,
                        RoadWay = task.Roadway,
                        SourceAddress = task.SourceAddress,
                        TargetAddress = task.TargetAddress,
                        TaskState = task.TaskState.Value,
                        Id = 0,
                        TaskType = task.TaskType,
                    };
                    //WMSTaskDTO taskDTO = new WMSTaskDTO()
                    //{
                    //    TaskNum = task.TaskNum.Value,
                    //    Grade = 1,
                    //    PalletCode = task.PalletCode,
                    //    RoadWay = task.Roadway,
                    //    SourceAddress = task.SourceAddress,
                    //    TargetAddress = task.TargetAddress,
                    //    TaskState = task.TaskState.Value,
                    //    Id = 0,
                    //    TaskType = task.TaskType,
                    //    ProductionLine = task.ProductionLine,
                    //};
                    WMSTaskDTO taskDTO = CreateTaskDTO(task);
                    return content.OK(data: taskDTO);
                }
            }
            // 调用CreateNewTask方法,创建新任务
            content = await CreateNewTask(input);
        }
@@ -73,56 +62,58 @@
    }
    /// <summary>
    ///
    ///
    /// </summary>
    /// <param name="input">请求参数</param>
    /// <param name="flag">实框空框标识</param>
    /// <returns></returns>
    private async Task<WebResponseContent> CreateNewTask(RequestTaskDto input)
    {
        WebResponseContent content = new WebResponseContent();
        //DtLocationInfo locationInfo = await RequestLocation(input);
        //if (locationInfo == null) throw new Exception("库位已满");
        var stationinfo = _stationManagerRepository.QueryFirst(x => x.stationChildCode == input.Position);
        if (stationinfo == null) throw new Exception("未知站台");
        if (stationinfo.stationType != 7)
        try
        {
            if (input.PalletCode == null || input.PalletCode.Trim() == "")
                return content.Error($"【{stationinfo.remark}】托盘条码为空");
        }
        var task = await CreateNewTaskByStation(input, stationinfo);
            WebResponseContent content = new WebResponseContent();
        // 尝试添加新任务
        if (task == null) return content.Error();
        var taskId = await BaseDal.AddDataAsync(task);
        bool isResult = taskId > 0;
        if (isResult)
        {
            // 创建WMS任务
            WMSTaskDTO taskDTO = new WMSTaskDTO()
            var stationinfo = _stationManagerRepository.QueryFirst(x => x.stationChildCode == input.Position);
            if (stationinfo == null) throw new Exception("未知站台");
            if (stationinfo.stationType != 7)
            {
                TaskNum = task.TaskNum.Value,
                Grade = 1,
                PalletCode = task.PalletCode,
                RoadWay = task.Roadway,
                SourceAddress = task.SourceAddress,
                TargetAddress = task.TargetAddress,
                TaskState = task.TaskState.Value,
                Id = 0,
                TaskType = task.TaskType,
            };
            content.OK(data: taskDTO);
                if (input.PalletCode == null || input.PalletCode.Trim() == "")
                    return content.Error($"【{stationinfo.remark}】托盘条码为空");
            }
            var task = await CreateNewTaskByStation(input, stationinfo);
            // 尝试添加新任务
            if (task == null) return content.Error();
            var taskId = await BaseDal.AddDataAsync(task);
            bool isResult = taskId > 0;
            if (isResult)
            {
                // 创建WMS任务
                WMSTaskDTO taskDTO = new WMSTaskDTO()
                {
                    TaskNum = task.TaskNum.Value,
                    Grade = task.Grade.Value,
                    PalletCode = task.PalletCode,
                    RoadWay = task.Roadway,
                    SourceAddress = task.SourceAddress,
                    TargetAddress = task.TargetAddress,
                    TaskState = task.TaskState.Value,
                    Id = 0,
                    TaskType = task.TaskType,
                    ProductionLine = task.ProductionLine
                };
                content.OK(data: taskDTO);
            }
            else
                content.Error("添加任务失败");
            return content;
        }
        else
            content.Error("添加任务失败");
        return content;
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    /// <summary>
@@ -134,56 +125,63 @@
    /// <exception cref="Exception"></exception>
    private async Task<Dt_Task> CreateNewTaskByStation(RequestTaskDto input, Dt_StationManager stationManager)
    {
        Dt_Task task = null;
        switch (stationManager.stationType)
        try
        {
            case 6:
            case 1:
                task = await CreateInTaskAsync(input, stationManager); break;
            //case 2:
            case 3:  //异常排出给WCS处理
            //case 4:
            case 5:
                task = await CreateInToOutTaskAsync(input, stationManager); break;
            case 7:
                task = await CreateEmptyOutTaskAsync(input, stationManager); break;
            case 15:
                task = await CheckAbnormalTaskAsync(input, stationManager); break;
            default:
                throw new Exception("未知站台类型");
            Dt_Task task = null;
            switch (stationManager.stationType)
            {
                case 6:
                case 1:
                    task = await CreateInTaskAsync(input, stationManager); break;
                //case 2:
                case 3:  //异常排出给WCS处理
                         //case 4:
                case 5:
                    task = await CreateInToOutTaskAsync(input, stationManager); break;
                case 7:
                    task = await CreateEmptyOutTaskAsync(input, stationManager); break;
                case 15:
                    task = await CheckAbnormalTaskAsync(input, stationManager); break;
                default:
                    throw new Exception("未知站台类型");
            }
            return task;
        }
        return task;
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    #region 直接出库任务
    private async Task<Dt_Task> CreateInToOutTaskAsync(RequestTaskDto input, Dt_StationManager stationManager)
    {
        //if (stationManager.stationType != 5) throw new Exception("错误的调取");
        input.EquiCodeMOM = "24MEJQ11-1006-1";
        // 创建一个TrayCellsStatusDto对象,并赋值
        TrayCellsStatusDto trayCells = new TrayCellsStatusDto()
        try
        {
            Software = "WMS",
            TrayBarcode = input.PalletCode,
            //EquipmentCode = "EQ_CWJZ01"
            EquipmentCode = input.EquiCodeMOM
        };
            //if (stationManager.stationType != 5) throw new Exception("错误的调取");
        // 调用GetTrayCellStatusAsync方法,获取整盘电芯
        WebResponseContent content = await GetTrayCellStatusAsync(trayCells);
        // 如果状态为false,则返回content
        if (!content.Status) throw new Exception(content.Message);
            input.EquiCodeMOM = "24MEJQ11-1006-1";
        // 添加组盘信息
        // 将content.Data转换为ResultTrayCellsStatus对象
        var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString());
        if (result.SerialNos.Count <= 0)
        {
            ConsoleHelper.WriteErrorLine(result.MOMMessage);
            if (stationManager.stationType != 3)
            // 创建一个TrayCellsStatusDto对象,并赋值
            TrayCellsStatusDto trayCells = new TrayCellsStatusDto()
            {
                Software = "WMS",
                TrayBarcode = input.PalletCode,
                //EquipmentCode = "EQ_CWJZ01"
                EquipmentCode = input.EquiCodeMOM
            };
            // 调用GetTrayCellStatusAsync方法,获取整盘电芯
            WebResponseContent content = await GetTrayCellStatusAsync(trayCells);
            // 如果状态为false,则返回content
            if (!content.Status) throw new Exception(content.Message);
            // 组盘信息
            // 将content.Data转换为ResultTrayCellsStatus对象
            var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString());
            if (!result.Success)
            {
                var taskNG = new Dt_Task
                {
@@ -200,69 +198,115 @@
                    TaskState = (int)TaskInStatusEnum.Line_InFinish,
                    TaskType = (int)TaskOutboundTypeEnum.InToOut,
                    TaskNum = await BaseDal.GetTaskNo(),
                    Creater = "Systeam"
                    Creater = "System",
                    ProductionLine = result.ProductionLine,
                    ProcessCode = result.ProcessCode,
                };
                return taskNG;
            }
            else
            {
                return null;
            }
        }
        // 处理异常电芯情况
        var serialNosError = result.SerialNos.Where(x => x.SerialNoStatus != 1).ToList();
        if (serialNosError.Count > 0)
        {
            if (stationManager.stationType != 3)
            if (result.SerialNos.Count <= 0)
            {
                var taskNG = new Dt_Task
                ConsoleHelper.WriteErrorLine(result.MOMMessage);
                if (stationManager.stationType != 3)
                {
                    CurrentAddress = input.Position,
                    Grade = 1,
                    Roadway = input.Roadways,
                    TargetAddress = stationManager.stationNGLocation,
                    Dispatchertime = DateTime.Now,
                    MaterialNo = "",
                    NextAddress = stationManager.stationNGChildCode,
                    OrderNo = null,
                    PalletCode = input.PalletCode,
                    SourceAddress = stationManager.stationLocation,
                    TaskState = (int)TaskInStatusEnum.Line_InFinish,
                    TaskType = (int)TaskOutboundTypeEnum.OutNG,
                    TaskNum = await BaseDal.GetTaskNo(),
                    Creater = "Systeam"
                };
                return taskNG;
                    var taskNG = new Dt_Task
                    {
                        CurrentAddress = input.Position,
                        Grade = 3,
                        Roadway = input.Roadways,
                        TargetAddress = stationManager.stationNGLocation,
                        Dispatchertime = DateTime.Now,
                        MaterialNo = "",
                        NextAddress = stationManager.stationNGChildCode,
                        OrderNo = null,
                        PalletCode = input.PalletCode,
                        SourceAddress = stationManager.stationLocation,
                        TaskState = (int)TaskInStatusEnum.Line_InFinish,
                        TaskType = (int)TaskOutboundTypeEnum.InToOut,
                        TaskNum = await BaseDal.GetTaskNo(),
                        Creater = "System"
                    };
                    return taskNG;
                }
                else
                {
                    //无电芯 → 当空框? 还是返回异常?
                    return null;
                }
            }
            // 处理异常电芯情况
            //var serialNosError = result.SerialNos.Where(x => x.SerialNoStatus != 1 && x.SerialNoStatus != 4).ToList();
            //if (serialNosError.Count > 0)
            //{
            //    if (stationManager.stationType != 3)
            //    {
            //        var taskNG = new Dt_Task
            //        {
            //            CurrentAddress = input.Position,
            //            Grade = 1,
            //            Roadway = input.Roadways,
            //            TargetAddress = stationManager.stationNGLocation,
            //            Dispatchertime = DateTime.Now,
            //            MaterialNo = "",
            //            NextAddress = stationManager.stationNGChildCode,
            //            OrderNo = null,
            //            PalletCode = input.PalletCode,
            //            SourceAddress = stationManager.stationLocation,
            //            TaskState = (int)TaskInStatusEnum.Line_InFinish,
            //            TaskType = (int)TaskOutboundTypeEnum.InToOut,
            //            TaskNum = await BaseDal.GetTaskNo(),
            //            Creater = "System",
            //            ProductionLine = result.ProductionLine,
            //            ProcessCode = result.ProcessCode,
            //        };
            //        return taskNG;
            //    }
            //    else
            //    {
            //        Console.WriteLine($"站台{stationManager.stationChildCode}MOM返回电芯异常:{result.MOMMessage}");
            //        return null;
            //    }
            //}
            //else
            //{
            //    throw new Exception($"站台{stationManager.stationChildCode}MOM返回电芯异常:{result.MOMMessage}");
            //}
            var targetStation = _stationManagerRepository.QueryFirst(x => x.stationPLC == stationManager.stationPLC && x.Roadway == stationManager.Roadway && x.stationType == 2);
            var task = new Dt_Task
            {
                CurrentAddress = input.Position,
                Grade = 3,
                Roadway = input.Roadways,
                TargetAddress = targetStation.stationLocation,
                Dispatchertime = DateTime.Now,
                MaterialNo = "",
                NextAddress = input.Roadways,
                OrderNo = null,
                PalletCode = input.PalletCode,
                SourceAddress = stationManager.stationLocation,
                TaskState = (int)TaskInStatusEnum.Line_InFinish,
                TaskType = (int)TaskOutboundTypeEnum.InToOut,
                TaskNum = await BaseDal.GetTaskNo(),
                Creater = "System",
                ProductionLine = result.ProductionLine,
                ProcessCode = result.ProcessCode,
            };
            return task;
        }
        var targetStation = _stationManagerRepository.QueryFirst(x => x.stationPLC == stationManager.stationPLC && x.Roadway == stationManager.Roadway && x.stationType == 2);
        var task = new Dt_Task
        catch (Exception ex)
        {
            CurrentAddress = input.Position,
            Grade = 1,
            Roadway = input.Roadways,
            TargetAddress = targetStation.stationLocation,
            Dispatchertime = DateTime.Now,
            MaterialNo = "",
            NextAddress = input.Roadways,
            OrderNo = null,
            PalletCode = input.PalletCode,
            SourceAddress = stationManager.stationLocation,
            TaskState = (int)TaskInStatusEnum.Line_InFinish,
            TaskType = (int)TaskOutboundTypeEnum.InToOut,
            TaskNum = await BaseDal.GetTaskNo(),
            Creater = "Systeam"
        };
        return task;
            throw new Exception(ex.Message);
        }
    }
    #endregion
    #endregion 直接出库任务
    #region 入库任务
    private async Task<Dt_Task> CreateInTaskAsync(RequestTaskDto input, Dt_StationManager stationManager)
    {
        if (stationManager.stationType != 1 && stationManager.stationType != 6) throw new Exception("错误的调取");
@@ -280,25 +324,33 @@
        if (locationInfo == null) throw new Exception("库位已满");
        // 创建一个TrayCellsStatusDto对象,并赋值
        TrayCellsStatusDto trayCells = new TrayCellsStatusDto()
        {
            Software = "WMS",
            TrayBarcode = input.PalletCode,
            //EquipmentCode = "EQ_CWJZ01"
            EquipmentCode = input.EquiCodeMOM
        };
        // 调用GetTrayCellStatusAsync方法,获取整盘电芯
        WebResponseContent content = await GetTrayCellStatusAsync(trayCells);
        // 如果状态为false,则返回content
        if (!content.Status) throw new Exception(content.Message);
        // 将content.Data转换为ResultTrayCellsStatus对象
        var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString());
        if (stationManager.stationType == 6 && result.ProductionLine.IsNullOrEmpty())
        {
            ConsoleHelper.WriteErrorLine($"当前托盘无产线,联系MOM添加产线");
            throw new Exception("当前托盘无产线,联系MOM添加产线");
        }
        if (stationManager.stationType == 1)
        {
            // 创建一个TrayCellsStatusDto对象,并赋值
            TrayCellsStatusDto trayCells = new TrayCellsStatusDto()
            {
                Software = "WMS",
                TrayBarcode = input.PalletCode,
                //EquipmentCode = "EQ_CWJZ01"
                EquipmentCode = input.EquiCodeMOM
            };
            #region
            // 调用GetTrayCellStatusAsync方法,获取整盘电芯
            WebResponseContent content = await GetTrayCellStatusAsync(trayCells);
            // 如果状态为false,则返回content
            if (!content.Status) throw new Exception(content.Message);
            // 添加组盘信息
            // 将content.Data转换为ResultTrayCellsStatus对象
            var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString());
            if (result.SerialNos.Count <= 0)
            {
                ConsoleHelper.WriteErrorLine(result.MOMMessage);
@@ -317,7 +369,9 @@
                    TaskState = (int)TaskInStatusEnum.Line_InFinish,
                    TaskType = (int)TaskOutboundTypeEnum.InToOut,
                    TaskNum = await BaseDal.GetTaskNo(),
                    Creater = "Systeam"
                    Creater = "System",
                    ProductionLine = result.ProductionLine,
                    ProcessCode = result.ProcessCode,
                };
                return taskNG;
            }
@@ -347,6 +401,7 @@
            ////// 调用GetProcessResponseAsync方法,获取工艺响应
            ////var processResponse = await GetProcessResponseAsync(process, input.Position);
            var isBox = await _boxingInfoRepository.AddDataNavAsync(boxing);
            #endregion 入库任务
        }
        var task = new Dt_Task
@@ -364,27 +419,35 @@
            TaskState = (int)TaskInStatusEnum.Line_InFinish,
            TaskType = stationManager.stationType == 1 ? (int)TaskInboundTypeEnum.Inbound : (int)TaskInboundTypeEnum.InTray,
            TaskNum = await BaseDal.GetTaskNo(),
            Creater = "Systeam"
            Creater = "System",
            ProductionLine = result.ProductionLine,
            ProcessCode = result.ProcessCode,
        };
        ConsoleHelper.WriteSuccessLine($"修改前:" + locationInfo.LocationStatus.ToString());
        int lastStatus = locationInfo.LocationStatus;
        ConsoleHelper.WriteSuccessLine($"修改前:" + lastStatus.ToString());
        locationInfo.LocationStatus = (int)LocationEnum.FreeDisable;
        ConsoleHelper.WriteSuccessLine($"修改后:" + locationInfo.LocationStatus.ToString());
        await UpdateLocationAsync(locationInfo);
        _locationStatusChangeRecordRepository.AddLocationStatusChangeRecord(locationInfo, lastStatus, (int)StatusChangeTypeEnum.AutomaticStorage, task.TaskNum);
        return task;
    }
    #endregion
    #endregion 请求任务入库
    #region 库位分配
    #region 获取货位
    /// <summary>
    ///
    ///
    /// </summary>
    /// <param name="requestTask">请求参数</param>
    /// <param name="isCheckRequest">是否未检测库位类型</param>
    /// <returns></returns>
    private async Task<DtLocationInfo> RequestLocation(RequestTaskDto requestTask, bool isCheckRequest = false)
    {
        #region 获取货位
        try
        {
            List<DtLocationInfo> locations;
@@ -395,6 +458,10 @@
            else
            {
                locations = await _locationRepository.QueryDataAsync(x => x.LocationStatus == (int)LocationEnum.Distribute && x.RoadwayNo == requestTask.Roadways && x.EnalbeStatus == 1 && x.LocationType == 1);
                if (locations.IsNullOrEmpty())
                {
                    locations = await _locationRepository.QueryDataAsync(x => x.LocationStatus == (int)LocationEnum.Free && x.RoadwayNo == requestTask.Roadways && x.EnalbeStatus == 1 && x.LocationType == 1);
                }
            }
            if (locations == null)
@@ -409,15 +476,49 @@
            Console.WriteLine(err.Message.ToString());
            return null;
        }
        #endregion 获取货位
    }
    #endregion
    #endregion 获取货位
    #region 异常口入库获取库位
    private async Task<DtLocationInfo> RequestLocationByAbnormal(RequestTaskDto requestTask, bool isCheckRequest = false)
    {
        try
        {
            List<DtLocationInfo> locations;
            if (isCheckRequest)
            {
                locations = await _locationRepository.QueryDataAsync(x => x.LocationStatus == (int)LocationEnum.Free && x.RoadwayNo == requestTask.Roadways && x.EnalbeStatus == 1 && x.LocationType == 2 && x.Remark == "1");
            }
            else
            {
                locations = await _locationRepository.QueryDataAsync(x => x.LocationStatus == (int)LocationEnum.Free && x.RoadwayNo == requestTask.Roadways && x.EnalbeStatus == 1 && x.LocationType == 1);
            }
            if (locations == null)
            {
                return null;
            }
            return locations.OrderBy(x => x.Layer).ThenBy(x => x.Column).ThenBy(x => x.Row).FirstOrDefault();
        }
        catch (Exception err)
        {
            Console.WriteLine(err.Message.ToString());
            return null;
        }
    }
    #endregion 异常口入库获取库位
    #endregion 库位分配
    // 获取工艺申请
    private async Task<ProcessApplyDto> GetProcessApplyAsync(RequestTaskDto input, ResultTrayCellsStatus content)
    private Task<ProcessApplyDto> GetProcessApplyAsync(RequestTaskDto input, ResultTrayCellsStatus content)
    {
        // 创建一个ProcessApplyDto对象,并赋值
        return new ProcessApplyDto()
        return Task.FromResult(new ProcessApplyDto()
        {
            EquipmentCode = input.EquiCodeMOM,
            Software = "WMS",
@@ -426,23 +527,30 @@
            {
                SerialNo = item.SerialNo
            }).ToList()
        };
        });
    }
    #endregion 请求空框出库
    #endregion 请求任务入库
    #region 创建空框出库任务
    public async Task<Dt_Task> CreateEmptyOutTaskAsync(RequestTaskDto input, Dt_StationManager stationManager)
    {
        try
        {
            if (stationManager.stationType != 7) throw new Exception("错误的调取");
            // 从缓存中获取库存信息
            //IDictionary<string, DtStockInfo>? stockInfos = _simpleCacheService.HashGetAll<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo);
            //List<DtStockInfo> stockInfoList = stockInfos.Values.ToList();
            //var stockinfo1 = stockInfoList.OrderBy(x => x.CreateDate)
            //    .ToList();
            //var stockinfo = stockinfo1.Where(x => x.LocationInfo != null && !x.IsFull && x.LocationInfo.RoadwayNo == stationManager.Roadway)
            //    .FirstOrDefault();
            var stockinfo = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
                .Includes(x => x.LocationInfo)
                //.Includes(x=>x.StockInfoDetails)
                .Where(x => !x.IsFull && x.LocationInfo.RoadwayNo == stationManager.Roadway)
                .OrderBy(x => x.CreateDate)
                .OrderBy(x=>x.CreateDate)
                .FirstAsync();
            if (stockinfo == null) return null;
@@ -450,7 +558,7 @@
            var task = new Dt_Task
            {
                CurrentAddress = input.Position,
                Grade = 1,
                Grade = 2,
                Roadway = input.Roadways,
                TargetAddress = stationManager.stationLocation,
                Dispatchertime = DateTime.Now,
@@ -462,20 +570,22 @@
                TaskState = (int)TaskOutStatusEnum.OutNew,
                TaskType = (int)TaskOutboundTypeEnum.OutTray,
                TaskNum = await BaseDal.GetTaskNo(),
                Creater = "Systeam"
                Creater = "System",
                ProductionLine = stockinfo.ProductionLine,
            };
            //_simpleCacheService.HashDel<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, new string[] { task.PalletCode });
            return task;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    #endregion
    #region 直接出库任务完成
    public async Task<WebResponseContent> CompleteInToOutTaskAsync(Dt_Task task)
    {
        WebResponseContent content = new WebResponseContent();
@@ -483,7 +593,8 @@
        {
            //添加历史
            var taskHty = CreateHistoricalTask(task);
            // 添加历史任务
            var isTaskHtyAdd = await _task_HtyRepository.AddDataAsync(taskHty) > 0;
            //删除任务
            BaseDal.DeleteData(task);
@@ -494,13 +605,24 @@
            return content.Error(ex.Message);
        }
    }
    #endregion
    #region 异常口任务检测
    /// <summary>
    /// 异常排出口入库校验  所有异常交给WCS做原地址NG处理
    /// </summary>
    /// <param name="input"></param>
    /// <param name="stationManager"></param>
    /// <returns></returns>
    /// <exception cref="Exception"></exception>
    public async Task<Dt_Task> CheckAbnormalTaskAsync(RequestTaskDto input, Dt_StationManager stationManager)
    {
        try
        {
            if (stationManager.stationType == 15) throw new Exception("");
            // 创建一个TrayCellsStatusDto对象,并赋值
            TrayCellsStatusDto trayCells = new TrayCellsStatusDto()
            {
@@ -513,32 +635,41 @@
            // 调用GetTrayCellStatusAsync方法,获取整盘电芯
            WebResponseContent content = await GetTrayCellStatusAsync(trayCells);
            // 如果状态为false,则返回content
            if (!content.Status) throw new Exception(content.Message);
            if (!content.Status)  //获取整盘电芯数据, 如异常 使用空框类型入库
            {
                ConsoleHelper.WriteErrorLine(content.Message);
                throw new Exception("MOM整盘电芯属性获取异常");
            }
            // 添加组盘信息
            // 将content.Data转换为ResultTrayCellsStatus对象
            var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString());
            if (result.SerialNos.Count <= 0)
            if (result.SerialNos.Count <= 0)  //如调用成功 但电芯为0则定为空盘
            {
                ConsoleHelper.WriteErrorLine(result.MOMMessage);
                var taskNG = new Dt_Task
                DtLocationInfo EmptylocationInfo = await RequestLocationByAbnormal(input);
                if (EmptylocationInfo == null) throw new Exception("库位已满");
                var Epmtytask = new Dt_Task
                {
                    CurrentAddress = input.Position,
                    Grade = 1,
                    Grade = 4,  //优先处理异常排出口的任务 防止正常需排出异常口的任务堵线
                    Roadway = input.Roadways,
                    TargetAddress = stationManager.stationNGLocation,
                    TargetAddress = EmptylocationInfo.LocationCode,
                    Dispatchertime = DateTime.Now,
                    MaterialNo = "",
                    NextAddress = stationManager.stationNGChildCode,
                    NextAddress = input.Roadways,
                    OrderNo = null,
                    PalletCode = input.PalletCode,
                    SourceAddress = stationManager.stationLocation,
                    TaskState = (int)TaskInStatusEnum.Line_InFinish,
                    TaskType = (int)TaskOutboundTypeEnum.InToOut,
                    TaskType = (int)TaskInboundTypeEnum.InTray,
                    TaskNum = await BaseDal.GetTaskNo(),
                    Creater = "Systeam"
                    Creater = "System",
                    ProductionLine = result.ProductionLine,
                    ProcessCode = result.ProcessCode,
                };
                return taskNG;
                return Epmtytask;
            }
            //Console.WriteLine(result);
@@ -551,51 +682,79 @@
            var boxing = CreateBoxingInfo(result, input.PalletCode);
            if (boxing == null) throw new Exception("组盘失败");
            //// 调用GetProcessApplyAsync方法,获取工艺路线
            //ProcessApplyDto process = await GetProcessApplyAsync(input, result);
            if (!stationManager.Roadway.Contains("FR"))  //非分容库区 入库验证工艺路线
            {
                // 调用GetProcessApplyAsync方法,获取工艺路线
                ProcessApplyDto process = await GetProcessApplyAsync(input, result);
            //// 如果process为null,则返回content
            //if (process == null) return content;
                // 如果process为null,则返回content
                if (process == null) throw new Exception("工艺请求参数异常");
            //// 调用_processApplyService.GetProcessApplyAsync方法,获取工艺申请
            //content = await _processApplyService.GetProcessApplyAsync(process);
                // 调用_processApplyService.GetProcessApplyAsync方法,获取工艺申请
                content = await _processApplyService.GetProcessApplyAsync(process);
            //// 如果状态为false,则返回null
            //if (!content.Status) return content.Error("工艺申请失败");
                // 如果状态为false,则返回null
                if (!content.Status) throw new Exception("工艺申请失败");
            }
            ////// 调用GetProcessResponseAsync方法,获取工艺响应
            ////var processResponse = await GetProcessResponseAsync(process, input.Position);
            var isBox = await _boxingInfoRepository.AddDataNavAsync(boxing);
            DtLocationInfo locationInfo = null;
            if (stationManager.Roadway.Contains("FR"))
            {
                locationInfo = await RequestLocation(input, true);
            }
            else
            {
                locationInfo = await RequestLocationByAbnormal(input);
            }
            //DtLocationInfo locationInfo = await RequestLocation(input);
            if (locationInfo == null) throw new Exception("库位已满");
            var task = new Dt_Task
            {
                CurrentAddress = input.Position,
                Grade = 1,
                Grade = 3,  //优先处理异常排出口的任务 防止正常需排出异常口的任务堵线
                Roadway = input.Roadways,
                TargetAddress = stationManager.stationLocation,
                TargetAddress = locationInfo.LocationCode,
                Dispatchertime = DateTime.Now,
                MaterialNo = "",
                NextAddress = input.Roadways,
                OrderNo = null,
                //PalletCode = stockinfo.PalletCode,
                //SourceAddress = stockinfo.LocationCode,
                TaskState = (int)TaskOutStatusEnum.OutNew,
                TaskType = (int)TaskOutboundTypeEnum.OutTray,
                PalletCode = input.PalletCode,
                SourceAddress = stationManager.stationLocation,
                TaskState = (int)TaskInStatusEnum.Line_InFinish,
                TaskType = (int)TaskInboundTypeEnum.Inbound,
                TaskNum = await BaseDal.GetTaskNo(),
                Creater = "Systeam"
                Creater = "System",
                ProductionLine = result.ProductionLine,
                ProcessCode = result.ProcessCode,
            };
            var isBox = await _boxingInfoRepository.AddDataNavAsync(boxing);
            int lastStatus = locationInfo.LocationStatus;
            ConsoleHelper.WriteSuccessLine($"修改前:" + lastStatus.ToString());
            locationInfo.LocationStatus = (int)LocationEnum.FreeDisable;
            ConsoleHelper.WriteSuccessLine($"修改后:" + locationInfo.LocationStatus.ToString());
            await UpdateLocationAsync(locationInfo);
            _locationStatusChangeRecordRepository.AddLocationStatusChangeRecord(locationInfo, lastStatus, (int)StatusChangeTypeEnum.AutomaticStorage, task.TaskNum);
            return task;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
    #endregion
    #region 移库任务事务
    private async Task<bool> ExecuteTransaction(DtStockInfo stock, Dt_Task_Hty taskHty, DtLocationInfo fromLocation, DtLocationInfo toLocation, int taskId)
    {
        _unitOfWorkManage.BeginTran();
@@ -634,12 +793,13 @@
            throw new Exception(err.Message); // 抛出异常以便外部捕获
        }
    }
    #endregion
    #endregion MyRegion
    #region 检测高温库是否有可出库库存
    public WebResponseContent StockCheckingAsync()
    {
        WebResponseContent webResponseContent = new WebResponseContent();
        try
        {
@@ -649,21 +809,27 @@
                {
                    try
                    {
                        Thread.Sleep(10000);
                        Thread.Sleep(TimeSpan.FromMinutes(10));
                        var area = await _areaInfoRepository.QueryFirstAsync(x => x.AreaCode == "GWSC1");
                        var devices = SqlSugarHelper.DbWCS.Queryable<Dt_DeviceInfo>()
                            .Where(x => x.DeviceStatus == "1")
                            .Where(x => x.DeviceCode.Contains("GWSC"))
                            .ToList();
                        var deviceCode = devices.Select(x => x.DeviceCode).ToList();
                        var stockInfo = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
                             .Includes(x => x.LocationInfo) // 预加载LocationInfo
                             .Includes(x => x.StockInfoDetails) // 预加载StockInfoDetails
                             .Where(x => x.AreaCode == area.AreaCode && x.OutboundTime < DateTime.Now && x.IsFull == true) // 过滤条件
                             .Where(x => x.LocationInfo.LocationStatus == (int)LocationEnum.InStock && x.LocationInfo.AreaId == area.AreaID) // 过滤条件
                             .WhereIF(!deviceCode.IsNullOrEmpty(), x => deviceCode.Contains(x.LocationInfo.RoadwayNo))
                             .OrderBy(x => x.OutboundTime) // 排序
                             .ToListAsync(); // 获取第一个元素
                        if (stockInfo.Count <= 0) continue;
                        foreach (var item in stockInfo)
                        {
                            var hasTask = BaseDal.QueryFirst(x => x.PalletCode == item.PalletCode);
                            if (hasTask != null)
                            {
@@ -682,7 +848,6 @@
                            // 创建任务DTO
                            WMSTaskDTO taskDTO = CreateTaskDTO(task);
                            var configs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                            var wmsBase = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.WCSIPAddress)?.ConfigValue;
                            var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.ReceiveTask)?.ConfigValue;
@@ -696,10 +861,13 @@
                            var content = JsonConvert.DeserializeObject<WebResponseContent>(result);
                            if (content.Status)
                            {
                                int lastStatus = item.LocationInfo.LocationStatus;
                                await BaseDal.AddDataAsync(task);
                                // 更新库存位置状态为不可用
                                item.LocationInfo.LocationStatus = (int)LocationEnum.InStockDisable;
                                await _locationRepository.UpdateDataAsync(item.LocationInfo);
                                _locationStatusChangeRecordRepository.AddLocationStatusChangeRecord(item.LocationInfo, lastStatus, (int)StatusChangeTypeEnum.AutomaticDelivery, task.TaskNum);
                            }
                        }
                    }
@@ -718,15 +886,39 @@
        }
    }
    #endregion
    #endregion 检测高温库是否有可出库库存
    #region 常温补空托盘至分容
    private static readonly Dictionary<string, (int Count, DateTime LastRequestTime)> requestTrackerToCW = new();
    public async Task<WebResponseContent> GetFROutTrayToCW(RequestTaskDto taskDTO)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var station = _stationManagerRepository.QueryFirst(x => x.stationChildCode == taskDTO.Position);
            //string requestKey = JsonConvert.SerializeObject(taskDTO);
            //// 检查请求次数和时间限制
            //if (requestTrackerToCW.TryGetValue(requestKey, out var requestInfo))
            //{
            //    if (requestInfo.Count > 5 && DateTime.Now < requestInfo.LastRequestTime.AddMinutes(2))
            //    {
            //        // 如果请求次数超过限制且未超过10分钟,抛出异常
            //        throw new InvalidOperationException("请求次数已达到限制,请稍后再试。");
            //    }
            //}
            //// 更新请求跟踪信息
            //if (requestTrackerToCW.ContainsKey(requestKey))
            //{
            //    requestTrackerToCW[requestKey] = (requestInfo.Count + 1, DateTime.Now);
            //}
            //else
            //{
            //    requestTrackerToCW[requestKey] = (1, DateTime.Now);
            //}
            var station = _stationManagerRepository.QueryFirst(x => x.stationChildCode == taskDTO.Position && x.stationStatus == "1");
            var locations = _locationRepository.QueryData(x => x.RoadwayNo == station.Roadway && x.LocationStatus == (int)LocationEnum.Free && x.LocationType == 1);
            if (locations.Count > 10)
@@ -735,7 +927,7 @@
                var location = locations.OrderBy(x => x.Layer).ThenBy(x => x.Column).ThenBy(x => x.Row).FirstOrDefault();
                var stockInfo = await QueryStockInfoForEmptyTrayAsync("CWSC1");
                var stockInfo = await QueryStockInfoForEmptyTrayFRAsync("CWSC1", "10086", taskDTO.ProductionLine);
                if (stockInfo != null)
                {
@@ -745,6 +937,7 @@
                    WMSTaskDTO wmsTask = CreateTaskDTO(task);
                    // 更新库存位置状态为不可用
                    int lastStatus = location.LocationStatus;
                    stockInfo.LocationInfo.LocationStatus = (int)LocationEnum.InStockDisable;
                    location.LocationStatus = (int)LocationEnum.Distribute;
                    await _unitOfWorkManage.UseTranAsync(async () =>
@@ -754,7 +947,11 @@
                        await _locationRepository.UpdateDataAsync(location);
                    });
                    _locationStatusChangeRecordRepository.AddLocationStatusChangeRecord(location, lastStatus, (int)StatusChangeTypeEnum.AutomaticDelivery, task.TaskNum);
                    // 返回成功响应
                    //requestTrackerToCW.Remove(requestKey);
                    return content.OK(data: wmsTask);
                }
                else
@@ -768,27 +965,139 @@
        return content;
    }
    /// <summary>
    /// 查询空盘库存信息
    /// </summary>
    private async Task<DtStockInfo> QueryStockInfoForEmptyTrayFRAsync(string areaCode, string position, string productLine)
    {
        var area = await _areaInfoRepository.QueryFirstAsync(x => x.AreaCode == areaCode);
        ConsoleHelper.WriteColorLine(position + "..." + areaCode, ConsoleColor.Magenta);
        var station = await _stationManagerRepository.QueryFirstAsync(x => x.stationChildCode == position && x.stationType == 17);
        ConsoleHelper.WriteColorLine(station.Roadway, ConsoleColor.Magenta);
        var stackers = station.Roadway.Split(',').ToList();
        var deviceCode = SqlSugarHelper.DbWCS.Queryable<Dt_DeviceInfo>()
            .Where(x => x.DeviceStatus == "1")
            .Where(x => stackers.Contains(x.DeviceCode))
            .ToList().Select(x => x.DeviceCode).ToList();
        // 从缓存中获取库存信息
        //IDictionary<string, DtStockInfo>? stockInfos = _simpleCacheService.HashGetAll<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo);
        //List<DtStockInfo> stockInfoList = stockInfos.Values.ToList();
        //var result1 = stockInfoList.Where(x => x.ProductionLine == productLine)
        //    .Where(x => x.AreaCode == areaCode && x.IsFull == false)
        //    .WhereIF(!deviceCode.IsNullOrEmpty(), x => x.LocationInfo != null && deviceCode.Contains(x.LocationInfo.RoadwayNo))
        //    .OrderBy(x => x.CreateDate) // 排序
        //    .ToList(); // 转换为列表
        //var result = result1.Where(x => x.StockInfoDetails != null && x.StockInfoDetails.Any(y => y.MaterielCode == "空托盘"))
        //    .Where(x => x.LocationInfo != null && x.LocationInfo.LocationStatus == (int)LocationEnum.InStock && x.LocationInfo.AreaId == area.AreaID && x.LocationInfo.EnalbeStatus == (int)EnableEnum.Enable) // 过滤条件
        //    .FirstOrDefault(); // 获取第一个元素
        //if (result != null)
        //    _simpleCacheService.HashDel<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, new string[] { result.PalletCode });
        //else
        //    ConsoleHelper.WriteColorLine($"常温{productLine}空托盘库存不足", ConsoleColor.Red);
        var result = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
                                .Includes(x => x.LocationInfo)
                                .Includes(x => x.StockInfoDetails)
                                .Where(x => x.AreaCode == areaCode && x.IsFull == false && x.ProductionLine == productLine)
                                .WhereIF(!deviceCode.IsNullOrEmpty(), x => deviceCode.Contains(x.LocationInfo.RoadwayNo))
                                .Where(x => x.LocationInfo.LocationStatus == (int)LocationEnum.InStock && x.LocationInfo.AreaId == area.AreaID && x.LocationInfo.EnalbeStatus == (int)EnableEnum.Enable) // 过滤条件
                                .Where(x => x.StockInfoDetails.Any(y => y.MaterielCode == "空托盘"))
                                .OrderBy(x => x.CreateDate) // 排序
                                .FirstAsync();
        if (result.IsNullOrEmpty())
            ConsoleHelper.WriteColorLine($"常温{productLine}空托盘库存不足", ConsoleColor.Red);
        return result;
    }
    #endregion
    #region  常温3出库至包装
    // 用于追踪每个请求的调用次数和最后请求时间。
    private static readonly Dictionary<string, (int Count, DateTime LastRequestTime)> requestTracker = new();
    /// <summary>
    /// 常温3出库至包装
    /// </summary>
    /// <param name="json"></param>
    /// <returns></returns>
    public async Task<WebResponseContent> RequestOutTaskToBZAsync(RequestTaskDto json)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            Dt_StationManager station = _stationManagerRepository.QueryFirst(x => x.stationChildCode == json.Position);
            Dt_StationManager station = _stationManagerRepository.QueryFirst(x => x.stationChildCode == json.Position && x.stationType == 12 && x.stationArea == "Call");
            if (station == null) { throw new Exception($"未找到包装站台信息,请检查传入参数{json.Position}"); }
            DtStockInfo stockInfo = _stockInfoRepository.QueryFirst(X => X.IsFull && X.AreaCode == "CWSC3" && X.ProductionLine == "ZJ-7");
            if (stockInfo == null) throw new Exception("库内无满足条件的库存可出库");
            var deviceCode = SqlSugarHelper.DbWCS.Queryable<Dt_DeviceInfo>()
                .Where(x => x.DeviceStatus == "1")
                .Where(x => x.DeviceCode.Contains("CWSC")) // 过滤条件
                .ToList().Select(x => x.DeviceCode).ToList();
            Dt_StationManager OutStation = _stationManagerRepository.QueryFirst(x => x.stationPLC == "1016" && x.stationType == 10);
            //var outBoundMateriel = AppSettings.app<OutBoundMateriel>("OutBoundMateriel");
            var outBoundMateriel = _dt_ChangeoversRepository.QueryData(x => x.Status == "1").ToList();
            List<string>? materielCodes = outBoundMateriel.Count != 0
                ? outBoundMateriel.Where(x => x.ProductionLine == station.productLine && x.ProcessCode == "CWSC3")
                                  .Select(x => x.MaterielCode)
                                  .ToList()
                : null;
            #region
            //// 从缓存中获取库存信息
            //IDictionary<string, DtStockInfo>? stockInfos = _simpleCacheService.HashGetAll<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo);
            //List<DtStockInfo> stockInfoList = stockInfos.Values.ToList();
            //// 修改后的查询代码
            //var stockInfo1 = stockInfoList
            //    .Where(x => x.ProductionLine == station.productLine)
            //    .Where(x => x.AreaCode == "CWSC3" && x.IsFull == true)
            //    .OrderBy(x => x.OutboundTime)
            //    .ToList();
            //var stockInfo = stockInfo1
            //    // 增加对 LocationInfo 的空值检查
            //    .Where(x => x.LocationInfo != null && x.LocationInfo.LocationStatus == (int)LocationEnum.InStock)
            //    // 增加对 LocationInfo 的空值检查
            //    .WhereIF(!deviceCode.IsNullOrEmpty(), x => x.LocationInfo != null && deviceCode.Contains(x.LocationInfo.RoadwayNo))
            //    .WhereIF(!materielCodes.IsNullOrEmpty(), x => x.StockInfoDetails != null && x.StockInfoDetails.Any(y => materielCodes.Contains(y.MaterielCode)))
            //    .FirstOrDefault();
            // 修改后的查询代码
            #endregion
            var stockInfo = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
                                .Includes(x => x.LocationInfo)
                                .Includes(x => x.StockInfoDetails)
                                .Where(x => x.ProductionLine == station.productLine)
                                .Where(x => x.AreaCode == "CWSC3" && x.IsFull == true)
                                // 增加对 LocationInfo 的空值检查
                                .Where(x => x.LocationInfo.LocationStatus == (int)LocationEnum.InStock)
                                // 增加对 LocationInfo 的空值检查
                                .WhereIF(!deviceCode.IsNullOrEmpty(), x => deviceCode.Contains(x.LocationInfo.RoadwayNo))
                                .WhereIF(!materielCodes.IsNullOrEmpty(), x => x.StockInfoDetails.Any(y => materielCodes.Contains(y.MaterielCode)))
                                .OrderBy(x => x.OutboundTime)
                                .FirstAsync();
            if (stockInfo == null) throw new Exception($"库内{station.productLine}无满足条件的库存可出库");
            DtLocationInfo locationInfo = _locationRepository.QueryFirst(x => x.AreaId == 5 && x.LocationCode == stockInfo.LocationCode);
            Dt_StationManager OutStation = _stationManagerRepository.QueryFirst(x => x.stationPLC == "1016" && x.stationType == 10 && x.Roadway == locationInfo.RoadwayNo && x.stationStatus == "1");
            // 创建新任务实例
            var task = new Dt_Task
            {
                CurrentAddress = stockInfo.LocationInfo.RoadwayNo,
                CurrentAddress = stockInfo.LocationCode,
                Grade = 1,
                Roadway = stockInfo.LocationInfo.RoadwayNo,
                Roadway = locationInfo.RoadwayNo,
                TargetAddress = json.Position,
                Dispatchertime = DateTime.Now,
                MaterialNo = "",
@@ -799,17 +1108,544 @@
                TaskState = (int)TaskOutStatusEnum.OutNew,
                TaskType = (int)TaskOutboundTypeEnum.Outbound,
                TaskNum = await BaseDal.GetTaskNo(),
                Creater = "Systeam"
                Creater = "System",
                ProductionLine = stockInfo.ProductionLine,
                ProcessCode = stockInfo.ProcessCode,
            };
            WMSTaskDTO taskDTO = CreateTaskDTO(task);
            int lastStatus = locationInfo.LocationStatus;
            BaseDal.AddData(task);
            stockInfo.LocationInfo.LocationStatus = (int)LocationEnum.InStockDisable;
            _locationRepository.UpdateData(stockInfo.LocationInfo);
            //_simpleCacheService.HashDel<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo, new string[] { taskDTO.PalletCode });
            _locationStatusChangeRecordRepository.AddLocationStatusChangeRecord(stockInfo.LocationInfo, lastStatus, (int)StatusChangeTypeEnum.AutomaticDelivery, task.TaskNum);
            return content.OK(data: taskDTO);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"CW3至包装出库异常:{ex.ToString()}");
            return content.Error($"失败:{ex.Message}");
        }
    }
}
    #endregion
    #region 火警出库
    public WebResponseContent EmergencyTask(Object obj)
    {
        WebResponseContent content = new WebResponseContent();
        var emergencyTask = new DTSEmergencyTask();
        try
        {
            Console.WriteLine($"火警参数:{obj.ToString()}");
            emergencyTask = JsonConvert.DeserializeObject<DTSEmergencyTask>(obj.ToString());
            if (emergencyTask == null) throw new Exception("火警参数为空");
            string[] strings = emergencyTask.LocationCode.Split("-");
            string Roadway = strings[0];
            //switch (Roadways[0])
            //{
            //    case "J":
            //        Roadway = "JZSC" + Roadways[1];
            //        break;
            //    case "G":
            //        Roadway = "GWSC" + Roadways[1];
            //        break;
            //    case "C":
            //        Roadway = "CWSC" + Roadways[1];
            //        break;
            //    default: throw new Exception("未识别库位编码");
            //}
            int Row = Convert.ToInt16(strings[1]);
            int Column= Convert.ToInt16(strings[2]);
            int Layer= Convert.ToInt16(strings[3]);
            if (!strings[0].Contains("SC")) throw new Exception("未知库区");
            for (int i = 0; i < 2; i++)
            {
                DtLocationInfo locationInfo = _locationRepository.QueryFirst(x => x.Row == Row && x.Column == Column && x.Layer == (i == 0 ? Layer - 1 : Layer) && x.RoadwayNo == Roadway);
                if (locationInfo == null)
                {
                    throw new Exception("未知库位");
                }
                //查找消防站台
                var station = _stationManagerRepository.QueryFirst(t => t.Roadway == locationInfo.RoadwayNo && t.stationType == (int)StationManager.FireStation);
                if (station == null)
                {
                    throw new Exception("消防站台未配置!");
                }
                //查找库存信息
                var stockInfo = _stockInfoRepository.QueryFirst(x => x.LocationCode == locationInfo.LocationCode && x.LocationInfo.RoadwayNo == locationInfo.RoadwayNo);
                //托盘码
                string barcode = string.Empty;
                if (stockInfo != null)
                {
                    barcode = stockInfo.PalletCode;
                }
                else
                {
                    //无库存信息,生成随机托盘码
                    barcode = "M" + DateTime.Now.ToString("MMddHHmmss") + "-" + new Random().Next(100, 1000);
                }
                Dt_Task fireTask = BaseDal.QueryFirst(x => x.TaskType == (int)TaskOutboundTypeEnum.OutFireAlarm && x.SourceAddress == locationInfo.LocationCode && x.Roadway == station.Roadway);
                if (fireTask != null)
                {
                    throw new Exception("已添加火警出库任务");
                }
                int taskNum = BaseDal.GetTaskNo().Result;
                Dt_Task task = new Dt_Task
                {
                    CreateDate = DateTime.Now,
                    Creater = "DTS",
                    CurrentAddress = locationInfo.LocationCode,
                    Grade = i == 0 ? 5 : 3,
                    Dispatchertime = DateTime.Now,
                    PalletCode = barcode,
                    Roadway = station.Roadway,
                    SourceAddress = locationInfo.LocationCode,
                    TaskState = (int)TaskOutStatusEnum.OutNew,
                    TaskType = (int)TaskOutboundTypeEnum.OutFireAlarm,
                    TargetAddress = station.stationLocation,
                    NextAddress = station.stationLocation,
                    TaskNum = taskNum, //_taskRepository.GetTaskNo().Result,
                    TaskId = 0,
                };
                // 尝试添加新任务
                WMSTaskDTO taskDTO = new WMSTaskDTO()
                {
                    TaskNum = task.TaskNum.Value,
                    Grade = 1,
                    PalletCode = task.PalletCode,
                    RoadWay = task.Roadway,
                    SourceAddress = task.SourceAddress,
                    TargetAddress = task.TargetAddress,
                    TaskState = task.TaskState.Value,
                    Id = 0,
                    TaskType = task.TaskType,
                };
                List<WMSTaskDTO> taskDTOs = new List<WMSTaskDTO>();
                taskDTOs.Add(taskDTO);
                var configs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
                var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.WCSIPAddress)?.ConfigValue;
                var ReceiveByWMSTask = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.ReceiveWMSTask)?.ConfigValue;
                if (ReceiveByWMSTask == null || ipAddress == null)
                {
                    throw new Exception("WMS IP 未配置");
                }
                var wmsIpAddrss = ipAddress + ReceiveByWMSTask;
                var respon = HttpHelper.Post(wmsIpAddrss, JsonConvert.SerializeObject(taskDTOs));
                if (respon != null)
                {
                    WebResponseContent respone = JsonConvert.DeserializeObject<WebResponseContent>(respon.ToString());
                    if (respone.Status)
                    {
                        var taskId = BaseDal.AddData(task);
                    }
                    else
                    {
                        throw new Exception("WCS处理失败:" + respone.Message);
                    }
                }
                else
                {
                    throw new Exception("请求处理失败");
                }
            }
            LogFactory.GetLog("DTS火警出库").Info(true, $"\r\r--------------------------------------");
            LogFactory.GetLog("DTS火警出库").Info(true, obj.ToJsonString());
            return content.OK();
        }
        catch (Exception ex)
        {
            LogFactory.GetLog("DTS火警出库").Info(true, $"\r\r--------------------------------------");
            LogFactory.GetLog("DTS火警出库").Info(true, ex.Message);
            return content.Error(ex.Message);
        }
    }
    #endregion
    #region 分容空框入库改为直接出库
    public async Task<WebResponseContent> SetEmptyOutbyInToOutAsync(RequestTaskDto request)
    {
        WebResponseContent content = new WebResponseContent();
        var task = await BaseDal.QueryFirstAsync(x => x.PalletCode == request.PalletCode);
        if (!task.IsNullOrEmpty())
        {
            var fromStation = await _stationManagerRepository.QueryFirstAsync(x => x.stationChildCode == request.Position);
            var toStation = await _stationManagerRepository.QueryFirstAsync(x => x.stationType == 7 && x.productLine == fromStation.productLine && x.stationArea == fromStation.stationArea);
            if (!toStation.IsNullOrEmpty())
            {
                var location = await _locationRepository.QueryFirstAsync(x => x.LocationCode == task.TargetAddress && x.AreaId == int.Parse(fromStation.stationArea));
                task.TargetAddress = toStation.stationLocation;
                task.NextAddress = toStation.stationChildCode;
                task.Grade = 3;
                task.TaskType = (int)TaskOutboundTypeEnum.InToOut;
                task.TaskState = (int)TaskOutStatusEnum.OutNew;
                location.LocationStatus = (int)LocationEnum.Free;
                await _locationRepository.UpdateDataAsync(location);
                await BaseDal.UpdateDataAsync(task);
                return content.OK("成功");
            }
            else
            {
                ConsoleHelper.WriteErrorLine("分容空框入库改为直接出库:未找到对应站台");
                content.Error("未找到对应站台");
            }
        }
        else
        {
            ConsoleHelper.WriteErrorLine("分容空框入库改为直接出库:未找到任务");
            content.Error("未找到任务");
        }
        return content;
    }
    #endregion
    #region 分容空框出库改为直接出库
    /// <summary>
    /// 分容空框出库改为直接出库
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<WebResponseContent> SetEmptyOutbyInToOutOneAsync(RequestTaskDto request)
    {
        WebResponseContent content = new WebResponseContent();
        var task = await BaseDal.QueryFirstAsync(x => x.PalletCode == request.PalletCode);
        if (!task.IsNullOrEmpty())
        {
            var toStation = await _stationManagerRepository.QueryFirstAsync(x => x.stationChildCode == request.Position);
            var fromStation = await _stationManagerRepository.QueryFirstAsync(x => x.stationType == 6 && x.productLine == toStation.productLine && x.stationArea == toStation.stationArea);
            if (!fromStation.IsNullOrEmpty())
            {
                //var location = await _locationRepository.QueryFirstAsync(x => x.LocationCode == task.TargetAddress && x.AreaId == int.Parse(fromStation.stationArea));
                task.SourceAddress = toStation.stationLocation;
                task.CurrentAddress = toStation.stationChildCode;
                task.Grade = 3;
                task.TaskType = (int)TaskOutboundTypeEnum.InToOut;
                task.TaskState = (int)TaskOutStatusEnum.OutNew;
                //location.LocationStatus = (int)LocationEnum.Free;
                //await _locationRepository.UpdateDataAsync(location);
                await BaseDal.UpdateDataAsync(task);
                return content.OK("成功");
            }
            else
            {
                ConsoleHelper.WriteErrorLine("分容空框出库改为直接出库:未找到对应站台");
                content.Error("未找到对应站台");
            }
        }
        else
        {
            ConsoleHelper.WriteErrorLine("分容空框出库改为直接出库:未找到任务");
            content.Error("未找到任务");
        }
        return content;
    }
    /// <summary>
    /// 获取任务类型描述方法
    /// </summary>
    /// <param name="taskType"></param>
    /// <returns></returns>
    private string GetTaskTypeDesc(int taskType)
    {
        return taskType switch
        {
            (int)TaskInboundTypeEnum.Inbound => TaskInboundTypeEnum.Inbound.GetIntegralRuleTypeEnumDesc(),
            (int)TaskInboundTypeEnum.InTray => TaskInboundTypeEnum.InTray.GetIntegralRuleTypeEnumDesc(),
            (int)TaskInboundTypeEnum.InNG => TaskInboundTypeEnum.InNG.GetIntegralRuleTypeEnumDesc(),
            (int)TaskInboundTypeEnum.InQuality => TaskInboundTypeEnum.InQuality.GetIntegralRuleTypeEnumDesc(),
            (int)TaskOutboundTypeEnum.OutTray => TaskOutboundTypeEnum.OutTray.GetIntegralRuleTypeEnumDesc(),
            (int)TaskOutboundTypeEnum.Outbound => TaskOutboundTypeEnum.Outbound.GetIntegralRuleTypeEnumDesc(),
            (int)TaskOutboundTypeEnum.OutNG => TaskOutboundTypeEnum.OutNG.GetIntegralRuleTypeEnumDesc(),
            (int)TaskOutboundTypeEnum.InToOut=> TaskOutboundTypeEnum.InToOut.GetIntegralRuleTypeEnumDesc(),
        };
    }
    #endregion
    #region 大屏接口
    /// <summary>
    /// 获取任务信息
    /// </summary>
    /// <returns></returns>
    public WebResponseContent GetTaskInfo()
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var task = BaseDal.QueryData(x => true).ToList();
            var newTask = task.Select(x => new
            {
                x.PalletCode,
                x.Roadway,
                x.SourceAddress,
                x.TargetAddress,
                x.ProductionLine,
                TaskType = new List<string> { GetTaskTypeDesc(x.TaskType) }[0], // 每个任务独立生成 TaskType
                TaskState = new List<string> { GetTaskStateDesc(x.TaskState) }[0] // 每个任务独立生成 TaskState
            }).ToList();
            return content.OK(data: newTask);
        }
        catch (Exception ex)
        {
            return content.Error(ex.Message);
        }
    }
    // 定义获取任务状态描述方法
    private string GetTaskStateDesc(int? taskState)
    {
        return taskState switch
        {
            (int)TaskInStatusEnum.InNew => TaskInStatusEnum.InNew.GetIntegralRuleTypeEnumDesc(),
            (int)TaskInStatusEnum.Line_InExecuting => TaskInStatusEnum.Line_InExecuting.GetIntegralRuleTypeEnumDesc(),
            (int)TaskInStatusEnum.Line_InFinish => TaskInStatusEnum.Line_InFinish.GetIntegralRuleTypeEnumDesc(),
            (int)TaskInStatusEnum.SC_InExecuting => TaskInStatusEnum.SC_InExecuting.GetIntegralRuleTypeEnumDesc(),
            (int)TaskInStatusEnum.SC_InFinish => TaskInStatusEnum.SC_InFinish.GetIntegralRuleTypeEnumDesc(),
            (int)TaskOutStatusEnum.OutNew => TaskOutStatusEnum.OutNew.GetIntegralRuleTypeEnumDesc(),
            (int)TaskOutStatusEnum.SC_OutExecuting => TaskOutStatusEnum.SC_OutExecuting.GetIntegralRuleTypeEnumDesc(),
            (int)TaskOutStatusEnum.SC_OutFinish => TaskOutStatusEnum.SC_OutFinish.GetIntegralRuleTypeEnumDesc(),
            (int)TaskOutStatusEnum.Line_OutExecuting => TaskOutStatusEnum.Line_OutExecuting.GetIntegralRuleTypeEnumDesc(),
            (int)TaskOutStatusEnum.Line_OutFinish => TaskOutStatusEnum.Line_OutFinish.GetIntegralRuleTypeEnumDesc(),
        };
    }
    /// <summary>
    /// 获取库存信息
    /// </summary>
    /// <returns></returns>
    public WebResponseContent GetStockInfo()
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var now = DateTime.Now;
            var startOfDay = new DateTime(now.Year, now.Month, now.Day);
            var endOfDay = startOfDay.AddDays(1);
            var result = _stockInfoRepository.Db.Queryable<DtStockInfo>().ToList();
            return content.OK(data: result);
        }
        catch (Exception ex)
        {
            return content.Error(ex.Message);
        }
    }
    /// <summary>
    /// 获取货位状态
    /// </summary>
    /// <returns></returns>
    public WebResponseContent GetStockQuantity()
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var location = _locationRepository.Db.Queryable<DtLocationInfo>().ToList();
            return content.OK(data: location);
        }
        catch (Exception ex)
        {
            return content.Error(ex.Message);
        }
    }
    public WebResponseContent Getproductionvolume()
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var now = DateTime.Now;
            var startOfDay = new DateTime(now.Year, now.Month, now.Day);
            var endOfDay1 = startOfDay.AddDays(-7);
            var taskHty = _task_HtyRepository.Db.Queryable<Dt_Task_Hty>().Where(it => it.CreateDate > endOfDay1).ToList();
            return content.OK(data: taskHty);
        }
        catch (Exception ex)
        {
            return content.Error(ex.Message);
        }
    }
    public WebResponseContent Getoutput()
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var now = DateTime.Now;
            var firstDayOfYear = new DateTime(now.Year, 1, 1);
            var taskHty = _task_HtyRepository.Db.Queryable<Dt_Task_Hty>().Where(it => it.CreateDate > firstDayOfYear && it.TaskType == 100).ToList();
            return content.OK(data: taskHty);
        }
        catch (Exception ex)
        {
            return content.Error(ex.Message);
        }
    }
    #endregion
    public WebResponseContent GetTimeout()
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var now = DateTime.Now;
            // 使用Subtract方法
            var threeHoursAgo = now.Subtract(TimeSpan.FromHours(3));
            List<DtStockInfo> dtStocks = _stockInfoRepository.Db.Queryable<DtStockInfo>()
                               .Where(x => x.OutboundTime < threeHoursAgo).ToList();
            return content.OK1(total1:dtStocks.Count, data: dtStocks);
        }
        catch (Exception ex)
        {
            return content.Error(ex.Message);
        }
    }
    public WebResponseContent Getproductionstatistics()
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var now = DateTime.Now;
            var thirtyDaysAgo = now.AddDays(-30);
            var roadwayMappings = new Dictionary<string, string> {
    { "JZ", "静置库" },
    { "CH", "陈化库" },
    { "FR", "分容库" },
    { "GW", "高温库" },
    { "CW", "常温库" }
};
            var roadwayKeys = roadwayMappings.Keys.ToArray();
            var taskTypes = new[] { 100, 104, 200, 204 };
            var taskHty = _task_HtyRepository.Db.Queryable<Dt_Task_Hty>()
                .Where(it =>
                    it.CreateDate >= thirtyDaysAgo &&
                    it.CreateDate <= now &&
                    taskTypes.Contains(it.TaskType) &&
                    roadwayKeys.Any(rk => it.Roadway.Contains(rk)))
                .ToList()
                .Select(t => new {
                    OriginalRoadway = t.Roadway,
                    RoadwayKey = roadwayKeys.FirstOrDefault(rk => t.Roadway.Contains(rk)),
                    TaskType = t.TaskType,
                    CreateDate = t.CreateDate
                })
                .Where(t => t.RoadwayKey != null)
                .GroupBy(t => new {
                    RoadwayKey = t.RoadwayKey,
                    Date = t.CreateDate.Date,
                    Hour = t.CreateDate.Hour
                })
                .OrderByDescending(group => group.Key.Date)
                .ThenByDescending(group => group.Key.Hour)
                .Select(group => new TaskHourData
                {
                    Hour = $"{group.Key.Date:yyyy/M/d} {(group.Key.Hour == 23 ? "23:00-24:00" : $"{group.Key.Hour}:00-{group.Key.Hour + 1}:00")}",
                    Roadway = roadwayMappings[group.Key.RoadwayKey],
                    TaskType100Count = group.Count(t => t.TaskType == 100),
                    TaskType104Count = group.Count(t => t.TaskType == 104),
                    TaskType200Count = group.Count(t => t.TaskType == 200),
                    TaskType204Count = group.Count(t => t.TaskType == 204)
                })
                .ToList();
            // 按日期和库位分组,找出每天的最后一个小时
            var dailyLastHours = taskHty
                .GroupBy(item => new {
                    Date = DateTime.Parse(item.Hour.Split(' ')[0]),
                    item.Roadway
                })
                .Select(group => new {
                    Date = group.Key.Date,
                    Roadway = group.Key.Roadway,
                    LastHour = group.Max(item => int.Parse(item.Hour.Split(' ')[1].Split(':')[0]))
                })
                .ToList();
            // 生成汇总数据(调整Hour格式为"日期+日汇总")
            var summaryItems = dailyLastHours
                .Select(dailyLastHour => {
                    // 找到该日期该库位的所有小时数据
                    var dailyItems = taskHty
                        .Where(item =>
                            DateTime.Parse(item.Hour.Split(' ')[0]) == dailyLastHour.Date &&
                            item.Roadway == dailyLastHour.Roadway)
                        .ToList();
                    return new TaskHourData
                    {
                        Hour = $"{dailyLastHour.Date:yyyy/M/d}日汇总", // 修改Hour格式
                        Roadway = dailyLastHour.Roadway,
                        TaskType100Count = dailyItems.Sum(item => item.TaskType100Count),
                        TaskType104Count = dailyItems.Sum(item => item.TaskType104Count),
                        TaskType200Count = dailyItems.Sum(item => item.TaskType200Count),
                        TaskType204Count = dailyItems.Sum(item => item.TaskType204Count),
                        IsSummary = true
                    };
                })
                .ToList();
            // 将汇总数据添加到结果中
            taskHty = taskHty.Concat(summaryItems).ToList();
            // 调整排序逻辑:先按日期降序,再按是否汇总(汇总在前),最后按小时降序
            taskHty = taskHty
                .OrderByDescending(item => {
                    var datePart = item.Hour.Contains("日汇总")
                ? item.Hour.Split("日汇总")[0]
                : item.Hour.Split(' ')[0];
                    return DateTime.Parse(datePart);
                })
            .ThenByDescending(item => item.IsSummary) // 汇总数据排在前面
            .ThenByDescending(item => {
                if (!item.IsSummary && item.Hour.Contains(' '))
                {
                    return int.Parse(item.Hour.Split(' ')[1].Split(':')[0]);
                }
                return 0;
            })
            .ToList();
            return content.OK1(total1: taskHty.Count, data: taskHty);
        }
        catch (Exception ex)
        {
            return content.Error(ex.Message);
        }
    }
}
internal class TaskHourData
{
    public string Hour { get; set; }
    public string Roadway { get; set; }
    public int TaskType100Count { get; set; }
    public int TaskType104Count { get; set; }
    public int TaskType200Count { get; set; }
    public int TaskType204Count { get; set; }
    public bool IsSummary { get; set; } = false;
}