mahao
1 year ago
4 changed files with 464 additions and 3 deletions
@ -0,0 +1,238 @@ |
|||||
|
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; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp; |
||||
|
using Volo.Abp.Application.Dtos; |
||||
|
using Volo.Abp.Caching; |
||||
|
using Win.Abp.Snowflakes; |
||||
|
using Win.Sfs.BaseData.ImportExcelCommon; |
||||
|
using Win.Sfs.SettleAccount.CommonManagers; |
||||
|
using Win.Sfs.SettleAccount.Constant; |
||||
|
using Win.Sfs.SettleAccount.Entities.BQ.Dtos; |
||||
|
using Win.Sfs.SettleAccount.ExcelImporter; |
||||
|
using Win.Sfs.SettleAccount.ExportReports; |
||||
|
using Win.Sfs.Shared.RepositoryBase; |
||||
|
|
||||
|
namespace Win.Sfs.SettleAccount.Entities.Prices |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 印度件价格
|
||||
|
/// </summary>
|
||||
|
[AllowAnonymous] |
||||
|
[Route("api/settleaccount/[controller]/[action]")]
|
||||
|
public class PriceListAppServiceYinDu : SettleAccountApplicationBase<PriceListYinDu> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 数据上下文
|
||||
|
/// </summary>
|
||||
|
private readonly SettleAccountDbContext _settleAccountDbContext; |
||||
|
private readonly INormalEfCoreRepository<PriceListYinDu, Guid> _repository; |
||||
|
|
||||
|
public PriceListAppServiceYinDu( |
||||
|
SettleAccountDbContext settleAccountDbContext, |
||||
|
INormalEfCoreRepository<PriceListYinDu, Guid> repository, |
||||
|
IDistributedCache<PriceListYinDu> cache, |
||||
|
IExcelImportAppService excelImportService, |
||||
|
ISnowflakeIdGenerator snowflakeIdGenerator, |
||||
|
ICommonManager commonManager |
||||
|
) : base(cache, excelImportService, snowflakeIdGenerator, commonManager) |
||||
|
{ |
||||
|
_settleAccountDbContext = settleAccountDbContext; |
||||
|
_repository = repository; |
||||
|
} |
||||
|
|
||||
|
#region 导入、导出
|
||||
|
/// <summary>
|
||||
|
/// 导入
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// <seealso cref="PriceListAppServiceBJ.ImportAsync"/>
|
||||
|
/// </remarks>
|
||||
|
[HttpPost] |
||||
|
public async Task<IActionResult> ImportAsync([FromForm] IFormFileCollection files, string version) |
||||
|
{ |
||||
|
var checkList = new List<ErrorExportDto>(); |
||||
|
var _exportImporter = new ExportImporter(); |
||||
|
var result = await _exportImporter.UploadExcelImportByHeadDesc<PriceListYinDuImportDto>(files, _excelImportService).ConfigureAwait(false); |
||||
|
var filter = new List<string> |
||||
|
{ |
||||
|
"1049" |
||||
|
}; |
||||
|
result = result.Where(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 newPrice = ObjectMapper.Map<List<PriceListYinDuImportDto>, List<PriceListYinDu>>(result); |
||||
|
newPrice = newPrice.GroupBy(p => new { p.Date, p.ClientCode, p.LU, p.BeginDate, p.EndDate }).Select(p => p.FirstOrDefault()).ToList(); |
||||
|
|
||||
|
#region 校验
|
||||
|
if (newPrice.Any()) |
||||
|
{ |
||||
|
var query = from item1 in newPrice |
||||
|
join item2 in newPrice |
||||
|
on new { item1.Date, item1.ClientCode, item1.LU } equals new { item2.Date, item2.ClientCode, item2.LU } |
||||
|
where (item1.BeginDate > item2.BeginDate && item1.EndDate < item2.EndDate) || (item2.BeginDate > item1.BeginDate && item2.EndDate < item1.EndDate) || (item1.BeginDate == item2.BeginDate && item1.EndDate != item2.EndDate) || (item1.BeginDate != item2.BeginDate && item1.EndDate == item2.EndDate) |
||||
|
select item1; |
||||
|
var repeat = query.Distinct().ToList(); |
||||
|
foreach (var item in repeat) |
||||
|
{ |
||||
|
checkList.Add(new ErrorExportDto(string.Empty, string.Empty, string.Empty, string.Empty, item.LU, string.Empty, $"合同号:{item.ContractNo},合同签订时间:{item.Date:yyyy-MM-dd},时间区间存在交集", string.Empty)); |
||||
|
} |
||||
|
|
||||
|
//foreach (var item in CheckPriceListContinuity(newPrice))
|
||||
|
//{
|
||||
|
// checkList.Add(new ErrorExportDto(string.Empty, string.Empty, string.Empty, string.Empty, item.LU, string.Empty, $"合同号:{item.ContractNo},合同签订时间:{item.Date:yyyy-MM-dd},时间区间【{item.BeginDate:yyyy-MM-dd}至{item.EndDate:yyyy-MM-dd}】不连续", string.Empty));
|
||||
|
//}
|
||||
|
} |
||||
|
if (checkList.Count > 0) |
||||
|
{ |
||||
|
var fileName = await ExportErrorReportAsync(checkList).ConfigureAwait(false); |
||||
|
return new JsonResult(new { code = ApplicationConsts.ImportFailCode, message = "导入失败", fileName }); |
||||
|
} |
||||
|
#endregion
|
||||
|
|
||||
|
newPrice.ForEach(t => t.IsCancel = true); |
||||
|
newPrice.GroupBy(t => new { t.ClientCode, t.LU }) |
||||
|
.SelectMany(t => |
||||
|
{ |
||||
|
var data = t.OrderByDescending(t => t.Date).First().Date; |
||||
|
return t.Where(t => t.Date == data); |
||||
|
}) |
||||
|
.ForEach(t => t.IsCancel = false); |
||||
|
|
||||
|
var importLus = newPrice.Select(t => t.LU).Distinct().ToList(); |
||||
|
var oldPrices = _settleAccountDbContext.Set<PriceListYinDu>() |
||||
|
.Where(t => t.IsCancel == false) |
||||
|
.Where(t => importLus.Contains(t.LU)) |
||||
|
.ToList(); |
||||
|
//系统中合同日期比导入文件中的合同日期晚
|
||||
|
var oldPriceNewDate = from oldPriceItem in oldPrices |
||||
|
from newPriceItem in newPrice.FindAll(t => t.IsCancel == false) |
||||
|
where oldPriceItem.ClientCode == newPriceItem.ClientCode && oldPriceItem.LU == newPriceItem.LU && oldPriceItem.Date > newPriceItem.Date |
||||
|
select new { oldPriceItem, newPriceItem }; |
||||
|
oldPrices.ForEach(t => t.IsCancel = true); |
||||
|
if (oldPriceNewDate.Any()) |
||||
|
{ |
||||
|
oldPrices.FindAll(t => t.IsCancel == true && oldPriceNewDate.Select(t => t.oldPriceItem).Contains(t)).ForEach(t => t.IsCancel = false); |
||||
|
newPrice.FindAll(t => t.IsCancel == false && oldPriceNewDate.Select(t => t.newPriceItem).Contains(t)).ForEach(t => t.IsCancel = true); |
||||
|
} |
||||
|
foreach (var item in newPrice) |
||||
|
{ |
||||
|
item.Update(GuidGenerator.Create()); |
||||
|
} |
||||
|
using var transaction = await _settleAccountDbContext.Database.BeginTransactionAsync().ConfigureAwait(false); |
||||
|
try |
||||
|
{ |
||||
|
await _settleAccountDbContext.BulkUpdateAsync<PriceListYinDu>(oldPrices).ConfigureAwait(false); |
||||
|
await _settleAccountDbContext.BulkInsertAsync<PriceListYinDu>(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 = "导入成功" }); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 价格表时间是否连续
|
||||
|
/// </summary>
|
||||
|
private List<PriceListYinDu> CheckPriceListContinuity(List<PriceListYinDu> priceList) |
||||
|
{ |
||||
|
var result = new List<PriceListYinDu>(); |
||||
|
if (priceList.Count <= 1) |
||||
|
{ |
||||
|
return result; // 只有一个或零个价格条目
|
||||
|
} |
||||
|
|
||||
|
var dateGroups = priceList.GroupBy(t => t.Date); |
||||
|
foreach (var dateGroup in dateGroups) |
||||
|
{ |
||||
|
var clientCodeGroups = dateGroup.GroupBy(t => t.ClientCode); |
||||
|
foreach (var clientCodeGroup in clientCodeGroups) |
||||
|
{ |
||||
|
if (clientCodeGroup.ToList().Count <= 1) |
||||
|
{ |
||||
|
continue; |
||||
|
} |
||||
|
var sortedList = clientCodeGroup.OrderBy(t => t.LU).ThenBy(t => t.BeginDate).ToList(); |
||||
|
for (var i = 1; i < sortedList.Count; i++) |
||||
|
{ |
||||
|
if (sortedList[i].LU == sortedList[i - 1].LU && sortedList[i].BeginDate != sortedList[i - 1].EndDate.AddDays(1)) |
||||
|
{ |
||||
|
result.Add(sortedList[i]); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return result; // 所有价格时间都连续
|
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 导出
|
||||
|
/// </summary>
|
||||
|
[HttpPost] |
||||
|
public async Task<string> ExportAsync(RequestDto input) |
||||
|
{ |
||||
|
string fileName = $"印度件价格_{Guid.NewGuid()}.xlsx"; |
||||
|
var entities = await _repository.GetListByFilterAsync(input.Filters, input.Sorting, int.MaxValue, 0, true).ConfigureAwait(false); |
||||
|
var dtos = ObjectMapper.Map<List<PriceListYinDu>, List<PriceListYinDuExportDto>>(entities); |
||||
|
|
||||
|
ExportImporter _exportImporter = new ExportImporter(); |
||||
|
var result = await _exportImporter.ExcelExporter(dtos).ConfigureAwait(false); |
||||
|
result.ShouldNotBeNull(); |
||||
|
|
||||
|
await _excelImportService.SaveBlobAsync(new SaveExcelImportInputDto { Name = fileName, Content = result }).ConfigureAwait(false); |
||||
|
return fileName; |
||||
|
} |
||||
|
#endregion
|
||||
|
|
||||
|
#region CURD
|
||||
|
/// <summary>
|
||||
|
/// 获取列表
|
||||
|
/// </summary>
|
||||
|
[HttpPost] |
||||
|
public async Task<PagedResultDto<PriceListYinDuDto>> GetListAsync(RequestDto input) |
||||
|
{ |
||||
|
var entities = await _repository.GetListByFilterAsync(input.Filters, input.Sorting, input.MaxResultCount, input.SkipCount, true).ConfigureAwait(false); |
||||
|
var totalCount = await _repository.GetCountByFilterAsync(input.Filters).ConfigureAwait(false); |
||||
|
var dtos = ObjectMapper.Map<List<PriceListYinDu>, List<PriceListYinDuDto>>(entities); |
||||
|
return new PagedResultDto<PriceListYinDuDto>(totalCount, dtos); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 修改实体
|
||||
|
/// </summary>
|
||||
|
[HttpPost] |
||||
|
public async Task<PriceListYinDuDto> UpdateAsync(PriceListYinDuUpdateDto input) |
||||
|
{ |
||||
|
var entity = await _settleAccountDbContext.Set<PriceListYinDu>().FindAsync(input.Id).ConfigureAwait(false); |
||||
|
entity.IsCancel = input.IsCancel; |
||||
|
if (entity.IsCancel == false) |
||||
|
{ |
||||
|
var existPrices = _settleAccountDbContext.Set<PriceListYinDu>() |
||||
|
.Where(t => t.LU == entity.LU) |
||||
|
.Where(t => t.IsCancel == false) |
||||
|
.Where(t => t.Id != entity.Id) |
||||
|
.ToList(); |
||||
|
|
||||
|
var existPrice = existPrices.Find(t => (entity.BeginDate >= t.BeginDate && entity.BeginDate < t.EndDate) || (t.BeginDate >= entity.BeginDate && t.BeginDate < entity.EndDate)); |
||||
|
if (existPrice != null) |
||||
|
{ |
||||
|
throw new UserFriendlyException($"无法启用!此记录启用时间区间与区间【{existPrice.BeginDate:yyyy-MM-dd}至{existPrice.EndDate:yyyy-MM-dd}】存在交集", "400"); |
||||
|
} |
||||
|
} |
||||
|
await _settleAccountDbContext.SaveChangesAsync().ConfigureAwait(false); |
||||
|
var dto = ObjectMapper.Map<PriceListYinDu, PriceListYinDuDto>(entity); |
||||
|
return dto; |
||||
|
} |
||||
|
#endregion
|
||||
|
} |
||||
|
} |
Loading…
Reference in new issue