You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

249 lines
8.4 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using EFCore.BulkExtensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using SettleAccount.Domain.BQ;
using Shouldly;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Guids;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Uow;
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.Entities.CodeSettings;
using Win.Sfs.SettleAccount.Entities.ImportMap;
using Win.Sfs.SettleAccount.ExcelImporter;
using Win.Sfs.Shared.CacheBase;
using Win.Sfs.Shared.RepositoryBase;
using Win.Utils;
namespace Win.Sfs.SettleAccount.Entities.CodeSettingTables
{
/// <summary>
/// 通用代码-相关应用服务
/// </summary>
[AllowAnonymous]
[Route("api/settleaccount/[controller]/[action]")]
public class CodeSettingAppService : SettleAccountApplicationBase<CodeSetting>
{
//private readonly ISettleAccountBranchEfCoreRepository<CodeSetting, Guid> _repository;
private readonly INormalEfCoreRepository<CodeSetting, Guid> _repository;
/// <summary>
/// 构建方法
/// </summary>
public CodeSettingAppService(IGuidGenerator guidGenerator,
IObjectMapper objectMapper,
INormalEfCoreRepository<CodeSetting, Guid> repository,
IDistributedCache<CodeSetting> cache,
IExcelImportAppService excelImportService,
ISnowflakeIdGenerator snowflakeIdGenerator,
ICommonManager commonManager
) : base(cache,excelImportService,snowflakeIdGenerator,commonManager)
{
_repository = repository;
}
/// <summary>
/// 导入功能
/// </summary>
[HttpPost]
[DisableRequestSizeLimit]
public async Task<string> CodeSettingUploadExcelImport([FromForm] IFormFileCollection files)
{
ExportImporter _exportImporter = new ExportImporter();
var result = await _exportImporter.UploadExcelImport<CodeSettingImportDto>(files, _excelImportService);
var _ls = ObjectMapper.Map<List<CodeSettingImportDto>, List<CodeSetting>>(result);
foreach (var itm in _ls)
{
var _first = _repository.FirstOrDefault(p => p.Project == itm.Project && p.Value == itm.Value);
if (_first != null)
{
_first.Update(_first.Project, _first.Value, itm.Description);
await _repository.UpdateAsync(_first);
}
else
{
itm.SetId(GuidGenerator.Create(), GuidGenerator.Create());
await _repository.InsertAsync(itm);
}
}
var bulkConfig = new BulkConfig { SetOutputIdentity = true, BatchSize = 10000 };
//_repository.GetDbContext().BulkInsert(_ls, bulkConfig);
return ApplicationConsts.SuccessStr;
}
/// <summary>
/// 按ID获取唯一实体
/// </summary>
[HttpPost]
[Route("{id}")]
virtual public async Task<CodeSettingDto> GetAsync(Guid id)
{
var result = await _repository.GetAsync(id);
var dto = ObjectMapper.Map<CodeSetting, CodeSettingDto>(result);
return dto;
}
private async Task<CodeSetting> GetFromCacheAsync(Guid id)
{
var result = await _repository.GetAsync(id);
return result;
}
/// <summary>
/// 获取列表
/// </summary>
[HttpPost]
public async Task<PagedResultDto<CodeSettingDto>> GetListAsync(RequestDto input)
{
var entities = await _repository.GetListByFilterAsync(input.Filters, input.Sorting, input.MaxResultCount, input.SkipCount, true);
var totalCount = await _repository.GetCountByFilterAsync(input.Filters);
var dtos = ObjectMapper.Map<List<CodeSetting>, List<CodeSettingDto>>(entities);
return new PagedResultDto<CodeSettingDto>(totalCount, dtos);
}
/// <summary>
/// 新增实体
/// </summary>
/// <param name="input">新增实体DTO</param>
/// <returns>实体DTO</returns>
[HttpPost]
virtual public async Task<CodeSettingDto> CreateAsync(CodeSettingCreateDto input)
{
var _first = _repository.Where(p => p.Project == input.Project && p.Value==input.Value).FirstOrDefault();
if (_first != null)
{
throw new BusinessException("001", "已经存数据请修改后创建");
}
var entity = new CodeSetting(
GuidGenerator.Create(),
input.BranchId,
input.Project,
input.Value,
input.Description
);
await _repository.InsertAsync(entity);
//create cache
//await Cache.SetAsync(entity.Id.ToString(), entity,CacheStrategyConst.FIVE_MINUTES);
var dto = ObjectMapper.Map<CodeSetting, CodeSettingDto>(entity);
return dto;
}
[HttpPost]
virtual public async Task<string> ExportAsync(CodeSettingRequestDto input)
{
string _fileName = string.Format("通用代码设置_{0}.xlsx",DateTime.Now.ToString("yyyyMMdd"));
var entities = await _repository.GetListByFilterAsync(input.Filters, input.Sorting, int.MaxValue,
0, true);
var dtoDetails = ObjectMapper.Map<List<CodeSetting>, List<CodeSettingExportDto>>(entities);
//声明导出容器
ExportImporter _exportImporter = new ExportImporter();
var result = await _exportImporter.ExcelExporter(dtoDetails);
result.ShouldNotBeNull();
//保存导出文件到服务器存成二进制
await _excelImportService.SaveBlobAsync(
new SaveExcelImportInputDto
{
Name = _fileName,
Content = result
}
);
return _fileName;
}
/// <summary>
/// 修改实体
/// </summary>
/// <param name="id">ID</param>
/// <param name="input">修改实体DTO</param>
/// <returns>实体DTO</returns>
[HttpPost]
[Route("{id}")]
virtual public async Task<CodeSettingDto> UpdateAsync(Guid id, CodeSettingUpdateDto input)
{
var _first = _repository.Where(p => p.Project == input.Project && p.Value == input.Value).FirstOrDefault();
if (_first != null)
{
throw new BusinessException("001", "已经存数据请修改!");
}
var entity = await _repository.GetAsync(id);
entity.Update(input.Project, input.Value, input.Description);
await _repository.UpdateAsync(entity);
//update cache
//await Cache.SetAsync(id.ToString(), entity, CacheStrategyConst.FIVE_MINUTES);
var dto = ObjectMapper.Map<CodeSetting, CodeSettingDto>(entity);
return dto;
}
/// <summary>
/// 删除实体
/// </summary>
/// <param name="id">ID</param>
/// <returns>无</returns>
[HttpPost]
[Route("{id}")]
virtual public async Task DeleteAsync(Guid id)
{
var entity = await GetFromCacheAsync(id);
await Cache.DeleteAsync<CodeSetting>(id.ToString());
await _repository.DeleteAsync(id);
}
/// <summary>
/// 按IDs删除实体列表
/// </summary>
/// <param name="ids">IDs</param>
/// <returns>是否执行成功</returns>
[HttpPost]
virtual public async Task<bool> DeleteListAsync(List<Guid> ids)
{
var _query = _repository.Where(p => ids.Contains(p.Id));
int i= await _query.BatchDeleteAsync();
if (i == 0)
{
return false;
}
return true;
}
}
}