using JNPF.Common.Dtos;
using JNPF.Common.Enums;
using JNPF.Common.Security;
using JNPF.DatabaseAccessor;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.Extras.Thirdparty.DingDing;
using JNPF.Extras.Thirdparty.Email;
using JNPF.Extras.Thirdparty.WeChat;
using JNPF.FriendlyException;
using JNPF.Systems.Entitys.Dto.SysConfig;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Entitys.System;
using JNPF.Systems.Interfaces.System;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
namespace JNPF.Systems.System;
///
/// 系统配置
/// 版 本:V3.2
/// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com)
/// 日 期:2021-06-01.
///
[ApiDescriptionSettings(Tag = "System", Name = "SysConfig", Order = 211)]
[Route("api/system/[controller]")]
public class SysConfigService : ISysConfigService, IDynamicApiController, ITransient
{
///
/// 系统配置仓储.
///
private readonly ISqlSugarRepository _repository;
///
/// 初始化一个类型的新实例.
///
public SysConfigService(
ISqlSugarRepository repository)
{
_repository = repository;
}
#region GET
///
/// 获取系统配置.
///
///
[HttpGet("")]
public async Task GetInfo()
{
var array = new Dictionary();
var data = await _repository.AsQueryable().Where(x => x.Category.Equals("SysConfig")).ToListAsync();
foreach (var item in data)
{
if (!array.ContainsKey(item.Key)) array.Add(item.Key, item.Value);
}
return array.ToObject();
}
///
/// 获取所有超级管理员.
///
///
[HttpGet("getAdminList")]
public async Task GetAdminList()
{
return await _repository.AsSugarClient().Queryable()
.Where(x => x.IsAdministrator == 1 && x.DeleteMark == null)
.Select(x => new AdminUserOutput()
{
id = x.Id,
account = x.Account,
realName = x.RealName
}).ToListAsync();
}
#endregion
#region Post
///
/// 邮箱链接测试.
///
///
[HttpPost("Email/Test")]
public void EmailTest([FromBody] MailParameterInfo input)
{
var result = MailUtil.CheckConnected(input);
if (!result)
throw Oops.Oh(ErrorCode.D9003);
}
///
/// 钉钉链接测试.
///
///
[HttpPost("testDingTalkConnect")]
public void testDingTalkConnect([FromBody] DingParameterInfo input)
{
var dingUtil = new DingUtil(input.dingSynAppKey, input.dingSynAppSecret);
if (string.IsNullOrEmpty(dingUtil.token))
throw Oops.Oh(ErrorCode.D9003);
}
///
/// 企业微信链接测试.
///
///
///
[HttpPost("{type}/testQyWebChatConnect")]
public void testQyWebChatConnect(int type, [FromBody] WeChatParameterInfo input)
{
var appSecret = type == 0 ? input.qyhAgentSecret : input.qyhCorpSecret;
var weChatUtil = new WeChatUtil(input.qyhCorpId, appSecret);
if (string.IsNullOrEmpty(weChatUtil.accessToken))
throw Oops.Oh(ErrorCode.D9003);
}
///
/// 更新系统配置.
///
///
///
[HttpPut]
[UnitOfWork]
public async Task Update([FromBody] SysConfigOutput input)
{
var configDic = input.ToObject>();
var entitys = new List();
foreach (var item in configDic.Keys)
{
if (configDic[item] != null)
{
if (item == "tokentimeout")
{
long time = 0;
if (long.TryParse(configDic[item].ToString(), out time))
{
if (time > 8000000)
{
throw Oops.Oh(ErrorCode.D9008);
}
}
}
if (item == "verificationCodeNumber")
{
int codeNum = 3;
if (int.TryParse(configDic[item].ToString(), out codeNum))
{
if (codeNum > 6 || codeNum < 3) throw Oops.Oh(ErrorCode.D9009);
}
}
SysConfigEntity sysConfigEntity = new SysConfigEntity();
sysConfigEntity.Id = SnowflakeIdHelper.NextId();
sysConfigEntity.Key = item;
sysConfigEntity.Value = configDic[item].ToString();
sysConfigEntity.Category = "SysConfig";
entitys.Add(sysConfigEntity);
}
}
await Save(entitys, "SysConfig");
}
///
/// 更新赋予超级管理员.
///
///
///
[HttpPut("setAdminList")]
[UnitOfWork]
public async Task SetAdminList([FromBody] SetAdminInput input)
{
await _repository.AsSugarClient().Updateable().SetColumns(x => x.IsAdministrator == 0).Where(x => x.IsAdministrator == 1 && !x.Account.Equals("admin")).ExecuteCommandAsync();
await _repository.AsSugarClient().Updateable().SetColumns(x => x.IsAdministrator == 1).Where(x => input.adminIds.Contains(x.Id)).ExecuteCommandAsync();
}
#endregion
#region PublicMethod
///
/// 系统配置信息.
///
/// 分类.
/// 键.
///
[NonAction]
public async Task GetInfo(string category, string key)
{
return await _repository.GetFirstAsync(s => s.Category == category && s.Key == key);
}
#endregion
#region PrivateMethod
///
/// 保存.
///
///
///
///
private async Task Save(List entitys, string category)
{
await _repository.DeleteAsync(x => x.Category.Equals(category));
await _repository.AsInsertable(entitys).ExecuteCommandAsync();
}
#endregion
}