You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
6.8 KiB
173 lines
6.8 KiB
using System.Data;
|
|
using System.Threading.Tasks;
|
|
using Znyc.Cloudcar.Admin.Commons;
|
|
using Znyc.Cloudcar.Admin.Commons.Dtos;
|
|
using Znyc.Cloudcar.Admin.Commons.Entitys;
|
|
using Znyc.Cloudcar.Admin.Commons.Enums;
|
|
using Znyc.Cloudcar.Admin.Commons.Mapping;
|
|
using Znyc.Cloudcar.Admin.Commons.Pages;
|
|
using Znyc.Cloudcar.Admin.Commons.Services;
|
|
using Znyc.Cloudcar.Admin.Security.Dtos;
|
|
using Znyc.Cloudcar.Admin.Security.Entitys;
|
|
using Znyc.Cloudcar.Admin.Security.IRepositories;
|
|
using Znyc.Cloudcar.Admin.Security.IServices;
|
|
|
|
namespace Znyc.Cloudcar.Admin.Security.Services
|
|
{
|
|
/// <summary>
|
|
/// </summary>
|
|
public class UserService : BaseService<UserEntity, UserOutputDto, long>, IUserService
|
|
{
|
|
private readonly ICertificationRepository _certificationRepository;
|
|
private readonly ILoginLogsRepository _loginLogsRepository;
|
|
private readonly IUserRepository _userRepository;
|
|
|
|
public UserService(IUserRepository repository,
|
|
ILoginLogsRepository loginLogsRepository,
|
|
ICertificationRepository certificationRepository) : base(repository)
|
|
{
|
|
_userRepository = repository;
|
|
_certificationRepository = certificationRepository;
|
|
_loginLogsRepository = loginLogsRepository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步更新实体。
|
|
/// </summary>
|
|
/// <param name="entity">实体</param>
|
|
/// <param name="id">主键ID</param>
|
|
/// <param name="trans">事务对象</param>
|
|
/// <returns></returns>
|
|
public override async Task<bool> UpdateAsync(UserEntity entity, long id, IDbTransaction trans = null)
|
|
{
|
|
bool result = await repository.UpdateAsync(entity, id, trans);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
|
|
/// </summary>
|
|
/// <param name="search">查询的条件</param>
|
|
/// <returns>指定对象的集合</returns>
|
|
public override async Task<PageResult<UserOutputDto>> FindWithPagerAsync(SearchInputDto<UserEntity> search)
|
|
{
|
|
bool order = search.Order == "asc" ? false : true;
|
|
string where = GetDataPrivilege(false);
|
|
if (!string.IsNullOrEmpty(search.Keywords))
|
|
{
|
|
@where += string.Format(" and (UserName like '%{0}%')", search.Keywords);
|
|
};
|
|
PagerInfo pagerInfo = new PagerInfo
|
|
{
|
|
CurrenetPageIndex = search.CurrenetPageIndex,
|
|
PageSize = search.PageSize
|
|
};
|
|
System.Collections.Generic.List<UserEntity> list = await repository.FindWithPagerAsync(where, pagerInfo, search.Sort, order);
|
|
PageResult<UserOutputDto> pageResult = new PageResult<UserOutputDto>
|
|
{
|
|
CurrentPage = pagerInfo.CurrenetPageIndex,
|
|
Items = list.MapTo<UserOutputDto>(),
|
|
ItemsPerPage = pagerInfo.PageSize,
|
|
TotalItems = pagerInfo.RecordCount
|
|
};
|
|
return pageResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步分页查询(根据时间状态关键字)
|
|
/// </summary>
|
|
/// <param name="search"></param>
|
|
/// <returns></returns>
|
|
public async Task<PageResult<UserOutputDto>> FindWithPagerSearchAsync(SearchUserModel search)
|
|
{
|
|
PagerInfo pagerInfo = new PagerInfo
|
|
{
|
|
CurrenetPageIndex = search.CurrenetPageIndex,
|
|
PageSize = search.PageSize
|
|
};
|
|
|
|
System.Collections.Generic.List<UserOutputDto> list = (await _userRepository.FindUserWithPagerAsync(search, pagerInfo)).MapTo<UserOutputDto>();
|
|
System.Collections.Generic.List<LoginLogsEntity> LastLoginLogsList = await _loginLogsRepository.GetLastLoginLogAsync();
|
|
|
|
foreach (UserOutputDto item in list)
|
|
{
|
|
item.PositivePhoto = "";
|
|
item.ReversePhoto = "";
|
|
item.IdCard = "";
|
|
item.IssuedAddress = "";
|
|
item.Gender = "";
|
|
item.CertificationState = (int)UserCertificationEnum.None;
|
|
CertificationEntity certification = await _certificationRepository.GetWhereAsync($"UserId={item.Id}");
|
|
if (certification != null)
|
|
{
|
|
item.CertificationId = certification.Id;
|
|
item.PositivePhoto = string.Format(CommonConst.Default_Identity_Prefix, item.Id) + certification.PositivePhoto;
|
|
item.ReversePhoto = string.Format(CommonConst.Default_Identity_Prefix, item.Id) + certification.ReversePhoto;
|
|
item.IdCard = certification.IdCard;
|
|
item.IssuedAddress = certification.IssuedAddress;
|
|
item.Gender = certification.Gender;
|
|
item.CertificationState = certification.State;
|
|
}
|
|
item.AvatarUrl = item.AvatarUrl;
|
|
item.LastLoginTime = LastLoginLogsList.Find(x => x.CreatedUserId == item.Id)?.LoginTime;
|
|
item.Contact = item.Contact;
|
|
}
|
|
|
|
PageResult<UserOutputDto> pageResult = new PageResult<UserOutputDto>
|
|
{
|
|
CurrentPage = pagerInfo.CurrenetPageIndex,
|
|
Items = list,
|
|
ItemsPerPage = pagerInfo.PageSize,
|
|
TotalItems = pagerInfo.RecordCount
|
|
};
|
|
return pageResult;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异步禁用/启用用户
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="status">1-启用/99-禁用</param>
|
|
/// <returns></returns>
|
|
public async Task<CommonResult> UpdateStatusAsync(long id, int status)
|
|
{
|
|
CommonResult result = new CommonResult();
|
|
UserEntity user = await repository.GetAsync(id);
|
|
if (!(user?.Id > 0))
|
|
{
|
|
result.Success = false;
|
|
result.ErrMsg = "用户不存在";
|
|
return result;
|
|
}
|
|
|
|
user.State = status;
|
|
await repository.UpdateAsync(user, id);
|
|
result.Success = true;
|
|
return result;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 异步禁用/启用用户
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<CommonResult> UpdateIsPromoteAsync(long id)
|
|
{
|
|
CommonResult result = new CommonResult();
|
|
UserEntity user = await repository.GetAsync(id);
|
|
if (!(user?.Id > 0))
|
|
{
|
|
result.Success = false;
|
|
result.ErrMsg = "用户不存在";
|
|
return result;
|
|
}
|
|
|
|
user.IsPromote = !user.IsPromote;
|
|
await repository.UpdateAsync(user, id);
|
|
result.Success = true;
|
|
return result;
|
|
}
|
|
|
|
}
|
|
}
|