123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- 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<UserDto> CreateUser(UserCreateOrUpdateDto dto)
- {
- bool rev = _unitOfWork.UserRepository.Any(x => x.JobNo == dto.JobNo);
- if (rev)
- {
- return ResultDto<UserDto>.Fail("工号已存在!");
- }
- var user = _mapper.Map<User>(dto);
- user.CreateTime = DateTime.Now;
- user.Password = $"{dto.JobNo}&123456".To32MD5();
- _unitOfWork.UserRepository.Create(user);
- _unitOfWork.SaveChanges();
- var userDto = _mapper.Map<UserDto>(user);
- return ResultDto<UserDto>.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<IEnumerable<UserDto>> GetUserList(string? keyword = null)
- {
- var userDtos = new List<UserDto>();
- var users = _unitOfWork.UserRepository.GetList(x => x.Name.Contains(keyword) || x.JobNo.Contains(keyword));
- if (users == null)
- {
- return ResultDto<IEnumerable<UserDto>>.Success(userDtos);
- }
- foreach (var user in users)
- {
- var userDto = _mapper.Map<UserDto>(user);
- var role = _unitOfWork.RoleRepository.GetById(user.RoleId);
- if (role != null)
- {
- userDto.RoleName = role.Name;
- }
- userDtos.Add(userDto);
- }
- return ResultDto<IEnumerable<UserDto>>.Success(userDtos);
- }
- public PageDto<IEnumerable<UserDto>> GetUserPageList(int pageIndex, int pageSize, string? keyword = null)
- {
- var userDtos = new List<UserDto>();
- 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<UserDto>(user);
- var role = _unitOfWork.RoleRepository.GetById(user.RoleId);
- if (role != null)
- {
- userDto.RoleName = role.Name;
- }
- userDtos.Add(userDto);
- }
- }
- return new PageDto<IEnumerable<UserDto>>(total, userDtos);
- }
- public ResultDto<IEnumerable<Role>> GetAllRoles()
- {
- var roles = _unitOfWork.RoleRepository.GetList();
- return ResultDto<IEnumerable<Role>>.Success(roles);
- }
- public PageDto<IEnumerable<Role>> 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<IEnumerable<Role>>(total, roles);
- }
- public ResultDto<UserDto> GetUserById(string id)
- {
- var user = _unitOfWork.UserRepository.GetById(id);
- if (user == null)
- {
- return ResultDto<UserDto>.Fail("资源不存在!");
- }
- var userDto = _mapper.Map<UserDto>(user);
- var role = _unitOfWork.RoleRepository.GetById(user.RoleId);
- if (role != null)
- {
- userDto.RoleName = role.Name;
- }
- return ResultDto<UserDto>.Success(userDto);
- }
- public ResultDto<Role> GetRoleById(string id)
- {
- var role = _unitOfWork.RoleRepository.GetById(id);
- return ResultDto<Role>.Success(role);
- }
- public ResultDto<Role> CreateRole(RoleCreateOrUpdateDto dto)
- {
- bool rev = _unitOfWork.RoleRepository.Any(x => x.Name == dto.Name);
- if (rev)
- {
- return ResultDto<Role>.Fail("角色名称已存在!");
- }
- var role = _mapper.Map<Role>(dto);
- role.CreateTime = DateTime.Now;
- _unitOfWork.RoleRepository.Create(role);
- _unitOfWork.SaveChanges();
- return ResultDto<Role>.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<MenuDto> 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<MenuDto> GetPermissions(string roleId)
- {
- IList<MenuDto> menuDtos = new List<MenuDto>();
- var role = _unitOfWork.RoleRepository.GetById(roleId);
- if (role == null)
- {
- return menuDtos;
- }
- IList<Menu> menus = new List<Menu>();
- 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<RoleMenu>()
- join menu in context.Set<Menu>()
- 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<zNode> GetZNodes()
- {
- IList<zNode> zNodes = new List<zNode>();
- 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;
- }
- }
- }
|