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.
 
 
 
 
 
 

403 lines
17 KiB

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Caching;
using Volo.Abp.Guids;
using Volo.Abp.ObjectMapping;
using Win.Abp.Snowflakes;
using Win.Sfs.BaseData.ImportExcelCommon;
using Win.Sfs.SettleAccount.CommonManagers;
using Win.Sfs.SettleAccount.Entities.Boms;
using Win.Sfs.SettleAccount.Entities.ImportMap;
using Win.Sfs.SettleAccount.Entities.Materials;
using Win.Sfs.SettleAccount.ExcelImporter;
using Win.Sfs.Shared.RepositoryBase;
namespace Win.Sfs.SettleAccount.Boms
{
/// <summary>
/// BOM
/// </summary>
[AllowAnonymous]
[Route("api/settleaccount/BomAppService")]
public class BomAppService : SettleAccountApplicationBase<Bom>
{
/// <summary>
/// BOM仓储
/// </summary>
private readonly INormalEfCoreRepository<Bom, Guid> _repository;
/// <summary>
/// 构造
/// </summary>
public BomAppService(
INormalEfCoreRepository<Bom, Guid> repository,
IDistributedCache<Bom> cache,
IExcelImportAppService excelImportService,
ISnowflakeIdGenerator snowflakeIdGenerator,
ICommonManager commonManager
) : base(cache,excelImportService,snowflakeIdGenerator,commonManager)
{
_repository = repository;
}
#region 导出
/// <summary>
/// 导出
/// </summary>
[HttpPost]
[Route("Export")]
public virtual async Task<string> ExportAsync(BomRequestDto input)
{
string _fileName = string.Format("BOM_{0}.xlsx", DateTime.Now.ToString("yyyyMMdd"));
var entities = await _repository.GetListByFilterAsync(input.Filters, input.Sorting, int.MaxValue,
0, true);
var dtoDetails = ObjectMapper.Map<List<Bom>, List<BomExportDto> >(entities);
//声明导出容器
ExportImporter _exportImporter = new ExportImporter();
var result = await _exportImporter.ExcelExporter(dtoDetails);
result.ShouldNotBeNull();
//保存导出文件到服务器存成二进制
await _excelImportService.SaveBlobAsync(
new SaveExcelImportInputDto
{
Name = _fileName,
Content = result
}
);
return _fileName;
}
#endregion
#region CURD
/// <summary>
/// 获取列表
/// </summary>
[HttpPost]
[Route("list")]
public async Task<PagedResultDto<BomDto>> GetListAsync(BomRequestDto 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<Bom>, List<BomDto>>(entities);
return new PagedResultDto<BomDto>(totalCount, dtos);
}
#endregion
#region 原方法(废弃)
// #region 导入导出功能
// /// <summary>
// /// 导入功能
// /// </summary>
// /// <param name="files">上传的文件(前端已经限制只能上传一个附件)</param>
// /// <returns></returns>
// [HttpPost]
// [Route("ExcelImport-Map")]
// [DisableRequestSizeLimit]
// [Authorize(SettleAccountPermissions.Boms.Create)]
// public async Task<string> BomUploadExcelImportMap([FromForm] IFormFileCollection files, Guid branchId, string year, string period, string version, string customerCode, string factory)
// {
// var _mapList=_importColumnMapRepository.Where(p => p.ProjectName ==SettleAccountModuleName.Bom ).ToList();
// ExportImporter _exportImporter = new ExportImporter();
// var result = await _exportImporter.ExtendExcelImport<ImportBomDto>(files, _excelImportService,_mapList);
// var entityList = ObjectMapper.Map<List<ImportBomDto>, List<Bom>>(result);
// var _versionQuery = _versionRepository.Where(p => p.Version == version && p.Factory == factory);
// await _versionQuery.BatchDeleteAsync();
// var _query = _repository.Where(p => p.Version == version && p.Factory == factory);
// await _query.BatchDeleteAsync();
// var checkList = new List<ErrorExportDto>();
// var _matList = await _materialRepository.GetAllAsync(GuidGenerator.Create());
// var _group = entityList.GroupBy(x => new { x.ParentItemCode, x.ChildItemCode, x.Version }).Select(p => new { Count = p.Count(), ParentItmeCode = p.Key.ParentItemCode, ChildItemCode = p.Key.ChildItemCode });
// foreach (var itm in _group)
// {
// if (itm.Count > 1)
// {
// checkList.Add(new ErrorExportDto(version, customerCode, string.Empty, string.Empty, string.Empty, string.Empty, string.Format("不能导入父编码{0},子编码{1}有重复数据", itm.ParentItmeCode, itm.ChildItemCode), string.Empty));
// }
// }
// var _id = GuidGenerator.Create();
// var _bomList = new List<BomVersion>();
// _bomList.Add(new BomVersion(_id, branchId, year, period, version, customerCode, factory));
// foreach (var itm in entityList)
// {
// if (!_matList.Any(p => p.MaterialCode == itm.ParentItemCode))
// {
// checkList.Add(new ErrorExportDto(version, customerCode, string.Empty, string.Empty, itm.ParentItemCode, string.Empty, string.Format("物料主数据不存在物料号{0}!", itm.ParentItemCode), string.Empty));
// continue;
// }
// itm.SetValue(GuidGenerator.Create(), branchId, year, period, version, _id, factory);
// }
// if (checkList.Count > 0)
// {
// return await ExportErrorReportAsync(checkList);
// }
// await _repository.GetDbContext().BulkInsertAsync<Bom>(entityList);
// await _versionRepository.GetDbContext().BulkInsertAsync(_bomList);
// //st.Stop();
// return ApplicationConsts.SuccessStr;
// }
// /// <summary>
// /// 导入功能
// /// </summary>
// /// <param name="files">上传的文件(前端已经限制只能上传一个附件)</param>
// /// <returns></returns>
// [HttpPost]
// [Route("ExcelImport")]
// [DisableRequestSizeLimit]
// [Authorize(SettleAccountPermissions.Boms.Create)]
// public async Task<string> BomUploadExcelImport([FromForm] IFormFileCollection files, Guid branchId, string year, string period, string version, string customerCode,string factory)
// {
// ExportImporter _exportImporter = new ExportImporter();
// var result = await _exportImporter.UploadExcelImport<ImportBomDto>(files, _excelImportService);
// var entityList = ObjectMapper.Map<List<ImportBomDto>, List<Bom>>(result);
// var _versionQuery = _versionRepository.Where(p => p.Version == version && p.Factory==factory );
// await _versionQuery.BatchDeleteAsync();
// var _query = _repository.Where(p => p.Version == version && p.Factory == factory);
// await _query.BatchDeleteAsync();
// var checkList = new List<ErrorExportDto>();
// var _matList =await _materialRepository.GetAllAsync(GuidGenerator.Create());
// var _group = entityList.GroupBy(x => new { x.ParentItemCode, x.ChildItemCode, x.Version }).Select(p => new { Count = p.Count(),ParentItmeCode=p.Key.ParentItemCode, ChildItemCode=p.Key.ChildItemCode });
// foreach (var itm in _group)
// {
// if (itm.Count > 1)
// {
// checkList.Add(new ErrorExportDto(version, customerCode, string.Empty, string.Empty, string.Empty, string.Empty, string.Format("不能导入父编码{0},子编码{1}有重复数据", itm.ParentItmeCode, itm.ChildItemCode), string.Empty));
// }
// }
// var _id = GuidGenerator.Create();
// var _bomList = new List<BomVersion>();
// _bomList.Add(new BomVersion(_id, branchId, year, period, version, customerCode,factory));
// foreach (var itm in entityList)
// {
// if (!_matList.Any(p => p.MaterialCode == itm.ParentItemCode))
// {
// checkList.Add(new ErrorExportDto(version, customerCode, string.Empty, string.Empty, itm.ParentItemCode, string.Empty, string.Format("物料主数据不存在物料号{0}!", itm.ParentItemCode), string.Empty));
// continue;
// }
// itm.SetValue(GuidGenerator.Create(),branchId,year,period,version,_id,factory);
// }
// if (checkList.Count > 0)
// {
// return await ExportErrorReportAsync(checkList);
// }
// await _repository.GetDbContext().BulkInsertAsync<Bom>(entityList);
// await _versionRepository.GetDbContext().BulkInsertAsync(_bomList);
// //st.Stop();
// return ApplicationConsts.SuccessStr;
// }
// /// <summary>
// /// 导出文件
// /// </summary>
// /// <param name="input"></param>
// /// <returns></returns>
// [HttpPost]
// [Route("Export")]
// [Authorize(SettleAccountPermissions.Boms.Default)]
// virtual public async Task<string> ExportAsync(BomRequestDto input)
// {
// IExporter _csv = new CsvExporter();
// IExporter _excel = new ExcelExporter();
// //导出加上版本过滤条件,不能全导出
// if (input.ParentId != Guid.Empty)
// {
// input.Filters.Add(new FilterCondition() { Action = EnumFilterAction.Equal, Column = "ParentId", Logic = EnumFilterLogic.And, Value = input.ParentId.ToString() });
// }
// //else
// //{
// // return new PagedResultDto<BomDto>(0, new List<BomDto>());
// //}
// var entities = await _repository.GetListByFilterAsync(input.BranchId, input.Filters, input.Sorting, int.MaxValue,
// 0, true);
// var dtoDetails = ObjectMapper.Map<List<Bom>, List<BomExportDto>>(entities);
// string _fileName = string.Empty;
// //声明导出容器
// byte[] result = null;
// switch (input.FileType)
// {
// case 0:
// _fileName = string.Format("产品结构_{0}.csv", input.UserId.ToString());
// result = await _csv.ExportAsByteArray(dtoDetails);
// break;
// case 1:
// _fileName = string.Format("产品结构_{0}.xlsx", input.UserId.ToString());
// result = await _excel.ExportAsByteArray(dtoDetails);
// break;
// }
// result.ShouldNotBeNull();
// //保存导出文件到服务器存成二进制
// await _excelImportService.SaveBlobAsync(
// new SaveExcelImportInputDto
// {
// Name = _fileName,
// Content = result
// }
// );
// return _fileName;
// }
// #endregion
// /// <summary>
// /// 按ID获取唯一实体
// /// </summary>
// /// <remarks>
// /// 返回实体全部属性
// /// </remarks>
// /// <param name="id">ID</param>
// /// <returns>实体DTO</returns>
// [HttpGet]
// [Route("{id}")]
//[Authorize(SettleAccountPermissions.Boms.Default)]
// virtual public async Task<BomDto> GetAsync(Guid id)
// {
// var result = await _repository.GetAsync(id);
// var dto = _objectMapper.Map<Bom, BomDto>(result);
// return dto;
// }
// private async Task<Bom> GetFromCacheAsync(Guid id)
// {
// var result = await _repository.GetAsync(id);
// return result;
// }
// private async Task<long> GetCountAsync(BomRequestDto input)
// {
// return await _repository.GetCountByFilterAsync(input.BranchId, input.Filters);
// }
// private async Task<long> GetCountAsync(BomVersionRequestDto input)
// {
// return await _versionRepository.GetCountByFilterAsync(input.BranchId, input.Filters);
// }
// /// <summary>
// /// 获取实体总数
// /// </summary>
// /// <returns>实体总数</returns>
// [HttpGet]
// [Route("count")]
//[Authorize(SettleAccountPermissions.Boms.Default)]
// virtual public async Task<long> GetTotalCountAsync(Guid branchId)
// {
// return await _repository.GetCountAsync(branchId);
// }
// /// <summary>
// /// 删除实体
// /// </summary>
// /// <param name="id">ID</param>
// /// <returns>无</returns>
// [HttpDelete]
// [Route("{id}")]
//[Authorize(SettleAccountPermissions.Boms.Delete)]
// virtual public async Task DeleteAsync(Guid id)
// {
// //var entity = await GetFromCacheAsync(id);
// //await Cache.DeleteAsync<Bom>(id.ToString());
// await _repository.DeleteAsync(id);
// }
// /// <summary>
// /// 按IDs删除实体列表
// /// </summary>
// /// <param name="ids">IDs</param>
// /// <returns>是否执行成功</returns>
// [HttpPost]
// [Route("delete")]
//[Authorize(SettleAccountPermissions.Boms.Delete)]
// virtual public async Task<bool> DeleteListAsync(List<Guid> ids)
// {
// //foreach (var id in ids)
// //{
// // var entity = await GetFromCacheAsync(id);
// //}
// return await _repository.DeleteListAsync(ids);
// }
// ///// <summary>
// /// <summary>
// /// 根据筛选条件获取实体列表
// /// </summary>
// /// <remarks>
// /// 请求条件包括:筛选条件列表,排序条件,数据数量,页码
// /// </remarks>
// /// <param name="input">请求条件</param>
// /// <returns>实体DTO列表</returns>
// [HttpPost]
// [Route("list")]
// [Authorize(SettleAccountPermissions.Boms.Default)]
// public async Task<PagedResultDto<BomDto>> GetListAsync(Guid parentId, BomRequestDto input)
// {
// if (input.ParentId != Guid.Empty)
// {
// input.Filters.Add(new FilterCondition() { Action = EnumFilterAction.Equal, Column = "ParentId", Logic = EnumFilterLogic.And, Value = input.ParentId.ToString() });
// }
// else
// {
// return new PagedResultDto<BomDto>(0,new List<BomDto>());
// }
// var entities = await _repository.GetListByFilterAsync(input.BranchId, input.Filters, input.Sorting, input.MaxResultCount,
// input.SkipCount, true);
// var totalCount = await GetCountAsync(input);
// var dtos = _objectMapper.Map<List<Bom>, List<BomDto>>(entities);
// return new PagedResultDto<BomDto>(totalCount, dtos);
// }
// ///// <summary>
// /// <summary>
// /// 根据筛选条件获取实体列表
// /// </summary>
// /// <remarks>
// /// 请求条件包括:筛选条件列表,排序条件,数据数量,页码
// /// </remarks>
// /// <param name="input">请求条件</param>
// /// <returns>实体DTO列表</returns>
// [HttpPost]
// [Route("listVersion")]
// [Authorize(SettleAccountPermissions.Boms.Default)]
// public async Task<PagedResultDto<BomVersionDto>> GetVersionListAsync(BomVersionRequestDto input)
// {
// var entities = await _versionRepository.GetListByFilterAsync(input.BranchId, input.Filters, input.Sorting, int.MaxValue,
// input.SkipCount, true);
// var totalCount = await GetCountAsync(input);
// var dtos = _objectMapper.Map<List<BomVersion>, List<BomVersionDto>>(entities);
// //foreach (var itm in dtos)
// //{
// // itm.Version = itm.Version + string.Format("({0})", itm.Factory);
// //}
// return new PagedResultDto<BomVersionDto>(totalCount, dtos);
// }
#endregion
}
}