| | |
| | | using Newtonsoft.Json; |
| | | using SqlSugar; |
| | | using System.Diagnostics; |
| | | using WIDESEA_Common.Constants; |
| | | using WIDESEA_Common.StockEnum; |
| | | using WIDESEA_Core; |
| | |
| | | public IMesService _mesService { get; } |
| | | |
| | | private readonly IMesLogService _mesLogService; |
| | | private readonly IMesUploadHelper _mesUploadHelper; |
| | | |
| | | /// <summary> |
| | | /// 构造函数 |
| | |
| | | /// <param name="stockInfoService">库存信息服务</param> |
| | | /// <param name="stockInfoDetail_HtyService">库存明细历史服务</param> |
| | | /// <param name="stockInfo_HtyService">库存历史服务</param> |
| | | /// <param name="mesUploadHelper">MES异步上传辅助服务</param> |
| | | public StockService( |
| | | IStockInfoDetailService stockInfoDetailService, |
| | | IStockInfoService stockInfoService, |
| | |
| | | IMesService mesService, |
| | | IWarehouseService warehouseService, |
| | | ISqlSugarClient sqlSugarClient, |
| | | IMesLogService mesLogService) |
| | | IMesLogService mesLogService, |
| | | IMesUploadHelper mesUploadHelper) |
| | | { |
| | | StockInfoDetailService = stockInfoDetailService; |
| | | StockInfoService = stockInfoService; |
| | |
| | | _warehouseService = warehouseService; |
| | | SqlSugarClient = sqlSugarClient; |
| | | _mesLogService = mesLogService; |
| | | _mesUploadHelper = mesUploadHelper; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | var result = await StockInfoDetailService.Repository.UpdateDataAsync(detailEntities); |
| | | if (!result) return content.Error("换盘失败"); |
| | | |
| | | // 检查源托盘是否还有剩余库存明细,若无则删除源托盘库存头 |
| | | var remainingSourceDetails = StockInfoDetailService.Repository.QueryData(d => d.StockId == sourceStock.Id); |
| | | if (!remainingSourceDetails.Any()) |
| | | { |
| | | if (await StockInfoService.Repository.Db.Deleteable<Dt_StockInfo>().Where(s => s.Id == sourceStock.Id).ExecuteCommandAsync() <= 0) |
| | | return content.Error("删除源托盘库存失败"); |
| | | } |
| | | |
| | | return content.OK("换盘成功"); |
| | | }); |
| | | } |
| | |
| | | |
| | | if (await StockInfo_HtyService.Repository.AddDataAsync(CreateStockHistory(new[] { sourceStock }, "拆盘")) <= 0) |
| | | return content.Error("拆盘历史记录保存失败"); |
| | | |
| | | // 删除已拆出的库存明细 |
| | | var detailIds = detailEntities.Select(d => d.Id).ToList(); |
| | | if (await StockInfoDetailService.Repository.Db.Deleteable<Dt_StockInfoDetail>().In(detailIds).ExecuteCommandAsync() <= 0) |
| | | return content.Error("删除库存明细失败"); |
| | | |
| | | // 检查源托盘是否还有剩余库存明细,若无则删除源托盘库存头 |
| | | var remainingSourceDetails = StockInfoDetailService.Repository.QueryData(d => d.StockId == sourceStock.Id); |
| | | if (!remainingSourceDetails.Any()) |
| | | { |
| | | if (await StockInfoService.Repository.Db.Deleteable<Dt_StockInfo>().Where(s => s.Id == sourceStock.Id).ExecuteCommandAsync() <= 0) |
| | | return content.Error("删除源托盘库存失败"); |
| | | } |
| | | |
| | | return content.OK("拆盘成功"); |
| | | }); |
| | |
| | | ContainCode = palletCode, |
| | | SfcList = sfcList |
| | | }; |
| | | _ = Task.Run(() => |
| | | { |
| | | var stopwatch = Stopwatch.StartNew(); |
| | | try |
| | | string requestJson = unbindRequest.ToJson(); |
| | | var localToken = token; |
| | | |
| | | _mesUploadHelper.FireAndForget( |
| | | palletCode, |
| | | MesUploadStatusEnum.拆盘上传成功, |
| | | "UnBindContainer", |
| | | requestJson, |
| | | () => |
| | | { |
| | | var unbindResult = string.IsNullOrWhiteSpace(token) |
| | | var result = string.IsNullOrWhiteSpace(localToken) |
| | | ? _mesService.UnBindContainer(unbindRequest) |
| | | : _mesService.UnBindContainer(unbindRequest, token); |
| | | stopwatch.Stop(); |
| | | |
| | | bool isSuccess = unbindResult?.Data?.IsSuccess ?? false; |
| | | int status = isSuccess |
| | | ? (int)MesUploadStatusEnum.拆盘上传成功 |
| | | : (int)MesUploadStatusEnum.拆盘上传失败; |
| | | |
| | | // 更新MES上传状态 |
| | | StockInfoService.UpdateMesUploadStatusAsync(palletCode, status).ConfigureAwait(false); |
| | | |
| | | // 记录MES日志 |
| | | _mesLogService.LogAsync(new MesApiLogDto |
| | | { |
| | | PalletCode = palletCode, |
| | | ApiType = "UnBindContainer", |
| | | RequestJson = unbindRequest.ToJson(), |
| | | ResponseJson = System.Text.Json.JsonSerializer.Serialize(unbindResult), |
| | | IsSuccess = isSuccess, |
| | | ErrorMessage = unbindResult?.Data?.Msg ?? unbindResult?.ErrorMessage ?? "未知错误", |
| | | ElapsedMs = (int)stopwatch.ElapsedMilliseconds, |
| | | Creator = "System" |
| | | }).ConfigureAwait(false); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 调用失败 |
| | | StockInfoService.UpdateMesUploadStatusAsync(palletCode, (int)MesUploadStatusEnum.拆盘上传失败).ConfigureAwait(false); |
| | | } |
| | | }); |
| | | : _mesService.UnBindContainer(unbindRequest, localToken); |
| | | return ( |
| | | result?.Data?.IsSuccess ?? false, |
| | | System.Text.Json.JsonSerializer.Serialize(result), |
| | | result?.Data?.Msg ?? result?.ErrorMessage ?? "未知错误", |
| | | _mesService.BuildConfig(token ?? string.Empty).ToJson() |
| | | ); |
| | | }); |
| | | |
| | | // 4. 删除临时表记录 |
| | | await SqlSugarClient.Deleteable<Dt_SplitTemp>().Where(t => t.PalletCode == palletCode).ExecuteCommandAsync(); |
| | |
| | | public async Task<WebResponseContent> GroupPalletConfirmAsync(string palletCode, string deviceName) |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | var stopwatch = Stopwatch.StartNew(); |
| | | try |
| | | { |
| | | if (string.IsNullOrWhiteSpace(palletCode)) |
| | |
| | | }).ToList() |
| | | }; |
| | | string requestJson = bindRequest.ToJson(); |
| | | var localToken = token; |
| | | |
| | | // 3. Fire-and-forget异步调用MES绑定 |
| | | _ = Task.Run(() => |
| | | { |
| | | var stopwatch = Stopwatch.StartNew(); |
| | | try |
| | | _mesUploadHelper.FireAndForget( |
| | | palletCode, |
| | | MesUploadStatusEnum.组盘上传成功, |
| | | "BindContainer", |
| | | requestJson, |
| | | () => |
| | | { |
| | | var bindResult = string.IsNullOrWhiteSpace(token) |
| | | var result = string.IsNullOrWhiteSpace(localToken) |
| | | ? _mesService.BindContainer(bindRequest) |
| | | : _mesService.BindContainer(bindRequest, token); |
| | | stopwatch.Stop(); |
| | | |
| | | bool isSuccess = bindResult?.Data?.IsSuccess ?? false; |
| | | int status = isSuccess |
| | | ? (int)MesUploadStatusEnum.组盘上传成功 |
| | | : (int)MesUploadStatusEnum.组盘上传失败; |
| | | |
| | | // 更新MES上传状态 |
| | | StockInfoService.UpdateMesUploadStatusAsync(palletCode, status).ConfigureAwait(false); |
| | | |
| | | // 记录MES日志 |
| | | _mesLogService.LogAsync(new MesApiLogDto |
| | | { |
| | | PalletCode = palletCode, |
| | | ApiType = "BindContainer", |
| | | RequestJson = requestJson, |
| | | ResponseJson = System.Text.Json.JsonSerializer.Serialize(bindResult), |
| | | IsSuccess = isSuccess, |
| | | ErrorMessage = bindResult?.ErrorMessage ?? "未知错误", |
| | | ElapsedMs = (int)stopwatch.ElapsedMilliseconds, |
| | | Creator = "System" |
| | | }).ConfigureAwait(false); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 调用失败 |
| | | StockInfoService.UpdateMesUploadStatusAsync(palletCode, (int)MesUploadStatusEnum.组盘上传失败).ConfigureAwait(false); |
| | | } |
| | | }); |
| | | : _mesService.BindContainer(bindRequest, localToken); |
| | | return ( |
| | | result?.Data?.IsSuccess ?? false, |
| | | System.Text.Json.JsonSerializer.Serialize(result), |
| | | result?.Data?.Msg ?? result?.ErrorMessage ?? "未知错误", |
| | | _mesService.BuildConfig(token ?? string.Empty).ToJson() |
| | | ); |
| | | }); |
| | | |
| | | return content.OK("批量组盘确认成功"); |
| | | } |