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.
 
 
 
 
 
 

351 lines
14 KiB

using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Csv;
using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskJob.EventArgs;
using TaskJob.Services;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Win.Sfs.BaseData.ImportExcelCommon;
using Win.Sfs.SettleAccount.Constant;
using Win.Sfs.SettleAccount.Entities.ImportMap;
using Win.Sfs.SettleAccount.Entities.TaskJobs;
using Win.Sfs.SettleAccount.ExcelImporter;
using Win.Sfs.SettleAccount.MaterialRelationships;
using Win.Sfs.Shared.Filter;
namespace Win.Sfs.SettleAccount.Entities.MaterialRelationships
{
/// <summary>
/// 总成与结算件关系
/// </summary>
//[AllowAnonymous]
//[Authorize(SettleAccountPermissions.MaterialRelationships.Default)]
[ApiExplorerSettings(IgnoreApi = true)]
[Route("api/settleaccount/MaterialRelationshipDetail")]
public class MaterialRelationshipTHAppService : ApplicationService
/*, IMaterialRelationshipTHAppService*/
{
private readonly MaterialRelationshipManagerTH _mng;
private readonly IExcelImportAppService _excelImportService;
private readonly ISettleAccountBranchEfCoreRepository<ImportColumnMap, Guid> _mapRepository;
private readonly TaskJobService _service;
public MaterialRelationshipTHAppService(
IExcelImportAppService excelImportService,
ISettleAccountBranchEfCoreRepository<ImportColumnMap, Guid> mapRepository,
MaterialRelationshipManagerTH mng,
TaskJobService service
)
{
_mapRepository = mapRepository;
_excelImportService = excelImportService;
_service = service;
_mng = mng;
}
/// <summary>
/// 按ID获取唯一实体
/// </summary>
/// <remarks>
/// 返回实体全部属性
/// </remarks>
/// <param name="id">ID</param>
/// <returns>实体DTO</returns>
[HttpGet]
[Route("{id}")]
//[Authorize(SettleAccountPermissions.MaterialRelationships.Default)]
virtual public async Task<MaterialRelationshipDetailDto> GetAsync(Guid id)
{
var result = await _mng.GetAsync(id); ;
var dto = ObjectMapper.Map<MaterialRelationshipDetail, MaterialRelationshipDetailDto>(result);
return dto;
}
/// <summary>
/// 导入功能
/// </summary>
/// <param name="files">上传的文件(前端已经限制只能上传一个附件)</param>
/// <returns></returns>
[HttpPost]
[Route("ExcelImport-TH")]
[DisableRequestSizeLimit]
//[Authorize(SettleAccountPermissions.EstimatedInventoryDetails.Default)]
public async Task<string> EstimatedInventoryDetailUploadExcelImportTH([FromForm] IFormFileCollection files, Guid branchId, string year, string period, string version, string customerCode, string factory)
{
List<CustomCondition> customConditionList = new List<CustomCondition>();
customConditionList.Add(new CustomCondition() { Name = "Version", Value = version ?? string.Empty });
customConditionList.Add(new CustomCondition() { Name = "CustomerCode", Value = customerCode ?? string.Empty });
customConditionList.Add(new CustomCondition() { Name = "Year", Value = year ?? string.Empty });
customConditionList.Add(new CustomCondition() { Name = "Period", Value = period ?? string.Empty });
customConditionList.Add(new CustomCondition() { Name = "Factory", Value = factory ?? string.Empty });
customConditionList.Add(new CustomCondition() { Name = "Site", Value = "TH" });
var _taskid = await _service.ImportEnqueueAsync<ImportTaskArgs>(files, "总成结算零件关系表", CurrentUser, typeof(SecRelationShipImportService), customConditionList, (rs) =>
{
});
return _taskid;
}
/// <summary>
/// 根据筛选条件获取实体列表
/// </summary>
/// <remarks>
/// 请求条件包括:筛选条件列表,排序条件,数据数量,页码
/// </remarks>
/// <param name="input">请求条件</param>
/// <returns>实体DTO列表</returns>
[HttpPost]
[Route("list")]
//[Authorize(SettleAccountPermissions.MaterialRelationships.Default)]
virtual public async Task<PagedResultDto<MaterialRelationshipDetailDto>> GetListAsync(MaterialRelationshipDetailRequestDto input)
{
if (!string.IsNullOrEmpty(input.Version))
{
input.Filters.Add(new FilterCondition() { Action = EnumFilterAction.Equal, Column = "Version", Logic = EnumFilterLogic.And, Value = input.Version });
}
if (!string.IsNullOrEmpty(input.CustomerCode))
{
input.Filters.Add(new FilterCondition() { Action = EnumFilterAction.Equal, Column = "CustomerCode", Logic = EnumFilterLogic.And, Value = input.CustomerCode });
}
var entities = await _mng.GetListAsync(input.Filters, input.Sorting, input.MaxResultCount,
input.SkipCount);
var totalCount = await GetCountAsync(input);
var dtos = ObjectMapper.Map<List<MaterialRelationshipDetail>, List<MaterialRelationshipDetailDto>>(entities);
return new PagedResultDto<MaterialRelationshipDetailDto>(totalCount, dtos);
}
/// <summary>
/// 根据筛选条件获取实体列表
/// </summary>
/// <remarks>
/// 请求条件包括:筛选条件列表,排序条件,数据数量,页码
/// </remarks>
/// <param name="input">请求条件</param>
/// <returns>实体DTO列表</returns>
[HttpPost]
[Route("versionlist")]
//[Authorize(SettleAccountPermissions.MaterialRelationships.Default)]
virtual public async Task<PagedResultDto<MaterialRelationshipVersionDto>> GetVersionListAsync(MaterialRelationshipDetailRequestDto input)
{
var entities = await _mng.GetVersionListAsync(input.Filters, input.Sorting, input.MaxResultCount, input.SkipCount);
var totalCount = await GetCountAsync(input);
var dtos = ObjectMapper.Map<List<MaterialRelationshipVersion>, List<MaterialRelationshipVersionDto>>(entities);
return new PagedResultDto<MaterialRelationshipVersionDto>(totalCount, dtos);
}
private async Task<long> GetCountAsync(MaterialRelationshipDetailRequestDto input)
{
return await _mng.GetCountAsync(input.Filters, GuidGenerator.Create());
}
/// <summary>
/// 获取实体总数
/// </summary>
/// <returns>实体总数</returns>
[HttpGet]
[Route("count")]
//[Authorize(SettleAccountPermissions.MaterialRelationships.Default)]
virtual public async Task<long> GetTotalCountAsync(Guid branchId)
{
return await _mng.GetCountAsync(new List<FilterCondition>(), branchId);
}
/// <summary>
/// 获取全部实体列表
/// </summary>
/// <returns>实体DTO列表</returns>
[HttpGet]
[Route("all")]
//[Authorize(SettleAccountPermissions.MaterialRelationships.Default)]
virtual public async Task<ListResultDto<MaterialRelationshipDetailDto>> GetAllAsync(Guid branchId)
{
var entities = await _mng.GetAllAsync(branchId);
var dtos = ObjectMapper.Map<List<MaterialRelationshipDetail>, List<MaterialRelationshipDetailDto>>(entities);
return new ListResultDto<MaterialRelationshipDetailDto>(dtos);
}
/// <summary>
/// 批量导入实体列表
/// </summary>
/// <remarks>
/// 以ID为依据,数据库中找不到ID的实体会新增,已有ID的实体会修改
/// </remarks>
/// <param name="entities">实体列表</param>
/// <returns>是否导入成功</returns>
//[HttpPost]
//[Route("ExcelImport-Map")]
//[Authorize(SettleAccountPermissions.MaterialRelationships.Create)]
//virtual public async Task<string> ImportAsync([FromForm] IFormFileCollection files, string version)
//{
// ExportImporter _exportImporter = new ExportImporter();
// var _mapList = _mapRepository.Where(p => p.ProjectName == SettleAccountModuleName.MaterialRelationshipDetail).ToList();
// var result = await _exportImporter.ExtendExcelImport<MaterialRelationshipDetailImportDto>(files, _excelImportService, _mapList);
// var entityList = ObjectMapper.Map<List<MaterialRelationshipDetailImportDto>, List<MaterialRelationshipDetail>>(result);
// var matialCodeList = entityList.Select(p => p.MaterialCode).Distinct().ToList();
// List<MaterialRelationshipDetail> _ls = new List<MaterialRelationshipDetail>();
// foreach (var itm in matialCodeList)
// {
// var MaterialRelationshipDetail = entityList.Where(p => p.MaterialCode == itm).Distinct().ToList();
// var _list = MaterialRelationshipDetail.OrderBy(p => p.BeginDate).ToList();
// int i = 0;//查询下条记录
// foreach (var price in _list)
// {
// i++;
// if (_list.Count == i)
// {
// price.EndDate = DateTime.MaxValue;
// break;
// }
// else
// {
// var _nextPrice = _list[i];
// price.EndDate = _nextPrice.BeginDate.AddDays(-1);
// }
// _ls.Add(price);
// }
// }
// await _mng.ImportAsync(_ls, version);
// return ApplicationConsts.SuccessStr;
//}
///// <summary>
///// 修改实体
///// </summary>
///// <param name="id">ID</param>
///// <param name="input">修改实体DTO</param>
///// <returns>实体DTO</returns>
//[HttpPut]
//[Route("{id}")]
//[Authorize(SettleAccountPermissions.MaterialRelationships.Update)]
//virtual public async Task<MaterialRelationshipDetailDto> UpdateAsync(Guid id, MaterialRelationshipDetailDto input)
//{
// var entity = new MaterialRelationshipDetail(
// );
// var _ent = await _mng.UpdateAsync(id, entity);
// var dto = ObjectMapper.Map<MaterialRelationshipDetail, MaterialRelationshipDetailDto>(_ent);
// return dto;
//}
/// <summary>
/// 删除实体
/// </summary>
/// <param name="id">ID</param>
/// <returns>无</returns>
[HttpDelete]
[Route("{id}")]
//[Authorize(SettleAccountPermissions.MaterialRelationships.Delete)]
virtual public async Task DeleteAsync(Guid id)
{
await _mng.DeleteAsync(id);
}
/// <summary>
/// 按IDs删除实体列表
/// </summary>
/// <param name="ids">ID列表</param>
/// <returns>是否执行成功</returns>
[HttpPost]
[Route("delete")]
virtual public async Task<bool> DeleteListAsync(List<Guid> ids)
{
return await _mng.DeleteListAsync(ids);
}
/// <summary>
/// 导出文件
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[Route("Export")]
//[Authorize(SettleAccountPermissions.MaterialRelationships.Default)]
virtual public async Task<string> ExportAsync(MaterialRelationshipDetailRequestDto input)
{
IExporter _csv = new CsvExporter();
IExporter _excel = new ExcelExporter();
//2021-09-27 添加过滤条件
if (!string.IsNullOrEmpty(input.Version))
{
input.Filters.Add(new FilterCondition() { Action = EnumFilterAction.Equal, Column = "Version", Logic = EnumFilterLogic.And, Value = input.Version });
}
if (!string.IsNullOrEmpty(input.CustomerCode))
{
input.Filters.Add(new FilterCondition() { Action = EnumFilterAction.Equal, Column = "CustomerCode", Logic = EnumFilterLogic.And, Value = input.CustomerCode });
}
var entities = await _mng.GetListAsync(input.Filters, input.Sorting, int.MaxValue,
0, true);
var dtoDetails = ObjectMapper.Map<List<MaterialRelationshipDetail>, List<MaterialRelationshipDetailExportDto>>(entities);
string _fileName = string.Empty;
//声明导出容器
byte[] result = null;
_fileName = string.Format("总成与结算件关系_{0}.xlsx",Guid.NewGuid().ToString());
result = await _excel.ExportAsByteArray(dtoDetails);
result.ShouldNotBeNull();
//保存导出文件到服务器存成二进制
await _excelImportService.SaveBlobAsync(
new SaveExcelImportInputDto
{
Name = _fileName,
Content = result
}
);
return _fileName;
}
}
}