376 lines
15 KiB
C#
376 lines
15 KiB
C#
using langguanApi.Common;
|
||
using langguanApi.Extensions.AutoDI;
|
||
using langguanApi.Model;
|
||
using langguanApi.Model.Dto;
|
||
using langguanApi.Model.Dto.SystemConfigurationDto;
|
||
using langguanApi.Model.Entity;
|
||
using Mapster;
|
||
using Microsoft.AspNetCore.Mvc.Filters;
|
||
using MongoDB.Bson.IO;
|
||
using MongoDB.Driver.Linq;
|
||
using Org.BouncyCastle.Asn1.Ocsp;
|
||
using System.Collections.Generic;
|
||
using System.Linq.Expressions;
|
||
|
||
namespace langguanApi.Service
|
||
{
|
||
[ServiceInjection(InjectionType.Transient)]
|
||
public class UserService : BaseService<UserEntity>
|
||
{
|
||
private ILogger<UserService> _logger;
|
||
private RoleService _roleService;
|
||
public UserService(IConfiguration config, ILogger<UserService> logger, RoleService roleService) : base(config, nameof(UserEntity).Replace("Entity", ""))
|
||
{
|
||
_logger = logger;
|
||
_roleService = roleService;
|
||
}
|
||
/// <summary>
|
||
/// 登录
|
||
/// </summary>
|
||
/// <param name="username"></param>
|
||
/// <param name="password"></param>
|
||
/// <returns></returns>
|
||
public async Task<UserEntity> login(string username, string password)
|
||
{
|
||
Expression<Func<UserEntity, bool>> exp = filter =>
|
||
filter.Username == username && filter.Password == StringHelper.MD5Encrypt32(password);
|
||
var list = await base.GetListWithExp(exp);
|
||
return list.FirstOrDefault();
|
||
}
|
||
/// <summary>
|
||
/// 根据用户名获取用户信息
|
||
/// </summary>
|
||
/// <param name="username"></param>
|
||
/// <returns></returns>
|
||
public async Task<UserEntity> GetByUsername(string username)
|
||
{
|
||
Expression<Func<UserEntity, bool>> exp = filter =>
|
||
filter.Username == username;
|
||
var list = await base.GetListWithExp(exp);
|
||
return list.FirstOrDefault();
|
||
}
|
||
/// <summary>
|
||
/// 根据用户ID获取用户信息
|
||
/// </summary>
|
||
/// <param name="id"></param>
|
||
/// <returns></returns>
|
||
public async Task<UserEntity> GetById(string id)
|
||
{
|
||
return await base.GetAsync(id);
|
||
}
|
||
///新加用户
|
||
public async Task<ApiResult> AddUser(UserAdd user)
|
||
{
|
||
var entity = user.Adapt<UserEntity>();
|
||
entity.Password = StringHelper.MD5Encrypt32(user.Password);
|
||
var result = await base.CreateAsync(entity);
|
||
return new ApiResult()
|
||
{
|
||
code = 0,
|
||
data = result
|
||
};
|
||
}
|
||
/// <summary>
|
||
/// 分页取数据
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
public async Task<ApiResult> GetPage(reqpage input)
|
||
{
|
||
Expression<Func<UserEntity, bool>> exp = filter => true;
|
||
if (!string.IsNullOrEmpty(input.key))
|
||
{
|
||
exp = filter => filter.Username.Contains(input.key) || filter.Phone.Contains(input.key) || filter.Email.Contains(input.key);
|
||
}
|
||
return await base.GetPager(new ReqPaing()
|
||
{
|
||
pageSize = input.pageSize,
|
||
current = input.current
|
||
}, exp);
|
||
}
|
||
|
||
#region 用户管理
|
||
/// <summary>
|
||
/// 用户是否存在
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
public async Task<bool> Exist(UserDto input)
|
||
{
|
||
var entity = input.Adapt<UserEntity>();
|
||
Expression<Func<UserEntity, bool>> exp = filter => filter.Id == entity.Id;
|
||
return await base.Exist(exp);
|
||
}
|
||
/// <summary>
|
||
///新增用户
|
||
/// </summary>
|
||
/// <param name="input">新增用户dto</param>
|
||
/// <returns></returns>
|
||
public async Task<ApiResult> AddUser(UserDto input)
|
||
{
|
||
try
|
||
{
|
||
#region 校验参数
|
||
if (string.IsNullOrEmpty(input.Username))
|
||
{
|
||
return new ApiResult { code = 1, msg = "用户名非空" };
|
||
}
|
||
if (string.IsNullOrEmpty(input.Tel))
|
||
{
|
||
return new ApiResult { code = 1, msg = "手机号非空" };
|
||
}
|
||
if (string.IsNullOrEmpty(input.Email))
|
||
{
|
||
return new ApiResult { code = 1, msg = "邮箱非空" };
|
||
}
|
||
if (string.IsNullOrEmpty(input.RoleId))
|
||
{
|
||
return new ApiResult { code = 1, msg = "角色不能为空" };
|
||
}
|
||
#endregion
|
||
#region 组织数据
|
||
var entity = input.Adapt<UserEntity>();
|
||
entity.Phone = input.Tel;
|
||
if (!string.IsNullOrEmpty(input.Password))
|
||
{
|
||
entity.Password = StringHelper.MD5Encrypt32(input.Password);
|
||
}
|
||
#endregion
|
||
#region 保存数据
|
||
if (entity != null && string.IsNullOrEmpty(entity.Id))
|
||
{
|
||
await base.CreateAsync(entity);
|
||
}
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError($"新增用户出现异常,请求参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," +
|
||
$"请求接口:'api/User/AddUser'," +
|
||
$"异常信息:{ex.Message}," +
|
||
$"异常位置:{ex.StackTrace}"
|
||
);
|
||
return new ApiResult { code = 1, msg = "保存用户信息失败" };
|
||
}
|
||
finally
|
||
{
|
||
_logger.LogInformation($"新增用户参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}");
|
||
}
|
||
return new ApiResult { code = 0, msg = "保存用户信息成功" };
|
||
}
|
||
/// <summary>
|
||
/// 获取用户列表
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
public async Task<ApiResult> GetUserList(UserQueryDto input)
|
||
{
|
||
List<UserDetailDto> list = new List<UserDetailDto>();
|
||
try
|
||
{
|
||
#region 组织查询条件
|
||
Expression<Func<UserEntity, bool>> exp = filter => filter.IsDelete == false;
|
||
if (!string.IsNullOrEmpty(input.key))
|
||
{
|
||
exp = filter => filter.Username.Contains(input.key)|| filter.Phone.Contains(input.key)|| filter.Email.Contains(input.key);
|
||
}
|
||
#endregion
|
||
#region 获取数据
|
||
var result = await base.GetListWithExp(exp);//获取人员信息
|
||
if (result.Count() > 0)
|
||
{
|
||
result = result.Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize);
|
||
}
|
||
else
|
||
{
|
||
return new ApiResult { code = 0, data = null, msg = "没有用户信息" };
|
||
}
|
||
var roleList = await _roleService.GetRoleListByIds(result.Select(s => s.RoleId).ToList());//根据角色Id获取角色信息
|
||
#endregion
|
||
#region 组装返回数据
|
||
foreach (var item in result)
|
||
{
|
||
list.Add(new UserDetailDto
|
||
{
|
||
UserId = item.Id,
|
||
Username = item.Username,
|
||
Email = item.Email,
|
||
RoleId = item.RoleId,
|
||
RoleName = roleList.FirstOrDefault(s => s.Id == item.RoleId)?.RoleName,
|
||
Tel = item.Phone,
|
||
});
|
||
}
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError($"获取用户列表出现异常,请求参数:userQuery:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," +
|
||
$"请求接口:'api/User/GetUserList'," +
|
||
$"异常信息:{ex.Message}," +
|
||
$"异常位置:{ex.StackTrace}"
|
||
);
|
||
return new ApiResult { code = 1, msg = "获取用户列表失败" };
|
||
}
|
||
finally
|
||
{
|
||
_logger.LogInformation($"获取用户列表参数:userQuery:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}");
|
||
}
|
||
return new ApiResult { code = 0, data = list,msg="获取信息成功" };
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据用户Id获取用户信息
|
||
/// </summary>
|
||
/// <param name="userId"></param>
|
||
/// <returns></returns>
|
||
public async Task<ApiResult> GetUserById(string userId)
|
||
{
|
||
UserDetailDto userDetail = null;
|
||
try
|
||
{
|
||
#region 校验数据
|
||
if (string.IsNullOrEmpty(userId))
|
||
{
|
||
return new ApiResult { code = 1, msg = "获取用户信息失败,userId非空" };
|
||
}
|
||
#endregion
|
||
#region 获取数据
|
||
var user = await base.GetAsync(userId);//根据userId获取用户信息
|
||
if (user == null)
|
||
{
|
||
return new ApiResult { code = 1, msg = "用户不存在" };
|
||
}
|
||
var role = await _roleService.GetRoleById(user.RoleId);//根据角色Id获取角色信息
|
||
if (role == null)
|
||
{
|
||
return new ApiResult { code = 1, msg = "角色不存在" };
|
||
}
|
||
#endregion
|
||
#region 组织需要展示的数据
|
||
userDetail = new UserDetailDto
|
||
{
|
||
UserId = user.Id,
|
||
Username = user.Username,
|
||
Email = user.Email,
|
||
RoleId = user.RoleId,
|
||
RoleName = role.RoleName,
|
||
Tel = user.Phone
|
||
};
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError($"获取用户信息出现异常,请求参数:userId:{userId}," +
|
||
$"请求接口:'api/User/GetUserById'," +
|
||
$"异常信息:{ex.Message}," +
|
||
$"异常位置:{ex.StackTrace}"
|
||
);
|
||
return new ApiResult { code = 1, msg = "获取用户信息失败" };
|
||
}
|
||
finally
|
||
{
|
||
_logger.LogInformation($"获取用户信息参数:userId:{userId}");
|
||
}
|
||
return new ApiResult { code = 0, data = userDetail };
|
||
}
|
||
|
||
|
||
//更新用户信息方法
|
||
public async Task<ApiResult> UpdateUser(UserDto input)
|
||
{
|
||
try
|
||
{
|
||
#region 校验参数
|
||
if (string.IsNullOrEmpty(input.Id))
|
||
{
|
||
return new ApiResult { code = 1, msg = "用户Id不能为空" };
|
||
}
|
||
if (string.IsNullOrEmpty(input.Username))
|
||
{
|
||
return new ApiResult { code = 1, msg = "用户名不能为空" };
|
||
}
|
||
if (string.IsNullOrEmpty(input.Tel))
|
||
{
|
||
return new ApiResult { code = 1, msg = "手机号不能为空" };
|
||
}
|
||
if (string.IsNullOrEmpty(input.Email))
|
||
{
|
||
return new ApiResult { code = 1, msg = "邮箱不能为空" };
|
||
}
|
||
if (string.IsNullOrEmpty(input.RoleId))
|
||
{
|
||
return new ApiResult { code = 1, msg = "角色不能为空" };
|
||
}
|
||
#endregion
|
||
#region 组织数据
|
||
var userEntity = input.Adapt<UserEntity>();
|
||
userEntity.Phone = input.Tel;
|
||
if (!string.IsNullOrEmpty(input.Password))
|
||
{
|
||
userEntity.Password = StringHelper.MD5Encrypt32(input.Password);
|
||
}
|
||
#endregion
|
||
#region 更新数据
|
||
await base.UpdateAsync(input.Id, userEntity);//更新用户信息
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError($"更新用户信息出现异常,请求参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," +
|
||
$"请求接口:'api/User/UpdateUser'," +
|
||
$"异常信息:{ex.Message}," +
|
||
$"异常位置:{ex.StackTrace}"
|
||
);
|
||
return new ApiResult { code = 1, msg = "更新用户信息失败" };
|
||
}
|
||
finally
|
||
{
|
||
_logger.LogInformation($"更新用户信息参数:user:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}");
|
||
}
|
||
return new ApiResult { code = 0, msg = "更新用户信息成功" };
|
||
}
|
||
|
||
/// <summary>
|
||
/// 作废用户的方法
|
||
/// </summary>
|
||
/// <param name="userId"></param>
|
||
/// <returns></returns>
|
||
public async Task<ApiResult> DeleteUser(string userId)
|
||
{
|
||
try
|
||
{
|
||
#region 校验数据
|
||
if (string.IsNullOrEmpty(userId))
|
||
{
|
||
return new ApiResult { code = 1, msg = "用户Id不能为空" };
|
||
}
|
||
#endregion
|
||
#region 更新数据
|
||
var user = await base.GetAsync(userId);//根据userId获取用户信息
|
||
if (user == null)
|
||
{
|
||
return new ApiResult { code = 1, msg = "用户不存在" };
|
||
}
|
||
user.IsDelete = true;
|
||
await base.UpdateAsync(userId, user);//更新用户信息
|
||
#endregion
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogError($"删除用户信息出现异常,请求参数:userId:{userId}," +
|
||
$"请求接口:'api/User/DeleteUser'," +
|
||
$"异常信息:{ex.Message}," +
|
||
$"异常位置:{ex.StackTrace}"
|
||
);
|
||
return new ApiResult { code = 1, msg = "删除用户信息失败" };
|
||
}
|
||
finally
|
||
{
|
||
_logger.LogInformation($"删除用户信息参数:userId:{userId}");
|
||
}
|
||
return new ApiResult { code = 0, msg = "删除用户信息成功" };
|
||
}
|
||
#endregion
|
||
}
|
||
}
|