2025-01-13 21:06:59 +08:00

79 lines
2.7 KiB
C#

using Microsoft.EntityFrameworkCore;
using YD_XinWei.Api.Context;
using YD_XinWei.Api.Services.Interface;
using YD_XinWei.Api.Utilities;
using YD_XinWei.Commons.Dto.Teacher;
using YD_XinWei.Commons.Exceptions;
namespace YD_XinWei.Api.Services.Impl
{
public class TeacherService : ITeacherService
{
public UserContext _userContext;
public TeacherService(UserContext userContext)
{
_userContext = userContext;
}
public async Task<TeacherProfileDto> TeacherProfile()
{
var userId = UserLoginContext.Current.UserId;
var res = await _userContext.Users.Where(x => x.User_Id == userId).Select(x => new TeacherProfileDto()
{
UserTrueName = x.UserTrueName,
BirthDate = x.BirthDate,
Gender = x.Gender,
HeadImageUrl = x.HeadImageUrl,
Height = x.Height,
Weight = x.Weight
}).FirstOrDefaultAsync();
return res;
}
public async Task ModifyTeacherProfile(TeacherProfileDto paramDto)
{
var userId = UserLoginContext.Current.UserId;
var model = await _userContext.Users.FirstOrDefaultAsync(x => x.User_Id == userId);
if (model == null)
throw new BizException("更新数据为空");
model.UserTrueName = !string.IsNullOrWhiteSpace(paramDto.UserTrueName) ? paramDto.UserTrueName : model.UserTrueName;
model.HeadImageUrl = !string.IsNullOrWhiteSpace(paramDto.HeadImageUrl) ? paramDto.HeadImageUrl : model.HeadImageUrl;
model.BirthDate = paramDto.BirthDate ?? model.BirthDate;
model.Gender = paramDto.Gender ?? model.Gender;
model.Height = paramDto.Height ?? model.Height;
model.Weight = paramDto.Weight ?? model.Weight;
if (_userContext.Entry(model).State == EntityState.Modified)
{
await _userContext.SaveChangesAsync();
}
}
public async Task ModifyTeacherPwd(ModifyTeacherPwdDto paramDto)
{
if (paramDto.NewrPwd != paramDto.ConfirmPwd)
throw new BizException("确认密码与新密码不一致");
var userId = UserLoginContext.Current.UserId;
var model = await _userContext.Users.FirstOrDefaultAsync(x => x.User_Id == userId && x.UserPwd == paramDto.OldPwd);
if (model == null)
throw new BizException("旧密码输入有误");
model.UserPwd = paramDto.NewrPwd;
if (_userContext.Entry(model).State == EntityState.Modified)
{
await _userContext.SaveChangesAsync();
}
}
}
}