using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; using FluentValidation; using Microsoft.Extensions.Configuration; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Data; using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; using Volo.Abp.Validation; using Win_in.Sfs.Scp.v1.Domain; using Volo.Abp.TenantManagement; namespace Win_in.Sfs.Scp.WebApi { /// /// 计划外入库单服务 /// [Authorize] [Route(RouteConsts.UnplannedReceipt)] [ApiExplorerSettings(GroupName = SwaggerGroupConsts.ScpWebApi)] public class UnplannedReceiptAppService : ReadOnlyAppService, IUnplannedReceiptAppService { private readonly IUnplannedReceiptRepository _unplannedReceiptRepository; private readonly ITbReceiptRepository _tbReceiptRepository; private readonly ITbReceiptDetailRepository _tbReceiptDetailRepository; private readonly ITaVenderRepository _taVenderRepository; private readonly ITenantStore _tenantStore; private readonly ITenantRepository _tenantRepository; private readonly IDataFilter _dataFilter; public UnplannedReceiptAppService( IUnplannedReceiptRepository repository , ITbReceiptRepository tbReceiptRepository , ITbReceiptDetailRepository tbReceiptDetailRepository , ITaVenderRepository taVenderRepository , ITenantStore tenantStore , ITenantRepository tenantRepository , IDataFilter dataFilter ) : base(repository) { _unplannedReceiptRepository = repository; _tbReceiptRepository = tbReceiptRepository; _tbReceiptDetailRepository = tbReceiptDetailRepository; _taVenderRepository = taVenderRepository; _tenantStore = tenantStore; _tenantRepository = tenantRepository; _dataFilter = dataFilter; } /// /// 按ID获取计划外入库单 (Get unplanned receipt by ID) /// /// 唯一ID(unique ID) /// [HttpGet] [Route("{id}")] [HiddenApi] public override async Task GetAsync(Guid id) { return await base.GetAsync(id); } /// /// 按请求条件获取计划外入库单列表(Get unplanned receipt list by request condition) /// /// 请求条件DTO(Request condition DTO) /// [HttpGet] [Route("paged-list")] [HiddenApi] public override async Task> GetListAsync(RequestDTO requestDTO) { return await base.GetListAsync(requestDTO); } /// /// 新增计划外入库单(Create unplanned receipt) /// /// /// [HttpPost] [Route("")] public virtual async Task> CreateAsync( UnplannedReceiptCreateDTO receiptCreateDTO) { var entity = ObjectMapper.Map(receiptCreateDTO); try { Validator.CheckSite(_tenantRepository, entity.Site); Validator.CheckSite(_tenantRepository, entity.Company); var tenant = await _tenantStore.FindAsync(entity.Site); using (CurrentTenant.Change(tenant.Id, tenant.Name)) { using (_dataFilter.Disable()) { Validator.CheckSupplierCode(_taVenderRepository, entity.Site, entity.Address); await UpsertTbReceiptAndTbReceiptDetailAsync(entity, tenant.Id); await CurrentUnitOfWork.SaveChangesAsync(); } } } catch (Exception ex) { var baseEx = ex.GetBaseException(); entity.ErrorCode = 1; entity.ErrorMessage = baseEx.Message; } var ret = await _unplannedReceiptRepository.InsertAsync(entity); var dto = ObjectMapper.Map(ret); dto.CreationTime = Clock.Now; if (dto.ErrorCode != 0) { throw new AbpValidationException(new List { new(dto.ErrorMessage) }); } else { return new OkObjectResult(dto); } } private async Task UpsertTbReceiptAndTbReceiptDetailAsync(UnplannedReceipt entity, Guid tenantId) { //使用AutoMapper执行类型转换 var tbReceipt = ObjectMapper.Map(entity); var tbReceiveDetail = ObjectMapper.Map(entity); tbReceipt.TenantId = tenantId; //根据传入数据新增或修改SCP数据 await _tbReceiptRepository.UpsertAsync(tbReceipt); tbReceiveDetail.TenantId = tenantId; //根据传入数据新增或修改SCP数据 await _tbReceiptDetailRepository.UpsertAsync(tbReceiveDetail); } } }