85 lines
3.6 KiB
C#
85 lines
3.6 KiB
C#
using CGamesServer.Business.Dto;
|
|
using CGamesServer.Business.Services.Interface;
|
|
using CGamesServer.DataModel;
|
|
using Models;
|
|
using System.Linq.Expressions;
|
|
|
|
namespace CGamesServer.Business.Services
|
|
{
|
|
public class UserDeviceColorService : IBusinessService
|
|
{
|
|
private readonly SqlSugarRepository<UserDeviceColor> _repoDeviceColor;
|
|
private readonly SqlSugarRepository<User> _repoUser;
|
|
|
|
public UserDeviceColorService(SqlSugarRepository<UserDeviceColor> repoDeviceColor, SqlSugarRepository<User> repoUser)
|
|
{
|
|
_repoDeviceColor = repoDeviceColor;
|
|
_repoUser = repoUser;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 添加或者更新颜色信息
|
|
/// </summary>
|
|
/// <param name="userName">用户的UserName</param>
|
|
/// <param name="color">颜色实体类List</param>
|
|
/// <returns></returns>
|
|
public async Task AddOrUpdateDevice(string userName, UserDeviceColor submitColor)
|
|
{
|
|
var existUser = await _repoUser.AsQueryable().Where(s => s.UserName == userName).SplitTable().FirstAsync().ConfigureAwait(false);
|
|
if (existUser == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var existColor = await _repoDeviceColor.AsQueryable().SplitTable()
|
|
.LeftJoin<User>(_repoUser.AsQueryable().SplitTable(), (color, user) => color.UserId == user.Id)
|
|
.Where((color, user) => user.UserName == userName && color.Color == submitColor.Color).Select((color, user) => color).FirstAsync().ConfigureAwait(false);
|
|
List<UserDeviceColor> updateColors = new List<UserDeviceColor>();
|
|
|
|
submitColor.UserId = existUser.Id;
|
|
if (submitColor.IsDefault)
|
|
{
|
|
var existDefaultColor = await _repoDeviceColor.AsQueryable().SplitTable()
|
|
.LeftJoin<User>(_repoUser.AsQueryable().SplitTable(), (color, user) => color.UserId == user.Id)
|
|
.Where((color, user) => user.UserName == userName && color.IsDefault).Select((color, user) => color).FirstAsync().ConfigureAwait(false);
|
|
if (existDefaultColor != null)
|
|
{
|
|
existDefaultColor.IsDefault = false;
|
|
updateColors.Add(existDefaultColor);
|
|
}
|
|
}
|
|
if (existColor == null)
|
|
{
|
|
submitColor.Id = 0;
|
|
submitColor.CreateTime = DateTime.Now;
|
|
await _repoDeviceColor.AsInsertable(submitColor).SplitTable().ExecuteReturnSnowflakeIdAsync().ConfigureAwait(false);
|
|
}
|
|
else
|
|
{
|
|
submitColor.Id = existColor.Id;
|
|
submitColor.CreateTime = DateTime.Now;
|
|
updateColors.Add(submitColor);
|
|
}
|
|
if (updateColors.Any())
|
|
{
|
|
await _repoDeviceColor.AsUpdateable(updateColors).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);//.UpdateRangeAsync(updateColors).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取用户的颜色信息
|
|
/// </summary>
|
|
/// <param name="userName"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<UserDeviceColor>> GetUserColors(string userName)
|
|
{
|
|
var existColors = await _repoDeviceColor.AsQueryable().SplitTable()
|
|
.LeftJoin<User>(_repoUser.AsQueryable().SplitTable(), (color, user) => color.UserId == user.Id)
|
|
.Where((color, user) => user.UserName == userName).Select((color, user) => color).ToListAsync().ConfigureAwait(false);
|
|
return existColors;
|
|
}
|
|
|
|
}
|
|
}
|