You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CaiQie/DB/Service/UserService.cs

269 lines
7.7 KiB
C#

using Chloe;
using DB.Dto;
using DB.Entity;
using NewLife.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using Tool;
namespace DB.Service
{
public class UserService
{
RoleService roleService=new RoleService();
public UserService()
{
}
public void Add(UserInfo useInfo)
{
using (var dbContext = DbFactory.GetContext)
{
dbContext.Insert(useInfo);
}
}
public UserInfo Query(long id)
{
using (var dbContext = DbFactory.GetContext)
{
var queryByKey = dbContext.QueryByKey<UserInfo>(id);
return queryByKey;
}
}
public void Update(UserInfo useInfo)
{
using (var dbContext = DbFactory.GetContext)
{
dbContext.Update(useInfo);
}
}
public void UpdateDel(IReadOnlyCollection<long> lsLongs)
{
using (var dbContext = DbFactory.GetContext)
{
foreach (var lsLong in lsLongs)
{
dbContext.Update<UserInfo>(a => a.ID == lsLong, a => new UserInfo()
{
IsDelete = 1
});
}
}
}
public PagedList<UserDto> GetPagedList(int pageIndex, int pageSize, string key)
{
var roleList = roleService.GetList();
List<UserDto> ls = new List<UserDto>();
PagingResult<UserInfo> userInfoPage;
using (var dbContext = DbFactory.GetContext)
{
userInfoPage = dbContext.Query<UserInfo>()
.WhereIfNotNull(key, x => x.UserName.Contains(key))
.OrderBy(x=>x.RoleId)
.Paging(pageIndex, pageSize);
foreach (var userInfo in userInfoPage.DataList)
{
var first = roleList.First(x => x.ID == userInfo.RoleId);
UserDto user = new UserDto
{
Id = userInfo.ID,
UserName = userInfo.UserName,
RoleId = first.ID,
RoleName = first.RoleName,
RoleLevel = first.RoleLevel
};
ls.Add(user);
}
}
PagedList<UserDto> paged = new PagedList<UserDto>(ls, userInfoPage.Totals.ToInt(),pageIndex,pageSize);
return paged;
}
public PagedList<UserDto> GetPagedList(int pageIndex, int pageSize, string key, List<int> lsLevel)
{
var roleList = roleService.GetList();
List<UserDto> ls = new List<UserDto>();
PagingResult<UserInfo> userInfoPage;
using (var dbContext = DbFactory.GetContext)
{
userInfoPage = dbContext.Query<UserInfo>()
.LeftJoin<Role>(((userinfo, role) => userinfo.RoleId == role.ID))
.Select((userinfo, role) => new
{
UserInfo = userinfo,
Role = role
}).WhereIf(!string.IsNullOrEmpty(key), x => x.UserInfo.UserName.Contains(key))
.Where(x=>lsLevel.Contains(x.Role.RoleLevel))
.OrderBy(x => x.Role.RoleLevel)
.Select(x => x.UserInfo)
.Paging(pageIndex, pageSize);
foreach (var userInfo in userInfoPage.DataList)
{
var first = roleList.First(x => x.ID == userInfo.RoleId);
UserDto user = new UserDto
{
Id = userInfo.ID,
UserName = userInfo.UserName,
RoleId = first.ID,
RoleName = first.RoleName,
RoleLevel = first.RoleLevel
};
ls.Add(user);
}
}
PagedList<UserDto> paged = new PagedList<UserDto>(ls, userInfoPage.Totals.ToInt(), pageIndex, pageSize);
return paged;
}
public UserDto GetOne(string name, string pwd)
{
using (var dbContext = DbFactory.GetContext)
{
var userInfo= dbContext.Query<UserInfo>()
.Where(x => x.UserName == name)
.Where(x => x.Pwd == pwd)
.FirstOrDefault();
if (userInfo != null)
{
var first = roleService.GetList().First(x => x.ID == userInfo.RoleId);
UserDto user = new UserDto
{
Id = userInfo.ID,
UserName = userInfo.UserName,
RoleId = first.ID,
RoleName = first.RoleName,
RoleLevel = first.RoleLevel
};
return user;
}
}
return null;
}
public UserDto GetOne(string name)
{
using (var dbContext = DbFactory.GetContext)
{
var userInfo = dbContext.Query<UserInfo>().
Where(x => x.UserName == name)
.FirstOrDefault();
if (userInfo != null)
{
var first = roleService.GetList().First(x => x.ID == userInfo.RoleId);
UserDto user = new UserDto
{
Id = userInfo.ID,
UserName = userInfo.UserName,
RoleId = first.ID,
RoleName = first.RoleName,
RoleLevel = first.RoleLevel
};
return user;
}
}
return null;
}
public UserDto GetOne(string name,long id)
{
using (var dbContext = DbFactory.GetContext)
{
var userInfo = dbContext.Query<UserInfo>().
Where(x => x.UserName == name)
.WhereIf(id>0,x=>x.ID!=id)
.FirstOrDefault();
if (userInfo != null)
{
var first = roleService.GetList().First(x => x.ID == userInfo.RoleId);
UserDto user = new UserDto
{
Id = userInfo.ID,
UserName = userInfo.UserName,
RoleId = first.ID,
RoleName = first.RoleName,
RoleLevel = first.RoleLevel
};
return user;
}
}
return null;
}
public List<string> GetAllName()
{
using (var dbContext = DbFactory.GetContext)
{
return dbContext.Query<UserInfo>()
.Select(x => x.UserName).ToList();
}
}
public List<string> GetAllNameByRoleId(long rId)
{
using (var dbContext = DbFactory.GetContext)
{
return dbContext.Query<UserInfo>()
.Where(x=>x.RoleId==rId)
.Select(x => x.UserName).ToList();
}
}
}
public class RoleService
{
public RoleService()
{
}
public IReadOnlyCollection<Role> GetList()
{
using (var dbContext = DbFactory.GetContext)
{
return dbContext.Query<Role>().ToList();
}
}
}
}