226 lines
8.4 KiB
C#
226 lines
8.4 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.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 => filter.Username.Contains(input.key) && filter.IsDelete == false;
|
||
return await base.GetPager(new ReqPaing()
|
||
{
|
||
pageSize = input.pageSize,
|
||
current = input.current
|
||
}, exp);
|
||
}
|
||
///更新用户信息
|
||
public async Task UpdateUser(UserUpdate user)
|
||
{
|
||
var entity = user.Adapt<UserEntity>();
|
||
entity.Password = StringHelper.MD5Encrypt32(user.Password);
|
||
await base.UpdateAsync(user.Id, entity);
|
||
}
|
||
///删除用户
|
||
public async Task DeleteUser(string id)
|
||
{
|
||
await base.RemoveAsync(id);
|
||
}
|
||
|
||
#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 组织数据
|
||
|
||
if (!string.IsNullOrEmpty(input.BrithdayDateTime))
|
||
{
|
||
input.Brithday = Convert.ToDateTime(input.BrithdayDateTime);
|
||
}
|
||
if (!string.IsNullOrEmpty(input.HiredateDateTime))
|
||
{
|
||
input.Hiredate = Convert.ToDateTime(input.HiredateDateTime);
|
||
}
|
||
input.IsEnable = 1;
|
||
var entity = input.Adapt<UserEntity>();
|
||
#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)
|
||
{
|
||
try
|
||
{
|
||
#region 组织查询条件
|
||
Expression<Func<UserEntity, bool>> exp = filter => filter.IsDelete == false;
|
||
if (!string.IsNullOrEmpty(input.Username))
|
||
{
|
||
exp = filter => filter.Username.Contains(input.Username);
|
||
}
|
||
#endregion
|
||
|
||
#region 获取数据
|
||
var result = await base.GetListWithExp(exp);//获取人员信息
|
||
var roleList = await _roleService.GetRoleListByIds(result.Select(s => s.roleId).ToList());
|
||
#endregion
|
||
#region 组装返回数据
|
||
List<UserListDto> list = new List<UserListDto>();
|
||
foreach (var item in result)
|
||
{
|
||
list.Add(new UserListDto
|
||
{
|
||
Username = item.Username,
|
||
Email = item.Email,
|
||
roleName = roleList.FirstOrDefault(s => s.Id == item.roleId)?.RoleName,
|
||
});
|
||
}
|
||
#endregion
|
||
return new ApiResult { code = 0, data = list };
|
||
}
|
||
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)}");
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
}
|