ZhiYi/ZhiYi.Core.Application/Services/Implements/ServerAppService.cs

158 lines
6.8 KiB
C#

using ZhiYi.Core.Application.Dtos.Message;
namespace ZhiYi.Core.Application.Services.Implements
{
public class ServerAppService : IServerAppService
{
private readonly IMapper _mapper;
private readonly SqlSugarClient _client;
private readonly IConfiguration _configuration;
private readonly ILogger<GroupAppService> _logger;
private readonly IDatabase _redis;
private readonly IConnectionClientManagerService _connectionClientManager;
public ServerAppService(
IMapper mapper,
IConfiguration configuration,
ILogger<GroupAppService> logger,
IConnectionMultiplexer redis,
IConnectionClientManagerService connectionClientManager
)
{
_mapper = mapper;
_configuration = configuration;
string connStr = _configuration.GetSection("ConnectionStrings:SqlConnection").Value;
if (_client == null)
{
_client = SugarClientInit.Instance.GetDdClient(connStr);
}
_logger = logger;
_redis = redis.GetDatabase();
_connectionClientManager = connectionClientManager;
}
public async Task CreateAsync(ServerCreationDto input)
{
input.TrimStringFields();
var exists = await _client.Queryable<ZhiYi_Group>()
.AnyAsync(x => x.Id == input.GroupId && x.CreateBy == input.UserId);
if (!exists)
throw new Exception("仅允许在自己的分组操作创建");
var group = _mapper.Map<ZhiYi_Server>(input);
group.Id = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
group.CreateTime = DateTime.Now;
try
{
//清空对应组的服务器缓存
await _redis.KeyDeleteAsync($"server:{input.GroupId}");
await _client.Insertable(group).ExecuteCommandAsync();
//通知使用分享码导入的人员服务器信息有新的记录
var groupRelationList = await _client.Queryable<ZhiYi_Group_Relation>()
.Where(x => x.Original_GroupId == input.GroupId)
.ToListAsync();
_ = Task.Run(() =>
{
groupRelationList.ForEach(async x =>
{
await _connectionClientManager.SendAsync(x.Owner.ToString(), JsonConvert.SerializeObject(new MessageDto { Type = "通知", Content = "服务器列表有更新" }));
});
});
}
catch (Exception ex)
{
_logger.LogError("创建服务器信息失败:{message}", ex.Message);
throw new Exception("创建服务器信息失败");
}
}
public async Task DeleteAsync(ServerDeleteDto input)
{
input.TrimStringFields();
var exists = await _client.Queryable<ZhiYi_Group>().AnyAsync(x => x.CreateBy == input.UserId && x.Id == input.GroupId);
if (!exists)
throw new Exception("仅允许在自己的分组操作删除");
//清空对应组的服务器缓存
await _redis.KeyDeleteAsync($"server:{input.GroupId}");
await _client.Deleteable<ZhiYi_Server>().Where(x => x.Id == input.Id).ExecuteCommandAsync();
//通知使用分享码导入的人员 服务器信息已删除
var groupRelationList = await _client.Queryable<ZhiYi_Group_Relation>()
.Where(x => x.Original_GroupId == input.GroupId)
.ToListAsync();
_ = Task.Run(() =>
{
groupRelationList.ForEach(async x =>
{
await _connectionClientManager.SendAsync(x.Owner.ToString(), JsonConvert.SerializeObject(new MessageDto { Type = "通知", Content = "服务器列表有更新" }));
});
});
}
public async Task<AppResponse<List<ZhiYi_Server>>> GetListAsync(long groupid)
{
//缓存获取
var serverValue = await _redis.StringGetAsync($"server:{groupid}");
if (serverValue.HasValue)
{
var serverDto = JsonConvert.DeserializeObject<List<ZhiYi_Server>>(serverValue.ToString());
return new AppResponse<List<ZhiYi_Server>> { Code = 200, Data = serverDto };
}
try
{
//数据库读
var serverList = await _client.Queryable<ZhiYi_Server>()
.Where(x => x.GroupId == groupid)
.ToListAsync();
await _redis.StringSetAsync($"server:{groupid}", JsonConvert.SerializeObject(serverList));
return new AppResponse<List<ZhiYi_Server>> { Code = 200, Data = serverList };
}
catch (Exception ex)
{
_logger.LogError("查询服务器列表失败:{message}", ex.Message);
throw new Exception("查询服务器列表失败");
}
}
public async Task UpdateAsync(ServerUpdationDto input)
{
input.TrimStringFields();
try
{
var exists = await _client.Queryable<ZhiYi_Group>().AnyAsync(x => x.CreateBy == input.UserId && x.Id == input.GroupId);
if (!exists)
throw new Exception("仅允许在自己的分组操作更新");
//清空对应组的服务器缓存
await _redis.KeyDeleteAsync($"server:{input.GroupId}");
//指定行跟踪
var server = await _client.Queryable<ZhiYi_Server>().FirstAsync(x => x.Id == input.Id);
_client.Tracking(server);
//更新实体列
CustomerAppService.ConditionalMap(input, server);
await _client.Updateable(server).ExecuteCommandAsync();
//通知使用分享码导入的人员服务器信息已更新
var groupRelationList = await _client.Queryable<ZhiYi_Group_Relation>()
.Where(x => x.Original_GroupId == input.GroupId)
.ToListAsync();
_ = Task.Run(() =>
{
groupRelationList.ForEach(async x =>
{
await _connectionClientManager.SendAsync(x.Owner.ToString(), JsonConvert.SerializeObject(new MessageDto { Type = "通知", Content = "服务器列表有更新" }));
});
});
}
catch (Exception ex)
{
_logger.LogError("修改服务器信息失败:{message}", ex.Message);
throw new Exception("修改服务器信息失败");
}
}
}
}