|
|
|
@ -1,33 +1,24 @@
|
|
|
|
|
using DS.Module.Core;
|
|
|
|
|
using DS.Module.SqlSugar;
|
|
|
|
|
using DS.Module.UserModule;
|
|
|
|
|
using DS.Module.Core.Extensions;
|
|
|
|
|
using DS.WMS.Core.Fee.Dtos;
|
|
|
|
|
using DS.WMS.Core.Fee.Entity;
|
|
|
|
|
using DS.WMS.Core.Fee.Interface;
|
|
|
|
|
using DS.WMS.Core.Sys.Entity;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using Mapster;
|
|
|
|
|
using DS.Module.Core.Extensions;
|
|
|
|
|
|
|
|
|
|
namespace DS.WMS.Core.Fee.Method
|
|
|
|
|
{
|
|
|
|
|
public class FeeCurrencyExchangeService : IFeeCurrencyExchangeService
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 汇率服务
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class FeeCurrencyExchangeService : FeeServiceBase, IFeeCurrencyExchangeService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private readonly ISqlSugarClient db;
|
|
|
|
|
private readonly IUser user;
|
|
|
|
|
private readonly ISaasDbService saasService;
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="serviceProvider"></param>
|
|
|
|
|
public FeeCurrencyExchangeService(IServiceProvider serviceProvider)
|
|
|
|
|
public FeeCurrencyExchangeService(IServiceProvider serviceProvider) : base(serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
db = _serviceProvider.GetRequiredService<ISqlSugarClient>();
|
|
|
|
|
user = _serviceProvider.GetRequiredService<IUser>();
|
|
|
|
|
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -37,11 +28,9 @@ namespace DS.WMS.Core.Fee.Method
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public DataResult<List<FeeCurrencyExchangeRes>> GetListByPage(PageRequest request)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
|
|
|
|
|
//序列化查询条件
|
|
|
|
|
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
|
|
|
|
|
var data = tenantDb.Queryable<FeeCurrencyExchange>()
|
|
|
|
|
var whereList = Db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
|
|
|
|
|
var data = TenantDb.Queryable<FeeCurrencyExchange>()
|
|
|
|
|
.Where(whereList)
|
|
|
|
|
.Select<FeeCurrencyExchangeRes>().ToQueryPage(request.PageCondition);
|
|
|
|
|
return data;
|
|
|
|
@ -54,10 +43,9 @@ namespace DS.WMS.Core.Fee.Method
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public DataResult EditFeeCurrencyExchange(FeeCurrencyExchangeReq req)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
if (req.Id == 0)
|
|
|
|
|
{
|
|
|
|
|
if (tenantDb.Queryable<FeeCurrencyExchange>()
|
|
|
|
|
if (TenantDb.Queryable<FeeCurrencyExchange>()
|
|
|
|
|
.Where(x => x.CurrencyCode == req.CurrencyCode && x.LocalCurrency == req.LocalCurrency).Any())
|
|
|
|
|
{
|
|
|
|
|
return DataResult.Failed("汇率设置已存在!", MultiLanguageConst.FeeCurrencyExchangeExist);
|
|
|
|
@ -65,17 +53,17 @@ namespace DS.WMS.Core.Fee.Method
|
|
|
|
|
|
|
|
|
|
var data = req.Adapt<FeeCurrencyExchange>();
|
|
|
|
|
|
|
|
|
|
var entity = tenantDb.Insertable(data).ExecuteReturnEntity();
|
|
|
|
|
var entity = TenantDb.Insertable(data).ExecuteReturnEntity();
|
|
|
|
|
|
|
|
|
|
return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var info = tenantDb.Queryable<FeeCurrencyExchange>().Where(x => x.Id == req.Id).First();
|
|
|
|
|
var info = TenantDb.Queryable<FeeCurrencyExchange>().Where(x => x.Id == req.Id).First();
|
|
|
|
|
|
|
|
|
|
info = req.Adapt(info);
|
|
|
|
|
|
|
|
|
|
tenantDb.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
|
|
|
|
|
TenantDb.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
|
|
|
|
|
return DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -87,12 +75,87 @@ namespace DS.WMS.Core.Fee.Method
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public DataResult<FeeCurrencyExchangeRes> GetFeeCurrencyExchangeInfo(string id)
|
|
|
|
|
{
|
|
|
|
|
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
|
|
|
|
|
var data = tenantDb.Queryable<FeeCurrencyExchange>()
|
|
|
|
|
var data = TenantDb.Queryable<FeeCurrencyExchange>()
|
|
|
|
|
.Where(x => x.Id == long.Parse(id))
|
|
|
|
|
.Select<FeeCurrencyExchangeRes>()
|
|
|
|
|
.First();
|
|
|
|
|
return DataResult<FeeCurrencyExchangeRes>.Success(data, MultiLanguageConst.DataQuerySuccess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取两种币别之间的汇率
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="exchange">币别信息</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public DataResult<ExchangeRate> GetExchangeRate(ExchangeRate exchange)
|
|
|
|
|
{
|
|
|
|
|
if (exchange.CurrencyFrom == exchange.CurrencyTo)
|
|
|
|
|
{
|
|
|
|
|
exchange.ReverseRate = exchange.Rate = 1m;
|
|
|
|
|
return DataResult<ExchangeRate>.Success(exchange);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long id = long.Parse(User.OrgId);
|
|
|
|
|
//获取本位币,默认=人民币
|
|
|
|
|
string localCurrency = Db.Queryable<SysOrg>().Where(x => x.Id == id).Select(x => x.LocalCurrency).First() ?? RMB_CODE;
|
|
|
|
|
|
|
|
|
|
//需要做二次转换
|
|
|
|
|
if (exchange.CurrencyFrom != localCurrency && exchange.CurrencyTo != localCurrency)
|
|
|
|
|
{
|
|
|
|
|
//获取中间汇率
|
|
|
|
|
var middleRate = GetLocalRate(exchange.CurrencyFrom, localCurrency, exchange.FeeType);
|
|
|
|
|
if (middleRate == null)
|
|
|
|
|
return DataResult<ExchangeRate>.FailedData(exchange, message: $"尚未维护币别:{exchange.CurrencyFrom}");
|
|
|
|
|
|
|
|
|
|
var rate = GetLocalRate(exchange.CurrencyTo, localCurrency, exchange.FeeType);
|
|
|
|
|
exchange.Rate = Math.Round(1 / (rate ?? 1m) * middleRate.GetValueOrDefault(), 4, MidpointRounding.AwayFromZero);
|
|
|
|
|
exchange.ReverseRate = Math.Round(1 / exchange.Rate, 4, MidpointRounding.AwayFromZero);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
string currency = exchange.CurrencyFrom == RMB_CODE ? exchange.CurrencyTo : exchange.CurrencyFrom;
|
|
|
|
|
var rate = GetLocalRate(currency, localCurrency, exchange.FeeType);
|
|
|
|
|
|
|
|
|
|
if (currency == exchange.CurrencyFrom)
|
|
|
|
|
{
|
|
|
|
|
exchange.Rate = rate ?? 1m;
|
|
|
|
|
exchange.ReverseRate = Math.Round(1 / exchange.Rate, 4, MidpointRounding.AwayFromZero);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
exchange.ReverseRate = rate ?? 1m;
|
|
|
|
|
exchange.Rate = Math.Round(1 / exchange.ReverseRate, 4, MidpointRounding.AwayFromZero);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DataResult<ExchangeRate>.Success(exchange);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
internal decimal? GetLocalRate(string currency, string localCurrency, FeeType? type)
|
|
|
|
|
{
|
|
|
|
|
var entity = TenantDb.Queryable<FeeCurrency>().Where(x =>
|
|
|
|
|
x.CodeName == currency).Includes(x => x.Exchanges).First();
|
|
|
|
|
|
|
|
|
|
if (entity == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var rate = entity.DefaultRate;
|
|
|
|
|
DateTime dtNow = DateTime.Now;
|
|
|
|
|
if (type.HasValue && entity.Exchanges != null && entity.Exchanges.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
//取当前时间范围内的最新一条,优先获取本位币
|
|
|
|
|
var item = entity.Exchanges.FindAll(x => x.Status == StatusEnum.Enable && x.LocalCurrency == localCurrency &&
|
|
|
|
|
x.StartDate >= dtNow && x.EndDate <= dtNow).OrderByDescending(x => x.CreateTime).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
item ??= entity.Exchanges.FindAll(x => x.Status == StatusEnum.Enable &&
|
|
|
|
|
x.StartDate >= dtNow && x.EndDate <= dtNow).OrderByDescending(x => x.CreateTime).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (item != null)
|
|
|
|
|
rate = type.Value == FeeType.Receivable ? item.DRValue : item.CRValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|