using AutoMapper; using ProductionLineMonitor.Application.Services.AdminService.Dtos; using ProductionLineMonitor.Core.Dtos; using ProductionLineMonitor.Core.IRepositories; using ProductionLineMonitor.Core.Models; using ProductionLineMonitor.Core.Utils; using ProductionLineMonitor.EntityFramework; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using static Microsoft.EntityFrameworkCore.DbLoggerCategory; namespace ProductionLineMonitor.Application.Services.AdminService { public class AdminService : IAdminService { protected readonly IUnitOfWork _unitOfWork; protected readonly IMapper _mapper; public AdminService(IUnitOfWork unitOfWork, IMapper mapper) { _unitOfWork = unitOfWork; _mapper = mapper; } public ResultDto CreateUser(UserCreateOrUpdateDto dto) { bool rev = _unitOfWork.UserRepository.Any(x => x.JobNo == dto.JobNo); if (rev) { return ResultDto.Fail("工号已存在!"); } var user = _mapper.Map(dto); user.CreateTime = DateTime.Now; user.Password = $"{dto.JobNo}&123456".To32MD5(); _unitOfWork.UserRepository.Create(user); _unitOfWork.SaveChanges(); var userDto = _mapper.Map(user); return ResultDto.Success(userDto); } public ResultDto ChangePassword(UserChangePasswordDto dto) { if (!Regex.IsMatch(dto.NewPassword, @"^(?:(?=.*[0-9].*)(?=.*[A-Za-z].*)(?=.*[^0-9A-Za-z].*)).{3,}")) { return ResultDto.Fail("密码请使用数字、字母、符号组成!"); } if (dto.NewPassword != dto.AgainNewPassword) { return ResultDto.Fail("两次输入新密码不一致!"); } var user = _unitOfWork.UserRepository.FirstOrDefault(x => x.JobNo == dto.JobNo); if (user == null) { return ResultDto.Fail("用户不存在!"); } string passwordMD5 = $"{dto.JobNo}&{dto.Password}".To32MD5(); if (user.Password != passwordMD5) { return ResultDto.Fail("原密码不正确!"); } user.Password = $"{dto.JobNo}&{dto.NewPassword}".To32MD5(); user.UpdateTime = DateTime.Now; _unitOfWork.UserRepository.Update(user); _unitOfWork.SaveChanges(); return ResultDto.Success(); } public UserLoginResultInfoDto? Login(string jobNo, string password) { var user = _unitOfWork.UserRepository.FirstOrDefault( x => x.JobNo == jobNo && x.Password == $"{jobNo}&{password}".To32MD5()); if (user == null) return null; UserLoginResultInfoDto userLoginResultInfo = new UserLoginResultInfoDto { JobNo = user.JobNo, Name = user.Name, RoleId = user.RoleId, CreateTime = user.CreateTime }; var role = _unitOfWork.RoleRepository.GetById(user.RoleId); if (role != null) { userLoginResultInfo.RoleName = role.Name; userLoginResultInfo.MenuDtos = GetPermissions(role.Id); } return userLoginResultInfo; } public ResultDto UpdateUser(string id, UserCreateOrUpdateDto dto) { var user = _unitOfWork.UserRepository.GetById(id); if (user == null) { return ResultDto.Fail("用户不存在!"); } _mapper.Map(dto, user); _unitOfWork.UserRepository.Update(user); _unitOfWork.SaveChanges(); return ResultDto.Success(); } public ResultDto DeleteUser(string id) { var user = _unitOfWork.UserRepository.GetById(id); if (user == null) { return ResultDto.Fail("用户不存在!"); } _unitOfWork.UserRepository.Delete(user); _unitOfWork.SaveChanges(); return ResultDto.Success(); } public ResultDto> GetUserList(string? keyword = null) { var userDtos = new List(); var users = _unitOfWork.UserRepository.GetList(x => x.Name.Contains(keyword) || x.JobNo.Contains(keyword)); if (users == null) { return ResultDto>.Success(userDtos); } foreach (var user in users) { var userDto = _mapper.Map(user); var role = _unitOfWork.RoleRepository.GetById(user.RoleId); if (role != null) { userDto.RoleName = role.Name; } userDtos.Add(userDto); } return ResultDto>.Success(userDtos); } public PageDto> GetUserPageList(int pageIndex, int pageSize, string? keyword = null) { var userDtos = new List(); var users = _unitOfWork.UserRepository.GetPageList( out int total, pageIndex, pageSize, o => o.OrderByDescending(o => o.CreateTime), x => x.Name.Contains(keyword) || x.JobNo.Contains(keyword)); if (users != null) { foreach (var user in users) { var userDto = _mapper.Map(user); var role = _unitOfWork.RoleRepository.GetById(user.RoleId); if (role != null) { userDto.RoleName = role.Name; } userDtos.Add(userDto); } } return new PageDto>(total, userDtos); } public ResultDto> GetAllRoles() { var roles = _unitOfWork.RoleRepository.GetList(); return ResultDto>.Success(roles); } public PageDto> GetRolePageList(int pageIndex, int pageSize, string? keyword = null) { var roles = _unitOfWork.RoleRepository.GetPageList(out int total, pageIndex, pageSize, o => o.OrderByDescending(o => o.CreateTime), x => x.Name.Contains(keyword)); return new PageDto>(total, roles); } public ResultDto GetUserById(string id) { var user = _unitOfWork.UserRepository.GetById(id); if (user == null) { return ResultDto.Fail("资源不存在!"); } var userDto = _mapper.Map(user); var role = _unitOfWork.RoleRepository.GetById(user.RoleId); if (role != null) { userDto.RoleName = role.Name; } return ResultDto.Success(userDto); } public ResultDto GetRoleById(string id) { var role = _unitOfWork.RoleRepository.GetById(id); return ResultDto.Success(role); } public ResultDto CreateRole(RoleCreateOrUpdateDto dto) { bool rev = _unitOfWork.RoleRepository.Any(x => x.Name == dto.Name); if (rev) { return ResultDto.Fail("角色名称已存在!"); } var role = _mapper.Map(dto); role.CreateTime = DateTime.Now; _unitOfWork.RoleRepository.Create(role); _unitOfWork.SaveChanges(); return ResultDto.Success(role); } public ResultDto UpdateRole(string id, RoleCreateOrUpdateDto dto) { var role = _unitOfWork.RoleRepository.GetById(id); if (role == null) { return ResultDto.Fail("角色不存在!"); } _mapper.Map(dto, role); role.UpdateTime = DateTime.Now; _unitOfWork.RoleRepository.Update(role); _unitOfWork.SaveChanges(); return ResultDto.Success(); } public ResultDto DeleteRole(string id) { var role = _unitOfWork.RoleRepository.GetById(id); if (role == null) { return ResultDto.Fail("角色不存在!"); } bool isUse = _unitOfWork.UserRepository.Any(x => x.RoleId == id); if (isUse) { return ResultDto.Fail("已有用户使用该角色,请先解除用户与角色绑定!"); } _unitOfWork.RoleRepository.Delete(role); _unitOfWork.SaveChanges(); return ResultDto.Success(); } public TreeDto GetMenus() { TreeDto treeDto = new TreeDto(); var menus = _unitOfWork.MenuRepository.GetList().OrderBy(o => o.Id); foreach (var menu in menus) { treeDto.Nodes.Add(new Node() { Id = menu.Id, Name = menu.Name }); } return treeDto; } public virtual ResultDto SetPermissions(string roleId, IEnumerable menuDtos) { var role = _unitOfWork.RoleRepository.GetById(roleId); if (role == null) { return ResultDto.Fail("角色不存在!"); } var roleMenus = _unitOfWork.RoleMenuRepository.GetList(x => x.RoleId == roleId); _unitOfWork.RoleMenuRepository.Delete(roleMenus); foreach (var menu in menuDtos) { _unitOfWork.RoleMenuRepository.Create(new RoleMenu() { RoleId = roleId, MenuId = menu.Id }); } _unitOfWork.SaveChanges(); return ResultDto.Success(); } public IList GetPermissions(string roleId) { IList menuDtos = new List(); var role = _unitOfWork.RoleRepository.GetById(roleId); if (role == null) { return menuDtos; } IList menus = new List(); if (role.Name == "管理员") { menus = _unitOfWork.MenuRepository.GetList().OrderBy(o => o.Id).ToList(); } else { var context = _unitOfWork.GetDbContext() as ProductionLineContext; if (context == null) { return menuDtos; } var query = from roleMenu in context.Set() join menu in context.Set() on roleMenu.MenuId equals menu.Id where roleMenu.RoleId == roleId orderby menu.Id select new { roleMenu, menu }; foreach (var item in query) { menus.Add(item.menu); } } foreach (var menu in menus) { if (menu.Level == 1) { menuDtos.Add(new MenuDto() { Id = menu.Id, Name = menu.Name, Icon = menu.Icon, Url = menu.Url, }); } } foreach (var menuDto in menuDtos) { var temps = menus.Where(x => x.FatherId == menuDto.Id).OrderBy(o => o.Id); foreach (var item in temps) { menuDto.Items.Add(new MenuDto { Icon = item.Icon, Url = item.Url, Name = item.Name, Id = item.Id }); } } return menuDtos; } public bool CheckIsInitialPassword(string jobNo) { var user = _unitOfWork.UserRepository.FirstOrDefault(x => x.JobNo == jobNo); if (user != null) { if (user.Password == $"{jobNo}&{123456}".To32MD5()) { return true; } else { return false; } } else { return false; } } public IEnumerable GetZNodes() { IList zNodes = new List(); var menus = _unitOfWork.MenuRepository.GetList().OrderBy(o => o.Id); foreach (var menu in menus) { zNode zNode = new zNode { Id = menu.Id, Name = menu.Name, PId = menu.FatherId ?? "0" }; int count = menus.Where(x => x.FatherId == menu.Id).Count(); if (count > 0) zNode.Open = true; else zNode.Open = false; zNodes.Add(zNode); } return zNodes; } public UserLoginResultInfoDto? Login(string jobNo) { var user = _unitOfWork.UserRepository.FirstOrDefault( x => x.JobNo == jobNo); if (user == null) return null; UserLoginResultInfoDto userLoginResultInfo = new UserLoginResultInfoDto { JobNo = user.JobNo, Name = user.Name, RoleId = user.RoleId, CreateTime = user.CreateTime }; var role = _unitOfWork.RoleRepository.GetById(user.RoleId); if (role != null) { userLoginResultInfo.RoleName = role.Name; userLoginResultInfo.MenuDtos = GetPermissions(role.Id); } return userLoginResultInfo; } } }