using System;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Znyc.Admin.Commons.Const;
using Znyc.Admin.Commons.Encrypt;
using Znyc.Admin.Commons.Enums;
using Znyc.Admin.Commons.Mapping;
using Znyc.Admin.Commons.Pages;
using Znyc.Admin.Commons.Services;
using Znyc.Admin.Security.Dtos;
using Znyc.Admin.Security.Entitys;
using Znyc.Admin.Security.IRepositories;
using Znyc.Admin.Security.IServices;
namespace Znyc.Admin.Security.Services
{
///
///
///
public class AdminUserService : BaseService, IAdminUserService
{
private readonly IAdminUserRepository _adminUserRepository;
private readonly IAdminUserLogOnRepository _adminUserLogOnRepository;
private readonly IRoleService _roleService;
private readonly IOrganizeService _organizeService;
///
///
///
///
///
///
///
///
public AdminUserService(IAdminUserRepository repository, IAdminUserLogOnRepository adminUserLogOnRepository,
IRoleService roleService, IOrganizeService organizeService) : base(repository)
{
_adminUserRepository = repository;
_roleService = roleService;
_organizeService = organizeService;
_adminUserLogOnRepository = adminUserLogOnRepository;
}
///
/// 用户登陆验证。
///
/// 用户名
/// 密码(第一次md5加密后)
/// 验证成功返回用户实体,验证失败返回null|提示消息
public async Task> Validate(string userName, string password)
{
AdminUser userEntity = await _adminUserRepository.GetUserByLogin(userName);
if (userEntity == null)
{
return new Tuple(null, ReturnConst.User_Not_Exist);
}
if (userEntity.Status == -1)
{
return new Tuple(null,ReturnConst.User_Disable);
}
AdminUserLogOn userSinginEntity = _adminUserLogOnRepository.GetByUserId(userEntity.Id);
string inputPassword = MD5Util
.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32(password).ToLower(), userSinginEntity.AdminUserSecretkey)
.ToLower()).ToLower();
if (inputPassword != userSinginEntity.AdminUserPassword)
{
return new Tuple(null, ReturnConst.Password_Error);
}
else
{
AdminUserLogOn userLogOn = _adminUserLogOnRepository.GetWhere("AdminUserId='" + userEntity.Id + "'");
// if (userLogOn.AllowEndTime < DateTime.Now)
// return new Tuple(null, "您的账号已过期,请联系系统管理员!");
// if (userLogOn.LockEndDate > DateTime.Now)
// {
// var dateStr = userLogOn.LockEndDate.ToEasyStringDQ();
// return new Tuple(null, "当前被锁定,请" + dateStr + "登录");
// }
// if (userLogOn.FirstVisitTime == null)
// userLogOn.FirstVisitTime = userLogOn.PreviousVisitTime = DateTime.Now;
// else
// userLogOn.PreviousVisitTime = DateTime.Now;
userLogOn.LogOnCount++;
userLogOn.LastVisitTime = DateTime.Now;
userLogOn.UserOnLine = true;
await _adminUserLogOnRepository.UpdateAsync(userLogOn, userLogOn.Id);
return new Tuple(userEntity, "");
}
}
///
/// 用户登陆验证。
///
/// 用户名
/// 密码(第一次md5加密后)
/// 用户类型
/// 验证成功返回用户实体,验证失败返回null|提示消息
public async Task> Validate(string userName, string password, UserType userType)
{
AdminUser userEntity = await _adminUserRepository.GetUserByLogin(userName);
if (userEntity == null)
{
return new Tuple(null, ReturnConst.User_Not_Exist);
}
if (userEntity.Status == -1)
{
return new Tuple(null,ReturnConst.User_Disable);
}
AdminUserLogOn userSinginEntity = _adminUserLogOnRepository.GetByUserId(userEntity.Id);
string inputPassword = MD5Util
.GetMD5_32(DEncrypt.Encrypt(MD5Util.GetMD5_32(password).ToLower(), userSinginEntity.AdminUserSecretkey)
.ToLower()).ToLower();
if (inputPassword != userSinginEntity.AdminUserPassword)
{
return new Tuple(null,ReturnConst.Password_Error);
}
else
{
AdminUserLogOn userLogOn = _adminUserLogOnRepository.GetWhere("UserId='" + userEntity.Id + "'");
//if (userLogOn.AllowEndTime < DateTime.Now)
// return new Tuple(null, "您的账号已过期,请联系系统管理员!");
//if (userLogOn.LockEndDate > DateTime.Now)
//{
// var dateStr = userLogOn.LockEndDate.ToEasyStringDQ();
// return new Tuple(null, "当前被锁定,请" + dateStr + "登录");
//}
//if (userLogOn.FirstVisitTime == null)
// userLogOn.FirstVisitTime = userLogOn.PreviousVisitTime = DateTime.Now;
//else
// userLogOn.PreviousVisitTime = DateTime.Now;
userLogOn.LogOnCount++;
userLogOn.LastVisitTime = DateTime.Now;
userLogOn.UserOnLine = true;
await _adminUserLogOnRepository.UpdateAsync(userLogOn, userLogOn.Id);
return new Tuple(userEntity, "");
}
}
///
/// 根据用户账号查询用户信息
///
///
///
public async Task GetByUserName(string userName)
{
return await _adminUserRepository.GetByUserName(userName);
}
///
/// 根据用户手机号码查询用户信息
///
/// 手机号码
///
public async Task GetUserByMobilePhone(string mobilephone)
{
return await _adminUserRepository.GetUserByMobilePhone(mobilephone);
}
///
/// 根据Account、手机号查询用户信息
///
/// 登录账号
///
public async Task GetUserByLogin(string account)
{
return await _adminUserRepository.GetUserByLogin(account);
}
///
/// 根据第三方OpenId查询用户信息
///
/// 第三方类型
/// OpenId值
///
public AdminUser GetUserByOpenId(string openIdType, string openId)
{
return _adminUserRepository.GetUserByOpenId(openIdType, openId);
}
///
/// 根据userId查询用户信息
///
/// 第三方类型
/// userId
///
public UserOpenIds GetUserOpenIdByuserId(string openIdType, long userId)
{
return _adminUserRepository.GetUserOpenIdByuserId(openIdType, userId);
}
///
/// 更新用户信息,第三方平台
///
///
///
///
///
public bool UpdateUserByOpenId(AdminUser entity, AdminUserLogOn userLogOnEntity, UserOpenIds userOpenIds,
IDbTransaction trans = null)
{
return _adminUserRepository.UpdateUserByOpenId(entity, userLogOnEntity, userOpenIds, trans);
}
///
/// 根据微信UnionId查询用户信息
///
/// UnionId值
///
public AdminUser GetUserByUnionId(string unionId)
{
return _adminUserRepository.GetUserByUnionId(unionId);
}
///
/// 更新用户
///
///
///
//public bool UpdateUserByOpenId(AdminUserInputDto userInPut)
//{
// var user = GetUserByOpenId(userInPut.OpenIdType, userInPut.OpenId);
// user.HeadIcon = userInPut.HeadIcon;
// user.UserName = userInPut.UserName;
// user.Gender = userInPut.Gender;
// return _adminUserRepository.Update(user, user.Id);
//}
///
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
///
/// 查询的条件
/// 指定对象的集合
public async Task> FindWithPagerSearchAsync(SearchUserModel search)
{
bool order = search.Order == "asc" ? false : true;
string where = GetDataPrivilege(false);
if (!string.IsNullOrEmpty(search.Keywords))
{
@where += string.Format(
" and (UserName like '%{0}%' or Account like '%{0}%' or MobilePhone like '%{0}%')",
search.Keywords);
}
if (!string.IsNullOrEmpty(search.StartTime))
{
@where += " and CreatedTime >='" + search.StartTime + " 00:00:00'";
}
if (!string.IsNullOrEmpty(search.EndTime))
{
@where += " and CreatedTime <='" + search.EndTime + " 23:59:59'";
}
PagerInfo pagerInfo = new PagerInfo
{
CurrenetPageIndex = search.CurrenetPageIndex,
PageSize = search.PageSize
};
List list = await repository.FindWithPagerAsync(where, pagerInfo, search.Sort, order);
List resultList = list.MapTo();
List listResult = new List();
foreach (AdminUserOutputDto item in resultList)
{
if (!string.IsNullOrEmpty(item.OrganizeId.ToString()))
{
item.OrganizeName = _organizeService.Get(item.OrganizeId)?.FullName;
}
if (!string.IsNullOrEmpty(item.RoleId.ToString().ToString()))
{
item.RoleName = _roleService.GetRoleNameStr(item.RoleId.ToString());
}
if (!string.IsNullOrEmpty(item.DepartmentId.ToString()))
{
item.DepartmentName = _organizeService.Get(item.DepartmentId).FullName;
}
//if (!string.IsNullOrEmpty(item.DutyId))
//{
// item.DutyName = _roleService.Get(item.DutyId).FullName;
//}
listResult.Add(item);
}
PageResult pageResult = new PageResult
{
CurrentPage = pagerInfo.CurrenetPageIndex,
Items = listResult,
ItemsPerPage = pagerInfo.PageSize,
TotalItems = pagerInfo.RecordCount
};
return pageResult;
}
public bool CreateUserByWxOpenId(UserInputDto userInPut)
{
throw new NotImplementedException();
}
public bool UpdateUserByOpenId(UserInputDto userInPut)
{
throw new NotImplementedException();
}
}
}