| | |
| | | package com.sandu.ximon.admin.service; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.sandu.common.enums.RoleLevelStatus; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.object.BaseConditionVO; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | import com.sandu.common.util.SpringContextHolder; |
| | | import com.sandu.ximon.admin.param.AdminParam; |
| | | import com.sandu.ximon.admin.param.PwdParam; |
| | | import com.sandu.ximon.admin.param.UserPwsParm; |
| | | import com.sandu.ximon.admin.security.SecurityUtils; |
| | | import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils; |
| | | import com.sandu.ximon.dao.bo.AdminBo; |
| | | import com.sandu.ximon.dao.bo.MenuNode; |
| | | import com.sandu.ximon.dao.domain.Admin; |
| | | import com.sandu.ximon.dao.domain.AdminRoleRelation; |
| | | import com.sandu.ximon.dao.domain.Client; |
| | | import com.sandu.ximon.dao.domain.Role; |
| | | import com.sandu.ximon.dao.domain.RoleMenuRelation; |
| | | import com.sandu.ximon.dao.enums.AdministratorEnums; |
| | | import com.sandu.ximon.dao.enums.OrderByEnums; |
| | | import com.sandu.ximon.dao.mapper.AdminMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.security.crypto.password.PasswordEncoder; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | |
| | | if (!adminRoleRelationService.save(adminRoleRelation)) { |
| | | throw new BusinessException("添加管理员角色失败"); |
| | | } |
| | | |
| | | /** |
| | | * 添加管理员日志记录开始 |
| | | */ |
| | | String content = "新注册管理员:" + param.getUsername(); |
| | | StoreOperationRecordsUtils.storeOperationData(null, null, "添加管理员", content); |
| | | /** |
| | | * 添加管理员日志记录结束 |
| | | */ |
| | | return true; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 修改密码 |
| | | * 修改当前登录用户密码 |
| | | * |
| | | * @param param |
| | | * @return |
| | | */ |
| | |
| | | if (admin == null) { |
| | | throw new BusinessException("用户不存在"); |
| | | } |
| | | admin.setId(userId); |
| | | //判断旧密码与数据库是否一致 |
| | | if (passwordEncoder.matches(param.getOldPass(), admin.getPassword())) { |
| | | //加密新密码 |
| | | admin.setId(userId); |
| | | admin.setPassword(passwordEncoder.encode(param.getNewPass())); |
| | | return updateById(admin); |
| | | } else { |
| | | throw new BusinessException("旧密码不正确,请重新确认密码!"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 修改管理员的密码 |
| | | * |
| | | * @param param |
| | | * @return |
| | | */ |
| | | public boolean updateAdminPassword(UserPwsParm param) { |
| | | Admin admin = getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getId, param.getUserid())); |
| | | if (admin == null) { |
| | | throw new BusinessException("用户不存在"); |
| | | } |
| | | |
| | | //加密新密码 |
| | | admin.setPassword(passwordEncoder.encode(param.getNewPass())); |
| | | return updateById(admin); |
| | | } |
| | |
| | | return updateById(one); |
| | | } |
| | | |
| | | public List<AdminBo> listAdmin(BaseConditionVO baseConditionVO, String keyword) { |
| | | public List<AdminBo> listAdmin(BaseConditionVO baseConditionVO, String keyword, Integer order, Integer seq) { |
| | | //排序字段 |
| | | String orderByResult = OrderByEnums.ADMIN_ID.getCode(); |
| | | //正序、倒叙 |
| | | String orderBySeq = OrderByEnums.ASC.getCode(); |
| | | if (order != null) { |
| | | switch (order) { |
| | | case 1: |
| | | orderByResult = OrderByEnums.ADMIN_ID.getCode(); |
| | | break; |
| | | case 2: |
| | | orderByResult = OrderByEnums.ADMIN_CREATE_TIME.getCode(); |
| | | break; |
| | | case 3: |
| | | orderByResult = OrderByEnums.ADMIN_LOGIN_TIME.getCode(); |
| | | break; |
| | | default: |
| | | } |
| | | } |
| | | if (seq != null) { |
| | | switch (seq) { |
| | | case 1: |
| | | orderBySeq = OrderByEnums.ASC.getCode(); |
| | | break; |
| | | case 2: |
| | | orderBySeq = OrderByEnums.DESC.getCode(); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | //排序方式 |
| | | String orderBy = orderByResult + " " + orderBySeq; |
| | | |
| | | if (baseConditionVO != null) { |
| | | PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize()); |
| | | PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize(), orderBy); |
| | | } |
| | | |
| | | return adminMapper.listAdmin(keyword); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户权限列表地 |
| | | */ |
| | | public List<MenuNode> getUserPermissionList() { |
| | | List<MenuNode> resultList; |
| | | //判断超管与普通管理员 |
| | | if (SecurityUtils.getAdministratorIdentity().equals(AdministratorEnums.ADMIN.getCode())) { |
| | | //参数为null时,获取所有的菜单权限 |
| | | resultList = SpringContextHolder.getBean(MenuService.class).getUserPermissionListById(null); |
| | | } else { |
| | | //通过用户UserID获取用户角色 |
| | | AdminRoleRelation one = SpringContextHolder.getBean(AdminRoleRelationService.class). |
| | | getOne(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, SecurityUtils.getUserId())); |
| | | //判空 |
| | | if (one == null) { |
| | | throw new BusinessException("该用户未绑定角色"); |
| | | } |
| | | |
| | | //通过RoleID获取MeunId列表 |
| | | List<RoleMenuRelation> menuIdList = SpringContextHolder.getBean(RoleMenuRelationService.class) |
| | | .list(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId, one.getRoleId())); |
| | | |
| | | //判空 |
| | | if (menuIdList.isEmpty()) { |
| | | return new ArrayList<>(); |
| | | } |
| | | List<Long> menuIds = new ArrayList<>(menuIdList.size()); |
| | | for (RoleMenuRelation bean : menuIdList) { |
| | | menuIds.add(bean.getMenuId()); |
| | | } |
| | | |
| | | resultList = SpringContextHolder.getBean(MenuService.class).getUserPermissionListById(menuIds); |
| | | |
| | | } |
| | | |
| | | return resultList; |
| | | } |
| | | } |