288 lines
12 KiB
C#
288 lines
12 KiB
C#
using CGamesServer.Business.Dto;
|
||
using CGamesServer.Business.Services.Interface;
|
||
using CGamesServer.DataModel;
|
||
using MessagePack;
|
||
using Microsoft.IdentityModel.Tokens;
|
||
using Models;
|
||
using SqlSugar;
|
||
using System.Linq.Expressions;
|
||
|
||
namespace CGamesServer.Business.Services
|
||
{
|
||
public class UserService : IBusinessService
|
||
{
|
||
private readonly SqlSugarRepository<User> _repo;
|
||
|
||
public UserService(SqlSugarRepository<User> repo)
|
||
{
|
||
_repo = repo;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 添加或者更新用户信息
|
||
/// </summary>
|
||
/// <param name="user">用户实体类</param>
|
||
/// <returns></returns>
|
||
public async Task<long> AddOrUpdateUser(User user)
|
||
{
|
||
if (string.IsNullOrEmpty(user.UserName))
|
||
{
|
||
return -1;
|
||
}
|
||
try
|
||
{
|
||
var existUser = await _repo.AsQueryable().Where(u => u.UserName == user.UserName).SplitTable().FirstAsync().ConfigureAwait(false);
|
||
if (existUser != null)
|
||
{
|
||
existUser.LoginTime = DateTime.Now;
|
||
existUser.AvatarUrl = user.AvatarUrl;
|
||
existUser.NickName = user.NickName;
|
||
existUser.Platform = user.Platform;
|
||
await _repo.AsUpdateable(existUser).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);//.UpdateAsync(existUser).ConfigureAwait(false);
|
||
return existUser.Id;
|
||
}
|
||
else
|
||
{
|
||
if (user.Id == 0)
|
||
{
|
||
user.RegisterTime = DateTime.Now;
|
||
user.LoginTime = DateTime.Now;
|
||
user.CreateTime = DateTime.Now;
|
||
long id = await _repo.AsInsertable(user).SplitTable().ExecuteReturnSnowflakeIdAsync().ConfigureAwait(false);//.InsertReturnSnowflakeIdAsync(user).ConfigureAwait(false);
|
||
return id;
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
//Console.WriteLine(ex);
|
||
return -1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 添加或者更新用户战绩分数
|
||
/// </summary>
|
||
/// <param name="userName">用户名</param>
|
||
/// <param name="addScore">用户要增加的分数</param>
|
||
/// <returns>返回0,表示更新成功,返回1,表示上传的用户名为空,返回-1,表示更新异常</returns>
|
||
public async Task<int> UpdateUserScore(string userName, int addScore, int destroyCount, int quiltDestroyCount)
|
||
{
|
||
if (string.IsNullOrEmpty(userName))
|
||
{
|
||
return 1;
|
||
}
|
||
if (addScore > 0)
|
||
{
|
||
var existUser = await _repo.AsQueryable().Where(u => u.UserName == userName).SplitTable().FirstAsync().ConfigureAwait(false);
|
||
if (existUser != null)
|
||
{
|
||
try
|
||
{
|
||
existUser.FlightScore += addScore;
|
||
existUser.DestroyCount += destroyCount;
|
||
existUser.QuiltDestroyCount += quiltDestroyCount;
|
||
await _repo.AsUpdateable(existUser).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);//.UpdateAsync(existUser).ConfigureAwait(false);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine(ex);
|
||
return -1;
|
||
}
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取排行榜信息
|
||
/// </summary>
|
||
/// <param name="pageNumber">页数</param>
|
||
/// <returns></returns>
|
||
public async Task<List<TheCharts>> GetTheCharts(int pageNumber)
|
||
{
|
||
var theCharts = await _repo.AsQueryable().Where(s => s.NickName != null).SplitTable().OrderByDescending(s => s.DestroyCount * (s.DestroyCount / s.QuiltDestroyCount))
|
||
.Select(s => new TheCharts
|
||
{
|
||
NickName = s.NickName,
|
||
AvatarUrl = s.AvatarUrl,
|
||
WarRecord = s.QuiltDestroyCount == 0 ? s.DestroyCount.ToString() : SqlFunc.Round(s.DestroyCount * ((double)s.DestroyCount / s.QuiltDestroyCount), 0).ToString(),
|
||
DestroyCount = s.DestroyCount,
|
||
QuiltDestroyCount = s.QuiltDestroyCount,
|
||
FlightSwitch = s.QuiltDestroyCount == 0 ? "1" : SqlFunc.Round((double)s.DestroyCount / s.QuiltDestroyCount, 2).ToString(),
|
||
Ranking = SqlFunc.MappingColumn(default(long), "ROW_NUMBER() OVER (ORDER BY (DestroyCount * (DestroyCount / QuiltDestroyCount)) DESC)"),
|
||
}).ToPageListAsync(pageNumber, 30).ConfigureAwait(false);
|
||
return theCharts;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取用户信息
|
||
/// </summary>
|
||
/// <param name="id">用户的Id</param>
|
||
/// <returns></returns>
|
||
public async Task<User> GetUser(long id)
|
||
{
|
||
var user = await _repo.AsQueryable().Where(u => u.Id == id).SplitTable().FirstAsync().ConfigureAwait(false);//.GetByIdAsync(id).ConfigureAwait(false);
|
||
return user;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 签到
|
||
/// </summary>
|
||
/// <param name="userName">用户名</param>
|
||
/// <param name="addScore">用户要增加的分数</param>
|
||
/// <returns>返回0,表示更新成功,返回1,表示上传的用户名为空,返回2,表明今天已经到签到上限</returns>
|
||
public async Task<int> GetSigninLimit(string userName)
|
||
{
|
||
if (string.IsNullOrEmpty(userName))
|
||
{
|
||
return 1;
|
||
}
|
||
var existUser = await _repo.AsQueryable().Where(u => u.UserName == userName).SplitTable().FirstAsync().ConfigureAwait(false);
|
||
if (existUser != null)
|
||
{
|
||
if (existUser.SigninTime != null)
|
||
{
|
||
if (existUser.SigninTime >= DateTime.Today && existUser.SigninTime < DateTime.Today.AddDays(1))
|
||
{
|
||
//每天最多签到2次
|
||
if (existUser.SigninCount >= 100)
|
||
{
|
||
return 100;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 签到
|
||
/// </summary>
|
||
/// <param name="userName">用户名</param>
|
||
/// <param name="addScore">用户要增加的分数</param>
|
||
/// <returns>返回0,表示更新成功,返回1,表示上传的用户名为空,返回2,表明今天已经到签到上限,返回-1,表示更新异常,</returns>
|
||
public async Task<int> Signin(string userName, int addScore)
|
||
{
|
||
if (string.IsNullOrEmpty(userName))
|
||
{
|
||
return 1;
|
||
}
|
||
addScore = 5;//暂时先锁定5分
|
||
if (addScore > 0)
|
||
{
|
||
var existUser = await _repo.AsQueryable().Where(u => u.UserName == userName).SplitTable().FirstAsync().ConfigureAwait(false);
|
||
if (existUser != null)
|
||
{
|
||
try
|
||
{
|
||
if (existUser.SigninTime == null)
|
||
{
|
||
existUser.FlightScore += addScore;
|
||
existUser.SigninTime = DateTime.Now;
|
||
existUser.SigninCount++;
|
||
}
|
||
else
|
||
{
|
||
if (existUser.SigninTime >= DateTime.Today && existUser.SigninTime < DateTime.Today.AddDays(1))
|
||
{
|
||
//每天最多签到2次
|
||
if (existUser.SigninCount >= 2)
|
||
{
|
||
return 2;
|
||
}
|
||
}
|
||
if (existUser.SigninTime < DateTime.Today)
|
||
{
|
||
existUser.SigninCount = 0;
|
||
}
|
||
existUser.FlightScore += addScore;
|
||
existUser.SigninTime = DateTime.Now;
|
||
existUser.SigninCount++;
|
||
}
|
||
await _repo.AsUpdateable(existUser).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);//.UpdateAsync(existUser).ConfigureAwait(false);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine(ex);
|
||
return -1;
|
||
}
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取用户战绩积分信息
|
||
/// </summary>
|
||
/// <param name="id">用户的Id</param>
|
||
/// <returns></returns>
|
||
public async Task<string> GetUserFlightScore(string userName)
|
||
{
|
||
var score = await _repo.AsQueryable().Where(s => s.UserName == userName).SplitTable().Select(s => s.FlightScore).FirstAsync().ConfigureAwait(false);
|
||
return score.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取用户Name获取用户信息
|
||
/// </summary>
|
||
/// <param name="userName">用户的userName</param>
|
||
/// <returns></returns>
|
||
public async Task<User> GetUserFromName(string userName)
|
||
{
|
||
var user = await _repo.AsQueryable().Where(s => s.UserName == userName).SplitTable().FirstAsync().ConfigureAwait(false);
|
||
return user;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 获取用户Name获取用户信息(包括排名)
|
||
/// </summary>
|
||
/// <param name="userName">用户的userName</param>
|
||
/// <returns></returns>
|
||
public async Task<UserWithRanking> GetUserAndRankingFromName(string userName)
|
||
{
|
||
//Console.WriteLine(_repo.AsQueryable().OrderByDescending(u => u.FlightScore).OrderByDescending(s => s.DestroyCount).OrderBy(s => s.QuiltDestroyCount).Select(u => new UserWithRanking
|
||
//{
|
||
// AvatarUrl = u.AvatarUrl,
|
||
// DestroyCount = u.DestroyCount,
|
||
// FlightScore = u.FlightScore,
|
||
// Id = u.Id,
|
||
// LoginTime = u.LoginTime,
|
||
// NickName = u.NickName,
|
||
// Platform = u.Platform,
|
||
// QuiltDestroyCount = u.QuiltDestroyCount,
|
||
// Ranking = SqlFunc.MappingColumn(default(long), "ROW_NUMBER() OVER (ORDER BY FlightScore DESC, DestroyCount DESC, QuiltDestroyCount ASC)"),
|
||
// RegisterTime = u.RegisterTime,
|
||
// UserName = u.UserName
|
||
//}).AS("Temp").MergeTable()
|
||
//.Where(u => u.UserName == userName).ToSql());
|
||
|
||
var user = await _repo.AsQueryable().SplitTable().OrderByDescending(u => u.DestroyCount * (u.DestroyCount / u.QuiltDestroyCount)).Select(u => new UserWithRanking
|
||
{
|
||
AvatarUrl = u.AvatarUrl,
|
||
DestroyCount = u.DestroyCount,
|
||
WarRecord = u.QuiltDestroyCount == 0 ? u.DestroyCount.ToString() : SqlFunc.Round(u.DestroyCount * ((double)u.DestroyCount / u.QuiltDestroyCount), 0).ToString(),
|
||
FlightSwitch = u.QuiltDestroyCount == 0 ? "1" : SqlFunc.Round((double)u.DestroyCount / u.QuiltDestroyCount, 2).ToString(),
|
||
Id = u.Id,
|
||
LoginTime = u.LoginTime,
|
||
NickName = u.NickName,
|
||
Platform = u.Platform,
|
||
QuiltDestroyCount = u.QuiltDestroyCount,
|
||
Ranking = SqlFunc.MappingColumn(default(long), "ROW_NUMBER() OVER (ORDER BY (DestroyCount * (DestroyCount / QuiltDestroyCount)) DESC)").ToString(),
|
||
RegisterTime = u.RegisterTime,
|
||
UserName = u.UserName
|
||
}).MergeTable()
|
||
.Where(u => u.UserName == userName).FirstAsync();
|
||
|
||
return user;
|
||
}
|
||
}
|
||
}
|