package com.jilongda.manage.authority.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jilongda.common.model.TDept; import com.jilongda.manage.authority.dto.SecUserDTO; import com.jilongda.manage.authority.dto.SecUsersDTO; import com.jilongda.manage.authority.mapper.SecResourcesMapper; import com.jilongda.manage.authority.mapper.SecRoleMapper; import com.jilongda.manage.authority.mapper.SecUserRoleMapper; import com.jilongda.manage.authority.model.SecResources; import com.jilongda.manage.authority.model.SecRole; import com.jilongda.manage.authority.model.SecUser; import com.jilongda.manage.authority.model.SecUserRole; import com.jilongda.manage.authority.query.SecUserLowerQuery; import com.jilongda.manage.authority.service.SecUserService; import com.jilongda.manage.authority.vo.SecResourceVO; import com.jilongda.manage.authority.vo.SecUsersVO; import com.jilongda.common.basic.PageInfo; import com.jilongda.common.exception.ServiceException; import com.jilongda.common.utils.SpringUtils; import com.jilongda.manage.authority.mapper.SecUserMapper; import org.apache.commons.compress.utils.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; import static java.util.Comparator.comparing; import static java.util.stream.Collectors.toList; /** *

* 服务实现类 *

* * @author xiaochen * @since 2022-06-08 */ @Service public class SecUserServiceImpl extends ServiceImpl implements SecUserService { @Autowired private SecResourcesMapper secResourcesMapper; @Autowired private SecUserMapper secUserMapper; @Autowired private SecUserRoleMapper secUserRoleMapper; @Autowired private PasswordEncoder passwordEncoder; @Autowired private SecRoleMapper secRoleMapper; @Override public List selectUserResourcesTag(Long uid) { List sysResources = secResourcesMapper.selectResourceByUid(uid); List resourcesIds = sysResources.stream().map(SecResources::getId).collect(Collectors.toList()); // 排序会导致结构问题 List allResources = secResourcesMapper.selectList(Wrappers.lambdaQuery(SecResources.class)); SecResourceVO item; //获取根节点的集合 List root = new ArrayList<>(); Map tempMap = new HashMap<>(3); //子节点集合 SecResourceVO parent; for (SecResources resources : allResources) { item = SpringUtils.beanCopy(resources, SecResourceVO.class); if (resourcesIds.contains(resources.getId())) { item.setFlag(true); } else { item.setFlag(false); } //每次循环都将集合重新循环添加到父集合中 tempMap.put(resources.getId(), item); parent = tempMap.get(resources.getParentId()); if (Objects.nonNull(parent)) { //父集合不为空时,获取下级集合,追加子集合 parent.getChildren().add(item); // 对子集排序,升序 parent.getChildren().sort(comparing(SecResourceVO::getSort)); } else { //根集合 root.add(item); // 对根排序,升序 root.sort(comparing(SecResourceVO::getSort)); } } return root; } @Override public List getResourceTag(Long uid) { List sysResources = secResourcesMapper.selectResourceByUid(uid); // 判空 if (CollectionUtils.isEmpty(sysResources)) return Lists.newArrayList(); // 找出所有父级 List parent = sysResources.stream().filter(resources -> resources.getParentId()==0).collect(Collectors.toList()); List parentList = new ArrayList<>(); for (SecResources secResources : parent) { SecResourceVO secResourceVO = SpringUtils.beanCopy(secResources, SecResourceVO.class); parentList.add(secResourceVO); } // 通过父级递归子集 getChildren(sysResources, parentList); return parentList; } private void getChildren(List sysResources, List parentList) { parentList.forEach(parent -> { List childrenList = sysResources.stream().filter(resources -> resources.getParentId().equals(parent.getId())).collect(toList()); List childrens = new ArrayList<>(); for (SecResources secResources : childrenList) { SecResourceVO secResourceVO = SpringUtils.beanCopy(secResources, SecResourceVO.class); childrens.add(secResourceVO); } parent.setChildren(childrens); if (!org.springframework.util.CollectionUtils.isEmpty(childrenList)) getChildren(sysResources, childrens); }); } /** * 后台系统用户列表 * * @param dto * @return */ @Override public PageInfo getSysUserList(SecUsersDTO dto) { PageInfo pageInfo = new PageInfo<>(dto.getPageNum(), dto.getPageSize()); // 查询角色 List sysUserListVOS = secUserMapper.getSecUserList(dto, pageInfo); // List secRoles = secRoleMapper.selectList(Wrappers.lambdaQuery(SecRole.class)); // for (SecUsersVO sysUserListVO : sysUserListVOS) { // List roles = secRoles.stream().filter(role -> Objects.nonNull(sysUserListVO.getRoleId()) && sysUserListVO.getRoleId().equals(role.getId())).collect(toList()); // sysUserListVO.setSecRole(CollectionUtils.isNotEmpty(roles) ? roles.get(0) : null); // tDepts.stream().filter(dept -> Objects.nonNull(sysUserListVO.getDeptId()) && sysUserListVO.getDeptId().equals(dept.getId())).forEach(dept -> { // sysUserListVO.setDeptName(dept.getDeptName()); // }); // } pageInfo.setRecords(sysUserListVOS); // 获取列表 return pageInfo; } @Override public void addOrUpdateUser(SecUserDTO dto) { if (Objects.isNull(dto.getId())) { // 检查登陆账号是否存在 SecUser sysUser = secUserMapper.selectOne(new LambdaQueryWrapper() .eq(SecUser::getPhone, dto.getPhone())); if (Objects.nonNull(sysUser)) { throw new ServiceException("该账号已存在,请勿重复注册"); } dto.setAccount(dto.getPhone()); dto.setUserType(3); dto.setLastLoginTime(LocalDateTime.now()); if (StringUtils.hasLength(dto.getPassword())) { dto.setPassword(passwordEncoder.encode(dto.getPassword())); } secUserMapper.insert(dto); } else { if (StringUtils.hasLength(dto.getPassword())) { dto.setPassword(passwordEncoder.encode(dto.getPassword())); } secUserMapper.updateById(dto); // 删除之前的 secUserRoleMapper.delete(Wrappers.lambdaQuery(SecUserRole.class).eq(SecUserRole::getUserId, dto.getId())); } SecUserRole sur = new SecUserRole(); sur.setRoleId(dto.getRoleId()); sur.setUserId(dto.getId()); secUserRoleMapper.insert(sur); } @Override public PageInfo queryLower(SecUserLowerQuery query) { PageInfo pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize()); List secUserList = secUserMapper.queryLower(query, pageInfo); pageInfo.setRecords(secUserList); return pageInfo; } @Override public void deleteById(Long uid) { secUserMapper.removeById(uid); } @Override public List getUserByRoleId(Long id) { return secUserMapper.getUserByRoleId(id); } @Override public List selectListByNamePhoneDeptId(String name,Long deptId) { return secUserMapper.selectListByNamePhoneDeptId(name,deptId); } @Override public List selectListByNamePhoneDeptIds(String name, List deptIds) { return secUserMapper.selectListByNamePhoneDeptIds(name,deptIds); } @Override public SecUser selectUserById(Long orderingPersonId) { return secUserMapper.selectUserById(orderingPersonId); } @Override public List queryList() { return secUserMapper.queryList(); } }