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 SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace Myshipping.Application
{
///
/// 费用币别
///
[ApiDescriptionSettings("Application", Name = "FeeCurrency", Order = 1)]
public class FeeCurrencyService : IDynamicApiController, IFeeCurrencyService, ITransient
{
private readonly SqlSugarRepository _repCode;
private readonly ILogger _logger;
private readonly ISysCacheService _cache;
private readonly IEventPublisher _publisher;
public FeeCurrencyService(SqlSugarRepository repCode,
ILogger logger,
ISysCacheService cache,
IEventPublisher publisher)
{
_repCode = repCode;
_logger = logger;
_cache = cache;
_publisher = publisher;
}
///
/// 币别分页查询
///
///
///
[HttpPost("/FeeCurrency/Page")]
public async Task Page(FeeCurrencyPageInput input)
{
var entities = await _repCode.AsQueryable()
.WhereIF(!string.IsNullOrEmpty(input.CodeName), u => u.CodeName.Contains(input.CodeName))
.WhereIF(!string.IsNullOrEmpty(input.Name), u => u.Name.Contains(input.Name))
.ToPagedListAsync(input.PageNo, input.PageSize);
var result = entities.Adapt>();
return result.XnPagedResult();
}
///
/// 保存
///
///
///
[HttpPost("/FeeCurrency/Save")]
public async Task Save(FeeCurrencySaveDto input)
{
if (input == null)
{
throw Oops.Bah("请传入正常数据!");
}
FeeCurrency entity = null;
if (input.Id == 0)
{
entity = input.Adapt();
entity.Id = YitIdHelper.NextId();
await _repCode.InsertAsync(entity);
}
else
{
entity = await _repCode.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == input.Id);
entity = input.Adapt(entity);
await _repCode.UpdateAsync(entity);
}
await CacheFeeCurrency();
return entity.Adapt();
}
///
/// 获取详情
///
///
///
[HttpGet("/FeeCurrency/Detail")]
public async Task Detail(long id)
{
var entity = await _repCode.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == id);
return entity.Adapt();
}
///
/// 删除
///
///
///
[HttpPost("/FeeCurrency/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();
await CacheFeeCurrency();
}
///
/// 获取币别列表
///
[HttpGet("/FeeCurrency/List")]
public async Task> List()
{
List result = await _cache.GetAsync>(CommonConst.CACHE_KEY_FEE_CURRENCY);
if (result?.Any() != true)
{
result = await CacheFeeCurrency();
}
result = result.Where(f => f.TenantId == UserManager.TENANT_ID).ToList();
return result;
}
///
/// 缓存币别列表
///
[NonAction]
public async Task> CacheFeeCurrency()
{
var allFeeCurrency = await _repCode.AsQueryable().Filter(null, true).ToListAsync();
var cacheFeeCode = allFeeCurrency.Adapt>();
await _cache.SetTimeoutAsync(CommonConst.CACHE_KEY_FEE_CURRENCY, cacheFeeCode, new TimeSpan(6, 0, 0));
return cacheFeeCode;
}
}
}