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
{
///
/// ASN服务(X12Asn Service)
///
[Authorize]
[Route(RouteConsts.X12Asn)]
[ApiExplorerSettings(GroupName = SwaggerGroupConsts.ScpWebApi)]
public class X12AsnAppService : ReadOnlyAppService, IX12AsnAppService
{
private readonly IX12AsnRepository _x12AsnRepository;
private readonly ITenantRepository _tenantRepository;
private readonly IOptions _options;
public X12AsnAppService(
IX12AsnRepository repository
, ITenantRepository tenantRepository
, IOptions options
) : base(repository)
{
_x12AsnRepository = repository;
_tenantRepository = tenantRepository;
_options = options;
}
///
/// 按ID获取ASN (Get X12Asn by ID)
///
/// 唯一ID(unique ID)
///
[HttpGet]
[Route("{id}")]
[HiddenApi]
public override async Task GetAsync(Guid id)
{
return await base.GetAsync(id);
}
///
/// 按条件获取ASN分页列表 (Get X12Asn paged 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);
}
///
/// 获取未读ASN列表(Get unread X12Asn list)
///
/// 地点(Site)
/// 数量(Count)
/// 是否自动更新状态(Auto update data status to finish)
///
[HttpGet]
[Route("unread-list")]
public async Task> 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
{
new(ex.Message)
});
}
var entities = await _x12AsnRepository.GetUnreadListAsync(site, count, autoUpdateStatus);
var dtos = BuildDtos(entities);
return new ListResultDto(dtos);
}
[HttpGet]
[Route("nopo-unread-list")]
public async Task> 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
{
new(ex.Message)
});
}
var entities = await _x12AsnRepository.GetNoPoUnreadListAsync(site, count, autoUpdateStatus);
var dtos = BuildDtos(entities);
return new ListResultDto(dtos);
}
///
/// 根据UID范围获取ASN列表(Get X12Asn list by UID range)
///
/// 地点(Site)
/// 开始UID(Begin UID)
/// 结束UID(End UID)
///
///
[HttpGet]
[Route("between-uid")]
public async Task> 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
{
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(dtos);
}
///
/// 根据日期范围获取ASN列表(Get X12Asn list by date range)
///
/// 地点(Site)
/// 开始日期(Begin date)
/// 结束日期(End date)
///
///
[HttpGet]
[Route("between-date")]
public async Task> 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
{
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(dtos);
}
///
/// 根据UID获取ASN(Get X12Asn by UID)
///
/// 地点(Site)
/// UID(UID)
///
///
[HttpGet]
[Route("by-uid")]
public async Task> 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
{
new(ex.Message)
});
}
var dto = BuildDto(entity);
return dto;
}
///
/// 根据单据编号获取ANS(Get X12Asn by asn number)
///
/// 地点(Site)
/// 单据编号(Asn number)
///
///
[HttpGet]
[Route("by-number")]
public async Task> 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
{
new(ex.Message)
});
}
var dto = BuildDto(entity);
return dto;
}
///
/// 根据单据编号更新ASN状态(Update X12Asn Status by asn number)
///
/// 地点(Site)
/// 单据编号(Asn number)
/// 状态(Status)
/// 0: 新增(new)
/// 1: 处理中(Processing)
/// 2: 完成(Finish)
/// 9: 搁置(Hold)
/// -1: 错误(Error)
///
///
///
[HttpPost]
[Route("update-status")]
public async Task> 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
{
new(ex.Message)
});
}
var result = await _x12AsnRepository.UpdateStatusAsync(entity.Id, status);
var dto = BuildDto(result);
return dto;
}
private List BuildDtos(List entities)
{
var dtos = ObjectMapper.Map, List>(entities);
return dtos;
}
private X12AsnDTO BuildDto(X12Asn entity)
{
var dto = ObjectMapper.Map(entity);
return dto;
}
}
}