You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
7.6 KiB

6 months ago
using System.Collections.Concurrent;
using System.ComponentModel;
using Autofac;
using Common.Enum;
using Entity.Base;
6 months ago
using Entity.DbModel.System;
using Entity.Dto.Req;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Redis;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Repository.System;
using SqlSugar;
namespace Service.System
{
[Scope("SingleInstance")]
public class SysConfigService : BaseServices<SysConfig>
{
6 months ago
private static readonly ConcurrentDictionary<string, string> Dictionary =
new ConcurrentDictionary<string, string>();
private readonly SysConfigRepository _sysConfigRep;
public SysConfigService(SysConfigRepository sysConfigRep)
{
_sysConfigRep = sysConfigRep;
}
6 months ago
RedisHelper redisHelper = AppInfo.Container.Resolve<RedisHelper>();
6 months ago
/// <summary>
/// 获取参数配置分页列表 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<SqlSugarPagedList<SysConfig>> Page(PageConfigReq input)
{
RefAsync<int> total = 0;
var items = await _sysConfigRep.QueryPageAsync(
!string.IsNullOrEmpty(input.Name), u => u.Name.Contains(input.Name),
!string.IsNullOrEmpty(input.Code), u => u.Code.Contains(input.Code),
!string.IsNullOrEmpty(input.GroupCode), u => u.GroupCode.Equals(input.GroupCode),
input.Page, input.PageSize, total, input);
return SqlSugarPagedExtensions.CreateSqlSugarPagedList(items, total, input.Page, input.PageSize);
}
/// <summary>
/// 获取参数配置列表 🔖
/// </summary>
/// <returns></returns>
[DisplayName("获取参数配置列表")]
public async Task<List<SysConfig>> GetList()
{
return await _sysConfigRep.QueryAsync();
}
/// <summary>
/// 获取分组列表 🔖
/// </summary>
/// <returns></returns>
public async Task<List<string>> GetGroupList()
{
return await _sysConfigRep.QueryByGroupByAsync<string>(u => u.GroupCode, u => u.GroupCode);
}
/// <summary>
/// 增加参数配置 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
//[ApiDescriptionSettings(Name = "Add"), HttpPost]
public async Task<string> AddConfig(AddConfigReq input)
{
string result = "";
var isExist = await _sysConfigRep.QueryByClauseAsync(u => u.Name == input.Name || u.Code == input.Code);
if (isExist != null)
result = "已存在同名或同编码参数配置";
int insertSesult = await _sysConfigRep.InsertAsync(input.Adapt<SysConfig>());
if (insertSesult > 0)
result = "增加参数配置成功";
return result;
}
/// <summary>
/// 更新参数配置 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
6 months ago
// [ApiDescriptionSettings(Name = "Update"), HttpPost]
[DisplayName("更新参数配置")]
public async Task<string> UpdateConfig(UpdateConfigReq input)
{
string result = "";
6 months ago
var isExist = await _sysConfigRep.QueryByClauseAsync(u =>
(u.Name == input.Name || u.Code == input.Code) && u.Id != input.Id);
if (isExist != null)
result = "已存在同名或同编码参数配置";
var config = input.Adapt<SysConfig>();
6 months ago
int updateResult = await _sysConfigRep.UpdateAsync(config, true);
if (updateResult > 0)
result = "更新参数配置成功";
redisHelper.Remove(config.Code);
return result;
}
6 months ago
/// <summary>
/// 删除参数配置 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
//[ApiDescriptionSettings(Name = "Delete"), HttpPost]
[DisplayName("删除参数配置")]
public async Task<string> DeleteConfig(DeleteConfigReq input)
{
string result = "";
var config = await _sysConfigRep.QueryByClauseAsync(u => u.Id == input.Id);
if (config.SysFlag == YesNoEnum.Y) // 禁止删除系统参数
result = "禁止删除系统参数";
bool deleteResult = await _sysConfigRep.DeleteAsync(config);
if (deleteResult)
result = "删除参数配置成功";
redisHelper.Remove(config.Code);
return result;
}
/// <summary>
/// 批量删除参数配置 🔖
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
6 months ago
// [ApiDescriptionSettings(Name = "BatchDelete"), HttpPost]
[DisplayName("批量删除参数配置")]
public async Task BatchDeleteConfig(List<long> ids)
{
foreach (var id in ids)
{
var config = await _sysConfigRep.QueryByClauseAsync(u => u.Id == id);
if (config.SysFlag == YesNoEnum.Y) // 禁止删除系统参数
continue;
6 months ago
await _sysConfigRep.DeleteAsync(config);
redisHelper.Remove(config.Code);
}
}
/// <summary>
/// 获取参数配置详情 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("获取参数配置详情")]
public async Task<SysConfig> GetDetail([FromQuery] ConfigReq input)
{
return await _sysConfigRep.QueryByClauseAsync(u => u.Id == input.Id);
}
6 months ago
/// <summary>
///
/// </summary>
/// <param name="key">GroupCode.code</param>
/// <returns></returns>
public string? Get(string key)
{
string[] keys = key.Split('.');
if (keys.Length != 2)
{
throw new ArgumentException("配置数据key格式错误");
}
if (Dictionary.TryGetValue(key, out string? value))
{
return value;
}
SysConfig sysConfig = _sysConfigRep.QueryByClause(i => i.GroupCode == keys[0] && i.Code == keys[1]);
if (sysConfig == null)
{
return null;
}
return sysConfig.Value;
}
/// <summary>
///
/// </summary>
/// <param name="key">GroupCode.code</param>
/// <param name="value"></param>
public void Set(string key, object value)
{
string[] keys = key.Split('.');
if (keys.Length != 2)
{
throw new ArgumentException("配置数据key格式错误");
}
string newValue = Convert.ToString(value);
Dictionary.AddOrUpdate(key, newValue, (s, s1) => newValue);
SysConfig sysConfig = _sysConfigRep.QueryByClause(i => i.GroupCode == keys[0] && i.Code == keys[1]);
if (sysConfig == null)
{
_sysConfigRep.Insert(new SysConfig()
{
GroupCode = keys[0],
Code = keys[1],
Value = newValue,
SysFlag = YesNoEnum.N,
Name = key
});
}
else
{
sysConfig.Value = newValue;
_sysConfigRep.Update(sysConfig);
}
}
}
6 months ago
}