using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Volo.Abp.Caching; using Volo.Abp.Domain.Repositories; using Win_in.Sfs.Basedata.Application.Contracts; using Win_in.Sfs.Basedata.Boms; using Win_in.Sfs.Basedata.Boms.DTOs; using Win_in.Sfs.Basedata.Domain; using Win_in.Sfs.Basedata.Domain.Shared; namespace Win_in.Sfs.Basedata.Application; [Authorize] [Route($"{BasedataConsts.RootPath}bom")] public class BomAppService : SfsBaseDataAppServiceBase, IBomAppService { private new readonly IBomRepository _repository; private readonly IBomManager _bomManager; private List _bomList = new(); public BomAppService(IBomRepository repository , IBomManager bomManager , IDistributedCache cache ) : base(repository, cache) { _repository = repository; _bomManager = bomManager; base.CreatePolicyName = BomPermissions.Create; base.UpdatePolicyName = BomPermissions.Update; base.DeletePolicyName = BomPermissions.Delete; } #region Get [HttpGet("get-by-productitemcode")] [AllowAnonymous] public virtual async Task> GetListAsync(string productItemCode, string mfgOp) { var entities = await _repository.GetListAsync(p => p.Product == productItemCode // && p.MFGOp == mfgOp ).ConfigureAwait(false); return ObjectMapper.Map, List>(entities); } [HttpGet("list/for-preparation-plan")] public virtual async Task> GetPlanListAsync(string productItemCode, string mfgOp) { var entities = await _bomManager.GetListWithPhantomItemAsync(productItemCode, mfgOp, Clock.Now, true) .ConfigureAwait(false); return ObjectMapper.Map, List>(entities); } [HttpGet("list/for-backflush")] public virtual async Task> GetBackFlushListAsync(string productItemCode, string mfgOp) { var entities = await _bomManager.GetListWithPhantomItemAsync(productItemCode, mfgOp, Clock.Now, false) .ConfigureAwait(false); return ObjectMapper.Map, List>(entities); } [HttpGet("get-by-productitemcode-sbs")] public virtual async Task> GetBomStepByStepAsync(string productItemCode, DateTime validTime, bool isStepByStep = true) { var entities = await _bomManager.GetRecursiveListAsync(productItemCode, validTime, isStepByStep) .ConfigureAwait(false); return ObjectMapper.Map, List>(entities); } /// /// 所有子物料号 /// /// /// [HttpGet("get-list-by-product")] public virtual async Task> GetListOfProductAsync(string product) { return await _bomManager.GetListOfProductAsync(product).ConfigureAwait(false); } /// /// 所有父物料号 /// /// /// [HttpGet("get-list-by-component")] public virtual async Task> GetListOfComponentAsync(string component) { return await _bomManager.GetListOfComponentAsync(component).ConfigureAwait(false); } /// /// 获取bomtree /// /// /// [HttpGet("get-bom-tree-by-code")] public virtual async Task> GetBomTreeByCodeAsync(string productCode) { var entities = await _bomManager.GetAllItemByCode(productCode).ConfigureAwait(false); var dtos = ObjectMapper.Map, List>(entities); return dtos; } /// /// 获取所有子物料关系 /// /// /// [HttpGet("get-list-by-component-with-tree")] public virtual async Task> GetBomTreeAsync(string component) { var entities = (await _bomManager.GetListOfProductAsync(component).ConfigureAwait(false)) .FindAll(t => t.Component != component); var dtos = ObjectMapper.Map, List>(entities); return await GetChildBomsAsync(dtos).ConfigureAwait(false); } private async Task> GetChildBomsAsync(List dtos) { foreach (var dto in dtos) { var entities = (await _bomManager.GetListOfProductAsync(dto.Component).ConfigureAwait(false)) .FindAll(t => t.Component != dto.Component); if (entities.Count > 0) { dto.ComponentDTOs = await GetChildBomsAsync(ObjectMapper.Map, List>(entities)) .ConfigureAwait(false); } } return dtos; } #endregion Get [HttpPost("upsert")] public virtual async Task UpsertAsync(BomEditInput input) { var entity = ObjectMapper.Map(input); entity.ComponentUom = input.ComponentUom; entity.ComponentQty = input.ComponentQty; await _repository.UpsertAsync(entity).ConfigureAwait(false); } [HttpPost("upsert-interface")] public virtual async Task UpsertAsyncByInterface(BomEditInput input) { var entity = ObjectMapper.Map(input); entity.ComponentUom = input.ComponentUom; entity.ComponentQty = input.ComponentQty; await _repository.UpsertAsyncByInterface(entity); } [HttpPost("add-upsert-lsit")] public virtual async Task AddOrUpsertListAsync(List inputs) { var entitys = new List(); foreach (var input in inputs) { var oldBom = await _repository .FirstOrDefaultAsync(t => t.Product == input.Product && t.Component == input.Component) .ConfigureAwait(false); if (oldBom == null) { var entity = ObjectMapper.Map(input); entity.ComponentUom = input.ComponentUom; entity.ComponentQty = input.ComponentQty; entitys.Add(entity); } else { oldBom.ComponentUom = input.ComponentUom; oldBom.ComponentQty = input.ComponentQty; oldBom.BeginTime = input.BeginTime; oldBom.EndTime = input.EndTime; entitys.Add(oldBom); } } await _repository.BulkMergeAsync(entitys).ConfigureAwait(false); } protected override async Task ValidateImportModelAsync(BomImportInput importInput, List validationRresult) { await CheckProductAsync(importInput.Product, validationRresult).ConfigureAwait(false); await CheckComponentAndComponentQtyAsync(importInput.Component, importInput.ComponentQty, validationRresult) .ConfigureAwait(false); } /// /// 根据总成号、总成数量取所有子物料及其汇总数量 /// /// 总成号 /// 总成数量 /// [HttpGet("get-material-total-qty")] public virtual async Task> GetMaterialTotalQtyAsync(string productCode, int productNum) { var entities = await _bomManager.GetMaterialTotalQtyAsync(productCode, productNum).ConfigureAwait(false); return ObjectMapper.Map, List>(entities); } [HttpPost("get-bom-sublist")] public async Task> GetSubcomponentsRecursiveList(List p_lst, EnumBomSelectedType p_type ) { var entities = ObjectMapper.Map, List>(p_lst); var list = await _bomManager.GetSubcomponentsRecursiveList(entities, p_type).ConfigureAwait(false); var sublist = ObjectMapper.Map, List>(list); return sublist; } }