using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Myshipping.Core.Entity;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
namespace Myshipping.Core.Service;
///
/// 系统参数配置服务
///
[ApiDescriptionSettings(Name = "Config", Order = 100)]
public class SysConfigService : ISysConfigService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _sysConfigRep; // 参数配置表仓储
private readonly ISysCacheService _sysCacheService;
public SysConfigService(SqlSugarRepository sysConfigRep, ISysCacheService sysCacheService)
{
_sysConfigRep = sysConfigRep;
_sysCacheService = sysCacheService;
}
///
/// 分页获取系统参数配置
///
///
///
[HttpGet("/sysConfig/page")]
public async Task QueryConfigPageList([FromQuery] ConfigInput input)
{
var configs = await _sysConfigRep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupCode), u => u.GroupCode == input.GroupCode.Trim())
.Where(u => u.Status != CommonStatus.DELETED).OrderBy(u => u.GroupCode)
.ToPagedListAsync(input.PageNo, input.PageSize);
return configs.XnPagedResult();
}
///
/// 获取系统参数配置列表
///
///
///
[HttpGet("/sysConfig/list")]
public async Task GetConfigList([FromQuery] ConfigInput input)
{
return await _sysConfigRep.Where(u => u.Status != CommonStatus.DELETED).ToListAsync();
}
///
/// 增加系统参数配置
///
///
///
[HttpPost("/sysConfig/add")]
public async Task AddConfig(AddConfigInput input)
{
var isExist = await _sysConfigRep.AnyAsync(u => u.Name == input.Name || u.Code == input.Code);
if (isExist)
throw Oops.Oh(ErrorCode.D9000);
var config = input.Adapt();
await _sysConfigRep.InsertAsync(config);
}
///
/// 删除系统参数配置
///
///
///
[HttpPost("/sysConfig/delete")]
public async Task DeleteConfig(DeleteConfigInput input)
{
var config = await _sysConfigRep.FirstOrDefaultAsync(u => u.Id == input.Id);
// 禁止删除系统参数
if (config.SysFlag == YesOrNot.Y.ToString())
throw Oops.Oh(ErrorCode.D9001);
await _sysConfigRep.DeleteAsync(config);
//刷新缓存
await _sysCacheService.DelAsync(config.Code);
}
///
/// 更新系统参数配置
///
///
///
[HttpPost("/sysConfig/edit")]
public async Task UpdateConfig(UpdateConfigInput input)
{
var isExist = await _sysConfigRep.AnyAsync(u => (u.Name == input.Name || u.Code == input.Code) && u.Id != input.Id);
if (isExist)
throw Oops.Oh(ErrorCode.D9000);
var config = input.Adapt();
await _sysConfigRep.AsUpdateable(config).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
//刷新缓存
await _sysCacheService.SetAsync(input.Code, input.Value);
}
///
/// 获取系统参数配置
///
///
///
[HttpGet("/sysConfig/detail")]
public async Task GetConfig([FromQuery] QueryConfigInput input)
{
return await _sysConfigRep.FirstOrDefaultAsync(u => u.Id == input.Id);
}
///
/// 获取配置信息
///
///
///
private async Task GetConfigCache(string code)
{
var value = await _sysCacheService.GetAsync(code);
if (string.IsNullOrEmpty(value))
{
var config = await _sysConfigRep.FirstOrDefaultAsync(u => u.Code == code);
value = config != null ? config.Value : "";
await _sysCacheService.SetAsync(code, value);
}
return value;
}
///
/// 更新配置缓存
///
///
///
///
public async Task UpdateConfigCache(string code, object value)
{
await _sysCacheService.SetAsync(code, value);
}
///
/// 获取演示环境开关是否开启,默认为false
///
///
[NonAction]
public async Task GetDemoEnvFlag()
{
var value = await GetConfigCache("DEMO_ENV_FLAG");
return bool.Parse(value);
}
///
/// 获取验证码开关标识
///
///
public async Task GetCaptchaOpenFlag()
{
var value = await GetConfigCache("CAPTCHA_OPEN");
return bool.Parse(value);
}
///
/// 获取默认密码
///
///
public async Task GetDefaultPassword()
{
var value = await GetConfigCache("DEFAULT_PASSWORD");
return value;
}
}