470 lines
18 KiB
C#
470 lines
18 KiB
C#
using System.Security.Claims;
|
|
using AdminService.System.Param;
|
|
using AdminService.System.Response;
|
|
using AutoMapper;
|
|
using Common;
|
|
using Common.Utils;
|
|
using CommonInfrastructure.Response;
|
|
using Domain.Admin.Admin;
|
|
using Domain.Admin.Admin.ENum;
|
|
using JWTHelper;
|
|
using Microsoft.Extensions.Logging;
|
|
using MongoDB.Driver;
|
|
using MongoRepository;
|
|
using RedisHelper;
|
|
|
|
namespace AdminService.System
|
|
{
|
|
public class SystemService: ISystemService
|
|
{
|
|
private readonly IMongoBaseRepository<SystemUser> _systemUserRepository;
|
|
private readonly IMongoBaseRepository<SystemUserRole> _systemRoleRepository;
|
|
private readonly IMongoBaseRepository<SystemNavigation> _navigationRepository;
|
|
private readonly IMongoBaseRepository<SystemDic> _dicRepository;
|
|
private readonly ICache _cache;
|
|
private readonly IMapper _mapper;
|
|
private readonly JwtTools _jwtTools;
|
|
|
|
public SystemService(IMongoBaseRepository<SystemUser> systemUserRepository, IMongoBaseRepository<SystemUserRole> systemRoleRepository, IMongoBaseRepository<SystemNavigation> navigationRepository, IMongoBaseRepository<SystemDic> dicRepository, ICache cache, IMapper mapper, JwtTools jwtTools)
|
|
{
|
|
_systemUserRepository = systemUserRepository;
|
|
_systemRoleRepository = systemRoleRepository;
|
|
_navigationRepository = navigationRepository;
|
|
_dicRepository = dicRepository;
|
|
_cache = cache;
|
|
_mapper = mapper;
|
|
_jwtTools = jwtTools;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<(bool isok, string msg)> AddUser(AddUserParam param)
|
|
{
|
|
var model = await _systemUserRepository.FindOneAsync(c => (c.UserName == param.UserName || c.DisplayName == param.DisplayName) && c.IsDelete==false);
|
|
if (model != null)
|
|
return (false, "用户名已存在");
|
|
|
|
model = _mapper.Map<SystemUser>(param);
|
|
model.Init();
|
|
model.Password = Md5Helper.GetMD5(model.Password);
|
|
|
|
return await _systemUserRepository.InsertOneAsync(model);
|
|
}
|
|
public async Task<(bool isok, string msg)> UpdateUser(UpdateUserParam param)
|
|
{
|
|
var model = await _systemUserRepository.FindOneAsync(c => (c.UserName == param.UserName || c.DisplayName == param.DisplayName) && c.Id!= param.Id && c.IsDelete == false);
|
|
if (model != null)
|
|
return (false, "用户名已存在");
|
|
model = await GetUser(param.Id);
|
|
if (model == null)
|
|
return (false, "要修改的用户不存在");
|
|
model.UserName = param.UserName;
|
|
model.DisplayName = param.DisplayName;
|
|
model.SystemRole = param.SystemRole;
|
|
if (!string.IsNullOrWhiteSpace(param.Icon))
|
|
model.Icon = param.Icon;
|
|
|
|
if (!string.IsNullOrWhiteSpace(param.Password) && model.Password != param.Password)
|
|
model.Password = Md5Helper.GetMD5(param.Password);
|
|
|
|
return await _systemUserRepository.ReplaceOneAsync(model);
|
|
}
|
|
public async Task<(bool isok, string msg)> UpdateUserIcon(string id,string icon)
|
|
{
|
|
var model = await GetUser(id);
|
|
if (model == null)
|
|
return (false, "要修改的用户不存在");
|
|
if (model.Icon == icon)
|
|
return (true, "修改成功");
|
|
model.Icon = icon;
|
|
return await _systemUserRepository.ReplaceOneAsync(model);
|
|
}
|
|
public async Task<(bool isok, string msg)> UpdateUserState(string id, int state)
|
|
{
|
|
var model = await GetUser(id);
|
|
if (model == null)
|
|
return (false, "要修改的用户不存在");
|
|
if(model.State==state)
|
|
return (true, "修改成功");
|
|
model.State= state;
|
|
return await _systemUserRepository.ReplaceOneAsync(model);
|
|
}
|
|
|
|
|
|
public async Task<SystemUser> GetUser(string id)
|
|
{
|
|
var model = await _systemUserRepository.FindByIdAsync(id);
|
|
if (model == null)
|
|
return null;
|
|
if (model.IsDelete)
|
|
return null;
|
|
return model;
|
|
}
|
|
|
|
|
|
public async Task<SystemUser> GetUserByName(string userName)
|
|
{
|
|
var model = await _systemUserRepository.FindOneAsync(c=>c.UserName==userName && c.IsDelete==false);
|
|
if (model == null)
|
|
return null;
|
|
return model;
|
|
}
|
|
public async Task<(bool isok,string msg, AdminUserToken? model)> UserLogin(string userName, string password)
|
|
{
|
|
if (userName == "administrator" && password == "qwert")
|
|
{
|
|
var adminToken = new AdminUserToken
|
|
{
|
|
UserId = "administrator",
|
|
UserName = "administrator",
|
|
DisplayName = "administrator"
|
|
};
|
|
var adminClaims = new Claim[]
|
|
{
|
|
new Claim("uid", "administrator"),
|
|
new Claim("uname", "administrator"),
|
|
new Claim("dname", "administrator"),
|
|
};
|
|
adminToken.Token = _jwtTools.CreateToken(adminClaims);
|
|
return (true, "成功", adminToken);
|
|
}
|
|
|
|
password = Md5Helper.GetMD5(password);
|
|
var model = await _systemUserRepository.FindOneAsync(c => c.UserName == userName && c.Password==password && c.IsDelete == false);
|
|
if (model == null)
|
|
return (false,"用户不存在",null);
|
|
if(model.State!=1)
|
|
return (false, "用户已被禁用", null);
|
|
var res = _mapper.Map<AdminUserToken>(model);
|
|
res.UserId = model.Id;
|
|
var claims = new Claim[]
|
|
{
|
|
new Claim("uid", model.Id),
|
|
new Claim("uname", model.UserName),
|
|
new Claim("dname", model.DisplayName),
|
|
};
|
|
res.Token = _jwtTools.CreateToken(claims);
|
|
return (true,"成功",res);
|
|
}
|
|
public async Task<(List<SystemUser> list, long count)> GetUserList(GetUserListParam param)
|
|
{
|
|
var where = LinqExtensions.True<SystemUser>().And(e => e.IsDelete == false)
|
|
.And(e => e.UserName != "administrator");
|
|
if (!string.IsNullOrWhiteSpace(param.KeyWord))
|
|
where = where.And(e => e.UserName.Contains(param.KeyWord) || e.DisplayName.Contains(param.KeyWord));
|
|
|
|
if (param.State > -1)
|
|
{
|
|
where = where.And(e => e.State == param.State);
|
|
}
|
|
|
|
var result = await _systemUserRepository.QueryPageAsync(where,c=>c.RegisterTime,true, param.PageSize??0, param.PageIndex??0);
|
|
|
|
return (result.list,result.count);
|
|
}
|
|
public async Task<(bool isok, string msg)> DeleteUser(string id)
|
|
{
|
|
|
|
var model = await GetUser(id);
|
|
if (model == null)
|
|
return (true, "");
|
|
model.IsDelete = true;
|
|
return await _systemUserRepository.ReplaceOneAsync(model);
|
|
}
|
|
|
|
public async Task<List<SystemNavigationResponse>> GetUserNav(LoginUser loginUser)
|
|
{
|
|
if (string.IsNullOrEmpty(loginUser?.UserId))
|
|
return null;
|
|
|
|
if (loginUser.UserId == "administrator")
|
|
{
|
|
var allNavList = await _navigationRepository.FilterByAsync(c => c.IsDelete == false);
|
|
if (!allNavList.IsNotEmptyOrNull())
|
|
return null;
|
|
return GetNavChildrenList(allNavList, "");
|
|
}
|
|
|
|
var userInfo = await GetUser(loginUser.UserId);
|
|
if (userInfo == null)
|
|
return null;
|
|
|
|
if (userInfo.SystemRole == null || string.IsNullOrEmpty(userInfo.SystemRole.Id))
|
|
return null;
|
|
|
|
var role = await _systemRoleRepository.FindOneAsync(c => c.Id == userInfo.SystemRole.Id && c.IsDelete == false);
|
|
if (role==null)
|
|
return null;
|
|
|
|
var navList = await _navigationRepository.FilterByAsync(c => c.IsDelete == false);
|
|
if (!navList.IsNotEmptyOrNull())
|
|
return null;
|
|
|
|
var res= GetUserNavList(navList,"", role.NavIds);
|
|
if(!res.isCheck)
|
|
return null;
|
|
return (res.list);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<(bool isok, string msg)> AddRole(AddRoleParam param)
|
|
{
|
|
var model = await _systemRoleRepository.FindOneAsync(c => c.RoleName == param.RoleName && c.IsDelete == false);
|
|
if (model != null)
|
|
return (false, "角色名已存在");
|
|
|
|
model = _mapper.Map<SystemUserRole>(param);
|
|
model.Init();
|
|
return await _systemRoleRepository.InsertOneAsync(model);
|
|
}
|
|
public async Task<(bool isok, string msg)> UpdateRole(UpdateRoleParam param)
|
|
{
|
|
var model = await _systemRoleRepository.FindOneAsync(c => c.RoleName == param.RoleName && c.Id != param.Id && c.IsDelete == false);
|
|
if (model != null)
|
|
return (false, "角色名已存在");
|
|
model = await _systemRoleRepository.FindByIdAsync(param.Id);
|
|
if (model == null)
|
|
return (false, "要修改的角色不存在");
|
|
model.RoleName = param.RoleName;
|
|
model.Description = param.Description;
|
|
return await _systemRoleRepository.ReplaceOneAsync(model);
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> UpdateRoleNav(UpdateRoleNavParam param)
|
|
{
|
|
|
|
var model = await _systemRoleRepository.FindByIdAsync(param.Id);
|
|
if (model == null)
|
|
return (false, "要修改的角色不存在");
|
|
if (model.IsDelete)
|
|
return (false, "要修改的角色不存在");
|
|
model.NavIds = param.NavIds;
|
|
return await _systemRoleRepository.ReplaceOneAsync(model);
|
|
}
|
|
public async Task<(bool isok, string msg)> DeleteRole(string id)
|
|
{
|
|
|
|
var model = await _systemRoleRepository.FindByIdAsync(id);
|
|
if (model == null)
|
|
return (true, "");
|
|
if (model.IsDelete)
|
|
return (true, "");
|
|
model.IsDelete=true;
|
|
return await _systemRoleRepository.ReplaceOneAsync(model);
|
|
}
|
|
|
|
public async Task<List<SystemUserRole>> GetRoleList()
|
|
{
|
|
var result = await _systemRoleRepository.FilterByAsync(c=>c.IsDelete==false);
|
|
|
|
return result;
|
|
}
|
|
|
|
public async Task<SystemUserRole> GetUserRole(string id)
|
|
{
|
|
var model = await _systemRoleRepository.FindByIdAsync(id);
|
|
if (model == null)
|
|
return null;
|
|
if (model.IsDelete)
|
|
return null;
|
|
return model;
|
|
}
|
|
|
|
|
|
public async Task<(bool isok, string msg)> AddNav(AddNavParam param)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(param.ParentId))
|
|
{
|
|
var parentModel = await _navigationRepository.FindByIdAsync(param.ParentId);
|
|
if (parentModel == null)
|
|
return (false, "未获取到父级对象");
|
|
}
|
|
var model = _mapper.Map<SystemNavigation>(param);
|
|
model.Init();
|
|
return await _navigationRepository.InsertOneAsync(model);
|
|
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> UpdateNav(UpdateNavParam param)
|
|
{
|
|
|
|
var model = await _navigationRepository.FindByIdAsync(param.Id);
|
|
if (model == null)
|
|
return (false, "要修改的菜单不存在");
|
|
if (model.IsDelete)
|
|
return (false, "要修改的菜单不存在");
|
|
model.ParentId = param.ParentId;
|
|
model.Name = param.Name;
|
|
model.Order = param.Order;
|
|
model.Icon = param.Icon;
|
|
model.Url = param.Url;
|
|
model.Category = (EnumNavigationCategory)param.Category;
|
|
model.Target = param.Target;
|
|
model.IsResource = (EnumResource)param.IsResource;
|
|
return await _navigationRepository.ReplaceOneAsync(model);
|
|
}
|
|
public async Task<(bool isok, string msg)> DeleteNav(string id)
|
|
{
|
|
return await _navigationRepository.DeleteByIdAsync(id);
|
|
}
|
|
|
|
public async Task<List<SystemNavigationResponse>> GetNavList(string navId)
|
|
{
|
|
var navList = await _navigationRepository.FilterByAsync(c => c.IsDelete == false);
|
|
|
|
if (!navList.IsNotEmptyOrNull())
|
|
return null;
|
|
List<SystemNavigationResponse> result = new List<SystemNavigationResponse>();
|
|
|
|
return GetNavChildrenList(navList, navId);
|
|
}
|
|
|
|
|
|
|
|
private List<SystemNavigationResponse> GetNavChildrenList(List<SystemNavigation> navList, string id)
|
|
{
|
|
List<SystemNavigationResponse> result = new List<SystemNavigationResponse>();
|
|
|
|
var list = navList.Where(c => c.ParentId == id).OrderBy(c=>c.Order);
|
|
if (!list.IsNotEmptyOrNull())
|
|
return result;
|
|
foreach (var item in list)
|
|
{
|
|
var model = _mapper.Map<SystemNavigationResponse>(item);
|
|
model.Children = GetNavChildrenList(navList, item.Id);
|
|
|
|
result.Add(model);
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
private (List<SystemNavigationResponse> list, bool isCheck) GetUserNavList(List<SystemNavigation> navList, string id, List<string> navIds)
|
|
{
|
|
List<SystemNavigationResponse> result = new List<SystemNavigationResponse>();
|
|
|
|
var list = navList.Where(c => c.ParentId == id && c.IsResource == 0).OrderBy(c => c.Order);
|
|
if (!list.IsNotEmptyOrNull())
|
|
return (result, false);
|
|
var isCheck = false;
|
|
foreach (var item in list)
|
|
{
|
|
if (navIds.Contains(item.Id))
|
|
isCheck = true;
|
|
var model = _mapper.Map<SystemNavigationResponse>(item);
|
|
var res = GetUserNavList(navList, item.Id, navIds);
|
|
model.Children = res.list;
|
|
if (res.isCheck)
|
|
isCheck = true;
|
|
if(isCheck)
|
|
result.Add(model);
|
|
}
|
|
return (result,isCheck);
|
|
|
|
}
|
|
|
|
|
|
public async Task<(bool isok, string msg)> AddDic(AddDicParam param)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(param.ParentId))
|
|
{
|
|
var parentModel = await _dicRepository.FindByIdAsync(param.ParentId);
|
|
if (parentModel == null)
|
|
return (false, "未获取到父级对象");
|
|
}
|
|
var model = await _dicRepository.FindOneAsync(c => c.Code == param.Code);
|
|
if (model != null)
|
|
return (false, "Code已存在");
|
|
model = _mapper.Map<SystemDic>(param);
|
|
model.Init();
|
|
return await _dicRepository.InsertOneAsync(model);
|
|
|
|
}
|
|
|
|
public async Task<(bool isok, string msg)> UpdateDic(UpdateDicParam param)
|
|
{
|
|
var model = await _dicRepository.FindOneAsync(c => c.Code == param.Code && c.Id!=param.Id);
|
|
if (model != null)
|
|
return (false, "Code已存在");
|
|
|
|
model = await _dicRepository.FindByIdAsync(param.Id);
|
|
if (model == null)
|
|
return (false, "要修改的字典不存在");
|
|
if (model.IsDelete)
|
|
return (false, "要修改的字典不存在");
|
|
model.ParentId = param.ParentId;
|
|
model.Name = param.Name;
|
|
model.Order = param.Order;
|
|
model.Icon = param.Icon;
|
|
model.Value = param.Value;
|
|
model.Code = param.Code;
|
|
model.Remark = param.Remark;
|
|
model.Remark1 = param.Remark1;
|
|
return await _dicRepository.ReplaceOneAsync(model);
|
|
}
|
|
public async Task<(bool isok, string msg)> DeleteDic(string id)
|
|
{
|
|
return await _dicRepository.DeleteByIdAsync(id);
|
|
}
|
|
|
|
public async Task<List<SystemDicResponse>> GetDicList(string dicId)
|
|
{
|
|
var dicList = await _dicRepository.FilterByAsync(c => c.IsDelete == false);
|
|
|
|
if (!dicList.IsNotEmptyOrNull())
|
|
return null;
|
|
List<SystemDicResponse> result = new List<SystemDicResponse>();
|
|
|
|
return GetDicChildrenList(dicList, dicId);
|
|
}
|
|
|
|
public async Task<List<SystemDicResponse>> GetDicListByCode(string code)
|
|
{
|
|
var dicList = await _dicRepository.FilterByAsync(c => c.IsDelete == false);
|
|
|
|
if (!dicList.IsNotEmptyOrNull())
|
|
return null;
|
|
List<SystemDicResponse> result = new List<SystemDicResponse>();
|
|
var model = dicList.FirstOrDefault(c => c.Code == code);
|
|
if (model == null)
|
|
return null;
|
|
return GetDicChildrenList(dicList, model.Id);
|
|
}
|
|
|
|
public async Task<SystemDic> GetDictByCode(string code)
|
|
{
|
|
var model = await _dicRepository.FindOneAsync(c => c.IsDelete == false&&c.Code==code);
|
|
return model;
|
|
}
|
|
|
|
|
|
private List<SystemDicResponse> GetDicChildrenList(List<SystemDic> navList, string id)
|
|
{
|
|
List<SystemDicResponse> result = new List<SystemDicResponse>();
|
|
|
|
var list = navList.Where(c => c.ParentId == id).OrderBy(c => c.Order);
|
|
if (!list.IsNotEmptyOrNull())
|
|
return result;
|
|
foreach (var item in list)
|
|
{
|
|
var model = _mapper.Map<SystemDicResponse>(item);
|
|
model.Children = GetDicChildrenList(navList, item.Id);
|
|
|
|
result.Add(model);
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|