调整事物
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace WMS.Web.Domain.Infrastructure
|
||||
{
|
||||
public interface ITransactionRepositories
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取事务 用来处理即时库存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IDbContextTransaction GetTransaction();
|
||||
|
||||
/// <summary>
|
||||
/// 获取事务 用来处理即时库存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
bool CommitTransaction(bool isRollback, IDbContextTransaction transaction);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
@@ -20,11 +21,14 @@ namespace WMS.Web.Domain.Services
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ILoginService _loginService;
|
||||
private readonly IChangeBoxRecordRepositories _changeBoxRecordRepositories;
|
||||
public ChangeMoveBoxService(IMapper mapper, ILoginService loginService, IChangeBoxRecordRepositories changeBoxRecordRepositories)
|
||||
public readonly ITransactionRepositories _transactionRepositories;
|
||||
public ChangeMoveBoxService(IMapper mapper, ILoginService loginService,
|
||||
IChangeBoxRecordRepositories changeBoxRecordRepositories, ITransactionRepositories transactionRepositories)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_loginService = loginService;
|
||||
_changeBoxRecordRepositories = changeBoxRecordRepositories;
|
||||
_transactionRepositories = transactionRepositories;
|
||||
}
|
||||
|
||||
public async Task<Result> Save(SaveChangeBoxRecordRequest dto, LoginInDto loginInfo)
|
||||
@@ -35,9 +39,17 @@ namespace WMS.Web.Domain.Services
|
||||
//需要填写序列号
|
||||
//需要修改库存
|
||||
|
||||
IDbContextTransaction _transaction = _transactionRepositories.GetTransaction();
|
||||
bool isRollback = false;
|
||||
bool isSuccess = true;
|
||||
entity = await _changeBoxRecordRepositories.Add(entity, true);
|
||||
if (entity == null)
|
||||
if (entity == null) isRollback = true;
|
||||
|
||||
//提交事务
|
||||
isSuccess = _transactionRepositories.CommitTransaction(isRollback, _transaction);
|
||||
if (!isSuccess)
|
||||
return Result.ReFailure(ResultCodes.DateWriteError);
|
||||
|
||||
return Result.ReSuccess();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
services.AddHttpContextAccessor();
|
||||
|
||||
services.AddTransient<ITransactionRepositories, TransactionRepositories>();
|
||||
|
||||
services.AddTransient<IInStockRepositories, InStockRepositories>();
|
||||
|
||||
services.AddTransient<IChangeBoxRecordRepositories, ChangeBoxRecordRepositories>();
|
||||
|
||||
47
src/WMS.Web.Repositories/TransactionRepositories.cs
Normal file
47
src/WMS.Web.Repositories/TransactionRepositories.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using WMS.Web.Domain.Infrastructure;
|
||||
using WMS.Web.Repositories.Configuration;
|
||||
|
||||
namespace WMS.Web.Repositories
|
||||
{
|
||||
public class TransactionRepositories: ITransactionRepositories
|
||||
{
|
||||
private RepositoryDbContext _context;
|
||||
|
||||
|
||||
public TransactionRepositories(RepositoryDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
public IDbContextTransaction GetTransaction()
|
||||
{
|
||||
return _context.Database.BeginTransaction();
|
||||
}
|
||||
|
||||
public bool CommitTransaction(bool isRollback, IDbContextTransaction transaction)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (transaction == null)
|
||||
return true;
|
||||
|
||||
if (isRollback)
|
||||
{
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
transaction.Commit();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user