Browse Source

事务测试成功

master
me 1 week ago
parent
commit
2c55fa4738
  1. 2
      API/TaskManager.EntityFramework/IRepository/IRepository.cs
  2. 10
      API/TaskManager.EntityFramework/Repository/Repository.cs
  3. 65
      API/Wood.Service/Commons/CommonService.cs
  4. 2
      API/Wood.Service/Controllers/NormalBaseController.cs
  5. 76
      API/Wood.Service/Datas/SupplierEmployeeDtService.cs
  6. 77
      API/Wood.Service/Datas/SupplierInfoDtService.cs
  7. 70
      API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs
  8. 78
      API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs

2
API/TaskManager.EntityFramework/IRepository/IRepository.cs

@ -13,6 +13,8 @@ namespace TaskManager.EntityFramework
{
public interface IRepository<TEntity> where TEntity : BaseEntity
{
void SetDbContext(JobDbContext context);
Task<TEntity> GetByIdAsync(long id);
Task<IEnumerable<TEntity>> GetAllAsync();
Task<TEntity> AddAsync(TEntity entity);

10
API/TaskManager.EntityFramework/Repository/Repository.cs

@ -18,8 +18,8 @@ namespace TaskManager.EntityFramework.Repository
public class Repository<TEntity> : IRepository<TEntity>
where TEntity : BaseEntity
{
private readonly JobDbContext _context;
private readonly DbSet<TEntity> _dbSet;
private JobDbContext _context;
private DbSet<TEntity> _dbSet;
public Repository(JobDbContext context)
{
@ -27,6 +27,12 @@ namespace TaskManager.EntityFramework.Repository
_dbSet = context.Set<TEntity>();
}
public void SetDbContext(JobDbContext context)
{
_context = context;
_dbSet = context.Set<TEntity>();
}
public async Task<TEntity> GetByIdAsync(long id)
{
return await _dbSet.FindAsync(id);

65
API/Wood.Service/Commons/CommonService.cs

@ -0,0 +1,65 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskManager.Entity;
using TaskManager.EntityFramework;
using Wood.Entity;
using Wood.Util;
namespace Wood.Service
{
public class CommonService : ControllerBase, IScoped
{
protected readonly JobDbContext _context;
protected readonly IServiceProvider _builder;
protected readonly IConfiguration _configuration;
public CommonService(JobDbContext context, IServiceProvider builder, IConfiguration configuration)
{
_builder = builder;
_context = context;
_configuration = configuration;
}
/// <summary>
/// 生成TaskSub
/// </summary>
/// <param name="dataCount">任务明细数量</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
[HttpGet]
public TaskSub BuildTaskSub(int dataCount, string taskName)
{
TaskConifgure? taskConfigureObj = _context.Set<TaskConifgure>().FirstOrDefault(itm => itm.TaskName == taskName);
if (taskConfigureObj == null)
{
throw new Exception($"任务配置表没有'{taskName}'任务");
}
TaskSub taskSub = new TaskSub();
taskSub.TableName = taskConfigureObj.TableName; //关联的数据表名称(如:订单表、用户表等,可为空)
taskSub.TaskName = taskConfigureObj.TaskName; //任务名称(用于业务层面标识任务,如:数据同步任务、报表生成任务)
taskSub.DataCount = dataCount; //数据总量(任务处理的数据条目数)
taskSub.Subscriber = taskConfigureObj.Client; //发布给那个客户
taskSub.FailedCount = 0; //失败次数(任务执行失败的累计次数)
taskSub.FailedInfo = ""; //失败详情(记录失败原因、异常堆栈等信息,支持长文本)
taskSub.Domain = ""; //所属域(多租户场景下标识租户,如:租户A、租户B)
taskSub.Site = ""; //站点标识(多站点部署时标识所属站点,如:Site1、Site2)
taskSub.CreateUser = CommonHelper.UserName; //从那个内部系统创建系统
taskSub.CreateTime = CommonHelper.CurrentTime; //创建时间(任务创建的时间戳)
taskSub.Remark = ""; //备注信息(任务相关的补充说明,支持长文本)
taskSub.UpdateUser = ""; //最后更新人(记录任务最后修改者)
taskSub.UpdateTime = null; //最后更新时间(任务最后修改的时间戳,可为空)
taskSub.SyncedPageCount = 0; //已同步页数
taskSub.ReadState = true;
taskSub.CreationTime = CommonHelper.CurrentTime;
taskSub.TaskId = CommonHelper.NewGuid;
return taskSub;
}
}
}

2
API/Wood.Service/Controllers/NormalBaseController.cs

@ -25,7 +25,7 @@ using Wood.Util.Filters;
namespace Wood.Service.Controllers
{
[AllowAnonymous]
public class NormalBaseController<T>:ControllerBase,IScoped where T:BaseEntity ,new()
public class NormalBaseController<T>:ControllerBase,ITransient where T:BaseEntity ,new()
{
protected readonly JobDbContext _context;

76
API/Wood.Service/Datas/SupplierEmployeeDtService.cs

@ -22,15 +22,24 @@ namespace Wood.Service.Datas
{
public class SupplierEmployeeDtService : NormalBaseController<SUPPLIER_EMPLOYEE_DT>
{
const string _taskName = "人员资质信息";
private readonly IRepository<TaskSub> _taskSubRepository;
private readonly IRepository<SUPPLIER_EMPLOYEE> _supplierEmployeeRepository;
private readonly IRepository<TaskConifgure> _taskConfigureRepository;
private readonly CommonService _commonService;
public SupplierEmployeeDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<SUPPLIER_EMPLOYEE_DT> repository, IRepository<TaskSub> taskSubRepository, IRepository<SUPPLIER_EMPLOYEE> supplierEmployeeRepository, IRepository<TaskConifgure> taskConfigureRepository) : base(context, builder, configuration, repository)
public SupplierEmployeeDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<SUPPLIER_EMPLOYEE_DT> repository, IRepository<TaskSub> taskSubRepository, IRepository<SUPPLIER_EMPLOYEE> supplierEmployeeRepository, IRepository<TaskConifgure> taskConfigureRepository, CommonService commonService) : base(context, builder, configuration, repository)
{
_taskSubRepository = taskSubRepository;
_supplierEmployeeRepository = supplierEmployeeRepository;
_taskConfigureRepository = taskConfigureRepository;
_taskSubRepository.SetDbContext(_context);
_supplierEmployeeRepository.SetDbContext(_context);
_taskConfigureRepository.SetDbContext(_context);
_commonService = commonService;
}
[HttpPost]
@ -61,24 +70,24 @@ namespace Wood.Service.Datas
}
throw new Exception(sb.ToString());
}
var empDtLst = impResult.Data;
var entityLst = impResult.Data;
using TransactionScope scope = new TransactionScope();
using var transaction = _context.Database.BeginTransaction();
try
{
#region 数据库操作
//添加任务
TaskSub taskSubObj = await BuildTaskSub(empDtLst.Count);
TaskSub taskSubObj = _commonService.BuildTaskSub(entityLst.Count, _taskName);
await _taskSubRepository.AddAsync(taskSubObj);
//添加任务明细
foreach (var empDtObj in empDtLst)
foreach (var empDtObj in entityLst)
{
SUPPLIER_EMPLOYEE empObj = ExpressionGenericMapper<SUPPLIER_EMPLOYEE_DT, SUPPLIER_EMPLOYEE>.Trans(empDtObj);
empObj.TaskId = taskSubObj.TaskId;
await _supplierEmployeeRepository.AddAsync(empObj);
}
foreach (var empDtObj in empDtLst)
foreach (var empDtObj in entityLst)
{
//以“供应商代码+工厂代码+车间代码+产线代码+工位代码+操作人员账号”为唯一标识,做新增或者更新存储
var firstObj = _context.Set<SUPPLIER_EMPLOYEE_DT>().FirstOrDefault(
@ -95,28 +104,30 @@ namespace Wood.Service.Datas
}
else
{
var ret = await base.Update(empDtObj);
}
}
#endregion
scope.Complete();
transaction.Commit();
return Ok(true);
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
}
}
public override async Task<ActionResult<SUPPLIER_EMPLOYEE_DT>> Create(SUPPLIER_EMPLOYEE_DT entity)
{
using TransactionScope scope = new TransactionScope();
using var transaction = _context.Database.BeginTransaction();
try
{
#region 数据库操作
//添加任务
TaskSub taskSubObj = await BuildTaskSub(1);
TaskSub taskSubObj = _commonService.BuildTaskSub(1, _taskName);
await _taskSubRepository.AddAsync(taskSubObj);
//添加任务明细
SUPPLIER_EMPLOYEE empObj = ExpressionGenericMapper<SUPPLIER_EMPLOYEE_DT, SUPPLIER_EMPLOYEE>.Trans(entity);
@ -141,23 +152,24 @@ namespace Wood.Service.Datas
}
#endregion
scope.Complete();
transaction.Commit();
return Ok(true);
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
}
}
public override async Task<IActionResult> Update(SUPPLIER_EMPLOYEE_DT entity)
{
using TransactionScope scope = new TransactionScope();
using var transaction = _context.Database.BeginTransaction();
try
{
#region 数据库操作
//添加任务
TaskSub taskSubObj = await BuildTaskSub(1);
TaskSub taskSubObj = _commonService.BuildTaskSub(1, _taskName);
await _taskSubRepository.AddAsync(taskSubObj);
//添加任务明细
SUPPLIER_EMPLOYEE empObj = ExpressionGenericMapper<SUPPLIER_EMPLOYEE_DT, SUPPLIER_EMPLOYEE>.Trans(entity);
@ -182,50 +194,14 @@ namespace Wood.Service.Datas
}
#endregion
scope.Complete();
transaction.Commit();
return Ok(true);
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
}
}
/// <summary>
/// 生成TaskSub
/// </summary>
/// <param name="dataCount">任务明细数量</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private async Task<TaskSub> BuildTaskSub(int dataCount)
{
List<TaskConifgure> taskConfigureLst = (await _taskConfigureRepository.GetAllAsync()).ToList();
TaskConifgure? taskConfigureObj = taskConfigureLst.FirstOrDefault(itm => itm.TaskName == "人员资质信息");
if (taskConfigureObj == null)
{
throw new Exception("任务配置表没有'人员资质信息'任务");
}
TaskSub taskSub = new TaskSub();
taskSub.TableName = taskConfigureObj.TableName; //关联的数据表名称(如:订单表、用户表等,可为空)
taskSub.TaskName = taskConfigureObj.TaskName; //任务名称(用于业务层面标识任务,如:数据同步任务、报表生成任务)
taskSub.DataCount = dataCount; //数据总量(任务处理的数据条目数)
taskSub.Subscriber = taskConfigureObj.Client; //发布给那个客户
taskSub.FailedCount = 0; //失败次数(任务执行失败的累计次数)
taskSub.FailedInfo = ""; //失败详情(记录失败原因、异常堆栈等信息,支持长文本)
taskSub.Domain = ""; //所属域(多租户场景下标识租户,如:租户A、租户B)
taskSub.Site = ""; //站点标识(多站点部署时标识所属站点,如:Site1、Site2)
taskSub.CreateUser = CommonHelper.UserName; //从那个内部系统创建系统
taskSub.CreateTime = CommonHelper.CurrentTime; //创建时间(任务创建的时间戳)
taskSub.Remark = ""; //备注信息(任务相关的补充说明,支持长文本)
taskSub.UpdateUser = ""; //最后更新人(记录任务最后修改者)
taskSub.UpdateTime = null; //最后更新时间(任务最后修改的时间戳,可为空)
taskSub.SyncedPageCount = 0; //已同步页数
taskSub.ReadState = true;
taskSub.CreationTime = CommonHelper.CurrentTime;
taskSub.TaskId = CommonHelper.NewGuid;
return taskSub;
}
}
}

77
API/Wood.Service/Datas/SupplierInfoDtService.cs

@ -21,15 +21,24 @@ namespace Wood.Service.Datas
{
public class SupplierInfoDtService : NormalBaseController<SUPPLIER_INFO_DT>
{
const string _taskName = "供应商基础信息";
private readonly IRepository<TaskSub> _taskSubRepository;
private readonly IRepository<SUPPLIER_INFO> _supplierInfoRepository;
private readonly IRepository<TaskConifgure> _taskConfigureRepository;
private readonly CommonService _commonService;
public SupplierInfoDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<SUPPLIER_INFO_DT> repository, IRepository<TaskSub> taskSubRepository, IRepository<SUPPLIER_INFO> supplierInfoRepository, IRepository<TaskConifgure> taskConfigureRepository) : base(context, builder, configuration, repository)
public SupplierInfoDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<SUPPLIER_INFO_DT> repository, IRepository<TaskSub> taskSubRepository, IRepository<SUPPLIER_INFO> supplierInfoRepository, IRepository<TaskConifgure> taskConfigureRepository, CommonService commonService) : base(context, builder, configuration, repository)
{
_taskSubRepository = taskSubRepository;
_supplierInfoRepository = supplierInfoRepository;
_taskConfigureRepository = taskConfigureRepository;
_commonService = commonService;
_taskSubRepository.SetDbContext(_context);
_supplierInfoRepository.SetDbContext(_context);
_taskConfigureRepository.SetDbContext(_context);
}
[HttpPost]
@ -60,24 +69,24 @@ namespace Wood.Service.Datas
}
throw new Exception(sb.ToString());
}
var empDtLst = impResult.Data;
var entityLst = impResult.Data;
using TransactionScope scope = new TransactionScope();
using var transaction = _context.Database.BeginTransaction();
try
{
#region 数据库操作
//添加任务
TaskSub taskSubObj = await BuildTaskSub(empDtLst.Count);
TaskSub taskSubObj = _commonService.BuildTaskSub(entityLst.Count, _taskName);
await _taskSubRepository.AddAsync(taskSubObj);
//添加任务明细
foreach (var empDtObj in empDtLst)
foreach (var empDtObj in entityLst)
{
SUPPLIER_INFO empObj = ExpressionGenericMapper<SUPPLIER_INFO_DT, SUPPLIER_INFO>.Trans(empDtObj);
empObj.TaskId = taskSubObj.TaskId;
await _supplierInfoRepository.AddAsync(empObj);
}
foreach (var empDtObj in empDtLst)
foreach (var empDtObj in entityLst)
{
//以“供应商代码+工厂代码+车间代码+产线代码+工位代码”为唯一标识,做新增或更新存储
var firstObj = _context.Set<SUPPLIER_INFO_DT>().FirstOrDefault(
@ -98,24 +107,27 @@ namespace Wood.Service.Datas
}
#endregion
scope.Complete();
transaction.Commit();
return Ok(true);
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
}
}
public override async Task<ActionResult<SUPPLIER_INFO_DT>> Create(SUPPLIER_INFO_DT entity)
{
using TransactionScope scope = new TransactionScope();
using var transaction = _context.Database.BeginTransaction();
try
{
#region 数据库操作
//添加任务
TaskSub taskSubObj = await BuildTaskSub(1);
TaskSub taskSubObj = _commonService.BuildTaskSub(1, _taskName);
await _taskSubRepository.AddAsync(taskSubObj);
//添加任务明细
SUPPLIER_INFO empObj = ExpressionGenericMapper<SUPPLIER_INFO_DT, SUPPLIER_INFO>.Trans(entity);
empObj.TaskId = taskSubObj.TaskId;
@ -138,23 +150,24 @@ namespace Wood.Service.Datas
}
#endregion
scope.Complete();
transaction.Commit();
return Ok(true);
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
}
}
public override async Task<IActionResult> Update(SUPPLIER_INFO_DT entity)
{
using TransactionScope scope = new TransactionScope();
using var transaction = _context.Database.BeginTransaction();
try
{
#region 数据库操作
//添加任务
TaskSub taskSubObj = await BuildTaskSub(1);
TaskSub taskSubObj = _commonService.BuildTaskSub(1, _taskName);
await _taskSubRepository.AddAsync(taskSubObj);
//添加任务明细
SUPPLIER_INFO empObj = ExpressionGenericMapper<SUPPLIER_INFO_DT, SUPPLIER_INFO>.Trans(entity);
@ -178,50 +191,14 @@ namespace Wood.Service.Datas
}
#endregion
scope.Complete();
transaction.Commit();
return Ok(true);
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
}
}
/// <summary>
/// 生成TaskSub
/// </summary>
/// <param name="dataCount">任务明细数量</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private async Task<TaskSub> BuildTaskSub(int dataCount)
{
List<TaskConifgure> taskConfigureLst = (await _taskConfigureRepository.GetAllAsync()).ToList();
TaskConifgure? taskConfigureObj = taskConfigureLst.FirstOrDefault(itm => itm.TaskName == "供应商基础信息");
if (taskConfigureObj == null)
{
throw new Exception("任务配置表没有'供应商基础信息'任务");
}
TaskSub taskSub = new TaskSub();
taskSub.TableName = taskConfigureObj.TableName; //关联的数据表名称(如:订单表、用户表等,可为空)
taskSub.TaskName = taskConfigureObj.TaskName; //任务名称(用于业务层面标识任务,如:数据同步任务、报表生成任务)
taskSub.DataCount = dataCount; //数据总量(任务处理的数据条目数)
taskSub.Subscriber = taskConfigureObj.Client; //发布给那个客户
taskSub.FailedCount = 0; //失败次数(任务执行失败的累计次数)
taskSub.FailedInfo = ""; //失败详情(记录失败原因、异常堆栈等信息,支持长文本)
taskSub.Domain = ""; //所属域(多租户场景下标识租户,如:租户A、租户B)
taskSub.Site = ""; //站点标识(多站点部署时标识所属站点,如:Site1、Site2)
taskSub.CreateUser = CommonHelper.UserName; //从那个内部系统创建系统
taskSub.CreateTime = CommonHelper.CurrentTime; //创建时间(任务创建的时间戳)
taskSub.Remark = ""; //备注信息(任务相关的补充说明,支持长文本)
taskSub.UpdateUser = ""; //最后更新人(记录任务最后修改者)
taskSub.UpdateTime = null; //最后更新时间(任务最后修改的时间戳,可为空)
taskSub.SyncedPageCount = 0; //已同步页数
taskSub.ReadState = true;
taskSub.CreationTime = CommonHelper.CurrentTime;
taskSub.TaskId = CommonHelper.NewGuid;
return taskSub;
}
}
}

70
API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs

@ -20,19 +20,26 @@ using Wood.Util;
namespace Wood.Service.Datas
{
public class SupplierProAttachmentDataDtService : NormalBaseController<SUPPLIER_PRO_ATTACHMENT_DATA_DT>
{
const string _taskName = "附件类数据";
private readonly IRepository<TaskSub> _taskSubRepository;
private readonly IRepository<SUPPLIER_PRO_ATTACHMENT_DATA> _supplierProAttachmentDataRepository;
private readonly IRepository<TaskConifgure> _taskConfigureRepository;
private readonly CommonService _commonService;
public SupplierProAttachmentDataDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<SUPPLIER_PRO_ATTACHMENT_DATA_DT> repository, IRepository<TaskSub> taskSubRepository, IRepository<SUPPLIER_PRO_ATTACHMENT_DATA> supplierProAttachmentDataRepository, IRepository<TaskConifgure> taskConfigureRepository) : base(context, builder, configuration, repository)
public SupplierProAttachmentDataDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<SUPPLIER_PRO_ATTACHMENT_DATA_DT> repository, IRepository<TaskSub> taskSubRepository, IRepository<SUPPLIER_PRO_ATTACHMENT_DATA> supplierProAttachmentDataRepository, IRepository<TaskConifgure> taskConfigureRepository, CommonService commonService) : base(context, builder, configuration, repository)
{
_taskSubRepository = taskSubRepository;
_supplierProAttachmentDataRepository = supplierProAttachmentDataRepository;
_taskConfigureRepository = taskConfigureRepository;
_taskSubRepository.SetDbContext(_context);
_supplierProAttachmentDataRepository.SetDbContext(_context);
_taskConfigureRepository.SetDbContext(_context);
_commonService = commonService;
}
[HttpPost]
@ -63,47 +70,48 @@ namespace Wood.Service.Datas
}
throw new Exception(sb.ToString());
}
var empDtLst = impResult.Data;
var entityLst = impResult.Data;
using TransactionScope scope = new TransactionScope();
using var transaction = _context.Database.BeginTransaction();
try
{
#region 数据库操作
//添加任务
TaskSub taskSubObj = await BuildTaskSub(empDtLst.Count);
TaskSub taskSubObj = _commonService.BuildTaskSub(entityLst.Count, _taskName);
await _taskSubRepository.AddAsync(taskSubObj);
//添加任务明细
foreach (var empDtObj in empDtLst)
foreach (var empDtObj in entityLst)
{
SUPPLIER_PRO_ATTACHMENT_DATA empObj = ExpressionGenericMapper<SUPPLIER_PRO_ATTACHMENT_DATA_DT, SUPPLIER_PRO_ATTACHMENT_DATA>.Trans(empDtObj);
empObj.TaskId = taskSubObj.TaskId;
await _supplierProAttachmentDataRepository.AddAsync(empObj);
}
foreach (var empDtObj in empDtLst)
foreach (var empDtObj in entityLst)
{
//存储新增数据
var ret = await base.Create(empDtObj);
}
#endregion
scope.Complete();
transaction.Commit();
return Ok(true);
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
}
}
public override async Task<ActionResult<SUPPLIER_PRO_ATTACHMENT_DATA_DT>> Create(SUPPLIER_PRO_ATTACHMENT_DATA_DT entity)
{
using TransactionScope scope = new TransactionScope();
using var transaction = _context.Database.BeginTransaction();
try
{
#region 数据库操作
//添加任务
TaskSub taskSubObj = await BuildTaskSub(1);
TaskSub taskSubObj = _commonService.BuildTaskSub(1, _taskName);
await _taskSubRepository.AddAsync(taskSubObj);
//添加任务明细
SUPPLIER_PRO_ATTACHMENT_DATA empObj = ExpressionGenericMapper<SUPPLIER_PRO_ATTACHMENT_DATA_DT, SUPPLIER_PRO_ATTACHMENT_DATA>.Trans(entity);
@ -114,51 +122,15 @@ namespace Wood.Service.Datas
#endregion
scope.Complete();
transaction.Commit();
return Ok(true);
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
}
}
/// <summary>
/// 生成TaskSub
/// </summary>
/// <param name="dataCount">任务明细数量</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private async Task<TaskSub> BuildTaskSub(int dataCount)
{
List<TaskConifgure> taskConfigureLst = (await _taskConfigureRepository.GetAllAsync()).ToList();
TaskConifgure? taskConfigureObj = taskConfigureLst.FirstOrDefault(itm => itm.TaskName == "附件类数据");
if (taskConfigureObj == null)
{
throw new Exception("任务配置表没有'附件类数据'任务");
}
TaskSub taskSub = new TaskSub();
taskSub.TableName = taskConfigureObj.TableName; //关联的数据表名称(如:订单表、用户表等,可为空)
taskSub.TaskName = taskConfigureObj.TaskName; //任务名称(用于业务层面标识任务,如:数据同步任务、报表生成任务)
taskSub.DataCount = dataCount; //数据总量(任务处理的数据条目数)
taskSub.Subscriber = taskConfigureObj.Client; //发布给那个客户
taskSub.FailedCount = 0; //失败次数(任务执行失败的累计次数)
taskSub.FailedInfo = ""; //失败详情(记录失败原因、异常堆栈等信息,支持长文本)
taskSub.Domain = ""; //所属域(多租户场景下标识租户,如:租户A、租户B)
taskSub.Site = ""; //站点标识(多站点部署时标识所属站点,如:Site1、Site2)
taskSub.CreateUser = CommonHelper.UserName; //从那个内部系统创建系统
taskSub.CreateTime = CommonHelper.CurrentTime; //创建时间(任务创建的时间戳)
taskSub.Remark = ""; //备注信息(任务相关的补充说明,支持长文本)
taskSub.UpdateUser = ""; //最后更新人(记录任务最后修改者)
taskSub.UpdateTime = null; //最后更新时间(任务最后修改的时间戳,可为空)
taskSub.SyncedPageCount = 0; //已同步页数
taskSub.ReadState = true;
taskSub.CreationTime = CommonHelper.CurrentTime;
taskSub.TaskId = CommonHelper.NewGuid;
return taskSub;
}
}
}

78
API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs

@ -20,19 +20,26 @@ using Wood.Util;
namespace Wood.Service.Datas
{
public class SupplierProProcessEquipmentDtService : NormalBaseController<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT>
{
const string _taskName = "工艺装备";
private readonly IRepository<TaskSub> _taskSubRepository;
private readonly IRepository<SUPPLIER_PRO_PROCESS_EQUIPMENT> _supplierProProcessEquipmentRepository;
private readonly IRepository<TaskConifgure> _taskConfigureRepository;
private readonly CommonService _commonService;
public SupplierProProcessEquipmentDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT> repository, IRepository<TaskSub> taskSubRepository, IRepository<SUPPLIER_PRO_PROCESS_EQUIPMENT> supplierProProcessEquipmentRepository, IRepository<TaskConifgure> taskConfigureRepository) : base(context, builder, configuration, repository)
public SupplierProProcessEquipmentDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT> repository, IRepository<TaskSub> taskSubRepository, IRepository<SUPPLIER_PRO_PROCESS_EQUIPMENT> supplierProProcessEquipmentRepository, IRepository<TaskConifgure> taskConfigureRepository, CommonService commonService) : base(context, builder, configuration, repository)
{
_taskSubRepository = taskSubRepository;
_supplierProProcessEquipmentRepository = supplierProProcessEquipmentRepository;
_taskConfigureRepository = taskConfigureRepository;
_taskSubRepository.SetDbContext(_context);
_supplierProProcessEquipmentRepository.SetDbContext(_context);
_taskConfigureRepository.SetDbContext(_context);
_commonService = commonService;
}
[HttpPost]
@ -63,24 +70,24 @@ namespace Wood.Service.Datas
}
throw new Exception(sb.ToString());
}
var empDtLst = impResult.Data;
var entityLst = impResult.Data;
using TransactionScope scope = new TransactionScope();
using var transaction = _context.Database.BeginTransaction();
try
{
#region 数据库操作
//添加任务
TaskSub taskSubObj = await BuildTaskSub(empDtLst.Count);
TaskSub taskSubObj = _commonService.BuildTaskSub(entityLst.Count, _taskName);
await _taskSubRepository.AddAsync(taskSubObj);
//添加任务明细
foreach (var empDtObj in empDtLst)
foreach (var empDtObj in entityLst)
{
SUPPLIER_PRO_PROCESS_EQUIPMENT empObj = ExpressionGenericMapper<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT, SUPPLIER_PRO_PROCESS_EQUIPMENT>.Trans(empDtObj);
empObj.TaskId = taskSubObj.TaskId;
await _supplierProProcessEquipmentRepository.AddAsync(empObj);
}
foreach (var empDtObj in empDtLst)
foreach (var empDtObj in entityLst)
{
//以供应商代码+奇瑞零件号+工艺编码+工艺版本为唯一数据,做新增或者更新存储
var firstObj = _context.Set<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT>().FirstOrDefault(
@ -100,23 +107,24 @@ namespace Wood.Service.Datas
}
#endregion
scope.Complete();
transaction.Commit();
return Ok(true);
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
}
}
public override async Task<ActionResult<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT>> Create(SUPPLIER_PRO_PROCESS_EQUIPMENT_DT entity)
{
using TransactionScope scope = new TransactionScope();
using var transaction = _context.Database.BeginTransaction();
try
{
#region 数据库操作
//添加任务
TaskSub taskSubObj = await BuildTaskSub(1);
TaskSub taskSubObj = _commonService.BuildTaskSub(1, _taskName);
await _taskSubRepository.AddAsync(taskSubObj);
//添加任务明细
SUPPLIER_PRO_PROCESS_EQUIPMENT empObj = ExpressionGenericMapper<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT, SUPPLIER_PRO_PROCESS_EQUIPMENT>.Trans(entity);
@ -139,23 +147,24 @@ namespace Wood.Service.Datas
}
#endregion
scope.Complete();
transaction.Commit();
return Ok(true);
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
}
}
public override async Task<IActionResult> Update(SUPPLIER_PRO_PROCESS_EQUIPMENT_DT entity)
{
using TransactionScope scope = new TransactionScope();
using var transaction = _context.Database.BeginTransaction();
try
{
#region 数据库操作
//添加任务
TaskSub taskSubObj = await BuildTaskSub(1);
TaskSub taskSubObj = _commonService.BuildTaskSub(1, _taskName);
await _taskSubRepository.AddAsync(taskSubObj);
//添加任务明细
SUPPLIER_PRO_PROCESS_EQUIPMENT empObj = ExpressionGenericMapper<SUPPLIER_PRO_PROCESS_EQUIPMENT_DT, SUPPLIER_PRO_PROCESS_EQUIPMENT>.Trans(entity);
@ -178,51 +187,14 @@ namespace Wood.Service.Datas
}
#endregion
scope.Complete();
transaction.Commit();
return Ok(true);
}
catch (Exception ex)
{
transaction.Rollback();
throw new Exception("方法体执行报错,事务回滚:" + ex.Message);
}
}
/// <summary>
/// 生成TaskSub
/// </summary>
/// <param name="dataCount">任务明细数量</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private async Task<TaskSub> BuildTaskSub(int dataCount)
{
List<TaskConifgure> taskConfigureLst = (await _taskConfigureRepository.GetAllAsync()).ToList();
TaskConifgure? taskConfigureObj = taskConfigureLst.FirstOrDefault(itm => itm.TaskName == "工艺装备");
if (taskConfigureObj == null)
{
throw new Exception("任务配置表没有'工艺装备'任务");
}
TaskSub taskSub = new TaskSub();
taskSub.TableName = taskConfigureObj.TableName; //关联的数据表名称(如:订单表、用户表等,可为空)
taskSub.TaskName = taskConfigureObj.TaskName; //任务名称(用于业务层面标识任务,如:数据同步任务、报表生成任务)
taskSub.DataCount = dataCount; //数据总量(任务处理的数据条目数)
taskSub.Subscriber = taskConfigureObj.Client; //发布给那个客户
taskSub.FailedCount = 0; //失败次数(任务执行失败的累计次数)
taskSub.FailedInfo = ""; //失败详情(记录失败原因、异常堆栈等信息,支持长文本)
taskSub.Domain = ""; //所属域(多租户场景下标识租户,如:租户A、租户B)
taskSub.Site = ""; //站点标识(多站点部署时标识所属站点,如:Site1、Site2)
taskSub.CreateUser = CommonHelper.UserName; //从那个内部系统创建系统
taskSub.CreateTime = CommonHelper.CurrentTime; //创建时间(任务创建的时间戳)
taskSub.Remark = ""; //备注信息(任务相关的补充说明,支持长文本)
taskSub.UpdateUser = ""; //最后更新人(记录任务最后修改者)
taskSub.UpdateTime = null; //最后更新时间(任务最后修改的时间戳,可为空)
taskSub.SyncedPageCount = 0; //已同步页数
taskSub.ReadState = true;
taskSub.CreationTime = CommonHelper.CurrentTime;
taskSub.TaskId = CommonHelper.NewGuid;
return taskSub;
}
}
}

Loading…
Cancel
Save