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

312 lines
9.4 KiB
C#

1 month ago
using Chloe;
1 month ago
1 month ago
using DB.Dto;
1 month ago
using DB.Entity;
1 month ago
using NewLife.Web;
using System;
using System.Collections.Generic;
using System.Linq;
1 month ago
using Tool;
1 month ago
namespace DB.Service
{
public class UserService
{
1 month ago
RoleService roleService=new RoleService();
1 month ago
public UserService()
{
}
1 month ago
public void Add(UserInfo useInfo)
{
using (var dbContext = DbFactory.GetContext)
{
dbContext.Insert(useInfo);
}
}
1 month ago
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
});
}
}
}
1 month ago
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;
}
6 days ago
public PagedList<UserDto> GetPagedList(int pageIndex, int pageSize, string key, List<int> lsLevel,long userId)
1 month ago
{
var roleList = roleService.GetList();
List<UserDto> ls = new List<UserDto>();
6 days ago
PagingResult<UserInfo> userInfoPage=new PagingResult<UserInfo>();
1 month ago
using (var dbContext = DbFactory.GetContext)
{
6 days ago
//操作工
if (lsLevel.Count == 1)
{
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=>x.UserInfo.ID==userId)
.Where(x => x.Role.RoleLevel==1)
.OrderBy(x => x.Role.RoleLevel)
.Select(x => x.UserInfo)
.Paging(pageIndex, pageSize);
}
if (lsLevel.Count == 2)
{
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 =>x.Role.RoleLevel==1)
.OrderBy(x => x.Role.RoleLevel)
.Select(x => x.UserInfo)
.Paging(pageIndex, pageSize);
}
if (lsLevel.Count == 3)
{
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 => (x.Role.RoleLevel == 1) || (x.Role.RoleLevel == 2))
.OrderBy(x => x.Role.RoleLevel)
.Select(x => x.UserInfo)
.Paging(pageIndex, pageSize);
}
1 month ago
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)
1 month ago
{
using (var dbContext = DbFactory.GetContext)
{
1 month ago
var userInfo= dbContext.Query<UserInfo>()
1 month ago
.Where(x => x.UserName == name)
.Where(x => x.Pwd == pwd)
.FirstOrDefault();
1 month ago
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;
}
1 month ago
}
1 month ago
return null;
1 month ago
}
1 month ago
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;
}
1 month ago
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;
}
1 month ago
1 month ago
public List<string> GetAllName()
{
using (var dbContext = DbFactory.GetContext)
{
return dbContext.Query<UserInfo>()
.Select(x => x.UserName).ToList();
}
}
1 month ago
2 weeks ago
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();
}
}
1 month ago
}
public class RoleService
{
public RoleService()
{
}
public IReadOnlyCollection<Role> GetList()
{
using (var dbContext = DbFactory.GetContext)
{
return dbContext.Query<Role>().ToList();
}
}
1 month ago
}
1 month ago
1 month ago
}