Files
Kg.SeaTime/Service/Application.Domain/Services/Service/User/UnitUserRelationService.cs
Putoo f8d4a28d53 121
2026-05-30 17:32:27 +08:00

123 lines
4.6 KiB
C#

namespace Application.Domain;
public class UnitUserRelationService(ISqlSugarClient DbClient, IRedisCache redis) : IUnitUserRelationService, ITransient
{
#region
public async Task<int> GetUserFriendCount(string userId)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_friend>();
return await db.Queryable<unit_user_friend>().Where(it => it.frId.Contains(userId)).CountAsync();
}
public async Task<List<RelationView>> GetUserFriend(string userId)
{
List<RelationView> result = new List<RelationView>();
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_friend>();
var data = await db.Queryable<unit_user_friend>().Where(it => it.frId.Contains(userId))
.ToListAsync();
foreach (var item in data)
{
RelationView temp = new RelationView();
string otUser = item.userId == userId ? item.toId : item.userId;
temp.user = await UserModelTool.GetUserView(otUser);
temp.near = (long)item.near;
var mapService = App.GetService<IGameMapService>();
var onMap = await mapService.GetUserOnMapInfo(otUser);
temp.mapName = onMap.mapName;
temp.cityName = onMap.cityName;
temp.sort = TimeExtend.GetTimeStampBySeconds((DateTime)item.addTime);
temp.isOnline = onMap.isOnline;
result.Add(temp);
}
return result;
}
public async Task<bool> CheckIsFriend(string userId, string toId)
{
string frId = StringAssist.GetSortKey(userId, toId);
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_friend>();
return await db.Queryable<unit_user_friend>().Where(it => it.frId == frId).AnyAsync();
}
public async Task<bool> AddFriend(string userId, string toId)
{
unit_user_friend friend = new unit_user_friend();
friend.frId = StringAssist.GetSortKey(userId, toId);
friend.userId = userId;
friend.toId = toId;
friend.near = 0;
friend.addTime = DateTime.Now;
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_friend>();
return await db.Insertable(friend).ExecuteCommandAsync() > 0;
}
public async Task<bool> DeleteFriend(string userId, string toId)
{
string frId = StringAssist.GetSortKey(userId, toId);
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_friend>();
return await db.Deleteable<unit_user_friend>().Where(it => it.frId == frId).ExecuteCommandAsync() > 0;
}
#endregion
#region
public async Task<List<RelationView>> GetUserEnemyData(string userId)
{
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_enemy>();
var data = await db.Queryable<unit_user_enemy>().Where(it => it.userId == userId)
.OrderByDescending(it => it.addTime)
.ToListAsync();
List<RelationView> result = new List<RelationView>();
foreach (var item in data)
{
RelationView temp = new RelationView();
temp.user = await UserModelTool.GetUserView(item.eId);
temp.near = 0;
var mapService = App.GetService<IGameMapService>();
var onMap = await mapService.GetUserOnMapInfo(item.eId);
temp.mapName = onMap.mapName;
temp.cityName = onMap.cityName;
temp.sort = TimeExtend.GetTimeStampBySeconds((DateTime)item.addTime);
temp.isOnline = onMap.isOnline;
result.Add(temp);
}
return result;
}
public async Task<bool> CheckUserEnemy(string userId, string eId)
{
string euId = $"{userId}_{eId}";
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_enemy>();
return await db.Queryable<unit_user_enemy>().Where(it => it.euId == euId).AnyAsync();
}
public async Task<bool> AddUserEnemy(string userId, string eId)
{
string euId = $"{userId}_{eId}";
unit_user_enemy enemy = new unit_user_enemy()
{
euId = euId,
userId = userId,
eId = eId,
addTime = DateTime.Now
};
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_enemy>();
return await db.Insertable(enemy).ExecuteCommandAsync() > 0;
}
public async Task<bool> DeleteUserEnemy(string userId, string eId)
{
string euId = $"{userId}_{eId}";
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_enemy>();
return await db.Deleteable<unit_user_enemy>().Where(it => it.euId == euId).ExecuteCommandAsync() > 0;
}
#endregion
}