| | |
| | | using Mapster; |
| | | using System.Text.Json; |
| | | using WIDESEA_Common.Constants; |
| | | using WIDESEA_Common.LocationEnum; |
| | | using WIDESEA_Common.StockEnum; |
| | | using WIDESEA_Common.TaskEnum; |
| | |
| | | using WIDESEA_Core.Enums; |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_DTO.Task; |
| | | using WIDESEA_IStockService; |
| | | using WIDESEA_Model.Models; |
| | | |
| | | namespace WIDESEA_TaskInfoService |
| | |
| | | return response.OK(); |
| | | |
| | | if (task.TaskStatus == (int)TaskInStatusEnum.InNew) |
| | | return CancelAgvInboundTask(task); |
| | | return await CancelAgvInboundTask(task); |
| | | |
| | | if (task.TaskStatus == (int)TaskOutStatusEnum.OutNew) |
| | | return await CancelAgvOutboundTaskAsync(task); |
| | |
| | | return response.Error($"当前托盘{dto.TrayNumber}已经入库了"); |
| | | |
| | | // 创建库存明细 |
| | | var details = new Dt_StockInfoDetail |
| | | { |
| | | MaterielCode = dto.ProductNo, |
| | | MaterielName = dto.ProductName, |
| | | StockQuantity = int.TryParse(dto.Quantity, out int quantity) ? quantity : 0, |
| | | Unit = dto.UomCode, |
| | | OrderNo = dto.ProductNo, |
| | | ProductionDate =dto.ProductionDate, |
| | | EffectiveDate = dto.LowerLimitTime, |
| | | SerialNumber = dto.TrayNumber, |
| | | Status = (int)StockStatusEmun.入库确认, |
| | | InboundOrderRowNo = 1, |
| | | Creater = StockConstants.AGV_USER, |
| | | CreateDate = DateTime.Now, |
| | | Remark = $"AGV入库任务创建,任务号:{dto.TaskId}" |
| | | }; |
| | | |
| | | // 创建库存主记录 |
| | | var stock = new Dt_StockInfo |
| | | { |
| | | PalletCode = dto.TrayNumber, |
| | | PalletType = dto.Floor, |
| | | WarehouseId = dto.YinYang == 1 ? (int)WarehouseEnum.FJ1 : (int)WarehouseEnum.ZJ1, |
| | | StockStatus = (int)StockStatusEmun.入库确认, |
| | | Creater = StockConstants.AGV_USER, |
| | | CreateDate = DateTime.Now, |
| | | Remark = $"AGV入库任务创建,任务号:{dto.TaskId}", |
| | | Details = new List<Dt_StockInfoDetail> { details } |
| | | }; |
| | | |
| | | task.TaskType = (int)TaskInboundTypeEnum.Inbound; |
| | | task.TaskStatus = (int)TaskInStatusEnum.InNew; |
| | | task.CurrentAddress = task.SourceAddress; |
| | | |
| | | var addResult = await BaseDal.AddDataAsync(task) > 0; |
| | | return addResult ? null : response.Error("入库任务创建失败"); |
| | | _unitOfWorkManage.BeginTran(); |
| | | try |
| | | { |
| | | // 先创建任务 |
| | | var taskResult = await BaseDal.AddDataAsync(task) > 0; |
| | | if (!taskResult) |
| | | { |
| | | _unitOfWorkManage.RollbackTran(); |
| | | return response.Error("入库任务创建失败"); |
| | | }
|
| | | var result = _stockInfoService.Repository.AddData(stock, x => x.Details);
|
| | | if (result)
|
| | | { |
| | | _unitOfWorkManage.CommitTran(); |
| | | return null; |
| | | }
|
| | | else |
| | | { |
| | | _unitOfWorkManage.RollbackTran(); |
| | | return response.Error("库存信息创建失败"); |
| | | }
|
| | | // 使用库存服务添加库存主记录和明细
|
| | | //var stockResult = await _stockInfoService.AddStockWithDetailsUsingTransactionAsync(stock); |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | _unitOfWorkManage.RollbackTran(); |
| | | return response.Error($"入库任务创建异常:{ex.Message}"); |
| | | } |
| | | } |
| | | |
| | | // 出库创建 |
| | | private async Task<AGVResponse?> CreateAgvOutboundTaskAsync(Dt_Task task, ApplyInOutDto dto) |
| | | { |
| | | AGVResponse response = new AGVResponse(); |
| | | |
| | | // 检查库存是否存在 |
| | | var stockInfo = await _stockInfoService.GetStockInfoAsync(dto.TrayNumber); |
| | | if (stockInfo == null) |
| | | return response.Error($"未找到托盘{dto.TrayNumber}的库存信息"); |
| | | |
| | | //if (stockInfo.WarehouseId != dto.YinYang) |
| | | // return response.Error($"托盘{dto.TrayNumber}不属于当前{(dto.YinYang == 1 ? "阴极" : "阳极")}"); |
| | | // 检查库存是否有明细(即是否真的有库存) |
| | | if (stockInfo.Details == null || !stockInfo.Details.Any()) |
| | | return response.Error($"托盘{dto.TrayNumber}没有库存明细,无法出库"); |
| | | |
| | | // 检查库存总数量是否大于0 |
| | | var totalQuantity = stockInfo.Details.Sum(d => d.StockQuantity); |
| | | if (totalQuantity <= 0) |
| | | return response.Error($"托盘{dto.TrayNumber}库存数量不足,无法出库"); |
| | | |
| | | // 根据dto参数进一步验证库存信息 |
| | | if (!string.IsNullOrEmpty(dto.ProductNo)) |
| | | { |
| | | // 检查库存明细中是否包含指定的物料编码 |
| | | var hasMatchingMaterial = stockInfo.Details.Any(d => d.MaterielCode == dto.ProductNo); |
| | | if (!hasMatchingMaterial) |
| | | return response.Error($"托盘{dto.TrayNumber}中没有物料编码为{dto.ProductNo}的库存,无法出库"); |
| | | } |
| | | |
| | | // 检查库存状态是否允许出库 |
| | | if (stockInfo.StockStatus != (int)StockStatusEmun.入库完成) |
| | | return response.Error($"托盘{dto.TrayNumber}正在移动中,请稍后!"); |
| | | |
| | | // 检查货位信息 |
| | | var locationInfo = await _locationInfoService.GetLocationInfo(stockInfo.LocationCode); |
| | | if (locationInfo == null) |
| | | return response.Error($"未找到托盘{stockInfo.LocationCode}的货位信息"); |
| | | |
| | | // 检查货位状态是否允许出库 |
| | | if (locationInfo.LocationStatus != (int)LocationStatusEnum.InStock) |
| | | return response.Error($"当前货位{locationInfo.LocationStatus}状态信息错误"); |
| | | |
| | | // 验证仓库ID是否匹配(根据dto的阴阳极参数) |
| | | var expectedWarehouseId = dto.YinYang == 1 ? (int)WarehouseEnum.FJ1 : (int)WarehouseEnum.ZJ1; |
| | | if (stockInfo.WarehouseId != expectedWarehouseId) |
| | | return response.Error($"托盘{dto.TrayNumber}不在预期的仓库中,无法出库"); |
| | | |
| | | task.TaskType = (int)TaskOutboundTypeEnum.Outbound; |
| | | task.TaskStatus = (int)TaskOutStatusEnum.OutNew; |
| | |
| | | task.TaskStatus = (int)TaskOutStatusEnum.OutFinish; |
| | | |
| | | _unitOfWorkManage.BeginTran(); |
| | | var deleteStockResult = _stockInfoService.DeleteData(stockInfo); |
| | | //var deleteStockResult = _stockInfoService.DeleteData(stockInfo) |
| | | var deleteStockResult = await _stockInfoService.DeleteStockWithDetailsAsync(stockInfo.Id); |
| | | var updateLocationResult = _locationInfoService.UpdateData(locationInfo); |
| | | BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成); |
| | | if (!deleteStockResult.Status || !updateLocationResult.Status) |
| | |
| | | if (existingStock != null) |
| | | return response.Error($"托盘{task.PalletCode}的库存信息已存在,请勿重复入库"); |
| | | |
| | | Dt_StockInfo stockInfo = new Dt_StockInfo |
| | | { |
| | | PalletCode = task.PalletCode, |
| | | StockStatus = (int)StockStatusEmun.入库确认, |
| | | LocationCode = locationInfo.LocationCode, |
| | | WarehouseId = task.WarehouseId, |
| | | Creater = "AGV", |
| | | CreateDate = DateTime.Now |
| | | }; |
| | | //Dt_StockInfo stockInfo = new Dt_StockInfo |
| | | //{ |
| | | // PalletCode = task.PalletCode, |
| | | // StockStatus = (int)StockStatusEmun.入库确认, |
| | | // LocationCode = locationInfo.LocationCode, |
| | | // WarehouseId = task.WarehouseId, |
| | | // Creater = "AGV", |
| | | // CreateDate = DateTime.Now |
| | | //}; |
| | | |
| | | locationInfo.LocationStatus = (int)LocationStatusEnum.FreeLock; |
| | | |
| | | _unitOfWorkManage.BeginTran(); |
| | | var updateTaskResult = BaseDal.UpdateData(task); |
| | | var updateLocationResult = _locationInfoService.UpdateData(locationInfo); |
| | | var addStockResult = _stockInfoService.AddData(stockInfo); |
| | | if (!updateTaskResult || !updateLocationResult.Status || !addStockResult.Status) |
| | | //var addStockResult = _stockInfoService.AddData(stockInfo); |
| | | if (!updateTaskResult || !updateLocationResult.Status /*|| !addStockResult.Status*/) |
| | | { |
| | | _unitOfWorkManage.RollbackTran(); |
| | | return response.Error("入库完成后,本地任务、库存或货位更新失败"); |
| | |
| | | } |
| | | |
| | | // AGV入库取消 |
| | | private AGVResponse CancelAgvInboundTask(Dt_Task task) |
| | | private async Task<AGVResponse> CancelAgvInboundTask(Dt_Task task) |
| | | { |
| | | AGVResponse response = new AGVResponse(); |
| | | task.TaskStatus = (int)TaskInStatusEnum.InCancel; |
| | | |
| | | _unitOfWorkManage.BeginTran(); |
| | | try |
| | | { |
| | | var stockInfo = await _stockInfoService.GetStockInfoAsync(task.PalletCode); |
| | | if (stockInfo != null) |
| | | { |
| | | var deleteResult = await _stockInfoService.DeleteStockWithDetailsAsync(stockInfo.Id); |
| | | if (!deleteResult.Status) |
| | | { |
| | | _unitOfWorkManage.RollbackTran(); |
| | | return response.Error($"删除库存失败: {deleteResult.Message}"); |
| | | } |
| | | } |
| | | |
| | | BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.自动完成 : OperateTypeEnum.人工完成); |
| | | |
| | | _unitOfWorkManage.CommitTran(); |
| | | return response.OK(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | _unitOfWorkManage.RollbackTran(); |
| | | return response.Error($"取消入库任务时发生异常: {ex.Message}"); |
| | | } |
| | | } |
| | | |
| | | |
| | | // AGV出库取消 |