lg_backend/langguanApi/Service/UserService.cs

247 lines
9.6 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.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);
}
if (!string.IsNullOrEmpty(input.Tel))
{
exp = filter => filter.Tel.Contains(input.Tel);
}
if (!string.IsNullOrEmpty(input.HiredateStart))
{
exp = filter => filter.Hiredate.Value >= DateTime.Parse(input.HiredateStart);
}
if (!string.IsNullOrEmpty(input.HiredateEnd))
{
exp = filter => filter.Hiredate.Value <= DateTime.Parse(input.HiredateEnd);
}
#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,
Education = item.Education,
Tel = item.Tel,
Email = item.Email,
roleName = roleList.FirstOrDefault(s => s.Id == item.roleId)?.RoleName,
Sex = item.Sex == 0 ? "男" : "女",
Address = item.Address,
IsAdmin = item.IsAdmin == 0 ? "否" : "是",
Native = item.Native,
University = item.University,
Hiredate = item.Hiredate.HasValue == true ? item.Hiredate.Value.ToString("yyyy-MM-dd") : "",
Brithday = item.Brithday.HasValue == true ? item.Brithday.Value.ToString("yyyy-MM-dd") : "",
});
}
#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
}
}