@ -1,25 +1,13 @@
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Threading.Tasks ;
using EFCore.BulkExtensions ;
using Magicodes.ExporterAndImporter.Core ;
using Magicodes.ExporterAndImporter.Excel ;
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.Extensions.Caching.Distributed ;
using Microsoft.EntityFrameworkCore ;
using Shouldly ;
using Volo.Abp ;
using System ;
using System.Collections.Generic ;
using System.Threading.Tasks ;
using Volo.Abp.Application.Dtos ;
using Volo.Abp.Application.Services ;
using Volo.Abp.BackgroundJobs ;
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.Boms ;
@ -31,41 +19,34 @@ using Win.Sfs.SettleAccount.Entities.Prices;
using Win.Sfs.SettleAccount.Entities.TaskJobs ;
using Win.Sfs.SettleAccount.ExcelImporter ;
using Win.Sfs.SettleAccount.ExportReports ;
using Win.Sfs.SettleAccount.MaterialRelationships ;
using Win.Sfs.Shared.Cache Base ;
using Win.Utils ;
using Win.Sfs.Shared.Repository Base ;
namespace Win.Sfs.SettleAccount.Entities.Materials
{
/// <summary>
/// 物料主数据应用服务
/// 物料主数据
/// </summary>
[Authorize(SettleAccountPermissions.Materials.Default)]
//[AllowAnonymous]
// [Authorize(SettleAccountPermissions.Materials.Default)]
[AllowAnonymous]
[Route("api/settleaccount/Material")]
public class MaterialAppService : SettleAccountApplicationBase < Material > , IMaterialAppService
public class MaterialAppService : SettleAccountApplicationBase < Material >
{
private readonly IExcelImportAppService _ excelImportService ;
private readonly ISettleAccountBranchEfCoreRepository < Material , Guid > _ repository ;
private readonly INormalEfCoreRepository < Material , Guid > _ repository ;
private readonly ISettleAccountBranchEfCoreRepository < Bom , Guid > _ bomRepository ;
private readonly ISettleAccountBranchEfCoreRepository < BomVersion , Guid > _ bomversionRepository ;
private readonly ISettleAccountBranchEfCoreRepository < PriceList , Guid > _ priceRepository ;
private readonly ISettleAccountBranchEfCoreRepository < PriceListVersion , Guid > _ priceversionRepository ;
private readonly ISettleAccountBranchEfCoreRepository < ImportColumnMap , Guid > _ mapRepository ;
private readonly ISettleAccountBranchEfCoreRepository < MaterialRelationship , Guid > _ relationshipRepository ;
/// <summary>
/// 构建方法
/// </summary>
/// <param name="guidGenerator">构建UID</param>
/// <param name="objectMapper">自动map</param>
/// <param name="repository">仓储接口</param>
/// <param name="cache">缓存</param>
public MaterialAppService (
ISettleAccountBranchEfCoreRepository < Material , Guid > repository ,
INormalEfCoreRepository < Material , Guid > repository ,
ISettleAccountBranchEfCoreRepository < ImportColumnMap , Guid > mapRepository ,
ISettleAccountBranchEfCoreRepository < Bom , Guid > bomRepository ,
ISettleAccountBranchEfCoreRepository < BomVersion , Guid > bomversionRepository ,
@ -92,461 +73,512 @@ namespace Win.Sfs.SettleAccount.Entities.Materials
}
#region 导入、导出
/// <summary>
/// 导入功能
/// 导出
/// </summary>
/// <param name="files">上传的文件(前端已经限制只能上传一个附件)</param>
/// <returns></returns>
[HttpPost]
[Route("ExcelImport-TH")]
[DisableRequestSizeLimit]
[Authorize(SettleAccountPermissions.Materials.Default)]
public async Task < string > MaterialUploadExcelImportMap ( [ FromForm ] IFormFileCollection files , string version )
[Route("Export")]
//[Authorize(SettleAccountPermissions.Materials.Default)]
public virtual async Task < string > ExportAsync ( MaterialRequestDto input )
{
ExportImporter _ exportImporter = new ExportImporter ( ) ;
var result = await _ exportImporter . ExtendExcelImport < MaterialImportDtoTH > ( files , _ excelImportService ) ;
string _f ileName = 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 < Material > , List < MaterialExportDto > > ( entities ) ;
await UpdateMaterial ( result , version ) ;
await UpdateBom ( result , version ) ;
await UpdatePrice ( result , version ) ;
//声明导出容器
ExportImporter _ exportImporter = new ExportImporter ( ) ;
return ApplicationConsts . SuccessStr ;
}
private async Task < List < ErrorExportDto > > UpdateBom ( List < MaterialImportDtoTH > result , string version )
{
var _ versionQuery = _ bomversionRepository . Where ( p = > p . Version = = version ) ;
await _ versionQuery . BatchDeleteAsync ( ) ;
var _ query = _ bomRepository . Where ( p = > p . Version = = version ) ;
await _ query . BatchDeleteAsync ( ) ;
var entityList = new List < Bom > ( ) ;
var result = await _ exportImporter . ExcelExporter ( dtoDetails ) ;
var _l s = result . Where ( p = > ! string . IsNullOrEmpty ( p . ChildMaterialCode ) & & ! string . IsNullOrEmpty ( p . MaterialCode ) ) . ToList ( ) ;
result . ShouldNotBeNull ( ) ;
foreach ( var itm in _l s )
{
entityList . Add ( new Bom ( GuidGenerator . Create ( ) , GuidGenerator . Create ( ) , itm . MaterialCode , itm . MaterialDesc , itm . ChildMaterialCode , itm . ChildMaterialDesc , string . Empty , itm . Qty , 0 , itm . Qty , string . Empty , DateTime . Now , DateTime . Now , string . Empty , 0 , GuidGenerator . Create ( ) , string . Empty ) ) ;
}
var checkList = new List < ErrorExportDto > ( ) ;
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 , string . Empty , 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 , GuidGenerator . Create ( ) , DateTime . Now . Year . ToString ( ) , DateTime . Now . Month . ToString ( ) , version , string . Empty , string . Empty ) ) ;
foreach ( var itm in entityList )
//保存导出文件到服务器存成二进制
await _ excelImportService . SaveBlobAsync (
new SaveExcelImportInputDto
{
itm . SetValue ( GuidGenerator . Create ( ) , GuidGenerator . Create ( ) , DateTime . Now . Year . ToString ( ) , DateTime . Now . Month . ToString ( ) , version , _ id , string . Empty ) ;
Name = _f ileName ,
Content = result
}
await _ bomRepository . GetDbContext ( ) . BulkInsertAsync < Bom > ( entityList ) ;
await _ bomversionRepository . GetDbContext ( ) . BulkInsertAsync ( _ bomList ) ;
return checkList ;
) ;
return _f ileName ;
}
#endregion
private async Task < List < ErrorExportDto > > UpdatePrice ( List < MaterialImportDtoTH > result , string version )
{
var checkList = new List < ErrorExportDto > ( ) ;
var _ priceversion = _ priceversionRepository . Where ( p = > p . Version = = version ) ;
await _ priceversion . BatchDeleteAsync ( ) ;
var _ prices = _ priceRepository . Where ( p = > p . Version = = version ) ;
await _ prices . BatchDeleteAsync ( ) ;
var _ materialList = result . Where ( p = > ! string . IsNullOrEmpty ( p . MaterialCode ) ) . Select ( p = > p . MaterialCode ) . Distinct ( ) . ToList ( ) ;
_ materialList . Remove ( "xxbmr016.p b+" ) ;
_ materialList . Remove ( null ) ;
_ materialList . Remove ( "END OF REPORT" ) ;
_ materialList . Remove ( "Report Attributes (see Comment)" ) ;
_ materialList . Remove ( "Report Criteria (see Comment)" ) ;
var _ partList = result . Where ( p = > ! string . IsNullOrEmpty ( p . ChildMaterialCode ) ) . Select ( p = > p . ChildMaterialCode ) . Distinct ( ) . ToList ( ) ;
_ partList . Remove ( null ) ;
List < PriceList > _ priceList = new List < PriceList > ( ) ;
List < PriceListVersion > _ priceVersion = new List < PriceListVersion > ( ) ;
foreach ( var itm in _ materialList )
{
var _f irst = result . FirstOrDefault ( p = > p . MaterialCode = = itm ) ;
if ( _f irst ! = null )
{
var _ price = new PriceList ( GuidGenerator . Create ( ) , DateTime . MinValue , DateTime . MaxValue , _f irst . MaterialPrice , _f irst . MaterialCode , 0 , GuidGenerator . Create ( ) , version , string . Empty ) ;
_ priceList . Add ( _ price ) ;
}
}
foreach ( var itm in _ partList )
{
var _f irst = result . FirstOrDefault ( p = > p . ChildMaterialCode = = itm ) ;
if ( _f irst ! = null )
#region CURD
/// <summary>
/// 获取列表
/// </summary>
[HttpPost]
[Route("list")]
//[Authorize(SettleAccountPermissions.Materials.Default)]
virtual public async Task < PagedResultDto < MaterialDto > > GetListAsync ( MaterialRequestDto input )
{
var _ price = new PriceList ( GuidGenerator . Create ( ) , DateTime . MinValue , DateTime . MaxValue , _f irst . ChildMaterialPrice , _f irst . ChildMaterialCode , 0 , GuidGenerator . Create ( ) , version , string . Empty ) ;
_ priceList . Add ( _ price ) ;
}
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 < Material > , List < MaterialDto > > ( entities ) ;
return new PagedResultDto < MaterialDto > ( totalCount , dtos ) ;
}
var _ versionList = new List < PriceListVersion > ( ) { new PriceListVersion ( GuidGenerator . Create ( ) , DateTime . Now . Year . ToString ( ) , DateTime . Now . Month . ToString ( ) , version ) } ;
await _ priceRepository . GetDbContext ( ) . BulkInsertAsync ( _ priceList ) ;
await _ priceversionRepository . GetDbContext ( ) . BulkInsertAsync ( _ versionList ) ;
#endregion
return checkList ;
}
#region 原方法(废弃)
///// <summary>
///// 导入功能
///// </summary>
///// <param name="files">上传的文件(前端已经限制只能上传一个附件)</param>
///// <returns></returns>
//[HttpPost]
//[Route("ExcelImport-TH")]
//[DisableRequestSizeLimit]
//[Authorize(SettleAccountPermissions.Materials.Default)]
//public async Task<string> MaterialUploadExcelImportMap([FromForm] IFormFileCollection files, string version)
//{
// ExportImporter _exportImporter = new ExportImporter();
// var result = await _exportImporter.ExtendExcelImport<MaterialImportDtoTH>(files, _excelImportService);
// await UpdateMaterial(result, version);
// await UpdateBom(result, version);
// await UpdatePrice(result, version);
// return ApplicationConsts.SuccessStr;
//}
//private async Task<List<ErrorExportDto>> UpdateBom(List<MaterialImportDtoTH> result, string version)
//{
// var _versionQuery = _bomversionRepository.Where(p => p.Version == version);
// await _versionQuery.BatchDeleteAsync();
// var _query = _bomRepository.Where(p => p.Version == version);
// await _query.BatchDeleteAsync();
// var entityList = new List<Bom>();
// var _ls = result.Where(p => !string.IsNullOrEmpty(p.ChildMaterialCode) && !string.IsNullOrEmpty(p.MaterialCode)).ToList();
// foreach (var itm in _ls)
// {
// entityList.Add(new Bom(GuidGenerator.Create(), GuidGenerator.Create(), itm.MaterialCode, itm.MaterialDesc, itm.ChildMaterialCode, itm.ChildMaterialDesc, string.Empty, itm.Qty, 0, itm.Qty, string.Empty, DateTime.Now, DateTime.Now, string.Empty, 0, GuidGenerator.Create(), string.Empty));
// }
// var checkList = new List<ErrorExportDto>();
// 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, string.Empty, 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, GuidGenerator.Create(), DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), version, string.Empty, string.Empty));
// foreach (var itm in entityList)
// {
// itm.SetValue(GuidGenerator.Create(), GuidGenerator.Create(), DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), version, _id, string.Empty);
// }
// await _bomRepository.GetDbContext().BulkInsertAsync<Bom>(entityList);
// await _bomversionRepository.GetDbContext().BulkInsertAsync(_bomList);
// return checkList;
private async Task < List < ErrorExportDto > > UpdateMaterial ( List < MaterialImportDtoTH > result , string version )
{
//}
var _l ist = await _ repository . GetAllAsync ( GuidGenerator . Create ( ) ) ;
//var _relationshiplist = _relationshipRepository.GetAllAsync(GuidGenerator.Create());
//private async Task<List<ErrorExportDto>> UpdatePrice(List<MaterialImportDtoTH> result, string version)
//{
// var checkList = new List<ErrorExportDto>();
// var _priceversion = _priceversionRepository.Where(p => p.Version == version);
// await _priceversion.BatchDeleteAsync();
// var _prices = _priceRepository.Where(p => p.Version == version);
// await _prices.BatchDeleteAsync();
// var _materialList = result.Where(p => !string.IsNullOrEmpty(p.MaterialCode)).Select(p => p.MaterialCode).Distinct().ToList();
// _materialList.Remove("xxbmr016.p b+");
// _materialList.Remove(null);
// _materialList.Remove("END OF REPORT");
// _materialList.Remove("Report Attributes (see Comment)");
// _materialList.Remove("Report Criteria (see Comment)");
// var _partList = result.Where(p => !string.IsNullOrEmpty(p.ChildMaterialCode)).Select(p => p.ChildMaterialCode).Distinct().ToList();
// _partList.Remove(null);
// List<PriceList> _priceList = new List<PriceList>();
// List<PriceListVersion> _priceVersion = new List<PriceListVersion>();
// foreach (var itm in _materialList)
// {
// var _first = result.FirstOrDefault(p => p.MaterialCode == itm);
// if (_first != null)
// {
// var _price = new PriceList(GuidGenerator.Create(), DateTime.MinValue, DateTime.MaxValue, _first.MaterialPrice, _first.MaterialCode, 0, GuidGenerator.Create(), version, string.Empty);
// _priceList.Add(_price);
// }
// }
// foreach (var itm in _partList)
// {
// var _first = result.FirstOrDefault(p => p.ChildMaterialCode == itm);
// if (_first != null)
// {
// var _price = new PriceList(GuidGenerator.Create(), DateTime.MinValue, DateTime.MaxValue, _first.ChildMaterialPrice, _first.ChildMaterialCode, 0, GuidGenerator.Create(), version, string.Empty);
// _priceList.Add(_price);
// }
// }
// var _versionList = new List<PriceListVersion>() { new PriceListVersion(GuidGenerator.Create(), DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString(), version) };
// await _priceRepository.GetDbContext().BulkInsertAsync(_priceList);
// await _priceversionRepository.GetDbContext().BulkInsertAsync(_versionList);
// return checkList;
//}
List < ErrorExportDto > _ errorList = new List < ErrorExportDto > ( ) ;
List < Material > _ insertls = new List < Material > ( ) ;
var materialList = result . Where ( p = > ! string . IsNullOrEmpty ( p . MaterialCode ) ) . Select ( p = > p . MaterialCode ) . Distinct ( ) . ToList ( ) ;
materialList . Remove ( "xxbmr016.p b+" ) ;
materialList . Remove ( null ) ;
materialList . Remove ( "END OF REPORT" ) ;
materialList . Remove ( "Report Attributes (see Comment)" ) ;
materialList . Remove ( "Report Criteria (see Comment)" ) ;
//private async Task<List<ErrorExportDto>> UpdateMaterial(List<MaterialImportDtoTH> result, string version)
//{
//xxbmr016.p b+
foreach ( var itm in materialList )
{
var _ material = result . Where ( p = > p . MaterialCode = = itm ) . FirstOrDefault ( ) ;
if ( _ material ! = null )
{
var _ m = new Material ( GuidGenerator . Create ( ) , GuidGenerator . Create ( ) , string . Empty , _ material . MaterialCode , _ material . MaterialDesc , string . Empty , _ material . EstimateType , string . Empty ) ;
_ insertls . Add ( _ m ) ;
// var _list = await _repository.GetAllAsync(GuidGenerator.Create());
// //var _relationshiplist = _relationshipRepository.GetAllAsync(GuidGenerator.Create());
// List<ErrorExportDto> _errorList = new List<ErrorExportDto>();
// List<Material> _insertls = new List<Material>();
// var materialList = result.Where(p => !string.IsNullOrEmpty(p.MaterialCode)).Select(p => p.MaterialCode).Distinct().ToList();
// materialList.Remove("xxbmr016.p b+");
// materialList.Remove(null);
// materialList.Remove("END OF REPORT");
// materialList.Remove("Report Attributes (see Comment)");
// materialList.Remove("Report Criteria (see Comment)");
// //xxbmr016.p b+
// foreach (var itm in materialList)
// {
// var _material = result.Where(p => p.MaterialCode == itm).FirstOrDefault();
// if (_material != null)
// {
// var _m = new Material(GuidGenerator.Create(), GuidGenerator.Create(), string.Empty, _material.MaterialCode, _material.MaterialDesc, string.Empty, _material.EstimateType, string.Empty);
// _insertls.Add(_m);
// }
// }
// var _ls = from r1 in _insertls
// join r2 in _list on r1.MaterialCode equals r2.MaterialCode
// into temp
// from t in temp.DefaultIfEmpty()
// where t == null
// select r1;
// var _ls1 = from r1 in _insertls
// join r2 in _list on r1.MaterialCode equals r2.MaterialCode
// select r1;
// var checkList = new List<ErrorExportDto>();
// var check = _ls.Where(p => string.IsNullOrEmpty(p.EstimateType)).ToList();
// if (_ls.Count() > 0)
// {
// await _repository.GetDbContext().BulkInsertAsync<Material>(_ls.ToList());
// }
// if (_ls1.Count() > 0)
// {
// await _repository.GetDbContext().BulkUpdateAsync(_ls1.ToList());
// }
// return _errorList;
//}
}
}
var _l s = from r1 in _ insertls
join r2 in _l ist on r1 . MaterialCode equals r2 . MaterialCode
into temp
from t in temp . DefaultIfEmpty ( )
where t = = null
select r1 ;
var _l s1 = from r1 in _ insertls
join r2 in _l ist on r1 . MaterialCode equals r2 . MaterialCode
select r1 ;
var checkList = new List < ErrorExportDto > ( ) ;
var check = _l s . Where ( p = > string . IsNullOrEmpty ( p . EstimateType ) ) . ToList ( ) ;
if ( _l s . Count ( ) > 0 )
{
await _ repository . GetDbContext ( ) . BulkInsertAsync < Material > ( _l s . ToList ( ) ) ;
}
if ( _l s1 . Count ( ) > 0 )
{
await _ repository . GetDbContext ( ) . BulkUpdateAsync ( _l s1 . ToList ( ) ) ;
}
return _ errorList ;
}
///// <summary>
///// 导入功能
///// </summary>
///// <param name="files">上传的文件(前端已经限制只能上传一个附件)</param>
///// <returns></returns>
//[HttpPost]
//[Route("ExcelImport")]
//[DisableRequestSizeLimit]
//[Authorize(SettleAccountPermissions.Materials.Default)]
//public async Task<string> MaterialUploadExcelImport([FromForm] IFormFileCollection files)
//{
// ExportImporter _exportImporter = new ExportImporter();
// var result = await _exportImporter.UploadExcelImport<MaterialImportDto>(files, _excelImportService);
// var _ls = ObjectMapper.Map<List<MaterialImportDto>, List<Material>>(result);
// var checkList = new List<ErrorExportDto>();
// foreach (var itm in _ls)
// {
// var _first = await _repository.FirstOrDefaultAsync(p => p.MaterialCode == itm.MaterialCode);
// if (_first == null)
// {
// var entity = new Material(
// GuidGenerator.Create(),
// GuidGenerator.Create(),
// itm.Factory,
// itm.MaterialCode,
// itm.MaterialDesc,
// itm.Unit,
// itm.EstimateType,
// itm.EstimateTypeDesc
// );
// await _repository.InsertAsync(entity);
// }
// else
// {
// _first.Update(itm.MaterialDesc, itm.Unit, itm.EstimateType, itm.EstimateTypeDesc, itm.Factory
// );
// await _repository.UpdateAsync(_first);
// }
// var _f = await _relationshipRepository.FirstOrDefaultAsync(p => p.ErpMaterialCode == itm.MaterialCode);
// if (_f != null)
// {
// _f.Update(itm.MaterialDesc, _f.MaterialProperty, _f.SettleMaterialCode, _f.ShipMaterailCode);
// await _relationshipRepository.UpdateAsync(_f);
// }
// else
// {
// var _r = new MaterialRelationship(GuidGenerator.Create(), GuidGenerator.Create(), itm.MaterialCode, itm.MaterialDesc, string.Empty, string.Empty, string.Empty, itm.EstimateType);
// await _relationshipRepository.InsertAsync(_r);
// }
// }
// return ApplicationConsts.SuccessStr;
//}
/// <summary>
/// 导入功能
/// </summary>
/// <param name="files">上传的文件(前端已经限制只能上传一个附件)</param>
/// <returns></returns>
[HttpPost]
[Route("ExcelImport")]
[DisableRequestSizeLimit]
[Authorize(SettleAccountPermissions.Materials.Default)]
public async Task < string > MaterialUploadExcelImport ( [ FromForm ] IFormFileCollection files )
{
ExportImporter _ exportImporter = new ExportImporter ( ) ;
var result = await _ exportImporter . UploadExcelImport < MaterialImportDto > ( files , _ excelImportService ) ;
var _l s = ObjectMapper . Map < List < MaterialImportDto > , List < Material > > ( result ) ;
var checkList = new List < ErrorExportDto > ( ) ;
foreach ( var itm in _l s )
{
var _f irst = await _ repository . FirstOrDefaultAsync ( p = > p . MaterialCode = = itm . MaterialCode ) ;
if ( _f irst = = null )
{
var entity = new Material (
GuidGenerator . Create ( ) ,
GuidGenerator . Create ( ) ,
itm . Factory ,
itm . MaterialCode ,
itm . MaterialDesc ,
itm . Unit ,
itm . EstimateType ,
itm . EstimateTypeDesc
) ;
await _ repository . InsertAsync ( entity ) ;
}
else
{
_f irst . Update ( itm . MaterialDesc , itm . Unit , itm . EstimateType , itm . EstimateTypeDesc , itm . Factory
) ;
await _ repository . UpdateAsync ( _f irst ) ;
}
var _f = await _ relationshipRepository . FirstOrDefaultAsync ( p = > p . ErpMaterialCode = = itm . MaterialCode ) ;
if ( _f ! = null )
{
_f . Update ( itm . MaterialDesc , _f . MaterialProperty , _f . SettleMaterialCode , _f . ShipMaterailCode ) ;
await _ relationshipRepository . UpdateAsync ( _f ) ;
}
else
{
var _ r = new MaterialRelationship ( GuidGenerator . Create ( ) , GuidGenerator . Create ( ) , itm . MaterialCode , itm . MaterialDesc , string . Empty , string . Empty , string . Empty , itm . EstimateType ) ;
await _ relationshipRepository . InsertAsync ( _ r ) ;
}
}
return ApplicationConsts . SuccessStr ;
}
///// <summary>
///// 按ID获取唯一实体
///// </summary>
///// <remarks>
///// 返回实体全部属性
///// </remarks>
///// <param name="id">ID</param>
///// <returns>实体DTO</returns>
//[HttpGet]
//[Route("{id}")]
//[Authorize(SettleAccountPermissions.Materials.Default)]
//virtual public async Task<MaterialDto> GetAsync(Guid id)
//{
/// <summary>
/// 按ID获取唯一实体
/// </summary>
/// <remarks>
/// 返回实体全部属性
/// </remarks>
/// <param name="id">ID</param>
/// <returns>实体DTO</returns>
[HttpGet]
[Route("{id}")]
[Authorize(SettleAccountPermissions.Materials.Default)]
virtual public async Task < MaterialDto > GetAsync ( Guid id )
{
// var result = await GetFromCacheAsync(id);
// var dto = ObjectMapper.Map<Material, MaterialDto>(result);
// return dto;
//}
var result = await GetFromCacheAsync ( id ) ;
var dto = ObjectMapper . Map < Material , MaterialDto > ( result ) ;
return dto ;
}
//private async Task<Material> GetFromCacheAsync(Guid id)
//{
// var result = await _repository.GetAsync(id);
private async Task < Material > GetFromCacheAsync ( Guid id )
{
var result = await _ repository . GetAsync ( id ) ;
// return result;
//}
return result ;
}
//private async Task<long> GetCountAsync(MaterialRequestDto input)
//{
// return await _repository.GetCountByFilterAsync(input.BranchId, input.Filters);
//}
private async Task < long > GetCountAsync ( MaterialRequestDto input )
{
return await _ repository . GetCountByFilterAsync ( input . BranchId , input . Filters ) ;
}
/////// <summary>
///// <summary>
///// 根据筛选条件获取实体列表
///// </summary>
///// <remarks>
///// 请求条件包括:筛选条件列表,排序条件,数据数量,页码
///// </remarks>
///// <param name="input">请求条件</param>
///// <returns>实体DTO列表</returns>
//[HttpPost]
//[Route("list")]
//[Authorize(SettleAccountPermissions.Materials.Default)]
//virtual public async Task<PagedResultDto<MaterialDto>> GetListAsync(MaterialRequestDto input)
//{
// var entities = await _repository.GetListByFilterAsync(input.BranchId, input.Filters, input.Sorting, input.MaxResultCount,
// input.SkipCount, true);
/// <summary>
/// 根据筛选条件获取实体列表
/// </summary>
/// <remarks>
/// 请求条件包括:筛选条件列表,排序条件,数据数量,页码
/// </remarks>
/// <param name="input">请求条件</param>
/// <returns>实体DTO列表</returns>
[HttpPost]
[Route("list")]
[Authorize(SettleAccountPermissions.Materials.Default)]
virtual public async Task < PagedResultDto < MaterialDto > > GetListAsync ( MaterialRequestDto input )
{
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<Material>, List<MaterialDto>>(entities);
var totalCount = await GetCountAsync ( input ) ;
var dtos = ObjectMapper . Map < List < Material > , List < MaterialDto > > ( entities ) ;
// return new PagedResultDto<MaterialDto>(totalCount, dtos);
//}
return new PagedResultDto < MaterialDto > ( totalCount , dtos ) ;
}
///// <summary>
///// 获取实体总数
///// </summary>
///// <returns>实体总数</returns>
//[HttpGet]
//[Route("count")]
//[Authorize(SettleAccountPermissions.Materials.Default)]
//virtual public async Task<long> GetTotalCountAsync(Guid branchId)
//{
// return await _repository.GetCountAsync(branchId);
//}
/// <summary>
/// 获取实体总数
/// </summary>
/// <returns>实体总数</returns>
[HttpGet]
[Route("count")]
[Authorize(SettleAccountPermissions.Materials.Default)]
virtual public async Task < long > GetTotalCountAsync ( Guid branchId )
{
return await _ repository . GetCountAsync ( branchId ) ;
}
///// <summary>
///// 获取全部实体列表
///// </summary>
///// <returns>实体DTO列表</returns>
//[HttpGet]
//[Route("all")]
//[Authorize(SettleAccountPermissions.Materials.Default)]
//virtual public async Task<ListResultDto<MaterialDto>> GetAllAsync(Guid branchId)
//{
// var entities = await _repository.GetAllAsync(branchId, true);
/// <summary>
/// 获取全部实体列表
/// </summary>
/// <returns>实体DTO列表</returns>
[HttpGet]
[Route("all")]
[Authorize(SettleAccountPermissions.Materials.Default)]
virtual public async Task < ListResultDto < MaterialDto > > GetAllAsync ( Guid branchId )
{
var entities = await _ repository . GetAllAsync ( branchId , true ) ;
// var dtos = ObjectMapper.Map<List<Material>, List<MaterialDto>>(entities);
var dtos = ObjectMapper . Map < List < Material > , List < MaterialDto > > ( entities ) ;
// return new ListResultDto<MaterialDto>(dtos);
//}
return new ListResultDto < MaterialDto > ( dtos ) ;
}
///// <summary>
///// 批量导入实体列表
///// </summary>
///// <remarks>
///// 以ID为依据,数据库中找不到ID的实体会新增,已有ID的实体会修改
///// </remarks>
///// <param name="entities">实体列表</param>
///// <returns>是否导入成功</returns>
////[HttpPost]
////[Route("import")]
//[Authorize(SettleAccountPermissions.Materials.Create)]
////virtual public async Task<bool> ImportAsync(List<MaterialCreateDto> dtos)
////{
//// var entities =ObjectMapper.Map<List<MaterialCreateDto>, List<Material>>(dtos);
//// var result = await _repository.ImportAsync(entities);
//// return result;
////}
///// <summary>
///// 新增实体
///// </summary>
///// <param name="input">新增实体DTO</param>
///// <returns>实体DTO</returns>
/// <summary>
/// 批量导入实体列表
/// </summary>
/// <remarks>
/// 以ID为依据,数据库中找不到ID的实体会新增,已有ID的实体会修改
/// </remarks>
/// <param name="entities">实体列表</param>
/// <returns>是否导入成功</returns>
//[HttpPost]
//[Route("import")]
[Authorize(SettleAccountPermissions.Materials.Create)]
//virtual public async Task<bool> ImportAsync(List<MaterialCreateDto> dtos)
//[Route("")]
//[Authorize(SettleAccountPermissions.Materials.Create)]
//virtual public async Task<MaterialDto> CreateAsync(MaterialCreateDto input)
//{
// var entities =ObjectMapper.Map<List<MaterialCreateDto>, List<Material>>(dtos);
// var result = await _repository.ImportAsync(entities);
// try
// {
// if (_repository.Any(p => p.MaterialCode == input.MaterialCode))
// {
// throw new BusinessException("8989", string.Format("已存在编号{0}的物料数据", input.MaterialCode));
// }
// var entity = new Material(
// GuidGenerator.Create(),
// input.BranchId,
// input.Factory,
// input.MaterialCode,
// input.MaterialDesc,
// input.Unit,
// input.EstimateType,
// input.EstimateTypeDesc
// );
// await _repository.InsertAsync(entity);
// var dto = ObjectMapper.Map<Material, MaterialDto>(entity);
// return dto;
// }
// catch
// {
// throw new BusinessException("", "数据项有重复,或其他问题请检查!");
// }
// return null;
// return result;
//}
/// <summary>
/// 新增实体
/// </summary>
/// <param name="input">新增实体DTO</param>
/// <returns>实体DTO</returns>
[HttpPost]
[Route("")]
[Authorize(SettleAccountPermissions.Materials.Create)]
virtual public async Task < MaterialDto > CreateAsync ( MaterialCreateDto input )
{
try
{
if ( _ repository . Any ( p = > p . MaterialCode = = input . MaterialCode ) )
{
throw new BusinessException ( "8989" , string . Format ( "已存在编号{0}的物料数据" , input . MaterialCode ) ) ;
}
var entity = new Material (
GuidGenerator . Create ( ) ,
input . BranchId ,
input . Factory ,
input . MaterialCode ,
input . MaterialDesc ,
input . Unit ,
input . EstimateType ,
input . EstimateTypeDesc
) ;
await _ repository . InsertAsync ( entity ) ;
var dto = ObjectMapper . Map < Material , MaterialDto > ( entity ) ;
return dto ;
}
catch
{
throw new BusinessException ( "" , "数据项有重复,或其他问题请检查!" ) ;
}
return null ;
}
/// <summary>
/// 修改实体
/// </summary>
/// <param name="id">ID</param>
/// <param name="input">修改实体DTO</param>
/// <returns>实体DTO</returns>
[HttpPut]
[Route("{id}")]
[Authorize(SettleAccountPermissions.Materials.Update)]
virtual public async Task < MaterialDto > UpdateAsync ( Guid id , MaterialUpdateDto input )
{
var entity = await _ repository . GetAsync ( id ) ;
await Cache . SetAsync ( id . ToString ( ) , entity , CacheStrategyConst . FIVE_MINUTES ) ;
///// <summary>
///// 修改实体
///// </summary>
///// <param name="id">ID</param>
///// <param name="input">修改实体DTO</param>
///// <returns>实体DTO</returns>
//[HttpPut]
//[Route("{id}")]
//[Authorize(SettleAccountPermissions.Materials.Update)]
//virtual public async Task<MaterialDto> UpdateAsync(Guid id, MaterialUpdateDto input)
//{
// var entity = await _repository.GetAsync(id);
var dto = ObjectMapper . Map < Material , MaterialDto > ( entity ) ;
return dto ;
// await Cache.SetAsync(id.ToString(), entity, CacheStrategyConst.FIVE_MINUTES);
// var dto = ObjectMapper.Map<Material, MaterialDto>(entity);
// return dto;
}
/// <summary>
/// 删除实体
/// </summary>
/// <param name="id">ID</param>
/// <returns>无</returns>
[HttpDelete]
[Route("{id}")]
[Authorize(SettleAccountPermissions.Materials.Delete)]
virtual public async Task DeleteAsync ( Guid id )
{
var entity = await GetFromCacheAsync ( id ) ;
await Cache . DeleteAsync < Material > ( id . ToString ( ) ) ;
await _ repository . DeleteAsync ( id ) ;
}
//}
/// <summary>
/// 按IDs删除实体列表
/// </summary>
/// <param name="ids">IDs</param>
/// <returns>是否执行成功</returns>
[HttpPost]
[Route("delete")]
[Authorize(SettleAccountPermissions.Materials.Delete)]
virtual public async Task < bool > DeleteListAsync ( List < Guid > ids )
{
var _ query = _ repository . Where ( p = > ids . Contains ( p . Id ) ) ;
int i = await _ query . BatchDeleteAsync ( ) ;
///// <summary>
///// 删除实体
///// </summary>
///// <param name="id">ID</param>
///// <returns>无</returns>
//[HttpDelete]
//[Route("{id}")]
//[Authorize(SettleAccountPermissions.Materials.Delete)]
//virtual public async Task DeleteAsync(Guid id)
//{
// var entity = await GetFromCacheAsync(id);
// await Cache.DeleteAsync<Material>(id.ToString());
// await _repository.DeleteAsync(id);
//}
if ( i = = 0 )
{
return false ;
}
return true ;
}
[HttpPost]
[Route("Export")]
[Authorize(SettleAccountPermissions.Materials.Default)]
virtual public async Task < string > ExportAsync ( MaterialRequestDto input )
{
string _f ileName = string . Format ( "物料主数据_{0}.xlsx" , DateTime . Now . ToString ( "yyyyMMdd" ) ) ;
var entities = await _ repository . GetListByFilterAsync ( input . BranchId , input . Filters , input . Sorting , int . MaxValue ,
0 , true ) ;
///// <summary>
///// 按IDs删除实体列表
///// </summary>
///// <param name="ids">IDs</param>
///// <returns>是否执行成功</returns>
//[HttpPost]
//[Route("delete")]
//[Authorize(SettleAccountPermissions.Materials.Delete)]
//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;
//}
//[HttpPost]
//[Route("Export")]
//[Authorize(SettleAccountPermissions.Materials.Default)]
//virtual public async Task<string> ExportAsync(MaterialRequestDto input)
//{
// string _fileName = string.Format("物料主数据_{0}.xlsx", DateTime.Now.ToString("yyyyMMdd"));
// var entities = await _repository.GetListByFilterAsync(input.BranchId, input.Filters, input.Sorting, int.MaxValue,
// 0, true);
var dtoDetails = ObjectMapper . Map < List < Material > , List < MaterialExportDto > > ( entities ) ;
// var dtoDetails = ObjectMapper.Map<List<Material>, List<MaterialExportDto>>(entities);
//声明导出容器
ExportImporter _ exportImporter = new ExportImporter ( ) ;
// //声明导出容器
// ExportImporter _exportImporter = new ExportImporter();
var result = await _ exportImporter . ExcelExporter ( dtoDetails ) ;
// var result = await _exportImporter.ExcelExporter(dtoDetails);
result . ShouldNotBeNull ( ) ;
// result.ShouldNotBeNull();
//保存导出文件到服务器存成二进制
await _ excelImportService . SaveBlobAsync (
new SaveExcelImportInputDto
{
Name = _f ileName ,
Content = result
}
) ;
return _f ileName ;
}
// //保存导出文件到服务器存成二进制
// await _excelImportService.SaveBlobAsync(
// new SaveExcelImportInputDto
// {
// Name = _fileName,
// Content = result
// }
// );
// return _fileName;
//}
#endregion