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.
 
 
 

354 lines
12 KiB

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using AutoMapper;
using AutoMapper.Configuration;
using FluentValidation;
using Microsoft.AspNetCore.Http;
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.MultiTenancy;
using Volo.Abp.Validation;
using IConfiguration = Microsoft.Extensions.Configuration.IConfiguration;
using Volo.Abp.TenantManagement;
using Win_in.Sfs.Scp.WebApi.Asns;
using Volo.Abp.Domain.Entities;
using System.Security.Cryptography;
using IdentityModel;
using Microsoft.Extensions.Options;
using Volo.Abp;
namespace Win_in.Sfs.Scp.WebApi
{
/// <summary>
/// ASN服务(X12Asn Service)
/// </summary>
[Authorize]
[Route(RouteConsts.X12Asn)]
[ApiExplorerSettings(GroupName = SwaggerGroupConsts.ScpWebApi)]
public class X12AsnAppService : ReadOnlyAppService<X12Asn, X12AsnDTO, Guid, RequestDTO>, IX12AsnAppService
{
private readonly IX12AsnRepository _x12AsnRepository;
private readonly ITenantRepository _tenantRepository;
private readonly IOptions<AsnOptions> _options;
public X12AsnAppService(
IX12AsnRepository repository
, ITenantRepository tenantRepository
, IOptions<AsnOptions> options
) : base(repository)
{
_x12AsnRepository = repository;
_tenantRepository = tenantRepository;
_options = options;
}
/// <summary>
/// 按ID获取ASN (Get X12Asn by ID)
/// </summary>
/// <param name="id">唯一ID(unique ID)</param>
/// <returns></returns>
[HttpGet]
[Route("{id}")]
[HiddenApi]
public override async Task<X12AsnDTO> GetAsync(Guid id)
{
return await base.GetAsync(id);
}
/// <summary>
/// 按条件获取ASN分页列表 (Get X12Asn paged 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<X12AsnDTO>> GetListAsync(RequestDTO requestDTO)
{
return await base.GetListAsync(requestDTO);
}
/// <summary>
/// 获取未读ASN列表(Get unread X12Asn list)
/// </summary>
/// <param name="site">地点(Site)</param>
/// <param name="count">数量(Count)</param>
/// <param name="autoUpdateStatus">是否自动更新状态(Auto update data status to finish)</param>
/// <returns></returns>
[HttpGet]
[Route("unread-list")]
public async Task<ListResultDto<X12AsnDTO>> GetUnreadListAsync(string site, int count, bool autoUpdateStatus)
{
try
{
Validator.CheckSite(_tenantRepository, site);
if (count < 1 || count > _options.Value.MaxCount)
{
throw new UserFriendlyException($"Count must between 1 and {_options.Value.MaxCount}");
}
}
catch (Exception ex)
{
throw new AbpValidationException(new List<ValidationResult>
{
new(ex.Message)
});
}
var entities = await _x12AsnRepository.GetUnreadListAsync(site, count, autoUpdateStatus);
var dtos = BuildDtos(entities);
return new ListResultDto<X12AsnDTO>(dtos);
}
[HttpGet]
[Route("nopo-unread-list")]
public async Task<ListResultDto<X12AsnDTO>> GetNoPoUnreadListAsync(string site, int count, bool autoUpdateStatus)
{
try
{
Validator.CheckSite(_tenantRepository, site);
if (count < 1 || count > _options.Value.MaxCount)
{
throw new UserFriendlyException($"Count must between 1 and {_options.Value.MaxCount}");
}
}
catch (Exception ex)
{
throw new AbpValidationException(new List<ValidationResult>
{
new(ex.Message)
});
}
var entities = await _x12AsnRepository.GetNoPoUnreadListAsync(site, count, autoUpdateStatus);
var dtos = BuildDtos(entities);
return new ListResultDto<X12AsnDTO>(dtos);
}
/// <summary>
/// 根据UID范围获取ASN列表(Get X12Asn list by UID range)
/// </summary>
/// <param name="site">地点(Site)</param>
/// <param name="beginUid">开始UID(Begin UID)</param>
/// <param name="endUid">结束UID(End UID)</param>
/// <returns></returns>
/// <exception cref="BadHttpRequestException"></exception>
[HttpGet]
[Route("between-uid")]
public async Task<ListResultDto<X12AsnDTO>> GetListAsync(string site, long beginUid, long endUid)
{
try
{
Validator.CheckSite(_tenantRepository, site);
if (endUid < beginUid)
{
throw new UserFriendlyException("beginUid can not bigger than endUid");
}
}
catch (Exception ex)
{
throw new AbpValidationException(new List<ValidationResult>
{
new(ex.Message)
});
}
var entities = await _x12AsnRepository.GetListAsync(p => p.Site == site && p.UID >= beginUid && p.UID <= endUid);
var dtos = BuildDtos(entities);
return new ListResultDto<X12AsnDTO>(dtos);
}
/// <summary>
/// 根据日期范围获取ASN列表(Get X12Asn list by date range)
/// </summary>
/// <param name="site">地点(Site)</param>
/// <param name="beginDate">开始日期(Begin date)</param>
/// <param name="endDate">结束日期(End date)</param>
/// <returns></returns>
/// <exception cref="BadHttpRequestException"></exception>
[HttpGet]
[Route("between-date")]
public async Task<ListResultDto<X12AsnDTO>> GetListAsync(string site, DateTime beginDate, DateTime endDate)
{
try
{
Validator.CheckSite(_tenantRepository, site);
if (endDate < beginDate)
{
throw new UserFriendlyException("beginDate can not after endDate");
}
}
catch (Exception ex)
{
throw new AbpValidationException(new List<ValidationResult>
{
new(ex.Message)
});
}
var entities = await _x12AsnRepository.GetListAsync(p => p.Site == site && p.EffectiveDate >= beginDate && p.EffectiveDate <= endDate);
var dtos = BuildDtos(entities);
return new ListResultDto<X12AsnDTO>(dtos);
}
/// <summary>
/// 根据UID获取ASN(Get X12Asn by UID)
/// </summary>
/// <param name="site">地点(Site)</param>
/// <param name="uid">UID(UID)</param>
/// <returns></returns>
/// <exception cref="BadHttpRequestException"></exception>
[HttpGet]
[Route("by-uid")]
public async Task<ActionResult<X12AsnDTO>> GetAsync(string site, long uid)
{
X12Asn entity;
try
{
Validator.CheckSite(_tenantRepository, site);
entity = await _x12AsnRepository.FirstOrDefaultAsync(p => p.Site == site && p.UID == uid);
if (entity == null)
{
throw new UserFriendlyException($"ASN of {uid} in {site} is not found");
}
}
catch (Exception ex)
{
throw new AbpValidationException(new List<ValidationResult>
{
new(ex.Message)
});
}
var dto = BuildDto(entity);
return dto;
}
/// <summary>
/// 根据单据编号获取ANS(Get X12Asn by asn number)
/// </summary>
/// <param name="site">地点(Site)</param>
/// <param name="number">单据编号(Asn number)</param>
/// <returns></returns>
/// <exception cref="BadHttpRequestException"></exception>
[HttpGet]
[Route("by-number")]
public async Task<ActionResult<X12AsnDTO>> GetAsync(string site, string number)
{
X12Asn entity;
try
{
Validator.CheckSite(_tenantRepository, site);
entity = await _x12AsnRepository.FirstOrDefaultAsync(p => p.Site == site && p.BillNum == number);
if (entity == null)
{
throw new UserFriendlyException($"ASN of {number} in {site} is not found");
}
}
catch (Exception ex)
{
throw new AbpValidationException(new List<ValidationResult>
{
new(ex.Message)
});
}
var dto = BuildDto(entity);
return dto;
}
/// <summary>
/// 根据单据编号更新ASN状态(Update X12Asn Status by asn number)
/// </summary>
/// <param name="site">地点(Site)</param>
/// <param name="number">单据编号(Asn number)</param>
/// <param name="status">状态(Status)
/// 0: 新增(new)
/// 1: 处理中(Processing)
/// 2: 完成(Finish)
/// 9: 搁置(Hold)
/// -1: 错误(Error)
/// </param>
/// <returns></returns>
/// <exception cref="BadHttpRequestException"></exception>
[HttpPost]
[Route("update-status")]
public async Task<ActionResult<X12AsnDTO>> UpdateStatusAsync(string site, string number, EnumExchangeDataStatus status)
{
X12Asn entity;
try
{
Validator.CheckSite(_tenantRepository, site);
entity = await _x12AsnRepository.FirstOrDefaultAsync(p => p.Site == site && p.BillNum == number);
if (entity == null)
{
throw new UserFriendlyException($"ASN of {number} in {site} is not found");
}
}
catch (Exception ex)
{
throw new AbpValidationException(new List<ValidationResult>
{
new(ex.Message)
});
}
var result = await _x12AsnRepository.UpdateStatusAsync(entity.Id, status);
var dto = BuildDto(result);
return dto;
}
private List<X12AsnDTO> BuildDtos(List<X12Asn> entities)
{
var dtos = ObjectMapper.Map<List<X12Asn>, List<X12AsnDTO>>(entities);
return dtos;
}
private X12AsnDTO BuildDto(X12Asn entity)
{
var dto = ObjectMapper.Map<X12Asn, X12AsnDTO>(entity);
return dto;
}
}
}