You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Application/Service/Fee/FeeCurrencyExchangeService.cs

187 lines
7.0 KiB
C#

using Furion.DependencyInjection;
11 months ago
using Furion.DynamicApiController;
using Furion.EventBus;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Application.Service.Fee;
11 months ago
using Myshipping.Application.Service.Fee.Dto;
using Myshipping.Core;
using Myshipping.Core.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace Myshipping.Application
{
/// <summary>
/// 汇率
/// </summary>
[ApiDescriptionSettings("Application", Name = "FeeCurrencyExchange", Order = 1)]
public class FeeCurrencyExchangeService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<FeeCurrencyExchange> _repCode;
private readonly ILogger<FeeCurrencyExchangeService> _logger;
private readonly ISysCacheService _cache;
private readonly IFeeCurrencyService _currencyService;
11 months ago
private readonly IEventPublisher _publisher;
public FeeCurrencyExchangeService(SqlSugarRepository<FeeCurrencyExchange> repCode,
ILogger<FeeCurrencyExchangeService> logger,
ISysCacheService cache,
IEventPublisher publisher,
IFeeCurrencyService currencyService)
11 months ago
{
_repCode = repCode;
_logger = logger;
_cache = cache;
_publisher = publisher;
_currencyService = currencyService;
11 months ago
}
/// <summary>
/// 费用代码查询
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/FeeCurrencyExchange/Page")]
public async Task<dynamic> Page(FeeCurrencyExchangePageInput input)
{
var entities = await _repCode.AsQueryable()
.WhereIF(!string.IsNullOrEmpty(input.Currency), u => u.Currency.Contains(input.Currency))
.WhereIF(input.StartTimeBegin.HasValue, u => u.StartTime > input.StartTimeBegin.Value)
.WhereIF(input.StartTimeEnd.HasValue, u => u.StartTime < input.StartTimeEnd.Value.AddDays(1))
.WhereIF(input.EndTimeBegin.HasValue, u => u.EndTime > input.EndTimeBegin.Value)
.WhereIF(input.EndTimeEnd.HasValue, u => u.EndTime < input.EndTimeEnd.Value.AddDays(1))
.ToPagedListAsync(input.PageNo, input.PageSize);
var result = entities.Adapt<SqlSugarPagedList<FeeCurrencyExchangePageOutput>>();
return result.XnPagedResult();
}
/// <summary>
/// 保存
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/FeeCurrencyExchange/Save")]
public async Task<FeeCurrencyExchangeSaveDto> Save(FeeCurrencyExchangeSaveDto input)
{
if (input == null)
{
throw Oops.Bah("请传入正常数据!");
}
var minDate = DateTime.Today.AddYears(-1);
if (input.StartTime < minDate || input.EndTime < minDate)
{
throw Oops.Bah("开始日期或结束日期有误!");
}
FeeCurrencyExchange entity = null;
if (input.Id == 0)
{
//判断时间范围重复
var cc = await _repCode.AsQueryable().Where(x => (input.StartTime >= x.StartTime && input.StartTime <= x.EndTime)
|| (input.EndTime >= x.StartTime && input.EndTime <= x.EndTime))
.CountAsync();
if (cc > 0)
{
throw Oops.Bah("日期范围存在交叉的数据!");
}
entity = input.Adapt<FeeCurrencyExchange>();
entity.Id = YitIdHelper.NextId();
await _repCode.InsertAsync(entity);
}
else
{
//判断时间范围重复
var cc = await _repCode.AsQueryable().Where(x => x.Id != input.Id &&
(
(input.StartTime >= x.StartTime && input.StartTime <= x.EndTime)
|| (input.EndTime >= x.StartTime && input.EndTime <= x.EndTime)
))
.CountAsync();
if (cc > 0)
{
throw Oops.Bah("日期范围存在交叉的数据!");
}
entity = await _repCode.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == input.Id);
entity = input.Adapt(entity);
await _repCode.UpdateAsync(entity);
}
return entity.Adapt<FeeCurrencyExchangeSaveDto>();
}
/// <summary>
/// 获取详情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet("/FeeCurrencyExchange/Detail")]
public async Task<FeeCurrencyExchangeSaveDto> Detail(long id)
{
var entity = await _repCode.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == id);
return entity.Adapt<FeeCurrencyExchangeSaveDto>();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
[HttpPost("/FeeCurrencyExchange/Delete")]
public async Task Delete(List<long> ids)
{
var list = await _repCode.AsQueryable().Filter(null, true).Where(x => ids.Contains(x.Id)).ToListAsync();
list.ForEach(x => x.IsDeleted = true);
await _repCode.AsUpdateable(list).ExecuteCommandAsync();
}
/// <summary>
/// 根据币别代码查询当前时间段的汇率或默认汇率
/// </summary>
/// <remarks>如果该币别没有维护当前时间段的汇率,则返回该币别的默认汇率</remarks>
/// <param name="currencyCode">币别代码</param>
/// <returns>币别当前时间段的汇率或默认汇率</returns>
[HttpGet("/FeeCurrencyExchange/QueryExchange")]
public async Task<FeeCurrencyExchangeDto> QueryExchange(string currencyCode)
{
FeeCurrencyExchange entity = await _repCode.AsQueryable()
.Where(x => x.Currency == currencyCode && x.StartTime <= DateTime.Now && x.EndTime >= DateTime.Now)
.FirstAsync();
if (entity != null)
return entity.Adapt<FeeCurrencyExchangeDto>();
var currencyList = await _currencyService.List();
var currency = currencyList.FirstOrDefault(c => c.CodeName == currencyCode) ?? throw Oops.Oh("请维护币别:" + currencyCode);
return new FeeCurrencyExchangeDto()
{
Currency = currencyCode,
CrValue = currency.DefaultRate,
DrValue = currency.DefaultRate,
Remark = $"币别{currencyCode}的默认汇率",
LocalCurr = "RMB"
};
}
11 months ago
}
}