wanshenmean
2026-03-17 737dec3c384f394fd6f9849b4480b697d1ba35d5
Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService.cs
@@ -1,4 +1,4 @@
using AutoMapper;
using MapsterMapper;
using Microsoft.Extensions.Configuration;
using SqlSugar;
using System.Text.Json;
@@ -8,7 +8,9 @@
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_DTO;
using WIDESEA_Core.Core;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.GradingMachine;
using WIDESEA_DTO.Task;
using WIDESEA_IBasicService;
using WIDESEA_IStockService;
@@ -24,6 +26,7 @@
        private readonly ILocationInfoService _locationInfoService;
        private readonly HttpClientHelper _httpClientHelper;
        private readonly IConfiguration _configuration;
        private readonly RoundRobinService _roundRobinService;
        public IRepository<Dt_Task> Repository => BaseDal;
@@ -42,13 +45,15 @@
            IStockInfoService stockInfoService,
            ILocationInfoService locationInfoService,
            HttpClientHelper httpClientHelper,
            IConfiguration configuration) : base(BaseDal)
            IConfiguration configuration,
            RoundRobinService roundRobinService) : base(BaseDal)
        {
            _mapper = mapper;
            _stockInfoService = stockInfoService;
            _locationInfoService = locationInfoService;
            _httpClientHelper = httpClientHelper;
            _configuration = configuration;
            _roundRobinService = roundRobinService;
        }
        #region WCS逻辑处理
@@ -206,6 +211,7 @@
                    var r when r.Contains("CW") => DateTime.Now.AddHours(1),
                    _ => DateTime.Now
                };
                stockInfo.StockStatus = StockStatusEmun.入库完成.GetHashCode();
                location.LocationStatus = LocationStatusEnum.InStock.GetHashCode();
@@ -213,14 +219,7 @@
                var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
                if (!updateLocationResult || !updateStockResult)
                    return WebResponseContent.Instance.Error("任务完成失败");
                var deleteTaskResult = await BaseDal.DeleteDataAsync(task);
                if (!deleteTaskResult) return WebResponseContent.Instance.Error("任务完成失败");
                var historyTask = _mapper.Map<Dt_Task_Hty>(task);
                historyTask.InsertTime = DateTime.Now;
                return WebResponseContent.Instance.OK("任务完成");
                return await CompleteTaskAsync(task);
            }
            catch (Exception ex)
            {
@@ -241,8 +240,9 @@
                var location = await _locationInfoService.GetLocationInfo(task.Roadway, task.SourceAddress);
                if (location == null) return WebResponseContent.Instance.Error("未找到对应的货位");
                var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode); stockInfo.LocationCode = location.LocationCode;
                stockInfo.LocationId = location.Id;
                var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode);
                stockInfo.LocationId = 0;
                stockInfo.LocationCode = null;
                stockInfo.OutboundDate = DateTime.Now;
                location.LocationStatus = LocationStatusEnum.Free.GetHashCode();
@@ -251,14 +251,50 @@
                var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
                if (!updateLocationResult || !updateStockResult)
                    return WebResponseContent.Instance.Error("任务完成失败");
                return await CompleteTaskAsync(task);
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error($"完成任务失败: {ex.Message}");
            }
        }
                var deleteTaskResult = await BaseDal.DeleteDataAsync(task);
                if (!deleteTaskResult) return WebResponseContent.Instance.Error("任务完成失败");
        /// <summary>
        /// 移库任务完成:修改库存位置与状态,修改源/目标货位状态,删除任务数据
        /// </summary>
        public async Task<WebResponseContent> RelocationFinishTaskAsync(CreateTaskDto taskDto)
        {
            try
            {
                var task = await BaseDal.QueryFirstAsync(s =>
                    s.PalletCode == taskDto.PalletCode &&
                    s.TaskType == TaskRelocationTypeEnum.Relocation.GetHashCode());
                if (task == null) return WebResponseContent.Instance.Error("未找到对应的移库任务");
                var historyTask = _mapper.Map<Dt_Task_Hty>(task);
                historyTask.InsertTime = DateTime.Now;
                var sourceLocation = await _locationInfoService.GetLocationInfo(task.Roadway, task.SourceAddress);
                if (sourceLocation == null) return WebResponseContent.Instance.Error("未找到移库源货位");
                return WebResponseContent.Instance.OK("任务完成");
                var targetLocation = await _locationInfoService.GetLocationInfo(task.Roadway, task.TargetAddress);
                if (targetLocation == null) return WebResponseContent.Instance.Error("未找到移库目标货位");
                var stockInfo = await _stockInfoService.GetStockInfoAsync(taskDto.PalletCode);
                if (stockInfo == null) return WebResponseContent.Instance.Error("未找到对应库存信息");
                stockInfo.LocationCode = targetLocation.LocationCode;
                stockInfo.LocationId = targetLocation.Id;
                stockInfo.StockStatus = StockStatusEmun.入库完成.GetHashCode();
                sourceLocation.LocationStatus = LocationStatusEnum.Free.GetHashCode();
                targetLocation.LocationStatus = LocationStatusEnum.InStock.GetHashCode();
                var updateSourceResult = await _locationInfoService.UpdateLocationInfoAsync(sourceLocation);
                var updateTargetResult = await _locationInfoService.UpdateLocationInfoAsync(targetLocation);
                var updateStockResult = await _stockInfoService.UpdateStockAsync(stockInfo);
                if (!updateSourceResult || !updateTargetResult || !updateStockResult)
                    return WebResponseContent.Instance.Error("移库任务完成失败");
                return await CompleteTaskAsync(task);
            }
            catch (Exception ex)
            {
@@ -380,20 +416,52 @@
        }
        /// <summary>
        /// 根据巷道确定目标地址
        /// 完成任务后统一处理(删除任务数据)
        /// </summary>
        private string DetermineTargetAddress(string roadway, Dictionary<string, string> addressMap)
        private async Task<WebResponseContent> CompleteTaskAsync(Dt_Task task)
        {
            var deleteTaskResult = await BaseDal.DeleteDataAsync(task);
            if (!deleteTaskResult) return WebResponseContent.Instance.Error("任务完成失败");
            // 保留历史对象构建逻辑,后续可接入历史表落库
            var historyTask = _mapper.Map<Dt_Task_Hty>(task);
            historyTask.InsertTime = DateTime.Now;
            return WebResponseContent.Instance.OK("任务完成");
        }
        /// <summary>
        /// 根据巷道确定目标地址(支持多出库口轮询)
        /// </summary>
        private string DetermineTargetAddress(string roadway, Dictionary<string, List<string>> addressMap)
        {
            if (string.IsNullOrWhiteSpace(roadway))
                return "10080"; // 默认地址
                return "10080";
            // 查找匹配的巷道前缀
            string matchedPrefix = null;
            foreach (var kvp in addressMap)
            {
                if (roadway.Contains(kvp.Key))
                    return kvp.Value;
                {
                    matchedPrefix = kvp.Key;
                    break;
                }
            }
            return "10080"; // 默认地址
            if (matchedPrefix == null)
                return "10080";
            var addresses = addressMap[matchedPrefix];
            if (addresses == null || addresses.Count == 0)
                return "10080";
            // 单个地址,直接返回
            if (addresses.Count == 1)
                return addresses[0];
            // 多个地址,使用轮询服务
            return _roundRobinService.GetNextAddress(matchedPrefix, addresses);
        }
        /// <summary>
@@ -405,38 +473,12 @@
            {
                // 1. 查询到期库存
                var expiredStocks = await _stockInfoService.Repository
                    .QueryDataAsync(s => s.OutboundDate <= DateTime.Now
                    .QueryDataNavAsync(s => s.OutboundDate <= DateTime.Now
                        && s.StockStatus == StockStatusEmun.入库完成.GetHashCode());
                if (expiredStocks == null || !expiredStocks.Any())
                {
                    return WebResponseContent.Instance.OK("无到期库存需要处理");
                }
                // 批量加载位置详情(优化 N+1 查询问题)
                var locationIds = expiredStocks
                    .Where(s => s.LocationId > 0)
                    .Select(s => s.LocationId)
                    .Distinct()
                    .Cast<object>()
                    .ToList();
                if (locationIds.Any())
                {
                    var locations = await _locationInfoService.Repository
                        .QureyDataByIdsAsync(locationIds);
                    // 创建位置字典以便快速查找
                    var locationDict = locations.ToDictionary(l => l.Id, l => l);
                    // 为每个库存关联位置详情
                    foreach (var stock in expiredStocks)
                    {
                        if (stock.LocationId > 0 && locationDict.ContainsKey(stock.LocationId))
                        {
                            stock.LocationDetails = locationDict[stock.LocationId];
                        }
                    }
                }
                // 过滤有位置且位置有库存的记录
@@ -472,8 +514,8 @@
                // 4. 获取配置的目标地址映射
                var targetAddressMap = _configuration.GetSection("AutoOutboundTask:TargetAddresses")
                    .Get<Dictionary<string, string>>()
                    ?? new Dictionary<string, string>();
                    .Get<Dictionary<string, List<string>>>()
                    ?? new Dictionary<string, List<string>>();
                // 5. 批量创建任务
                var taskList = new List<Dt_Task>();
@@ -497,7 +539,7 @@
                        TaskType = TaskTypeEnum.Outbound.GetHashCode(),
                        TaskStatus = TaskStatusEnum.New.GetHashCode(),
                        Grade = 1,
                        TaskNum = 0,  // 使用 0 让数据库自动生成任务号
                        TaskNum = await BaseDal.GetTaskNo(),
                        Creater = "system_auto"
                    };
                    taskList.Add(task);
@@ -509,23 +551,55 @@
                    return WebResponseContent.Instance.Error($"批量创建任务失败,共 {taskList.Count} 个任务");
                }
                // 任务创建成功后,同步锁定库存和货位状态,避免重复分配
                var stocksToUpdate = stocksToProcess
                    .Select(s =>
                    {
                        s.StockStatus = StockStatusEmun.出库锁定.GetHashCode();
                        return s;
                    })
                    .ToList();
                var updateStockResult = await _stockInfoService.Repository.UpdateDataAsync(stocksToUpdate);
                if (!updateStockResult)
                {
                    return WebResponseContent.Instance.Error($"任务创建成功,但库存状态更新失败,共 {stocksToUpdate.Count} 条");
                }
                var locationsToUpdate = stocksToProcess
                    .Where(s => s.LocationDetails != null)
                    .GroupBy(s => s.LocationDetails.Id)
                    .Select(g =>
                    {
                        var location = g.First().LocationDetails;
                        location.LocationStatus = LocationStatusEnum.InStockLock.GetHashCode();
                        return location;
                    })
                    .ToList();
                if (locationsToUpdate.Any())
                {
                    var updateLocationResult = await _locationInfoService.Repository.UpdateDataAsync(locationsToUpdate);
                    if (!updateLocationResult)
                    {
                        return WebResponseContent.Instance.Error($"任务创建成功,但货位状态更新失败,共 {locationsToUpdate.Count} 条");
                    }
                }
                // 6. 通知 WCS(异步,不影响主流程)
                _ = Task.Run(() =>
                {
                    foreach (var task in taskList)
                    try
                    {
                        try
                        {
                            var wmstaskDto = _mapper.Map<WMSTaskDTO>(task);
                            _httpClientHelper.Post<WebResponseContent>(
                                "http://logistics-service/api/logistics/notifyoutbound",
                                JsonSerializer.Serialize(wmstaskDto));
                        }
                        catch (Exception ex)
                        {
                            // WCS 通知失败不影响任务创建,记录日志即可
                            Console.WriteLine($"WCS 通知失败,任务编号: {task.TaskNum}, 错误: {ex.Message}");
                        }
                        var wmstaskDtos = _mapper.Map<List<WMSTaskDTO>>(taskList);
                        _httpClientHelper.Post<WebResponseContent>(
                            "http://localhost:9292/api/Task/ReceiveTask",
                            wmstaskDtos.ToJson());
                    }
                    catch (Exception ex)
                    {
                        // WCS 通知失败不影响任务创建,记录日志即可
                        Console.WriteLine($"WCS 批量通知失败,任务数量: {taskList.Count}, 错误: {ex.Message}");
                    }
                });
@@ -544,7 +618,7 @@
        /// <summary>
        /// 堆垛机取放货完成后物流通知化成分容柜完成信号
        /// </summary>
        public async Task<WebResponseContent> InOrOutCompletedAsync(InputDto input)
        public async Task<WebResponseContent> InOrOutCompletedAsync(GradingMachineInputDto input)
        {
            WebResponseContent content = new WebResponseContent();
            if (string.IsNullOrWhiteSpace(input.PalletCode) || string.IsNullOrWhiteSpace(input.LocationCode))
@@ -559,7 +633,7 @@
                {
                    var location = await _locationInfoService.GetLocationInfoAsync(input.LocationCode);
                    OutPutDto outPutDto = new OutPutDto()
                    OutputDto outPutDto = new OutputDto()
                    {
                        LocationCode = input.LocationCode,
                        PalletCode = input.PalletCode,
@@ -570,7 +644,7 @@
                }
                else
                {
                    OutPutDto outPutDto = new OutPutDto()
                    OutputDto outPutDto = new OutputDto()
                    {
                        LocationCode = input.LocationCode,
                        PalletCode = input.PalletCode,
@@ -593,7 +667,7 @@
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> SendLocationStatusAsync(InputDto input)
        public async Task<WebResponseContent> SendLocationStatusAsync(GradingMachineInputDto input)
        {
            WebResponseContent content = new WebResponseContent();
            if (string.IsNullOrWhiteSpace(input.LocationCode))
@@ -630,7 +704,7 @@
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> RequestOutboundAsync(InputDto input)
        public async Task<WebResponseContent> RequestOutboundAsync(GradingMachineInputDto input)
        {
            WebResponseContent content = new WebResponseContent();
            if (string.IsNullOrWhiteSpace(input.LocationCode) || string.IsNullOrWhiteSpace(input.PalletCode))
@@ -689,7 +763,7 @@
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<WebResponseContent> GetPalletCodeCellAsync(InputDto input)
        public async Task<WebResponseContent> GetPalletCodeCellAsync(GradingMachineInputDto input)
        {
            WebResponseContent content = new WebResponseContent();
            if (string.IsNullOrWhiteSpace(input.PalletCode) || string.IsNullOrWhiteSpace(input.LocationCode))
@@ -703,7 +777,7 @@
                {
                    return content.Error("未找到对应的托盘");
                }
                var outPutDtos = stockInfo.Details.Select(x => new OutPutDto()
                var outPutDtos = stockInfo.Details.Select(x => new OutputDto()
                {
                    LocationCode = input.LocationCode,
                    PalletCode = input.PalletCode,
@@ -722,4 +796,4 @@
        #endregion 分容柜接口
    }
}
}