160 lines
5.3 KiB
C#
160 lines
5.3 KiB
C#
![]() |
using AutoMapper;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using YD_WeChatApplet.Api.Entitys;
|
|||
|
using YD_WeChatApplet.Api.SmartSportsEntitys;
|
|||
|
using YD_WeChatApplet.Api.Utilities;
|
|||
|
using YD_WeChatApplet.Commons.Dto.ClientSide;
|
|||
|
using YD_WeChatApplet.Commons.Dto.HomeWork;
|
|||
|
using YD_WeChatApplet.Commons.Dto.Patriarch;
|
|||
|
using YD_WeChatApplet.Commons.Dto.Resource;
|
|||
|
using YD_WeChatApplet.Commons.Dto.School;
|
|||
|
using YD_WeChatApplet.Commons.Enum;
|
|||
|
using YD_WeChatApplet.Context;
|
|||
|
using YD_WeChatApplet.Services;
|
|||
|
|
|||
|
namespace YD_WeChatApplet.Api.Services.Impl
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 用户偏好
|
|||
|
/// </summary>
|
|||
|
public class UserPreferenceService : IUserPreferenceService
|
|||
|
{
|
|||
|
public UserContext _userContext;
|
|||
|
private readonly IMapper _mapper;
|
|||
|
public UserPreferenceService(UserContext userContext, IMapper mapper)
|
|||
|
{
|
|||
|
_userContext = userContext;
|
|||
|
_mapper = mapper;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 新增偏好
|
|||
|
/// </summary>
|
|||
|
/// <param name="dto"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public async Task AddPreference(AddPreferenceDto dto)
|
|||
|
{
|
|||
|
var entity = new WCA_UserPreference()
|
|||
|
{
|
|||
|
UserId = UserLoginContext.Current.UserId,
|
|||
|
ResourceId = dto.ResourceId,
|
|||
|
Countdown = dto.Countdown,
|
|||
|
Interval = dto.Interval,
|
|||
|
//IsMusic = dto.IsMusic,
|
|||
|
CreateTime = DateTime.Now
|
|||
|
};
|
|||
|
|
|||
|
await _userContext.AddAsync(entity);
|
|||
|
await _userContext.SaveChangesAsync();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取个人偏好
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public async Task<GetPreferenceDto> GetPreferences()
|
|||
|
{
|
|||
|
var userId = UserLoginContext.Current.UserId;
|
|||
|
|
|||
|
var preference = await _userContext.UserPreference.FirstOrDefaultAsync(x => x.UserId == userId);
|
|||
|
|
|||
|
if (preference == null)
|
|||
|
{
|
|||
|
var entity = new WCA_UserPreference()
|
|||
|
{
|
|||
|
UserId = UserLoginContext.Current.UserId,
|
|||
|
ResourceId = 1,
|
|||
|
Countdown = 3,
|
|||
|
Interval = 10,
|
|||
|
//IsMusic = dto.IsMusic,
|
|||
|
CreateTime = DateTime.Now
|
|||
|
};
|
|||
|
|
|||
|
await _userContext.AddAsync(entity);
|
|||
|
await _userContext.SaveChangesAsync();
|
|||
|
|
|||
|
return new GetPreferenceDto()
|
|||
|
{
|
|||
|
Id = entity.Id,
|
|||
|
Countdown = entity.Countdown,
|
|||
|
Interval = entity.Interval,
|
|||
|
ResourceId = entity.ResourceId,
|
|||
|
ResourceUrl = "",
|
|||
|
CreateTime = entity.CreateTime
|
|||
|
};
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
var model = await (
|
|||
|
from a in _userContext.UserPreference
|
|||
|
join s in _userContext.BackgroundMusic on a.ResourceId equals s.Id
|
|||
|
|
|||
|
where a.UserId == UserLoginContext.Current.UserId
|
|||
|
select new GetPreferenceDto()
|
|||
|
{
|
|||
|
Id = a.Id,
|
|||
|
ResourceId = a.ResourceId,
|
|||
|
Countdown = a.Countdown,
|
|||
|
Interval = a.Interval,
|
|||
|
ResourceUrl = s.Url,
|
|||
|
CreateTime = a.CreateTime
|
|||
|
}).FirstAsync();
|
|||
|
|
|||
|
return model;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 设置偏好
|
|||
|
/// </summary>
|
|||
|
/// <param name="dto"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public async Task SetPreferences(SetPreferenceDto dto)
|
|||
|
{
|
|||
|
var userId = UserLoginContext.Current.UserId;
|
|||
|
|
|||
|
var preference = await _userContext.UserPreference.FirstOrDefaultAsync(x => x.UserId == userId);
|
|||
|
if (preference == null)
|
|||
|
{
|
|||
|
var entity = new WCA_UserPreference()
|
|||
|
{
|
|||
|
UserId = userId,
|
|||
|
ResourceId = dto.ResourceId,
|
|||
|
Countdown = dto.Countdown,
|
|||
|
Interval = dto.Interval,
|
|||
|
//IsMusic = dto.IsMusic,
|
|||
|
CreateTime = DateTime.Now
|
|||
|
};
|
|||
|
|
|||
|
await _userContext.AddAsync(entity);
|
|||
|
await _userContext.SaveChangesAsync();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
preference.ResourceId = dto.ResourceId > 0 ? dto.ResourceId : preference.ResourceId;
|
|||
|
preference.Countdown = dto.Countdown > 0 ? dto.Countdown : preference.Countdown;
|
|||
|
preference.Interval = dto.Interval > 0 ? dto.Interval : preference.Interval;
|
|||
|
|
|||
|
_userContext.Update(preference);
|
|||
|
await _userContext.SaveChangesAsync();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取背景音乐列表
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public async Task<List<GeBackgroundMusicDto>> GetBackgroundMusics()
|
|||
|
{
|
|||
|
var res = await _userContext.BackgroundMusic.Select(x => new GeBackgroundMusicDto()
|
|||
|
{
|
|||
|
Id = x.Id,
|
|||
|
Name = x.Name,
|
|||
|
Url = x.Url
|
|||
|
}).ToListAsync();
|
|||
|
|
|||
|
return res;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|