using Myshipping.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Furion.Logging;
using Microsoft.Extensions.Logging;
using Myshipping.Core.Entity;
using Furion.FriendlyException;
namespace Myshipping.Core.Service
{
///
/// EDI参数设置服务
///
[ApiDescriptionSettings(Name = "DjyEdiSetting", Order = 1)]
public class DjyEdiSettingService : IDjyEdiSettingService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly ISysCacheService _cacheService;
private readonly ILogger _logger;
public DjyEdiSettingService(SqlSugarRepository rep, ILogger logger, ISysCacheService cacheService)
{
_rep = rep;
_logger = logger;
_cacheService = cacheService;
}
///
/// 分页查询EDI参数设置
///
///
///
[HttpGet("/DjyEdiSetting/page")]
public async Task Page([FromQuery] QueryDjyEdiSettingInput input)
{
var entities = await _rep.AsQueryable().Filter(null, true)
.WhereIF(!string.IsNullOrWhiteSpace(input.EDICODE), u => u.EDICODE == input.EDICODE)
.WhereIF(!string.IsNullOrWhiteSpace(input.EDINAME), u => u.EDINAME.Contains(input.EDINAME.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.CARRIERID), u => u.CARRIERID.Contains(input.CARRIERID.Trim()))
.WhereIF(input.TenantId > 0, u => u.TenantId == input.TenantId)
.WhereIF(!string.IsNullOrWhiteSpace(input.TenantName), u => u.TenantName.Contains(input.TenantName.Trim()))
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
///
/// 增加EDI参数设置
///
///
///
[HttpPost("/DjyEdiSetting/add")]
public async Task Add(AddDjyEdiSettingInput input)
{
var cc = _rep.AsQueryable()
.Filter(null, true)
.Count(x => x.EDICODE == input.EDICODE && x.TenantId == input.TenantId && x.CARRIERID == input.CARRIERID);
if (cc > 0)
{
throw Oops.Bah($"该租户({input.TenantName})已存在相同类型({input.EDICODE})相同船司({input.CARRIERID})的参数设置");
}
var entity = input.Adapt();
await _rep.InsertAsync(entity);
await CacheData();
return entity.Id;
}
///
/// 更新EDI参数设置
///
///
///
[HttpPost("/DjyEdiSetting/edit")]
public async Task Update(UpdateDjyEdiSettingInput input)
{
var entity = _rep.AsQueryable()
.Filter(null, true)
.First(x => x.Id == input.Id);
if (entity == null)
{
throw Oops.Bah($"未找到数据");
}
var cc = _rep.AsQueryable().Filter(null, true)
.Count(x => x.EDICODE == input.EDICODE && x.TenantId == input.TenantId && x.CARRIERID == input.CARRIERID && x.Id != input.Id);
if (cc > 0)
{
throw Oops.Bah($"该租户({input.TenantName})已存在相同类型({input.EDICODE})相同船司({input.CARRIERID})的参数设置");
}
entity = input.Adapt(entity);
await _rep.UpdateAsync(entity);
await CacheData();
return entity.Id;
}
///
/// 删除EDI参数设置
///
///
///
[HttpPost("/DjyEdiSetting/delete")]
public async Task Delete(GetDjyEdiSettingInput input)
{
var entity = await _rep.AsQueryable().Filter(null, true)
.FirstAsync(u => u.Id == input.Id);
if (entity == null)
{
throw Oops.Bah($"未找到数据");
}
await _rep.DeleteAsync(entity);
await CacheData();
}
///
/// 获取EDI参数设置
///
///
///
[HttpGet("/DjyEdiSetting/detail")]
public async Task Get([FromQuery] GetDjyEdiSettingInput input)
{
return await _rep.AsQueryable().Filter(null, true).FirstAsync(u => u.Id == input.Id);
}
///
/// 缓存数据
///
///
///
[NonAction]
public async Task CacheData(bool flag = false)
{
if (flag)
{
if (!_cacheService.Exists(CommonConst.CACHE_KEY_DJY_EDI_SETTING))
{
var list = _rep.AsQueryable().Filter(null, true).OrderBy(x=>x.EDINAME).ToList();
await _cacheService.SetAllEdiSetting(list);
}
}
else
{
var list = _rep.AsQueryable().Filter(null, true).OrderBy(x => x.EDINAME).ToList();
await _cacheService.SetAllEdiSetting(list);
}
}
/////
///// 获取EDI参数设置列表
/////
/////
/////
//[HttpGet("/DjyEdiSetting/list")]
//public async Task List([FromQuery] QueryDjyEdiSettingInput input)
//{
// return await _rep.ToListAsync();
//}
}
}