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 Myshipping.Core.Entity;
using Microsoft.Extensions.Logging;
using System;
using Furion.DataEncryption;
namespace Myshipping.Core.Service
{
///
/// 接口授权服务
///
[ApiDescriptionSettings(Name = "DjyApiAuth", Order = 1)]
public class DjyApiAuthService : IDjyApiAuthService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly ILogger _logger;
public DjyApiAuthService(SqlSugarRepository rep, ILogger logger)
{
_rep = rep;
_logger = logger;
}
///
/// 分页查询接口授权
///
///
///
[HttpGet("/DjyApiAuth/page")]
public async Task Page([FromQuery] QueryDjyApiAuthInput input)
{
var entities = await _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.ApiCode), u => u.ApiCode.Contains(input.ApiCode))
.WhereIF(!string.IsNullOrWhiteSpace(input.ApiName), u => u.ApiName.Contains(input.ApiName))
.WhereIF(!string.IsNullOrWhiteSpace(input.TenantName), u => u.TenantName == input.TenantName)
.WhereIF(!string.IsNullOrWhiteSpace(input.UserName), u => u.UserName == input.UserName)
.WhereIF(input.IsDisable.HasValue, u => u.IsDisable == input.IsDisable.Value)
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
///
/// 保存授权
///
///
///
[HttpPost("/DjyApiAuth/save")]
public async Task Save(SaveDjyApiAuthInput input)
{
DjyApiAuth entity = null;
if (input.Id == 0)
{
entity = input.Adapt();
entity.ApiKey = Guid.NewGuid().ToString().Replace("-", "").ToLower();
entity.ApiSecret = GenSecret();
await _rep.InsertAsync(entity);
}
else
{
entity = _rep.AsQueryable().Filter(null, true).First(x => x.Id == input.Id);
input.Adapt(entity);
await _rep.UpdateAsync(entity);
}
return entity.Id;
}
///
/// 删除接口授权
///
///
///
[HttpPost("/DjyApiAuth/delete")]
public async Task Delete(GetDjyApiAuthInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
///
/// 获取接口授权
///
///
///
[HttpGet("/DjyApiAuth/detail")]
public async Task Get([FromQuery] GetDjyApiAuthInput input)
{
return await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
///
/// 重置秘钥
///
///
///
[HttpPost("/DjyApiAuth/SecretReset")]
public async Task SecretReset(long id)
{
var entity = _rep.AsQueryable().Filter(null, true).First(x => x.Id == id);
entity.ApiSecret = GenSecret();
await _rep.UpdateAsync(entity);
return entity.ApiSecret;
}
///
/// 启用禁用
///
///
/// 可用:false,禁用:true
///
[HttpPost("/DjyApiAuth/SetDisable")]
public async Task SetDisable(long id, bool disable)
{
var entity = _rep.AsQueryable().Filter(null, true).First(x => x.Id == id);
entity.IsDisable = disable;
await _rep.UpdateAsync(entity);
}
///
/// 生成新秘钥
///
///
[NonAction]
private string GenSecret()
{
return DESCEncryption.Encrypt(Guid.NewGuid().ToString(), "13245687").ToLower();
}
/////
///// 获取接口授权列表
/////
/////
/////
//[HttpGet("/DjyApiAuth/list")]
//public async Task List([FromQuery] QueryDjyApiAuthInput input)
//{
// return await _rep.ToListAsync();
//}
}
}