using Dapper;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;
using Znyc.Cloudcar.Admin.Commons.IDbContext;
using Znyc.Cloudcar.Admin.Commons.Repositories;
using Znyc.Cloudcar.Admin.Security.Dtos;
using Znyc.Cloudcar.Admin.Security.Entitys;
using Znyc.Cloudcar.Admin.Security.IRepositories;
namespace Znyc.Cloudcar.Admin.Security.Repositories
{
///
/// 应用仓储实现
///
public class AdminUserRepository : BaseRepository, IAdminUserRepository
{
public AdminUserRepository()
{
}
public AdminUserRepository(IDbContextCore context) : base(context)
{
}
///
/// 根据用户账号查询用户信息
///
///
///
public async Task GetByUserName(string userName)
{
string sql = @"SELECT * FROM sys_adminuser t WHERE t.Account = @UserName";
return await DapperConn.QueryFirstOrDefaultAsync(sql, new { UserName = userName });
}
///
/// 根据用户手机号码查询用户信息
///
/// 手机号码
///
public async Task GetUserByMobilePhone(string mobilephone)
{
string sql = @"SELECT * FROM sys_adminuser t WHERE t.MobilePhone = @MobilePhone";
return await DapperConn.QueryFirstOrDefaultAsync(sql, new { MobilePhone = mobilephone });
}
///
/// 根据Email、Account、手机号查询用户信息
///
/// 登录账号
///
public async Task GetUserByLogin(string account)
{
string sql =
@"SELECT * FROM sys_adminuser t WHERE (t.Account = @Account or t.MobilePhone = @Account)";
return await DapperConn.QueryFirstOrDefaultAsync(sql, new { Account = account });
}
///
/// 根据微信UnionId查询用户信息
///
/// UnionId值
///
public AdminUserEntity GetUserByUnionId(string unionId)
{
string sql = string.Format("select * from sys_adminuser where UnionId = '{0}'", unionId);
return DapperConn.QueryFirstOrDefault(sql);
}
///
/// 根据第三方OpenId查询用户信息
///
/// 第三方类型
/// OpenId值
///
public AdminUserEntity GetUserByOpenId(string openIdType, string openId)
{
string sql = string.Format(
"select * from sys_adminuser as u join sys_adminuserOpenIds as o on u.Id = o.UserId and o.OpenIdType = '{0}' and o.OpenId = '{1}'",
openIdType, openId);
return DapperConn.QueryFirstOrDefault(sql);
}
///
/// 根据userId查询用户信息
///
/// 第三方类型
/// userId
///
public UserOpenIdsEntity GetUserOpenIdByuserId(string openIdType, long userId)
{
string sql = string.Format("select * from sys_adminuserOpenIds where OpenIdType = '{0}' and UserId = '{1}'",
openIdType, userId);
return DapperConn.QueryFirstOrDefault(sql);
}
///
/// 更新用户信息,第三方平台
///
///
///
///
public bool UpdateUserByOpenId(AdminUserEntity entity, AdminUserLogOnEntity userLogOnEntity,
UserOpenIdsEntity userOpenIds,
IDbTransaction trans = null)
{
DbContext.GetDbSet().Add(entity);
DbContext.GetDbSet().Add(userOpenIds);
return DbContext.SaveChanges() > 0;
}
///
/// 分页得到所有用户用于关注
///
///
///
///
///
public IEnumerable GetUserAllListFocusByPage(string currentpage,
string pagesize, long userId)
{
string sqlRecord = "";
string sql = "";
long countNotIn = (long.Parse(currentpage) - 1) * long.Parse(pagesize);
sqlRecord = @"select * from sys_adminuser where UserName <> '游客' and ismember=1 ";
sql = @"SELECT TOP " + pagesize +
@"
case when t2.Id is null then 'n'
else 'y' end as IfFocus ,
IsNull(t3.totalFocus,0) as TotalFocus,
t1.*
from
(select ISNULL(tin2.VipGrade,0) as VipGrade,
ISNULL(tin2.IsAuthentication,0) as IsAuthentication,
ISNULL(tin2.AuthenticationType,0) as AuthenticationType,
tin1.* from sys_adminuser tin1
left join sys_adminuserExtend tin2 on tin1.Id=tin2.UserId
where UserName <> '游客' and ismember=1) t1
left join
(select * from sys_adminuserFocus where CreatedUserId='" + userId + @"') t2
on t1.id=t2.focususerid
left join
(select top 100 percent focusUserID,count(*) as totalFocus from sys_adminuserFocus group by focusUserID order by totalfocus desc) t3
on t1.Id=t3.focusUserID
where t1.Id not in
(
select top " + countNotIn + @"
tt1.Id
from
(select ISNULL(tin2.VipGrade,0) as VipGrade,
ISNULL(tin2.IsAuthentication,0) as IsAuthentication,
ISNULL(tin2.AuthenticationType,0) as AuthenticationType,
tin1.* from sys_adminuser tin1
left join sys_adminuserExtend tin2 on tin1.Id=tin2.UserId
where UserName <> '游客' and ismember=1) tt1
left join
(select * from sys_adminuserFocus where CreatedUserId='" + userId + @"') tt2
on tt1.id=tt2.focususerid
left join
(select top 100 percent focusUserID,count(*) as totalFocus from sys_adminuserFocus group by focusUserID order by totalfocus desc) tt3
on tt1.Id=tt3.focusUserID
order by tt3.totalFocus desc
)
order by t3.totalFocus desc";
List list = new List();
IEnumerable infoOutputDto = DapperConn.Query(sql);
//得到总记录数
List recordCountList = DapperConn.Query(sqlRecord).AsList();
list = infoOutputDto.AsList();
for (int i = 0; i < list.Count; i++)
{
list[i].RecordCount = recordCountList.Count;
}
return list;
}
}
}