diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_SA_SERVICE.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_SA_SERVICE.cs index ee4d2f05..76239a8b 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_SA_SERVICE.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/BBAC_SA_SERVICE.cs @@ -400,11 +400,10 @@ public class BBAC_SA_SERVICE : SettleAccountApplicationBase //导入的零件号集合 var importPubSaLUs = bbacSaDetails.Select(t => t.LU).Distinct(); //销售价格 - var priceListEntitys = _priceListRepository.Where(t => importPubSaLUs.Contains(t.LU)).ToList(); + var priceListEntitys = _priceListRepository.Where(t => t.IsCancel == false).Where(t => importPubSaLUs.Contains(t.LU)).ToList(); var materialRelationshipEntitys = await _materialRelationshipRepository.GetListAsync(t => importPubSaLUs.Contains(t.SettleMaterialCode)).ConfigureAwait(false); bbacSaDetails.ForEach(bbacSaDetail => { - //根据物料号、结算日期获取价格 //根据物料号、结算日期获取价格 var priceListEntity = priceListEntitys .Where(t => t.LU == bbacSaDetail.LU) @@ -414,7 +413,7 @@ public class BBAC_SA_SERVICE : SettleAccountApplicationBase .FirstOrDefault(); bbacSaDetail.Price = priceListEntity?.Price ?? 0; - bbacSaDetail.PartCode = materialRelationshipEntitys.FirstOrDefault(t => t.SettleMaterialCode == bbacSaDetail.LU)?.ErpMaterialCode ?? bbacSaDetail.LU.Replace(new string(' ', 6), "-"); + bbacSaDetail.PartCode = materialRelationshipEntitys.FirstOrDefault(t => t.SettleMaterialCode == bbacSaDetail.LU)?.ErpMaterialCode; }); #endregion diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/HBPO_SA_SERVICE.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/HBPO_SA_SERVICE.cs index 6e6a01c7..186c333f 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/HBPO_SA_SERVICE.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/HBPO_SA_SERVICE.cs @@ -370,7 +370,7 @@ public class HBPO_SA_SERVICE : SettleAccountApplicationBase //导入的零件号集合 var importPubSaLUs = hbpoSaDetails.Select(t => t.LU).Distinct(); //销售价格 - var priceListEntitys = _priceListRepository.Where(t => importPubSaLUs.Contains(t.LU)).ToList(); + var priceListEntitys = _priceListRepository.Where(t => t.IsCancel == false).Where(t => importPubSaLUs.Contains(t.LU)).ToList(); var materialRelationshipEntitys = await _materialRelationshipRepository.GetListAsync(t => importPubSaLUs.Contains(t.SettleMaterialCode)).ConfigureAwait(false); hbpoSaDetails.ForEach(hbpoSaDetail => { @@ -383,7 +383,7 @@ public class HBPO_SA_SERVICE : SettleAccountApplicationBase .FirstOrDefault(); hbpoSaDetail.Price = priceListEntity?.Price ?? 0; - hbpoSaDetail.PartCode = materialRelationshipEntitys.FirstOrDefault(t => t.SettleMaterialCode == hbpoSaDetail.LU)?.ErpMaterialCode ?? hbpoSaDetail.LU.Replace(new string(' ', 6), "-"); + hbpoSaDetail.PartCode = materialRelationshipEntitys.FirstOrDefault(t => t.SettleMaterialCode == hbpoSaDetail.LU)?.ErpMaterialCode; }); #endregion diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/PUB_SA_SERVICE.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/PUB_SA_SERVICE.cs index d01a4f99..17361fa7 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/PUB_SA_SERVICE.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/PUB_SA_SERVICE.cs @@ -443,7 +443,8 @@ public class PUB_SA_SERVICE : SettleAccountApplicationBase if (businessType == EnumBusinessType.BeiJian) { //备件销售价格 - var priceListEntitys = _priceBjListRepository.Where(t => lus.Contains(t.LU)).ToList(); + var priceListEntitys = _priceBjListRepository.Where(t => t.IsCancel == false) + .Where(t => lus.Contains(t.LU)).ToList(); pubSaDetails.ForEach(importPubSaDetail => { //根据物料号、结算日期获取价格 @@ -459,7 +460,8 @@ public class PUB_SA_SERVICE : SettleAccountApplicationBase else { //销售价格 - var priceListEntitys = _priceListRepository.Where(t => lus.Contains(t.LU)).ToList(); + var priceListEntitys = _priceListRepository.Where(t => t.IsCancel == false) + .Where(t => lus.Contains(t.LU)).ToList(); pubSaDetails.ForEach(importPubSaDetail => { //根据物料号、结算日期获取价格 @@ -467,6 +469,7 @@ public class PUB_SA_SERVICE : SettleAccountApplicationBase .Where(t => t.LU == importPubSaDetail.LU) .Where(t => importPubSaDetail.SettleDate >= t.BeginTime && importPubSaDetail.SettleDate <= t.EndTime) .OrderByDescending(t => t.Date) + .ThenByDescending(t => t.CreationTime) .FirstOrDefault(); importPubSaDetail.Price = priceListEntity?.Price ?? 0; }); @@ -479,7 +482,7 @@ public class PUB_SA_SERVICE : SettleAccountApplicationBase var seAllMaterialRelationships = materialRelationshipEntitys.Union(materialRelationships); pubSaDetails.ForEach(pubSaDetail => { - pubSaDetail.PartCode = materialRelationshipEntitys.FirstOrDefault(t => t.SettleMaterialCode == pubSaDetail.LU)?.ErpMaterialCode ?? pubSaDetail.LU.Replace(new string(' ', 6), "-"); + pubSaDetail.PartCode = materialRelationshipEntitys.FirstOrDefault(t => t.SettleMaterialCode == pubSaDetail.LU)?.ErpMaterialCode; }); #endregion diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/Prices/PriceListAppService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/Prices/PriceListAppService.cs index 17b50d64..5f80eb94 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/Prices/PriceListAppService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/Prices/PriceListAppService.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using EFCore.BulkExtensions; using Magicodes.ExporterAndImporter.Core; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.Authorization; @@ -14,8 +15,6 @@ using Win.Abp.Snowflakes; using Win.Sfs.BaseData.ImportExcelCommon; using Win.Sfs.SettleAccount.CommonManagers; using Win.Sfs.SettleAccount.Entities.BQ.Dtos; -using Win.Sfs.SettleAccount.Entities.ImportMap; -using Win.Sfs.SettleAccount.Entities.TaskJobs; using Win.Sfs.SettleAccount.ExcelImporter; using Win.Sfs.SettleAccount.ExportReports; using Win.Sfs.Shared.RepositoryBase; @@ -29,26 +28,26 @@ namespace Win.Sfs.SettleAccount.Entities.Prices; [Route("api/settleaccount/[controller]/[action]")] public class PriceListAppService : SettleAccountApplicationBase { + /// + /// 数据上下文 + /// + private readonly SettleAccountDbContext _settleAccountDbContext; /// /// 销售价格仓储 /// private readonly INormalEfCoreRepository _priceListRepository; - private readonly ISettleAccountBranchEfCoreRepository _mapRepository; - private readonly TaskJobService _service; public PriceListAppService( + SettleAccountDbContext settleAccountDbContext, INormalEfCoreRepository priceListRepository, - ISettleAccountBranchEfCoreRepository mapRepository, - TaskJobService service, IDistributedCache cache, IExcelImportAppService excelImportService, ISnowflakeIdGenerator snowflakeIdGenerator, ICommonManager commonManager ) : base(cache, excelImportService, snowflakeIdGenerator, commonManager) { + _settleAccountDbContext = settleAccountDbContext; _priceListRepository = priceListRepository; - _mapRepository = mapRepository; - _service = service; } #region 导入、导出 @@ -69,22 +68,52 @@ public class PriceListAppService : SettleAccountApplicationBase public virtual async Task ImportAsync([FromForm] IFormFileCollection files, string version) { var checkList = new List(); - ExportImporter _exportImporter = new ExportImporter(); + var _exportImporter = new ExportImporter(); var result = await _exportImporter.UploadExcelImportByHeadDesc(files, _excelImportService).ConfigureAwait(false); - List _checkls = new List(); - _checkls.Add("1040"); - _checkls.Add("1046"); - _checkls.Add("104T"); - result = result.Where(p => _checkls.Contains(p.Plant)).ToList(); - result.Where(t => !string.IsNullOrEmpty(t.ES1) || !string.IsNullOrEmpty(t.ES2)).ForEach(t => t.PartNo = t.PartNo + new string(' ', 6) + t.ES1 + t.ES2); + var filter = new List + { + "1040", + "1046", + "104T" + }; + result = result.FindAll(p => filter.Contains(p.Plant)).ToList(); + result.FindAll(t => !string.IsNullOrEmpty(t.ES1) || !string.IsNullOrEmpty(t.ES2)).ForEach(t => t.PartNo = t.PartNo + new string(' ', 6) + t.ES1 + t.ES2); var entityList = ObjectMapper.Map, List>(result); - entityList = entityList.GroupBy(p => new { p.LU, p.ClientCode, p.BeginTime, p.EndTime }).Select(p => p.FirstOrDefault()).ToList(); + var newPrice = entityList.GroupBy(p => p.LU).Select(p => p.FirstOrDefault()).ToList(); - foreach (var item in entityList) + var importLus = newPrice.Select(t => t.LU).Distinct().ToList(); + var oldPrices = _settleAccountDbContext.Set() + .Where(t => t.IsCancel == false) + .Where(t => importLus.Contains(t.LU)) + .ToList(); + //系统中合同日期比导入文件中的合同日期晚 + var oldpriceNewDate = from oldprice in oldPrices + from newprice in newPrice + where oldprice.LU == newprice.LU && oldprice.Date > newprice.Date + select oldprice; + oldPrices.ForEach(t => t.IsCancel = true); + if (oldpriceNewDate.Any()) + { + oldPrices.FindAll(t => oldpriceNewDate.Contains(t)).ForEach(t => t.IsCancel = false); + var importCancelLus = oldPrices.Select(t => t.LU).Distinct(); + newPrice.FindAll(t => importCancelLus.Contains(t.LU)).ForEach(t => t.IsCancel = true); + } + foreach (var item in newPrice) { item.Update(GuidGenerator.Create()); } - await _priceListRepository.InsertManyAsync(entityList).ConfigureAwait(false); + using var transaction = await _settleAccountDbContext.Database.BeginTransactionAsync().ConfigureAwait(false); + try + { + await _settleAccountDbContext.BulkUpdateAsync(oldPrices).ConfigureAwait(false); + await _settleAccountDbContext.BulkInsertAsync(newPrice).ConfigureAwait(false); + await transaction.CommitAsync().ConfigureAwait(false); + } + catch (Exception) + { + await transaction.RollbackAsync().ConfigureAwait(false); + return new JsonResult(new { Code = 200, Message = "导入失败" }); + } return new JsonResult(new { Code = 200, Message = "导入成功" }); } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/Prices/PriceListAppServiceBJ.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/Prices/PriceListAppServiceBJ.cs index 95d1bb46..0d733857 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/Prices/PriceListAppServiceBJ.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/Prices/PriceListAppServiceBJ.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using EFCore.BulkExtensions; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -25,9 +26,14 @@ namespace Win.Sfs.SettleAccount.Entities.Prices; [Route("api/settleaccount/[controller]/[action]")] public class PriceListAppServiceBJ : SettleAccountApplicationBase { + /// + /// 数据上下文 + /// + private readonly SettleAccountDbContext _settleAccountDbContext; private readonly INormalEfCoreRepository _repository; public PriceListAppServiceBJ( + SettleAccountDbContext settleAccountDbContext, INormalEfCoreRepository repository, IDistributedCache cache, IExcelImportAppService excelImportService, @@ -35,6 +41,7 @@ public class PriceListAppServiceBJ : SettleAccountApplicationBase ICommonManager commonManager ) : base(cache, excelImportService, snowflakeIdGenerator, commonManager) { + _settleAccountDbContext = settleAccountDbContext; _repository = repository; } @@ -46,23 +53,53 @@ public class PriceListAppServiceBJ : SettleAccountApplicationBase public async Task ImportAsync([FromForm] IFormFileCollection files, string version) { var checkList = new List(); - ExportImporter _exportImporter = new ExportImporter(); + var _exportImporter = new ExportImporter(); var result = await _exportImporter.UploadExcelImportByHeadDesc(files, _excelImportService).ConfigureAwait(false); - List _checkls = new List(); - _checkls.Add("1040"); - _checkls.Add("1046"); - _checkls.Add("104T"); - result = result.Where(p => _checkls.Contains(p.ClientCode)).ToList(); - + var filter = new List + { + "1040", + "1046", + "104T" + }; + result = result.Where(p => filter.Contains(p.ClientCode)).ToList(); var entityList = ObjectMapper.Map, List>(result); - entityList = entityList.GroupBy(p => new { p.LU, p.ClientCode, p.BeginDate, p.EndDate }).Select(p => p.FirstOrDefault()).ToList(); + var newPrice = entityList.GroupBy(p => p.LU).Select(p => p.FirstOrDefault()).ToList(); - foreach (var item in entityList) + var importLus = newPrice.Select(t => t.LU).Distinct().ToList(); + var oldPrices = _settleAccountDbContext.Set() + .Where(t => t.IsCancel == false) + .Where(t => importLus.Contains(t.LU)) + .ToList(); + //系统中合同日期比导入文件中的合同日期晚 + var oldpriceNewDate = from oldprice in oldPrices + from newprice in newPrice + where oldprice.LU == newprice.LU && oldprice.Date > newprice.Date + select oldprice; + oldPrices.ForEach(t => t.IsCancel = true); + if (oldpriceNewDate.Any()) + { + oldPrices.FindAll(t => oldpriceNewDate.Contains(t)).ForEach(t => t.IsCancel = false); + var importCancelLus = oldPrices.Select(t => t.LU).Distinct(); + newPrice.FindAll(t => importCancelLus.Contains(t.LU)).ForEach(t => t.IsCancel = true); + } + foreach (var item in newPrice) { item.Update(GuidGenerator.Create()); } - await _repository.InsertManyAsync(entityList).ConfigureAwait(false); + using var transaction = await _settleAccountDbContext.Database.BeginTransactionAsync().ConfigureAwait(false); + try + { + await _settleAccountDbContext.BulkUpdateAsync(oldPrices).ConfigureAwait(false); + await _settleAccountDbContext.BulkInsertAsync(newPrice).ConfigureAwait(false); + await transaction.CommitAsync().ConfigureAwait(false); + } + catch (Exception) + { + await transaction.RollbackAsync().ConfigureAwait(false); + return new JsonResult(new { Code = 200, Message = "导入失败" }); + } return new JsonResult(new { Code = 200, Message = "导入成功" }); + } ///