using Npgsql; 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 _logger; private readonly IDatabase _redis; private readonly IConnectionClientManagerService _connectionClientManager; private readonly SubscribeAndPublishService _subscribeAndPublishService; public ServerAppService( IMapper mapper, IConfiguration configuration, ILogger logger, IConnectionMultiplexer redis, IConnectionClientManagerService connectionClientManager, SubscribeAndPublishService subscribeAndPublishService ) { _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; _subscribeAndPublishService = subscribeAndPublishService; } public async Task CreateAsync(ServerCreationDto input) { var conn = new NpgsqlConnection("123"); input.TrimStringFields(); var exists = await _client.Queryable() .AnyAsync(x => x.Id == input.GroupId && x.CreateBy == input.UserId); if (!exists) throw new Exception("仅允许在自己的分组操作创建"); var group = _mapper.Map(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() .Where(x => x.Original_GroupId == input.GroupId) .ToListAsync(); _ = Task.Run(() => { groupRelationList.ForEach(x => { //await _connectionClientManager.SendAsync(x.Owner.ToString(), JsonConvert.SerializeObject(new MessageDto { Type = "通知", Content = "服务器列表有更新" })); _subscribeAndPublishService.Publish(JsonConvert.SerializeObject(new MessageDto { Type = "通知", Content = "服务器列表有更新" }), x.Owner.ToString()); }); }); } catch (Exception ex) { _logger.LogError("创建服务器信息失败:{message}", ex.Message); throw new Exception("创建服务器信息失败"); } } public async Task DeleteAsync(ServerDeleteDto input) { input.TrimStringFields(); var exists = await _client.Queryable().AnyAsync(x => x.CreateBy == input.UserId && x.Id == input.GroupId); if (!exists) throw new Exception("仅允许在自己的分组操作删除"); //清空对应组的服务器缓存 await _redis.KeyDeleteAsync($"server:{input.GroupId}"); await _client.Deleteable().Where(x => x.Id == input.Id).ExecuteCommandAsync(); //通知使用分享码导入的人员 服务器信息已删除 var groupRelationList = await _client.Queryable() .Where(x => x.Original_GroupId == input.GroupId) .ToListAsync(); _ = Task.Run(() => { groupRelationList.ForEach(x => { //await _connectionClientManager.SendAsync(x.Owner.ToString(), JsonConvert.SerializeObject(new MessageDto { Type = "通知", Content = "服务器列表有更新" })); _subscribeAndPublishService.Publish(JsonConvert.SerializeObject(new MessageDto { Type = "通知", Content = "服务器列表有更新" }), x.Owner.ToString()); }); }); } public async Task>> GetListAsync(long groupid) { //缓存获取 var serverValue = await _redis.StringGetAsync($"server:{groupid}"); if (serverValue.HasValue) { var serverDto = JsonConvert.DeserializeObject>(serverValue.ToString()); return new AppResponse> { Code = 200, Data = serverDto }; } try { //数据库读 var serverList = await _client.Queryable() .Where(x => x.GroupId == groupid) .ToListAsync(); await _redis.StringSetAsync($"server:{groupid}", JsonConvert.SerializeObject(serverList)); return new AppResponse> { 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().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().FirstAsync(x => x.Id == input.Id); _client.Tracking(server); //更新实体列 CustomerAppService.ConditionalMap(input, server); await _client.Updateable(server).ExecuteCommandAsync(); //通知使用分享码导入的人员服务器信息已更新 var groupRelationList = await _client.Queryable() .Where(x => x.Original_GroupId == input.GroupId) .ToListAsync(); _ = Task.Run(() => { groupRelationList.ForEach(x => { //await _connectionClientManager.SendAsync(x.Owner.ToString(), JsonConvert.SerializeObject(new MessageDto { Type = "通知", Content = "服务器列表有更新" })); _subscribeAndPublishService.Publish(JsonConvert.SerializeObject(new MessageDto { Type = "通知", Content = "服务器列表有更新" }), x.Owner.ToString()); }); }); } catch (Exception ex) { _logger.LogError("修改服务器信息失败:{message}", ex.Message); throw new Exception("修改服务器信息失败"); } } } }