xxyy
2025-03-06 aded48e0f32d28bf61479a3c50ac63946be6f469
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/Task/Partial/Dt_TaskService.cs
@@ -1,4 +1,5 @@
using Masuit.Tools;
using SqlSugar;
using WIDESEA_Common.CustomModels;
//using WIDESEA_Common.CustomModels;
@@ -459,6 +460,10 @@
            else
            {
                locations = await _locationRepository.QueryDataAsync(x => x.LocationStatus == (int)LocationEnum.Distribute && x.RoadwayNo == requestTask.Roadways && x.EnalbeStatus == 1 && x.LocationType == 1);
                if (locations == null)
                {
                    locations = await _locationRepository.QueryDataAsync(x => x.LocationStatus == (int)LocationEnum.Free && x.RoadwayNo == requestTask.Roadways && x.EnalbeStatus == 1 && x.LocationType == 1);
                }
            }
            if (locations == null)
@@ -537,12 +542,13 @@
        {
            if (stationManager.stationType != 7) throw new Exception("错误的调取");
            var stockinfo = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
                .Includes(x => x.LocationInfo)
                //.Includes(x=>x.StockInfoDetails)
                .Where(x => !x.IsFull && x.LocationInfo.RoadwayNo == stationManager.Roadway)
            // 从缓存中获取库存信息
            IDictionary<string, DtStockInfo>? stockInfos = _simpleCacheService.HashGetAll<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo);
            List<DtStockInfo> stockInfoList = stockInfos.Values.ToList();
            var stockinfo = stockInfoList.Where(x => x.LocationInfo != null && !x.IsFull && x.LocationInfo.RoadwayNo == stationManager.Roadway)
                .OrderBy(x => x.CreateDate)
                .FirstAsync();
                .FirstOrDefault();
            if (stockinfo == null) return null;
@@ -800,14 +806,21 @@
                {
                    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(); // 获取第一个元素
@@ -874,11 +887,34 @@
    #region 常温补空托盘至分容
    private static readonly Dictionary<string, (int Count, DateTime LastRequestTime)> requestTrackerToCW = new();
    public async Task<WebResponseContent> GetFROutTrayToCW(RequestTaskDto taskDTO)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            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);
@@ -911,6 +947,8 @@
                    _locationStatusChangeRecordRepository.AddLocationStatusChangeRecord(location, lastStatus, (int)StatusChangeTypeEnum.AutomaticDelivery, task.TaskNum);
                    // 返回成功响应
                    requestTrackerToCW.Remove(requestKey);
                    return content.OK(data: wmsTask);
                }
                else
@@ -937,32 +975,32 @@
        ConsoleHelper.WriteColorLine(station.Roadway, ConsoleColor.Magenta);
        var stackers = station.Roadway.Split(',').ToList();
        var devices = SqlSugarHelper.DbWCS.Queryable<Dt_DeviceInfo>()
        var deviceCode = SqlSugarHelper.DbWCS.Queryable<Dt_DeviceInfo>()
            .Where(x => x.DeviceStatus == "1")
            .Where(x => stackers.Contains(x.DeviceCode))
            .ToList();
            .ToList().Select(x => x.DeviceCode).ToList();
        var deviceCode = devices.Select(x => x.DeviceCode).ToList();
        // 从缓存中获取库存信息
        IDictionary<string, DtStockInfo>? stockInfos = _simpleCacheService.HashGetAll<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo);
        List<DtStockInfo> stockInfoList = stockInfos.Values.ToList();
        var result = await _stockInfoRepository.Db.Queryable<DtStockInfo>()
            .Includes(x => x.LocationInfo) // 预加载LocationInfo
            .Includes(x => x.StockInfoDetails) // 预加载StockInfoDetails
            .Where(x => x.ProductionLine == productLine)
        var result = stockInfoList.Where(x => x.ProductionLine == productLine)
            .Where(x => x.AreaCode == areaCode && x.IsFull == false)
            .Where(x => x.StockInfoDetails.Any(y => y.MaterielCode == "空托盘"))
            .Where(x => x.LocationInfo.LocationStatus == (int)LocationEnum.InStock && x.LocationInfo.AreaId == area.AreaID && x.LocationInfo.EnalbeStatus == (int)EnableEnum.Enable) // 过滤条件
            .WhereIF(!deviceCode.IsNullOrEmpty(), x => deviceCode.Contains(x.LocationInfo.RoadwayNo))
            .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) // 过滤条件
            .WhereIF(!deviceCode.IsNullOrEmpty(), x => x.LocationInfo != null && deviceCode.Contains(x.LocationInfo.RoadwayNo))
            .OrderBy(x => x.CreateDate) // 排序
            .FirstAsync(); // 转换为列表
            .FirstOrDefault(); // 转换为列表
        //var firstOrDefault = result[0]; // 查找第一个匹配的元素
        //return firstOrDefault;
        return result;
    }
    #endregion
    #region  常温3出库至包装
    // 用于追踪每个请求的调用次数和最后请求时间。
    private static readonly Dictionary<string, (int Count, DateTime LastRequestTime)> requestTracker = new();
    /// <summary>
    /// 常温3出库至包装
@@ -975,17 +1013,29 @@
        try
        {
            Dt_StationManager station = _stationManagerRepository.QueryFirst(x => x.stationChildCode == json.Position && x.stationType == 12 && x.stationArea == "Call");
            if (station == null) { throw new Exception($"未找到包装站台信息,请检查传入参数{json.Position}"); }
            var stockInfo = _stockInfoRepository.Db.Queryable<DtStockInfo>()
                    .Where(x => x.ProductionLine == station.productLine)
                    .Includes(x => x.LocationInfo) // 预加载LocationInfo
                    .Where(x => x.AreaCode == "CWSC3" && x.IsFull == true) // 过滤条件
                    .Where(x => x.LocationInfo.LocationStatus == (int)LocationEnum.InStock) // 过滤条件
                    .OrderBy(x => x.OutboundTime) // 排序
                    .First(); // 获取第一个元素
            var deviceCode = SqlSugarHelper.DbWCS.Queryable<Dt_DeviceInfo>()
                .Where(x => x.DeviceStatus == "1")
                .Where(x => x.DeviceCode.Contains("CWSC")) // 过滤条件
                .ToList().Select(x => x.DeviceCode).ToList();
            //DtStockInfo stockInfo = _stockInfoRepository.QueryFirst(X => X.IsFull && X.AreaCode == "CWSC3" && X.ProductionLine == station.productLine);
            if (stockInfo == null) throw new Exception("库内无满足条件的库存可出库");
            // 从缓存中获取库存信息
            IDictionary<string, DtStockInfo>? stockInfos = _simpleCacheService.HashGetAll<DtStockInfo>(WIDESEA_Cache.CacheConst.Cache_DtStockInfo);
            List<DtStockInfo> stockInfoList = stockInfos.Values.ToList();
            // 修改后的查询代码
            var stockInfo = stockInfoList
                .Where(x => x.ProductionLine == station.productLine)
                .Where(x => x.AreaCode == "CWSC3" && x.IsFull == true)
                // 增加对 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))
                .OrderBy(x => x.OutboundTime)
                .FirstOrDefault();
            if (stockInfo == null) throw new Exception($"库内{station.productLine}无满足条件的库存可出库");
            DtLocationInfo locationInfo = _locationRepository.QueryFirst(x => x.AreaId == 5 && x.LocationCode == stockInfo.LocationCode);
@@ -1153,4 +1203,91 @@
    }
    #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;
    }
    #endregion
}