lg_backend/langguanApi/Service/UserService.cs

376 lines
15 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}
}