This commit is contained in:
tongfei
2024-04-19 17:29:49 +08:00
parent 96e5beb7c9
commit 356253d154

View File

@@ -4,10 +4,12 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using WMS.Web.Core.Dto; using WMS.Web.Core.Dto;
using WMS.Web.Core.Dto.ChangeBoxRecord; using WMS.Web.Core.Dto.ChangeBoxRecord;
@@ -49,6 +51,8 @@ namespace WMS.Web.Domain.Services
private readonly IBoxInventoryRepositories _boxInventoryRepositories; private readonly IBoxInventoryRepositories _boxInventoryRepositories;
private readonly ILogger<InStockService> _logger; private readonly ILogger<InStockService> _logger;
private readonly IServiceScopeFactory _serviceScopeFactory; private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ConcurrentDictionary<int, SemaphoreSlim> _locks = new ConcurrentDictionary<int, SemaphoreSlim>();
public InStockService(IMapper mapper, ISerialNumbersRepositories serialNumbersRepositories, IErpService erpService, IBoxInventoryService boxInventoryService, ISerialNumberService serialNumberService, ILoginService loginService, IBoxRepositories boxRepositories, public InStockService(IMapper mapper, ISerialNumbersRepositories serialNumbersRepositories, IErpService erpService, IBoxInventoryService boxInventoryService, ISerialNumberService serialNumberService, ILoginService loginService, IBoxRepositories boxRepositories,
IBasicsRepositories basicsRepositories, IErpBasicDataExtendService erpBasicDataExtendService, IChangeMoveBoxService changeMoveBoxService, IInStockTaskBoxRepositories inStockTaskBoxRepositories, IBasicsRepositories basicsRepositories, IErpBasicDataExtendService erpBasicDataExtendService, IChangeMoveBoxService changeMoveBoxService, IInStockTaskBoxRepositories inStockTaskBoxRepositories,
IInStockRepositories inStockRepositories, IInStockTaskRepositories inStockTaskRepositories, IBoxInventoryRepositories boxInventoryRepositories, ILogger<InStockService> logger, IServiceScopeFactory serviceScopeFactory) IInStockRepositories inStockRepositories, IInStockTaskRepositories inStockTaskRepositories, IBoxInventoryRepositories boxInventoryRepositories, ILogger<InStockService> logger, IServiceScopeFactory serviceScopeFactory)
@@ -129,29 +133,42 @@ namespace WMS.Web.Domain.Services
/// <returns></returns> /// <returns></returns>
public async Task<Result> Shelf(PurchaseShelfRequest instock, LoginInDto loginInfo) public async Task<Result> Shelf(PurchaseShelfRequest instock, LoginInDto loginInfo)
{ {
IDbContextTransaction _transaction = _basicsRepositories.GetTransaction(); var taskId= instock.Details.Select(x => x.TaskId).FirstOrDefault();
bool isRollback = false;
bool isTransaction = false;
//1.添加入库单同步金蝶在save方法里面进行
var save_result = await this.ShelfSave(instock, InstockType.Purchase, loginInfo, isTransaction);
if (!save_result.IsSuccess) isRollback = true;
//实体 var taskLock = _locks.GetOrAdd(taskId, _ => new SemaphoreSlim(1, 1));
var entity = save_result.Data; await taskLock.WaitAsync();
try
//提交事务
var isSuccess = _basicsRepositories.CommitTransaction(isRollback, _transaction);
if (!isSuccess)
return save_result;
//同步金蝶
if (entity.Type == InstockType.Purchase)
{ {
OperateRequest oRequest = new OperateRequest(); IDbContextTransaction _transaction = _basicsRepositories.GetTransaction();
oRequest.Ids.Add(entity.Id); bool isRollback = false;
await Sync(oRequest, loginInfo, false); bool isTransaction = false;
//1.添加入库单同步金蝶在save方法里面进行
var save_result = await this.ShelfSave(instock, InstockType.Purchase, loginInfo, isTransaction);
if (!save_result.IsSuccess) isRollback = true;
//实体
var entity = save_result.Data;
//提交事务
var isSuccess = _basicsRepositories.CommitTransaction(isRollback, _transaction);
if (!isSuccess)
return save_result;
//同步金蝶
if (entity.Type == InstockType.Purchase)
{
OperateRequest oRequest = new OperateRequest();
oRequest.Ids.Add(entity.Id);
await Sync(oRequest, loginInfo, false);
}
return Result.ReSuccess();
} }
return Result.ReSuccess(); finally
{
taskLock.Release();
}
} }
/// <summary> /// <summary>
@@ -189,7 +206,7 @@ namespace WMS.Web.Domain.Services
return Result<BoxInStockTaskDto>.ReFailure(ResultCodes.BoxNoData); return Result<BoxInStockTaskDto>.ReFailure(ResultCodes.BoxNoData);
//判断箱不存在所选的物料 //判断箱不存在所选的物料
var isAnyHave = box.Details.Any(x => x.MaterialNumber == dto.MaterialNumber); var isAnyHave = box.Details.Any(x => x.MaterialNumber == dto.MaterialNumber);
if (!isAnyHave) if (!isAnyHave)
return Result<BoxInStockTaskDto>.ReFailure(ResultCodes.Contrast_Purchase_Count_Error); return Result<BoxInStockTaskDto>.ReFailure(ResultCodes.Contrast_Purchase_Count_Error);
//return Result<BoxInStockTaskDto>.ReFailure(box.BoxBillNo + ResultCodes.BoxNoMaterialError.Item2, ResultCodes.BoxNoMaterialError.Item1); //return Result<BoxInStockTaskDto>.ReFailure(box.BoxBillNo + ResultCodes.BoxNoMaterialError.Item2, ResultCodes.BoxNoMaterialError.Item1);
@@ -585,7 +602,7 @@ namespace WMS.Web.Domain.Services
return Result<InStockTask>.ReFailure(ResultCodes.InStockTaskBoxIsHaveData); return Result<InStockTask>.ReFailure(ResultCodes.InStockTaskBoxIsHaveData);
//序列号 //序列号
var serialNumbs= await _serialNumbersRepositories.GetEntityListByBoxIds(dto_boxIds); var serialNumbs = await _serialNumbersRepositories.GetEntityListByBoxIds(dto_boxIds);
//3.组装绑定关系表,要添加的集合 //3.组装绑定关系表,要添加的集合
var boxEntitys = await _boxRepositories.GetEntityList(dto_boxIds); var boxEntitys = await _boxRepositories.GetEntityList(dto_boxIds);
@@ -596,13 +613,13 @@ namespace WMS.Web.Domain.Services
taskBox.TaskId = entity.Id; taskBox.TaskId = entity.Id;
taskBox.Receiver(staffId); taskBox.Receiver(staffId);
var currentBox_serialNumbs= serialNumbs.Where(s => s.BoxId == taskBox.BoxId).ToList(); var currentBox_serialNumbs = serialNumbs.Where(s => s.BoxId == taskBox.BoxId).ToList();
var current_dto_box_dets = boxEntitys.Where(x => x.Id == item.BoxId).SelectMany(x => x.Details).ToList(); var current_dto_box_dets = boxEntitys.Where(x => x.Id == item.BoxId).SelectMany(x => x.Details).ToList();
taskBox.Details = _mapper.Map<List<InStockTaskBoxDetails>>(current_dto_box_dets); taskBox.Details = _mapper.Map<List<InStockTaskBoxDetails>>(current_dto_box_dets);
taskBox.Details.ForEach(x => taskBox.Details.ForEach(x =>
{ {
x.ErpDetailId = item.ErpDetailId; x.ErpDetailId = item.ErpDetailId;
x.SerialNumbers= currentBox_serialNumbs.Where(s => s.MaterialNumber == x.MaterialNumber).Select(s => s.SerialNumber).ToList(); x.SerialNumbers = currentBox_serialNumbs.Where(s => s.MaterialNumber == x.MaterialNumber).Select(s => s.SerialNumber).ToList();
}); });
taskBoxAdd.Add(taskBox); taskBoxAdd.Add(taskBox);
} }
@@ -680,7 +697,7 @@ namespace WMS.Web.Domain.Services
//return Result<ContrastMaterialsResponse>.ReFailure(item.BoxBillNo + ResultCodes.BoxNoMaterialError.Item2, ResultCodes.BoxNoMaterialError.Item1); //return Result<ContrastMaterialsResponse>.ReFailure(item.BoxBillNo + ResultCodes.BoxNoMaterialError.Item2, ResultCodes.BoxNoMaterialError.Item1);
} }
} }
//3.判断箱里面对应的物料是否存在;不存在就报错 //3.判断箱里面对应的物料是否存在;不存在就报错
foreach (var item in boxs) foreach (var item in boxs)
@@ -799,7 +816,7 @@ namespace WMS.Web.Domain.Services
entity.SyncFail(res.result.Message, s.ErpDetailId, loginInfo?.UserInfo?.StaffId ?? 0, res.syncStatus); entity.SyncFail(res.result.Message, s.ErpDetailId, loginInfo?.UserInfo?.StaffId ?? 0, res.syncStatus);
} }
} }
else else
{ {
erpDetails_tags.ForEach(x => { x.SuccessSync = SyncStatus.Fail; }); erpDetails_tags.ForEach(x => { x.SuccessSync = SyncStatus.Fail; });
entity.SyncFailAll("同步金蝶失败,金蝶存在已审核的采购入库单", loginInfo?.UserInfo?.StaffId ?? 0); entity.SyncFailAll("同步金蝶失败,金蝶存在已审核的采购入库单", loginInfo?.UserInfo?.StaffId ?? 0);