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.
130 lines
4.5 KiB
130 lines
4.5 KiB
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Volo.Abp.Application.Dtos;
|
|
using Volo.Abp.Application.Services;
|
|
using Win_in.Sfs.Scp.v1.Domain;
|
|
|
|
namespace Win_in.Sfs.Scp.WebApi
|
|
{
|
|
/// <summary>
|
|
/// 采购订单服务
|
|
/// </summary>
|
|
[Authorize]
|
|
[Route(RouteConsts.PurchaseOrder)]
|
|
[ApiExplorerSettings(GroupName = SwaggerGroupConsts.ScpWebApi)]
|
|
public class PurchaseOrderAppService : ReadOnlyAppService<PurchaseOrder, PurchaseOrderDTO, Guid, RequestDTO>, IPurchaseOrderAppService
|
|
{
|
|
private readonly IPurchaseOrderRepository _purchaseOrderRepository;
|
|
private readonly ITbPoRepository _tbPoRepository;
|
|
private readonly ITbPoDetailRepository _tbPoDetailRepository;
|
|
private readonly ITaVenderRepository _taVenderRepository;
|
|
private readonly string _validSites = Validator.VALID_SITES;
|
|
|
|
public PurchaseOrderAppService(
|
|
IPurchaseOrderRepository repository
|
|
, ITbPoRepository tbPoRepository
|
|
, ITbPoDetailRepository tbPoDetailRepository
|
|
, ITaVenderRepository taVenderRepository
|
|
, IConfiguration configuration
|
|
) : base(repository)
|
|
{
|
|
_purchaseOrderRepository = repository;
|
|
_tbPoRepository = tbPoRepository;
|
|
_tbPoDetailRepository = tbPoDetailRepository;
|
|
_taVenderRepository = taVenderRepository;
|
|
_validSites = configuration["ValidSites"];
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 按ID获取采购订单 (Get purchase order by ID)
|
|
/// </summary>
|
|
/// <param name="id">唯一ID(unique ID)</param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[Route("{id}")]
|
|
public override async Task<PurchaseOrderDTO> GetAsync(Guid id)
|
|
{
|
|
return await base.GetAsync(id);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 按条件获取采购订单列表 (Get purchase order list by request condition)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[Route("")]
|
|
public override async Task<PagedResultDto<PurchaseOrderDTO>> GetListAsync(RequestDTO input)
|
|
{
|
|
return await base.GetListAsync(input);
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 新增采购订单(Create New purchase order)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[Route("")]
|
|
public virtual async Task<PurchaseOrderDTO> CreateAsync(PurchaseOrderCreateDTO poCreateDTO)
|
|
{
|
|
var entity = ObjectMapper.Map<PurchaseOrderCreateDTO, PurchaseOrder>(poCreateDTO);
|
|
|
|
if (entity.PoType == "0")
|
|
{
|
|
entity.PoType = "1";
|
|
}
|
|
|
|
foreach (var detail in entity.Details)
|
|
{
|
|
detail.SetId(GuidGenerator);
|
|
}
|
|
|
|
try
|
|
{
|
|
Validator.CheckSite(_validSites,entity.Site);
|
|
Validator.CheckSupplierCode(_taVenderRepository,entity.Site, entity.SupplierCode);
|
|
await UpsertTbPoAndTbPoDetailAsync(entity);
|
|
await CurrentUnitOfWork.SaveChangesAsync();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var baseEx = ex.GetBaseException();
|
|
entity.ErrorCode = 1;
|
|
entity.ErrorMessage = baseEx.Message;
|
|
}
|
|
var ret = await _purchaseOrderRepository.InsertAsync(entity);
|
|
var dto = ObjectMapper.Map<PurchaseOrder, PurchaseOrderDTO>(ret);
|
|
return dto;
|
|
}
|
|
|
|
private async Task UpsertTbPoAndTbPoDetailAsync(PurchaseOrder entity)
|
|
{
|
|
//使用AutoMapper执行类型转换
|
|
var tbPo = ObjectMapper.Map<PurchaseOrder, TB_PO>(entity);
|
|
//根据传入数据新增或修改SCP数据
|
|
await _tbPoRepository.UpsertAsync(tbPo);
|
|
|
|
var poDetails = entity.Details;
|
|
|
|
foreach (var poDetail in poDetails)
|
|
{
|
|
//使用AutoMapper执行类型转换
|
|
var tbPoDetail = ObjectMapper.Map<PurchaseOrderDetail, TB_PO_DETAIL>(poDetail);
|
|
//接口数据中没有Site,从主表中获取
|
|
tbPoDetail.Site = tbPo.Site;
|
|
tbPoDetail.BeginTime = tbPo.BeginTime;
|
|
tbPoDetail.EndTime = tbPo.EndTime;
|
|
//根据传入数据新增或修改SCP数据
|
|
await _tbPoDetailRepository.UpsertAsync(tbPoDetail);
|
|
}
|
|
}
|
|
}
|
|
}
|