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(id); return queryByKey; } } public void Update(UserInfo useInfo) { using (var dbContext = DbFactory.GetContext) { dbContext.Update(useInfo); } } public void UpdateDel(IReadOnlyCollection lsLongs) { using (var dbContext = DbFactory.GetContext) { foreach (var lsLong in lsLongs) { dbContext.Update(a => a.ID == lsLong, a => new UserInfo() { IsDelete = 1 }); } } } public PagedList GetPagedList(int pageIndex, int pageSize, string key) { var roleList = roleService.GetList(); List ls = new List(); PagingResult userInfoPage; using (var dbContext = DbFactory.GetContext) { userInfoPage = dbContext.Query() .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 paged = new PagedList(ls, userInfoPage.Totals.ToInt(),pageIndex,pageSize); return paged; } public PagedList GetPagedList(int pageIndex, int pageSize, string key, List lsLevel) { var roleList = roleService.GetList(); List ls = new List(); PagingResult userInfoPage; using (var dbContext = DbFactory.GetContext) { userInfoPage = dbContext.Query() .LeftJoin(((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 paged = new PagedList(ls, userInfoPage.Totals.ToInt(), pageIndex, pageSize); return paged; } public UserDto GetOne(string name, string pwd) { using (var dbContext = DbFactory.GetContext) { var userInfo= dbContext.Query() .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(). 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(). 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 GetAllName() { using (var dbContext = DbFactory.GetContext) { return dbContext.Query() .Select(x => x.UserName).ToList(); } } public List GetAllNameByRoleId(long rId) { using (var dbContext = DbFactory.GetContext) { return dbContext.Query() .Where(x=>x.RoleId==rId) .Select(x => x.UserName).ToList(); } } } public class RoleService { public RoleService() { } public IReadOnlyCollection GetList() { using (var dbContext = DbFactory.GetContext) { return dbContext.Query().ToList(); } } } }