12121
This commit is contained in:
@@ -0,0 +1,227 @@
|
||||
namespace Application.Domain;
|
||||
|
||||
public class GameTeamService(ISqlSugarClient DbClient, IRedisCache redis) : IGameTeamService, ITransient
|
||||
{
|
||||
public async Task<List<TeamModel>> GetTeamDataPage(int area, string code, int page, int limit, RefAsync<int> total)
|
||||
{
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_team>();
|
||||
var data = await db.Queryable<game_team>().Where(it => it.areaId == area && it.code == code)
|
||||
.OrderByDescending(it => it.addTime).ToPageListAsync(page, limit, total);
|
||||
List<TeamModel> result = new List<TeamModel>();
|
||||
foreach (var item in data)
|
||||
{
|
||||
var temp = await GetTeamDataByTeamId(item.teamId);
|
||||
if (temp.state == 1)
|
||||
{
|
||||
result.Add(temp);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<unit_user_team> GetUserTeamData(string userId)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "TeamData", "UserData");
|
||||
if (await redis.HExistsHashAsync(key, userId))
|
||||
{
|
||||
return await redis.GetHashAsync<unit_user_team>(key, userId);
|
||||
}
|
||||
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_team>();
|
||||
var data = await db.Queryable<unit_user_team>().Where(it => it.userId == userId).SingleAsync();
|
||||
await redis.AddHashAsync(key, userId, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
public async Task<game_team> GetTeamInfo(string teamId)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "TeamData", "TeamData");
|
||||
if (await redis.HExistsHashAsync(key, teamId))
|
||||
{
|
||||
return await redis.GetHashAsync<game_team>(key, teamId);
|
||||
}
|
||||
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_team>();
|
||||
var data = await db.Queryable<game_team>().Where(it => it.teamId == teamId).SingleAsync();
|
||||
await redis.AddHashAsync(key, teamId, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
public async Task<List<unit_user_team>> GetTeamMember(string teamId)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "TeamData", "MemberData");
|
||||
if (await redis.HExistsHashAsync(key, teamId))
|
||||
{
|
||||
return await redis.GetHashAsync<List<unit_user_team>>(key, teamId);
|
||||
}
|
||||
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_team>();
|
||||
var data = await db.Queryable<unit_user_team>().Where(it => it.teamId == teamId).ToListAsync();
|
||||
await redis.AddHashAsync(key, teamId, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
private async Task<TeamModel> GetTeamData(string userId)
|
||||
{
|
||||
TeamModel result = new TeamModel();
|
||||
var userTeam = await GetUserTeamData(userId);
|
||||
if (userTeam.teamId == "0")
|
||||
{
|
||||
result.state = 0;
|
||||
result.user = new List<TeamUser>();
|
||||
result.team = new game_team();
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = await GetTeamDataByTeamId(userTeam.teamId);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TeamModel> GetTeamDataByTeamId(string teamId)
|
||||
{
|
||||
TeamModel result = new TeamModel();
|
||||
var teamInfo = await GetTeamInfo(teamId);
|
||||
if (teamInfo == null)
|
||||
{
|
||||
result.state = 0;
|
||||
result.user = new List<TeamUser>();
|
||||
result.team = new game_team();
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
var mapService = App.GetService<IGameMapService>();
|
||||
result.state = 1;
|
||||
result.team = teamInfo;
|
||||
var members = await GetTeamMember(teamId);
|
||||
List<TeamUser> user = new List<TeamUser>();
|
||||
foreach (var member in members)
|
||||
{
|
||||
TeamUser temp = new TeamUser();
|
||||
temp.user = await UserModelTool.GetUserView(member.userId);
|
||||
var onMap = await mapService.GetUserOnMapInfo(member.userId);
|
||||
temp.isOnline = onMap.isOnline;
|
||||
user.Add(temp);
|
||||
}
|
||||
|
||||
result.codeTips = "普通";
|
||||
result.user = user;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TeamModel> GetUserTeamInfo(string userId)
|
||||
{
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "TeamData", userId);
|
||||
if (await redis.ExistsAsync(key))
|
||||
{
|
||||
return await redis.GetAsync<TeamModel>(key);
|
||||
}
|
||||
|
||||
var data = await GetTeamData(userId);
|
||||
await redis.SetAsync(key, data, GameConfig.TeamCacheTime);
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
private async Task ClearTeamCache(string teamId)
|
||||
{
|
||||
var members = await GetTeamMember(teamId);
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "TeamData", "TeamData");
|
||||
await redis.DelHashAsync(key, teamId);
|
||||
key = string.Format(UserCache.BaseCacheKeys, "TeamData", "MemberData");
|
||||
await redis.DelHashAsync(key, teamId);
|
||||
List<string> memberDataKeys = new List<string>();
|
||||
List<string> userDataKeys = new List<string>();
|
||||
foreach (var member in members)
|
||||
{
|
||||
string mk = string.Format(UserCache.BaseCacheKeys, "TeamData", member.userId);
|
||||
memberDataKeys.Add(mk);
|
||||
userDataKeys.Add(member.userId);
|
||||
}
|
||||
|
||||
await redis.DelAsync(memberDataKeys.ToArray());
|
||||
string uk = string.Format(UserCache.BaseCacheKeys, "TeamData", "UserData");
|
||||
await redis.DelHashAsync(uk, userDataKeys.ToArray());
|
||||
}
|
||||
|
||||
public async Task<bool> Add(string userId, string teamId, bool isAdd = false, string name = "", int area = 0,
|
||||
string code = "",
|
||||
string par = "", int count = 0)
|
||||
{
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_team>();
|
||||
if (isAdd)
|
||||
{
|
||||
game_team team = new game_team()
|
||||
{
|
||||
teamId = teamId,
|
||||
areaId = area,
|
||||
name = name,
|
||||
code = code,
|
||||
par = par,
|
||||
count = count,
|
||||
masterId = userId,
|
||||
addTime = DateTime.Now
|
||||
};
|
||||
await db.Insertable(team).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
var result = await db.Updateable<unit_user_team>()
|
||||
.SetColumns(it => it.teamId == teamId)
|
||||
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
||||
if (result)
|
||||
{
|
||||
if (isAdd == false)
|
||||
{
|
||||
await ClearTeamCache(teamId);
|
||||
}
|
||||
|
||||
string key = string.Format(UserCache.BaseCacheKeys, "TeamData", "UserData");
|
||||
await redis.DelHashAsync(key, userId);
|
||||
key = string.Format(UserCache.BaseCacheKeys, "TeamData", userId);
|
||||
await redis.DelAsync(key);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> Remove(string userId, string teamId)
|
||||
{
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<unit_user_team>();
|
||||
var result = await db.Updateable<unit_user_team>()
|
||||
.SetColumns(it => it.teamId == "0")
|
||||
.Where(it => it.userId == userId).ExecuteCommandAsync() > 0;
|
||||
if (result)
|
||||
{
|
||||
await ClearTeamCache(teamId);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task DisTeam(string teamId)
|
||||
{
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_team>();
|
||||
await db.Deleteable<game_team>().Where(it => it.teamId == teamId).ExecuteCommandAsync();
|
||||
await db.Updateable<unit_user_team>()
|
||||
.SetColumns(it => it.teamId == "0")
|
||||
.Where(it => it.teamId == teamId).ExecuteCommandAsync();
|
||||
await ClearTeamCache(teamId);
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateTeamName(string teamId, string name)
|
||||
{
|
||||
var db = DbClient.AsTenant().GetConnectionWithAttr<game_team>();
|
||||
bool result = await db.Updateable<game_team>().SetColumns(it => it.name == name)
|
||||
.Where(it => it.teamId == teamId).ExecuteCommandAsync() > 0;
|
||||
if (result)
|
||||
{
|
||||
await ClearTeamCache(teamId);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -256,6 +256,12 @@ public class UnitUserService(ISqlSugarClient DbClient, IRedisCache redis) : IUni
|
||||
onlineTime.monthTime = 0;
|
||||
onlineTime.totalTime = 0;
|
||||
db.Insertable(onlineTime).AddQueue();
|
||||
|
||||
//注册队伍
|
||||
unit_user_team team = new unit_user_team();
|
||||
team.userId = userId;
|
||||
team.teamId = "0";
|
||||
db.Insertable(team).AddQueue();
|
||||
|
||||
await db.SaveQueuesAsync(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user