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.
363 lines
16 KiB
363 lines
16 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
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.EntityFrameworkCore;
|
|
using NUglify.Helpers;
|
|
using Omu.ValueInjecter;
|
|
using Volo.Abp;
|
|
using Volo.Abp.Caching;
|
|
using Volo.Abp.Domain.Entities;
|
|
using Volo.Abp.Domain.Repositories;
|
|
using Volo.Abp.ObjectMapping;
|
|
using Volo.Abp.Uow;
|
|
using Win_in.Sfs.Basedata.Application.Contracts;
|
|
using Win_in.Sfs.Basedata.Domain;
|
|
using Win_in.Sfs.Basedata.Domain.Shared;
|
|
using Win_in.Sfs.Basedata.Kittings.Inputs;
|
|
using Win_in.Sfs.Shared;
|
|
using Win_in.Sfs.Shared.Application.Contracts;
|
|
using Win_in.Sfs.Shared.Application.Contracts.ExportAndImport;
|
|
|
|
|
|
namespace Win_in.Sfs.Basedata.Application;
|
|
|
|
[Authorize]
|
|
[Route($"{BasedataConsts.RootPath}kitting")]
|
|
|
|
public class KittingAppService : SfsBaseDataWithCodeAppServiceBase<Kitting, KittingDTO, SfsBaseDataRequestInputBase, KittingEditInput, KittingImportInput>, IKittingAppService
|
|
{
|
|
private new readonly IKittingRepository _repository;
|
|
|
|
private readonly IExportImportService _excelService;
|
|
|
|
private new readonly IItemBasicRepository _itmBasicRepository ;
|
|
|
|
|
|
|
|
private readonly IKittingManager _manager;
|
|
|
|
public KittingAppService(IKittingRepository repository, IDistributedCache<KittingDTO> cache, IKittingManager manager,
|
|
IExportImportService excelService,
|
|
IItemBasicRepository itmBasicRepository
|
|
) : base(repository,cache)
|
|
{
|
|
_repository = repository;
|
|
_manager = manager;
|
|
_excelService = excelService;
|
|
_itmBasicRepository = itmBasicRepository;
|
|
//base.CreatePolicyName = KittingPermissions.Create;
|
|
//base.UpdatePolicyName = KittingPermissions.Update;
|
|
//base.DeletePolicyName = KittingPermissions.Delete;
|
|
}
|
|
[HttpPost]
|
|
[Route("")]
|
|
public override async Task<KittingDTO> CreateAsync(KittingEditInput input)
|
|
{
|
|
|
|
var detailquery=await _repository.WithDetailsAsync().ConfigureAwait(false);
|
|
var first = detailquery.FirstOrDefault(p => p.Code == input.Code);
|
|
var codes = input.Details.Select(p => p.ItemCode);
|
|
if (first != null)
|
|
{
|
|
throw new UserFriendlyException($"已存在编码:{input.Code}的Kitting箱");
|
|
//var query = from itm in input.Details
|
|
// join itm1 in first.Details on itm.PartCode equals itm1.PartCode
|
|
// into temp1
|
|
// from tm1 in temp1.DefaultIfEmpty()
|
|
// where tm1 == null
|
|
// select itm;
|
|
//var details = ObjectMapper.Map<List<KittingDetailInput>, List<KittingDetail>>(query.ToList());
|
|
//foreach (var itm in details)
|
|
//{
|
|
// itm.SetId(GuidGenerator.Create());
|
|
// itm.MasterId = first.Id;
|
|
// first.AddDetails(itm);
|
|
//}
|
|
//var entity= await _repository.UpdateAsync(first).ConfigureAwait(false);
|
|
//return ObjectMapper.Map<Kitting, KittingDTO>(entity);
|
|
}
|
|
else
|
|
{
|
|
var entity = ObjectMapper.Map<KittingEditInput, Kitting>(input);
|
|
entity.SetId(Guid.NewGuid());
|
|
entity.Details.ForEach(item =>
|
|
{
|
|
item.MasterId = entity.Id;
|
|
});
|
|
entity = await _repository.InsertAsync(entity).ConfigureAwait(false);
|
|
return ObjectMapper.Map<Kitting, KittingDTO>(entity);
|
|
}
|
|
|
|
//first.Details.Where(p => codes.Contains(p.PartCode));
|
|
//var entity = ObjectMapper.Map<KittingEditInput, Kitting>(input);
|
|
//entity.SetId(Guid.NewGuid());
|
|
//entity.Details.ForEach(item =>
|
|
//{
|
|
// item.MasterId = entity.Id;
|
|
//});
|
|
//entity = await _repository.InsertAsync(entity).ConfigureAwait(false);
|
|
//return ObjectMapper.Map<Kitting, KittingDTO>(entity);
|
|
}
|
|
|
|
[HttpPut]
|
|
[Route("{id}")]
|
|
public override async Task<KittingDTO> UpdateAsync(Guid id, KittingEditInput input)
|
|
{
|
|
return await base.UpdateAsync(id, input).ConfigureAwait(false);
|
|
}
|
|
|
|
[HttpPost("update")]
|
|
public virtual async Task UpdateAsync(KittingEditInput input)
|
|
{
|
|
var entity = ObjectMapper.Map<KittingEditInput, Kitting>(input);
|
|
//var dic=await _repository.GetAsync(r => r.Code == entity.Code);
|
|
// if (dic != null)
|
|
// {
|
|
// await _repository.DeleteAsync(dic);
|
|
// }
|
|
// await _repository.InsertAsync(entity);
|
|
await _repository.UpdateAsync(entity).ConfigureAwait(false);
|
|
}
|
|
|
|
|
|
[HttpPost("upsert")]
|
|
public virtual async Task UpsertAsync(KittingEditInput input)
|
|
{
|
|
var entity = ObjectMapper.Map<KittingEditInput, Kitting>(input);
|
|
await _repository.UpsertAsync(entity).ConfigureAwait(false);
|
|
}
|
|
[HttpPost("import")]
|
|
[Consumes("multipart/form-data")]
|
|
|
|
public override async Task<IActionResult> ImportAsync([FromForm] SfsImportRequestInput requestInput, [Required] IFormFile file)
|
|
{
|
|
var query= _repository.WithDetails().AsNoTracking();
|
|
using var ms = new MemoryStream();
|
|
await file.OpenReadStream().CopyToAsync(ms).ConfigureAwait(false);
|
|
var inputFileBytes = ms.GetAllBytes();
|
|
var importList= _excelService.Import<KittingImportInput>(inputFileBytes).ToList();
|
|
var checklist=importList.GroupBy(p => new { p.Code, p.ItemCode }).Where(g => g.Count() > 1).Select(p => new { Code = p.Key.Code, PartCode = p.Key.ItemCode });//导入重复报错 //检测是否导入重复
|
|
List<KittingErrorDto> errors = new List<KittingErrorDto>();
|
|
|
|
|
|
var importPartList = importList.Select(p => p.ItemCode).Distinct().ToList();
|
|
var partContainList = await _itmBasicRepository.GetListAsync(p => importPartList.Distinct().Contains(p.Code)).ConfigureAwait(false);
|
|
|
|
var partCodeList1 = partContainList.Select(p => p.Code);
|
|
|
|
var errorQuery = from itm in importPartList join itm1 in partCodeList1 on itm equals itm1 into temp from tm in temp.DefaultIfEmpty() where tm == null select itm;
|
|
|
|
if (errorQuery.Any())
|
|
{
|
|
foreach (var error in errorQuery)
|
|
{
|
|
errors.Add(new KittingErrorDto() { Code = "", PartCode = error, Content = "不在物品基础信息!" });
|
|
}
|
|
}
|
|
if (checklist.Any())
|
|
{
|
|
foreach (var error in checklist) {
|
|
errors.Add(new KittingErrorDto() { Code=error.Code,PartCode=error.PartCode, Content="记录有重复!" });
|
|
}
|
|
}
|
|
if (errors.Count > 0)
|
|
{
|
|
var fileContent = _excelService.Export(errors);
|
|
return new TestResult(fileContent.FileContents, ExportImportService.ContentType) { FileDownloadName = "错误信息" };
|
|
}
|
|
foreach (var itm in importList)
|
|
{
|
|
itm.ItemName = partContainList.FirstOrDefault(p => p.Code == itm.ItemCode)?.Name;
|
|
itm.ItemDesc1 = partContainList.FirstOrDefault(p => p.Code == itm.ItemCode)?.Desc1;
|
|
itm.ItemDesc2 = partContainList.FirstOrDefault(p => p.Code == itm.ItemCode)?.Desc2;
|
|
}
|
|
var codeList = importList.Select(p => p.Code).Distinct().ToList();
|
|
var includeList = query.Where(p => codeList.Contains(p.Code)).ToList();
|
|
var existCodeList = includeList.Select(p => p.Code).ToList();
|
|
var newCodeList = from itm in codeList
|
|
join itm1 in existCodeList on itm equals itm1
|
|
into temp
|
|
from tm in temp.DefaultIfEmpty()
|
|
where tm == null
|
|
select itm;
|
|
if(newCodeList.Any())
|
|
{
|
|
#region 新KITTING
|
|
var newKittingList = importList.Where(p => newCodeList.Contains(p.Code)).ToList();//新Kitting
|
|
List<KittingEditInput> newKittingInputList = new List<KittingEditInput>();
|
|
List<Kitting> newMainList = new List<Kitting>();
|
|
List<KittingDetail> newdetailList = new List<KittingDetail>();
|
|
foreach (var itm in newCodeList)
|
|
{
|
|
var newDetail = newKittingList.Where(p => p.Code == itm).ToList();
|
|
var first = newDetail.FirstOrDefault();
|
|
var kitting = new KittingEditInput();
|
|
kitting.InjectFrom(first);
|
|
foreach (var detail in newDetail)
|
|
{
|
|
var inputdetail = new KittingDetailInput();
|
|
inputdetail.InjectFrom(detail);
|
|
kitting.Details.Add(inputdetail);
|
|
}
|
|
newKittingInputList.Add(kitting);
|
|
}
|
|
var kittingList = ObjectMapper.Map<List<KittingEditInput>, List<Kitting>>(newKittingInputList);
|
|
|
|
|
|
foreach (var itm in kittingList)
|
|
{
|
|
var id = Guid.NewGuid();
|
|
itm.SetId(id);
|
|
foreach (var detail in itm.Details)
|
|
{
|
|
detail.SetId(Guid.NewGuid());
|
|
detail.MasterId = id;
|
|
|
|
}
|
|
}
|
|
|
|
await _repository.InsertManyAsync(kittingList).ConfigureAwait(false);
|
|
|
|
#endregion
|
|
}
|
|
if (existCodeList.Any())
|
|
{
|
|
var existKittingList = importList.Where(p => existCodeList.Contains(p.Code)).ToList();//新Kitting
|
|
List<KittingEditInput> existKittingInputList = new List<KittingEditInput>();
|
|
foreach (var include in includeList)
|
|
{
|
|
var newDetail = existKittingList.Where(p => p.Code == include.Code).ToList();
|
|
var innerExist = from itm in newDetail
|
|
join itm1 in include.Details on itm.ItemCode equals itm1.ItemCode
|
|
select itm;
|
|
if (innerExist.Any())
|
|
{
|
|
var list = innerExist.ToList();
|
|
foreach (var itm in include.Details)
|
|
{
|
|
var entity = list.FirstOrDefault(p => p.ItemCode == itm.ItemCode);
|
|
itm.ItemDesc1 = entity.ItemDesc1;
|
|
itm.ItemDesc2 = entity.ItemDesc2;
|
|
itm.Qty = entity.Qty;
|
|
itm.Configuration = entity.Configuration;
|
|
}
|
|
}
|
|
var leftExist = from itm in newDetail
|
|
join itm1 in include.Details on itm.ItemCode equals itm1.ItemCode
|
|
into temp
|
|
from tm in temp.DefaultIfEmpty()
|
|
where tm == null
|
|
select itm;
|
|
if (leftExist.Any())
|
|
{
|
|
var list = leftExist.ToList();
|
|
foreach (var itm in list)
|
|
{
|
|
var detail = new KittingDetail();
|
|
detail.InjectFrom(itm);
|
|
detail.SetId(Guid.NewGuid());
|
|
detail.MasterId = include.Id;
|
|
}
|
|
}
|
|
|
|
//var entity = new KittingEditInput();
|
|
//entity.InjectFrom(include);
|
|
|
|
//foreach (var detail in include.Details)
|
|
//{
|
|
// var detailInput = new KittingDetailInput();
|
|
// detailInput.InjectFrom(detail);
|
|
// entity.Details.Add(detailInput);
|
|
|
|
//}
|
|
|
|
//var newDetail = existKittingList.Where(p => p.Code == entity.Code).ToList();
|
|
//var innerExist = from itm in newDetail
|
|
// join itm1 in entity.Details on itm.PartCode equals itm1.PartCode
|
|
// select itm;
|
|
//if (innerExist.Any())
|
|
//{
|
|
// var list = innerExist.ToList();
|
|
// foreach (var itm in entity.Details)
|
|
// {
|
|
// var l = list.FirstOrDefault(p => p.PartCode == itm.PartCode);
|
|
// itm.Desc1 = l.Desc1;
|
|
// itm.Desc2 = l.Desc2;
|
|
// itm.Qty = l.Qty;
|
|
// itm.Conf = l.Conf;
|
|
// }
|
|
//}
|
|
//var leftExist = from itm in newDetail
|
|
// join itm1 in entity.Details on itm.PartCode equals itm1.PartCode
|
|
// into temp
|
|
// from tm in temp.DefaultIfEmpty()
|
|
// where tm == null
|
|
// select itm;
|
|
//if (leftExist.Any())
|
|
//{
|
|
// var list = leftExist.ToList();
|
|
// foreach (var itm in list)
|
|
// {
|
|
// var detail = new KittingDetailInput();
|
|
// detail.InjectFrom(itm);
|
|
// detail.Id = Guid.NewGuid();
|
|
// detail.MasterId = entity.Id;
|
|
// entity.Details.Add(detail);
|
|
// }
|
|
//}
|
|
//existKittingInputList.Add(entity);
|
|
}
|
|
// List<Kitting> list1 = new List<Kitting>();
|
|
// foreach (var itm in existKittingInputList)
|
|
// {
|
|
// Kitting kitting = new Kitting();
|
|
// kitting.InjectFrom(itm);
|
|
// kitting.SetId(itm.Id);
|
|
// foreach (var detail in itm.Details)
|
|
// {
|
|
// KittingDetail kittingDetail = new KittingDetail();
|
|
// kittingDetail.InjectFrom(detail);
|
|
// kittingDetail.SetId(detail.Id);
|
|
// kitting.AddDetails(kittingDetail);
|
|
// }
|
|
// list1.Add(kitting);
|
|
// }
|
|
//var details= list1.Select(p => p.Details).ToList();
|
|
// //await _repository.GetDbContext().BulkInsertAsync(newKittingDetailList).ConfigureAwait(continueOnCapturedContext: false);
|
|
// //await _repository.GetDbContext().BulkUpdateAsync(existKittingDetailList).ConfigureAwait(continueOnCapturedContext: false);
|
|
// // var ls= ObjectMapper.Map< List<KittingEditInput> ,List <Kitting>>(existKittingInputList);
|
|
// //await _repository.UpdateManyAsync(list1,true).ConfigureAwait(continueOnCapturedContext: false);
|
|
// await _manager.ImportDataAsync(includeList).ConfigureAwait(false);
|
|
}
|
|
|
|
if (errors.Count > 0)
|
|
{
|
|
var fileContent= _excelService.Export(errors);
|
|
return new TestResult(fileContent.FileContents, ExportImportService.ContentType) { FileDownloadName="错误信息" };
|
|
}
|
|
|
|
var fileContent1 = _excelService.Export(importList);
|
|
return new TestResult(fileContent1.FileContents, ExportImportService.ContentType) { FileDownloadName = "Kitting导入文件" };
|
|
// return new JsonResult(new { Code = 200, FileDownloadName="" });
|
|
}
|
|
|
|
|
|
|
|
protected override async Task<KittingDTO> GetFromRepositoryAsync(string code)
|
|
{
|
|
var displayName = typeof(KittingDTO).GetNameOfDisplay();
|
|
|
|
var query = await _repository.WithDetailsAsync().ConfigureAwait(false);
|
|
|
|
var entity = await query.Where(p => p.Code == code).FirstOrDefaultAsync().ConfigureAwait(false);
|
|
|
|
return ObjectMapper.Map<Kitting, KittingDTO>(entity);
|
|
}
|
|
}
|
|
|