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.

256 lines
9.0 KiB

7 months ago
using System.Collections.Concurrent;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
7 months ago
using Autofac;
using Common.Enum;
using Entity.Base;
using Entity.DbModel.System.SysBaseObject;
using Entity.Dto.Req;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Redis;
using Mapster;
using Microsoft.AspNetCore.Http;
7 months ago
using Microsoft.AspNetCore.Mvc;
7 months ago
using Org.BouncyCastle.Security;
7 months ago
using Repository.System;
using Service.Mgr;
using Service.Station;
7 months ago
using SqlSugar;
namespace Service.System
{
[Scope("SingleInstance")]
public class SysConfigService : BaseServices<SysConfig>
{
7 months ago
private static readonly ConcurrentDictionary<string, string> Dictionary = new ConcurrentDictionary<string, string>();
private readonly SysFileMgr _sysFileMgr;
7 months ago
private readonly SysConfigRepository _sysConfigRep;
public SysConfigService(SysConfigRepository sysConfigRep, SysFileMgr sysFileMgr)
7 months ago
{
_sysConfigRep = sysConfigRep;
_sysFileMgr = sysFileMgr;
7 months ago
}
RedisHelper redisHelper = AppInfo.Container.Resolve<RedisHelper>();
/// <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>
/// 获得同一分组下 SysConfig
/// </summary>
/// <param name="GroupCode"></param>
/// <returns></returns>
public async Task<List<SysConfig>> GetGroupList(string GroupCode)
{
return await _sysConfigRep.QueryListByClauseAsync(u => u.GroupCode == GroupCode);
}
7 months ago
/// <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>
// [ApiDescriptionSettings(Name = "Update"), HttpPost]
[DisplayName("更新参数配置")]
public async Task<string> UpdateConfig(UpdateConfigReq input)
{
string result = "";
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>();
int updateResult = await _sysConfigRep.UpdateAsync(config, true);
7 months ago
if (updateResult > 0)
result = "更新参数配置成功";
redisHelper.Remove(config.Code);
return result;
}
/// <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>
// [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;
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);
}
7 months ago
/// <summary>
///
/// </summary>
/// <param name="key">GroupCode.code</param>
7 months ago
/// <returns></returns>
public string? Get(string key)
{
string[] keys = key.Split('.');
if (keys.Length != 2)
7 months ago
{
throw new InvalidParameterException("配置数据key格式错误");
}
7 months ago
if (Dictionary.TryGetValue(key, out string? value))
{
return value;
}
7 months ago
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>
7 months ago
/// <param name="value"></param>
public bool Set(string key, object value)
7 months ago
{
bool setResult = false;
string[] keys = key.Split('.');
if (keys.Length != 2)
7 months ago
{
throw new InvalidParameterException("配置数据key格式错误");
}
7 months ago
string newValue = Convert.ToString(value);
7 months ago
Dictionary.AddOrUpdate(key, newValue, (s, s1) => newValue);
7 months ago
redisHelper.SetKeyValueStr(key, newValue);
7 months ago
7 months ago
SysConfig sysConfig = _sysConfigRep.QueryByClause(i => i.GroupCode == keys[0] && i.Code == keys[1]);
if (sysConfig == null)
{
int insertResult = _sysConfigRep.Insert(new SysConfig()
7 months ago
{
GroupCode = keys[0],
Code = keys[1],
7 months ago
Value = newValue,
7 months ago
SysFlag = YesNoEnum.N,
Name = key
});
setResult = insertResult > 0;
7 months ago
}
else
{
7 months ago
sysConfig.Value = newValue;
setResult = _sysConfigRep.Update(sysConfig);
7 months ago
}
return setResult;
}
private readonly string _imageType = ".jpg.png.bmp.gif.tif";
/// <summary>
/// 上传照片
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public async Task<SysFile> UploadAvatar([Required] IFormFile file, string path)
{
var sysFile = await _sysFileMgr.HandleUploadFile(file, path, _imageType);
bool setResult = Set(StationParamConst.Cover, sysFile.Url);
if (setResult)
return sysFile;
else
return null;
7 months ago
}
7 months ago
}
}