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.
144 lines
4.6 KiB
144 lines
4.6 KiB
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks.Dataflow;
|
|
using AutoMapper;
|
|
using AutoMapper.Configuration;
|
|
using FluentValidation;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Volo.Abp.Application.Dtos;
|
|
using Volo.Abp.Application.Services;
|
|
using Volo.Abp.Domain.Repositories;
|
|
using Volo.Abp.Uow;
|
|
using Win_in.Sfs.Scp.v1.Domain;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Volo.Abp.Clients;
|
|
using Volo.Abp.Data;
|
|
using Volo.Abp.MultiTenancy;
|
|
using Volo.Abp.Validation;
|
|
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;
|
|
using Volo.Abp.TenantManagement;
|
|
|
|
namespace Win_in.Sfs.Scp.WebApi
|
|
{
|
|
/// <summary>
|
|
/// 零件服务
|
|
/// </summary>
|
|
[Authorize]
|
|
[Route(RouteConsts.Part)]
|
|
[ApiExplorerSettings(GroupName = SwaggerGroupConsts.ScpWebApi)]
|
|
|
|
public class PartAppService : ReadOnlyAppService<Part, PartDTO, Guid, RequestDTO>, IPartAppService
|
|
{
|
|
private readonly IPartRepository _partRepository;
|
|
private readonly ITaPartRepository _taPartRepository;
|
|
private readonly ITenantStore _tenantStore;
|
|
private readonly ITenantRepository _tenantRepository;
|
|
private readonly IDataFilter _dataFilter;
|
|
|
|
public PartAppService(
|
|
IPartRepository repository
|
|
, ITaPartRepository taPartRepository
|
|
, ITenantStore tenantStore
|
|
, ITenantRepository tenantRepository
|
|
, IDataFilter dataFilter
|
|
) : base(repository)
|
|
{
|
|
_partRepository = repository;
|
|
_taPartRepository = taPartRepository;
|
|
_tenantStore = tenantStore;
|
|
_tenantRepository = tenantRepository;
|
|
_dataFilter = dataFilter;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按ID获取零件 (Get part by ID)
|
|
/// </summary>
|
|
/// <param name="id">唯一ID(unique ID)</param>
|
|
/// <returns></returns>
|
|
|
|
[HttpGet]
|
|
[Route("{id}")]
|
|
[HiddenApi]
|
|
public override async Task<PartDTO> GetAsync(Guid id)
|
|
{
|
|
return await base.GetAsync(id);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 按条件获取零件列表 (Get part list by request condition)
|
|
/// </summary>
|
|
/// <param name="requestDTO">请求条件DTO(Request condition DTO)</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[Route("paged-list")]
|
|
[HiddenApi]
|
|
public override async Task<PagedResultDto<PartDTO>> GetListAsync(RequestDTO requestDTO)
|
|
{
|
|
return await base.GetListAsync(requestDTO);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增零件(Create New part)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[Route("")]
|
|
public virtual async Task<ActionResult<PartDTO>> CreateAsync(PartCreateDto partCreateDTO)
|
|
{
|
|
var entity = ObjectMapper.Map<PartCreateDto, Part>(partCreateDTO);
|
|
|
|
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<IMultiTenant>())
|
|
{
|
|
|
|
await UpsertTaPartAsync(entity, tenant.Id);
|
|
await CurrentUnitOfWork.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var baseEx = ex.GetBaseException();
|
|
entity.ErrorCode = 1;
|
|
entity.ErrorMessage = baseEx.Message;
|
|
}
|
|
|
|
var ret = await _partRepository.InsertAsync(entity);
|
|
|
|
var dto = ObjectMapper.Map<Part, PartDTO>(ret);
|
|
dto.CreationTime = Clock.Now;
|
|
if (dto.ErrorCode != 0)
|
|
{
|
|
throw new AbpValidationException(new List<ValidationResult>
|
|
{
|
|
new(dto.ErrorMessage)
|
|
});
|
|
}
|
|
else
|
|
{
|
|
return new OkObjectResult(dto);
|
|
}
|
|
}
|
|
|
|
private async Task UpsertTaPartAsync(Part entity, Guid tenantId)
|
|
{
|
|
//使用AutoMapper执行类型转换
|
|
var taPart = ObjectMapper.Map<Part, TA_PART>(entity);
|
|
|
|
taPart.TenantId = tenantId;
|
|
//根据传入数据新增或修改SCP数据
|
|
await _taPartRepository.UpsertAsync(taPart);
|
|
}
|
|
}
|
|
}
|
|
|