From 00f16e91ddc1f9c912b0277fe7b5c8867115f29f Mon Sep 17 00:00:00 2001 From: me Date: Thu, 5 Jun 2025 08:47:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90datas4=E4=B8=AA=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=EF=BC=8C=E4=BB=BB=E5=8A=A1=20=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=98=8E=E7=BB=86=E5=85=A5=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/NormalBaseController.cs | 12 +- .../Datas/SupplierEmployeeDtService.cs | 180 +++++++++++++++++- .../Datas/SupplierInfoDtService.cs | 177 ++++++++++++++++- .../SupplierProAttachmentDataDtService.cs | 107 ++++++++++- .../SupplierProProcessEquipmentDtService.cs | 171 ++++++++++++++++- API/Wood.Util/CommonHelper.cs | 99 ++++++++++ 6 files changed, 718 insertions(+), 28 deletions(-) create mode 100644 API/Wood.Util/CommonHelper.cs diff --git a/API/Wood.Service/Controllers/NormalBaseController.cs b/API/Wood.Service/Controllers/NormalBaseController.cs index 6d434d9..1e23ea5 100644 --- a/API/Wood.Service/Controllers/NormalBaseController.cs +++ b/API/Wood.Service/Controllers/NormalBaseController.cs @@ -49,7 +49,7 @@ namespace Wood.Service.Controllers /// /// [HttpGet("{id}")] - public async Task> GetById(int id) + public virtual async Task> GetById(int id) { var entity = await _repository.GetByIdAsync(id); if (entity == null) return NotFound(); @@ -62,7 +62,7 @@ namespace Wood.Service.Controllers /// /// [HttpPost] - public async Task> Create(T entity) + public virtual async Task> Create(T entity) { entity.CreationTime = DateTime.Now; var createdEntity = await _repository.AddAsync(entity); @@ -74,7 +74,7 @@ namespace Wood.Service.Controllers /// /// [HttpPut("{id}")] - public async Task Update(T entity) + public virtual async Task Update(T entity) { var _first = await _repository.GetByIdAsync(entity.UId); if (_first == null) @@ -92,7 +92,7 @@ namespace Wood.Service.Controllers /// /// [HttpDelete("{id}")] - public async Task Delete(int id) + public virtual async Task Delete(int id) { await _repository.DeleteAsync(id); return new JsonResult(new { Code = 200, Message = "删除成功!" }); ; @@ -111,7 +111,7 @@ namespace Wood.Service.Controllers /// /// [HttpGet] - public async Task GetPaged( + public virtual async Task GetPaged( [FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 10, [FromQuery] string sortBy = "", @@ -145,7 +145,7 @@ namespace Wood.Service.Controllers /// [HttpGet] - public async Task Export([FromQuery] int pageNumber = 1, + public virtual async Task Export([FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 10, [FromQuery] string sortBy = "", [FromQuery] bool isAscending = true, diff --git a/API/Wood.Service/Datas/SupplierEmployeeDtService.cs b/API/Wood.Service/Datas/SupplierEmployeeDtService.cs index a8a2029..86ed989 100644 --- a/API/Wood.Service/Datas/SupplierEmployeeDtService.cs +++ b/API/Wood.Service/Datas/SupplierEmployeeDtService.cs @@ -14,16 +14,23 @@ using TaskManager.EntityFramework.Repository; using TaskManager.EntityFramework; using Wood.Service.Controllers; using Magicodes.ExporterAndImporter.Core.Models; +using Wood.Util; +using static Dapper.SqlMapper; +using System.Transactions; namespace Wood.Service.Datas { - - public class SupplierEmployeeDtService : NormalBaseController { + private readonly IRepository _taskSubRepository; + private readonly IRepository _supplierEmployeeRepository; + private readonly IRepository _taskConfigureRepository; - public SupplierEmployeeDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository) : base(context, builder, configuration, repository) + public SupplierEmployeeDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository, IRepository taskSubRepository, IRepository supplierEmployeeRepository, IRepository taskConfigureRepository) : base(context, builder, configuration, repository) { + _taskSubRepository = taskSubRepository; + _supplierEmployeeRepository = supplierEmployeeRepository; + _taskConfigureRepository = taskConfigureRepository; } [HttpPost] @@ -54,12 +61,171 @@ namespace Wood.Service.Datas } throw new Exception(sb.ToString()); } - foreach (var item in impResult.Data) //??增加批量插入 + var empDtLst = impResult.Data; + + using TransactionScope scope = new TransactionScope(); + try { - await Create(item); + #region 数据库操作 + //添加任务 + TaskSub taskSubObj = await BuildTaskSub(empDtLst.Count); + await _taskSubRepository.AddAsync(taskSubObj); + //添加任务明细 + foreach (var empDtObj in empDtLst) + { + SUPPLIER_EMPLOYEE empObj = ExpressionGenericMapper.Trans(empDtObj); + empObj.TaskId = taskSubObj.TaskId; + await _supplierEmployeeRepository.AddAsync(empObj); + } + + foreach (var empDtObj in empDtLst) + { + //以“供应商代码+工厂代码+车间代码+产线代码+工位代码+操作人员账号”为唯一标识,做新增或者更新存储 + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == empDtObj.SupplierCode + && itm.PlantId == empDtObj.PlantId + && itm.WorkshopId == empDtObj.WorkshopId + && itm.ProductionLineId == empDtObj.ProductionLineId + && itm.StationId == empDtObj.StationId + && itm.OperatorId == empDtObj.OperatorId + ); + if (firstObj == null) + { + var ret = await base.Create(empDtObj); + } + else + { + var ret = await base.Update(empDtObj); + } + } + #endregion + + scope.Complete(); + return Ok(true); + } + catch (Exception ex) + { + throw new Exception("方法体执行报错,事务回滚:" + ex.Message); } - return Ok(true); } - } + public override async Task> Create(SUPPLIER_EMPLOYEE_DT entity) + { + using TransactionScope scope = new TransactionScope(); + try + { + #region 数据库操作 + //添加任务 + TaskSub taskSubObj = await BuildTaskSub(1); + await _taskSubRepository.AddAsync(taskSubObj); + //添加任务明细 + SUPPLIER_EMPLOYEE empObj = ExpressionGenericMapper.Trans(entity); + empObj.TaskId = taskSubObj.TaskId; + await _supplierEmployeeRepository.AddAsync(empObj); + + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.PlantId == entity.PlantId + && itm.WorkshopId == entity.WorkshopId + && itm.ProductionLineId == entity.ProductionLineId + && itm.StationId == entity.StationId + && itm.OperatorId == entity.OperatorId + ); + if (firstObj == null) + { + var ret = await base.Create(entity); + } + else + { + throw new Exception("数据库已经存在,不能重复插入"); + } + #endregion + + scope.Complete(); + return Ok(true); + } + catch (Exception ex) + { + throw new Exception("方法体执行报错,事务回滚:" + ex.Message); + } + } + + public override async Task Update(SUPPLIER_EMPLOYEE_DT entity) + { + using TransactionScope scope = new TransactionScope(); + try + { + #region 数据库操作 + //添加任务 + TaskSub taskSubObj = await BuildTaskSub(1); + await _taskSubRepository.AddAsync(taskSubObj); + //添加任务明细 + SUPPLIER_EMPLOYEE empObj = ExpressionGenericMapper.Trans(entity); + empObj.TaskId = taskSubObj.TaskId; + await _supplierEmployeeRepository.AddAsync(empObj); + + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.PlantId == entity.PlantId + && itm.WorkshopId == entity.WorkshopId + && itm.ProductionLineId == entity.ProductionLineId + && itm.StationId == entity.StationId + && itm.OperatorId == entity.OperatorId + ); + if (firstObj == null) + { + throw new Exception("数据库不存在,不能更新"); + } + else + { + var ret = await base.Update(entity); + } + #endregion + + scope.Complete(); + return Ok(true); + } + catch (Exception ex) + { + throw new Exception("方法体执行报错,事务回滚:" + ex.Message); + } + } + + /// + /// 生成TaskSub + /// + /// 任务明细数量 + /// + /// + private async Task BuildTaskSub(int dataCount) + { + List 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; + } + } } diff --git a/API/Wood.Service/Datas/SupplierInfoDtService.cs b/API/Wood.Service/Datas/SupplierInfoDtService.cs index 9f9dcc0..aa31203 100644 --- a/API/Wood.Service/Datas/SupplierInfoDtService.cs +++ b/API/Wood.Service/Datas/SupplierInfoDtService.cs @@ -14,15 +14,22 @@ using TaskManager.EntityFramework.Repository; using TaskManager.EntityFramework; using Wood.Service.Controllers; using Magicodes.ExporterAndImporter.Core.Models; +using System.Transactions; +using Wood.Util; namespace Wood.Service.Datas { - - public class SupplierInfoDtService : NormalBaseController { - public SupplierInfoDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository) : base(context, builder, configuration, repository) + private readonly IRepository _taskSubRepository; + private readonly IRepository _supplierInfoRepository; + private readonly IRepository _taskConfigureRepository; + + public SupplierInfoDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository, IRepository taskSubRepository, IRepository supplierInfoRepository, IRepository taskConfigureRepository) : base(context, builder, configuration, repository) { + _taskSubRepository = taskSubRepository; + _supplierInfoRepository = supplierInfoRepository; + _taskConfigureRepository = taskConfigureRepository; } [HttpPost] @@ -53,12 +60,168 @@ namespace Wood.Service.Datas } throw new Exception(sb.ToString()); } - foreach (var item in impResult.Data) //??增加批量插入 + var empDtLst = impResult.Data; + + using TransactionScope scope = new TransactionScope(); + try + { + #region 数据库操作 + //添加任务 + TaskSub taskSubObj = await BuildTaskSub(empDtLst.Count); + await _taskSubRepository.AddAsync(taskSubObj); + //添加任务明细 + foreach (var empDtObj in empDtLst) + { + SUPPLIER_INFO empObj = ExpressionGenericMapper.Trans(empDtObj); + empObj.TaskId = taskSubObj.TaskId; + await _supplierInfoRepository.AddAsync(empObj); + } + + foreach (var empDtObj in empDtLst) + { + //以“供应商代码+工厂代码+车间代码+产线代码+工位代码”为唯一标识,做新增或更新存储 + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == empDtObj.SupplierCode + && itm.PlantId == empDtObj.PlantId + && itm.WorkshopId == empDtObj.WorkshopId + && itm.ProductionLineId == empDtObj.ProductionLineId + && itm.StationId == empDtObj.StationId + ); + if (firstObj == null) + { + var ret = await base.Create(empDtObj); + } + else + { + var ret = await base.Update(empDtObj); + } + } + #endregion + + scope.Complete(); + return Ok(true); + } + catch (Exception ex) { - await Create(item); + throw new Exception("方法体执行报错,事务回滚:" + ex.Message); } - return Ok(true); } - } + public override async Task> Create(SUPPLIER_INFO_DT entity) + { + using TransactionScope scope = new TransactionScope(); + try + { + #region 数据库操作 + //添加任务 + TaskSub taskSubObj = await BuildTaskSub(1); + await _taskSubRepository.AddAsync(taskSubObj); + //添加任务明细 + SUPPLIER_INFO empObj = ExpressionGenericMapper.Trans(entity); + empObj.TaskId = taskSubObj.TaskId; + await _supplierInfoRepository.AddAsync(empObj); + + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.PlantId == entity.PlantId + && itm.WorkshopId == entity.WorkshopId + && itm.ProductionLineId == entity.ProductionLineId + && itm.StationId == entity.StationId + ); + if (firstObj == null) + { + var ret = await base.Create(entity); + } + else + { + throw new Exception("数据库已经存在,不能重复插入"); + } + #endregion + + scope.Complete(); + return Ok(true); + } + catch (Exception ex) + { + throw new Exception("方法体执行报错,事务回滚:" + ex.Message); + } + } + + public override async Task Update(SUPPLIER_INFO_DT entity) + { + using TransactionScope scope = new TransactionScope(); + try + { + #region 数据库操作 + //添加任务 + TaskSub taskSubObj = await BuildTaskSub(1); + await _taskSubRepository.AddAsync(taskSubObj); + //添加任务明细 + SUPPLIER_INFO empObj = ExpressionGenericMapper.Trans(entity); + empObj.TaskId = taskSubObj.TaskId; + await _supplierInfoRepository.AddAsync(empObj); + + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.PlantId == entity.PlantId + && itm.WorkshopId == entity.WorkshopId + && itm.ProductionLineId == entity.ProductionLineId + && itm.StationId == entity.StationId + ); + if (firstObj == null) + { + throw new Exception("数据库不存在,不能更新"); + } + else + { + var ret = await base.Update(entity); + } + #endregion + + scope.Complete(); + return Ok(true); + } + catch (Exception ex) + { + throw new Exception("方法体执行报错,事务回滚:" + ex.Message); + } + } + + /// + /// 生成TaskSub + /// + /// 任务明细数量 + /// + /// + private async Task BuildTaskSub(int dataCount) + { + List 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; + } + } } diff --git a/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs b/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs index 8283525..d9ba37c 100644 --- a/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs +++ b/API/Wood.Service/Datas/SupplierProAttachmentDataDtService.cs @@ -15,6 +15,8 @@ using TaskManager.EntityFramework; using Wood.Service.Controllers; using Magicodes.ExporterAndImporter.Core.Models; using TaskManager.Entity.Entitys; +using System.Transactions; +using Wood.Util; namespace Wood.Service.Datas { @@ -22,8 +24,15 @@ namespace Wood.Service.Datas public class SupplierProAttachmentDataDtService : NormalBaseController { - public SupplierProAttachmentDataDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository) : base(context, builder, configuration, repository) + private readonly IRepository _taskSubRepository; + private readonly IRepository _supplierProAttachmentDataRepository; + private readonly IRepository _taskConfigureRepository; + + public SupplierProAttachmentDataDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository, IRepository taskSubRepository, IRepository supplierProAttachmentDataRepository, IRepository taskConfigureRepository) : base(context, builder, configuration, repository) { + _taskSubRepository = taskSubRepository; + _supplierProAttachmentDataRepository = supplierProAttachmentDataRepository; + _taskConfigureRepository = taskConfigureRepository; } [HttpPost] @@ -54,11 +63,101 @@ namespace Wood.Service.Datas } throw new Exception(sb.ToString()); } - foreach (var item in impResult.Data) //??增加批量插入 + var empDtLst = impResult.Data; + + using TransactionScope scope = new TransactionScope(); + try + { + #region 数据库操作 + //添加任务 + TaskSub taskSubObj = await BuildTaskSub(empDtLst.Count); + await _taskSubRepository.AddAsync(taskSubObj); + //添加任务明细 + foreach (var empDtObj in empDtLst) + { + SUPPLIER_PRO_ATTACHMENT_DATA empObj = ExpressionGenericMapper.Trans(empDtObj); + empObj.TaskId = taskSubObj.TaskId; + await _supplierProAttachmentDataRepository.AddAsync(empObj); + } + + foreach (var empDtObj in empDtLst) + { + //存储新增数据 + var ret = await base.Create(empDtObj); + } + #endregion + + scope.Complete(); + return Ok(true); + } + catch (Exception ex) { - await Create(item); + throw new Exception("方法体执行报错,事务回滚:" + ex.Message); } - return Ok(true); + } + + public override async Task> Create(SUPPLIER_PRO_ATTACHMENT_DATA_DT entity) + { + using TransactionScope scope = new TransactionScope(); + try + { + #region 数据库操作 + //添加任务 + TaskSub taskSubObj = await BuildTaskSub(1); + await _taskSubRepository.AddAsync(taskSubObj); + //添加任务明细 + SUPPLIER_PRO_ATTACHMENT_DATA empObj = ExpressionGenericMapper.Trans(entity); + empObj.TaskId = taskSubObj.TaskId; + await _supplierProAttachmentDataRepository.AddAsync(empObj); + + var ret = await base.Create(entity); + + #endregion + + scope.Complete(); + return Ok(true); + } + catch (Exception ex) + { + throw new Exception("方法体执行报错,事务回滚:" + ex.Message); + } + } + + /// + /// 生成TaskSub + /// + /// 任务明细数量 + /// + /// + private async Task BuildTaskSub(int dataCount) + { + List 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; } } diff --git a/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs b/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs index 6029e51..e86d90d 100644 --- a/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs +++ b/API/Wood.Service/Datas/SupplierProProcessEquipmentDtService.cs @@ -15,6 +15,8 @@ using TaskManager.EntityFramework; using Wood.Service.Controllers; using Magicodes.ExporterAndImporter.Core.Models; using TaskManager.Entity.Entitys; +using System.Transactions; +using Wood.Util; namespace Wood.Service.Datas { @@ -22,8 +24,15 @@ namespace Wood.Service.Datas public class SupplierProProcessEquipmentDtService : NormalBaseController { - public SupplierProProcessEquipmentDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository) : base(context, builder, configuration, repository) + private readonly IRepository _taskSubRepository; + private readonly IRepository _supplierProProcessEquipmentRepository; + private readonly IRepository _taskConfigureRepository; + + public SupplierProProcessEquipmentDtService(JobDbContext context, IServiceProvider builder, IConfiguration configuration, IRepository repository, IRepository taskSubRepository, IRepository supplierProProcessEquipmentRepository, IRepository taskConfigureRepository) : base(context, builder, configuration, repository) { + _taskSubRepository = taskSubRepository; + _supplierProProcessEquipmentRepository = supplierProProcessEquipmentRepository; + _taskConfigureRepository = taskConfigureRepository; } [HttpPost] @@ -54,11 +63,165 @@ namespace Wood.Service.Datas } throw new Exception(sb.ToString()); } - foreach (var item in impResult.Data) //??增加批量插入 + var empDtLst = impResult.Data; + + using TransactionScope scope = new TransactionScope(); + try + { + #region 数据库操作 + //添加任务 + TaskSub taskSubObj = await BuildTaskSub(empDtLst.Count); + await _taskSubRepository.AddAsync(taskSubObj); + //添加任务明细 + foreach (var empDtObj in empDtLst) + { + SUPPLIER_PRO_PROCESS_EQUIPMENT empObj = ExpressionGenericMapper.Trans(empDtObj); + empObj.TaskId = taskSubObj.TaskId; + await _supplierProProcessEquipmentRepository.AddAsync(empObj); + } + + foreach (var empDtObj in empDtLst) + { + //以供应商代码+奇瑞零件号+工艺编码+工艺版本为唯一数据,做新增或者更新存储 + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == empDtObj.SupplierCode + && itm.CheryProductNo == empDtObj.CheryProductNo + && itm.DeviceCode == empDtObj.DeviceCode + && itm.DeviceType == empDtObj.DeviceType + ); + if (firstObj == null) + { + var ret = await base.Create(empDtObj); + } + else + { + var ret = await base.Update(empDtObj); + } + } + #endregion + + scope.Complete(); + return Ok(true); + } + catch (Exception ex) + { + throw new Exception("方法体执行报错,事务回滚:" + ex.Message); + } + } + + public override async Task> Create(SUPPLIER_PRO_PROCESS_EQUIPMENT_DT entity) + { + using TransactionScope scope = new TransactionScope(); + try { - await Create(item); + #region 数据库操作 + //添加任务 + TaskSub taskSubObj = await BuildTaskSub(1); + await _taskSubRepository.AddAsync(taskSubObj); + //添加任务明细 + SUPPLIER_PRO_PROCESS_EQUIPMENT empObj = ExpressionGenericMapper.Trans(entity); + empObj.TaskId = taskSubObj.TaskId; + await _supplierProProcessEquipmentRepository.AddAsync(empObj); + + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.CheryProductNo == entity.CheryProductNo + && itm.DeviceCode == entity.DeviceCode + && itm.DeviceType == entity.DeviceType + ); + if (firstObj == null) + { + var ret = await base.Create(entity); + } + else + { + throw new Exception("数据库已经存在,不能重复插入"); + } + #endregion + + scope.Complete(); + return Ok(true); } - return Ok(true); + catch (Exception ex) + { + throw new Exception("方法体执行报错,事务回滚:" + ex.Message); + } + } + + public override async Task Update(SUPPLIER_PRO_PROCESS_EQUIPMENT_DT entity) + { + using TransactionScope scope = new TransactionScope(); + try + { + #region 数据库操作 + //添加任务 + TaskSub taskSubObj = await BuildTaskSub(1); + await _taskSubRepository.AddAsync(taskSubObj); + //添加任务明细 + SUPPLIER_PRO_PROCESS_EQUIPMENT empObj = ExpressionGenericMapper.Trans(entity); + empObj.TaskId = taskSubObj.TaskId; + await _supplierProProcessEquipmentRepository.AddAsync(empObj); + + var firstObj = _context.Set().FirstOrDefault( + itm => itm.SupplierCode == entity.SupplierCode + && itm.CheryProductNo == entity.CheryProductNo + && itm.DeviceCode == entity.DeviceCode + && itm.DeviceType == entity.DeviceType + ); + if (firstObj == null) + { + throw new Exception("数据库不存在,不能更新"); + } + else + { + var ret = await base.Update(entity); + } + #endregion + + scope.Complete(); + return Ok(true); + } + catch (Exception ex) + { + throw new Exception("方法体执行报错,事务回滚:" + ex.Message); + } + } + + /// + /// 生成TaskSub + /// + /// 任务明细数量 + /// + /// + private async Task BuildTaskSub(int dataCount) + { + List 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; } } diff --git a/API/Wood.Util/CommonHelper.cs b/API/Wood.Util/CommonHelper.cs new file mode 100644 index 0000000..3736a81 --- /dev/null +++ b/API/Wood.Util/CommonHelper.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace Wood.Util +{ + public static class CommonHelper + { + public static Guid NewGuid + { + get + { + return Guid.NewGuid(); + } + } + + public static string NewGuidStr + { + get + { + return Guid.NewGuid().ToString(); + } + } + + public static DateTime CurrentTime + { + get + { + return DateTime.Now; + } + } + + public static string CurrentTimeStr + { + get + { + DateTime dt = CurrentTime; + return dt.ToString("yyyy-MM-dd HH:mm:ss"); + } + } + + public static string CurrentMachineName + { + get + { + return Environment.MachineName; + } + } + + public static string UserName + { + get + { + return "Admin"; + } + } + } + + /// + /// Expression动态拼接+泛型缓存 + /// + /// + /// + public class ExpressionGenericMapper//Mapper`2 + { + private static Func _FUNC = null; + static ExpressionGenericMapper() + { + ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "p"); + List memberBindingList = new List(); + foreach (var item in typeof(TOut).GetProperties()) + { + MemberExpression property = Expression.Property(parameterExpression, typeof(TIn).GetProperty(item.Name)); + MemberBinding memberBinding = Expression.Bind(item, property); + memberBindingList.Add(memberBinding); + } + foreach (var item in typeof(TOut).GetFields()) + { + MemberExpression property = Expression.Field(parameterExpression, typeof(TIn).GetField(item.Name)); + MemberBinding memberBinding = Expression.Bind(item, property); + memberBindingList.Add(memberBinding); + } + MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New(typeof(TOut)), memberBindingList.ToArray()); + Expression> lambda = Expression.Lambda>(memberInitExpression, new ParameterExpression[] + { + parameterExpression + }); + _FUNC = lambda.Compile();//拼装是一次性的 + } + public static TOut Trans(TIn t) + { + return _FUNC(t); + } + } +}