using Furion.DependencyInjection; 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; 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 { /// /// 汇率 /// [ApiDescriptionSettings("Application", Name = "FeeCurrencyExchange", Order = 1)] public class FeeCurrencyExchangeService : IDynamicApiController, ITransient { private readonly SqlSugarRepository _repCode; private readonly ILogger _logger; private readonly ISysCacheService _cache; private readonly IFeeCurrencyService _currencyService; private readonly IEventPublisher _publisher; public FeeCurrencyExchangeService(SqlSugarRepository repCode, ILogger logger, ISysCacheService cache, IEventPublisher publisher, IFeeCurrencyService currencyService) { _repCode = repCode; _logger = logger; _cache = cache; _publisher = publisher; _currencyService = currencyService; } /// /// 费用代码查询 /// /// /// [HttpPost("/FeeCurrencyExchange/Page")] public async Task 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>(); return result.XnPagedResult(); } /// /// 保存 /// /// /// [HttpPost("/FeeCurrencyExchange/Save")] public async Task 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(); 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(); } /// /// 获取详情 /// /// /// [HttpGet("/FeeCurrencyExchange/Detail")] public async Task Detail(long id) { var entity = await _repCode.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == id); return entity.Adapt(); } /// /// 删除 /// /// /// [HttpPost("/FeeCurrencyExchange/Delete")] public async Task Delete(List 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(); } /// /// 根据币别代码查询当前时间段的汇率或默认汇率 /// /// 如果该币别没有维护当前时间段的汇率,则返回该币别的默认汇率 /// 币别代码 /// 币别当前时间段的汇率或默认汇率 [HttpGet("/FeeCurrencyExchange/QueryExchange")] public async Task 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(); 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" }; } } }