| | |
| | | package com.sandu.ximon.admin.service; |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.sandu.common.enums.RoleLevelStatus; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.object.BaseConditionVO; |
| | | import com.sandu.common.redis.RedisService; |
| | | import com.sandu.common.security.LoginUserInfo; |
| | | import com.sandu.common.security.token.TokenProvider; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | import com.sandu.common.util.IpUtil; |
| | | import com.sandu.common.util.SpringContextHolder; |
| | | import com.sandu.ximon.admin.dto.ClientDto; |
| | | import com.sandu.ximon.admin.dto.ClientDtoNode; |
| | | import com.sandu.ximon.admin.param.AddClientPrarm; |
| | | import com.sandu.ximon.admin.param.PwdParam; |
| | | import com.sandu.ximon.admin.param.UpdateClientPrarm; |
| | | import com.sandu.ximon.admin.param.UserPwsParm; |
| | | import com.sandu.ximon.admin.security.SecurityUtils; |
| | | import com.sandu.ximon.dao.domain.Client; |
| | | import com.sandu.ximon.dao.domain.ClientRoleRelation; |
| | | import com.sandu.ximon.dao.domain.Role; |
| | | import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils; |
| | | import com.sandu.ximon.dao.bo.MenuNode; |
| | | import com.sandu.ximon.dao.domain.*; |
| | | import com.sandu.ximon.dao.enums.AdministratorEnums; |
| | | import com.sandu.ximon.dao.enums.OrderByEnums; |
| | | import com.sandu.ximon.dao.mapper.ClientMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.security.core.GrantedAuthority; |
| | | 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.Collection; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class ClientService extends BaseServiceImpl<ClientMapper, Client> { |
| | | |
| | | private final ClientMapper clientMapper; |
| | | private final PasswordEncoder passwordEncoder; |
| | | private final ClientRoleRelationService clientRoleRelationService; |
| | | private final RoleService roleService; |
| | | private final TokenProvider tokenProvider; |
| | | |
| | | |
| | | public boolean addClient(AddClientPrarm addClientPrarm) { |
| | |
| | | if (role == null) { |
| | | throw new BusinessException("角色不存在"); |
| | | } |
| | | if (!RoleLevelStatus.NORMAL.getCode().equals(role.getLevel())) { |
| | | throw new BusinessException("无法添加超级管理员或用户管理员"); |
| | | if (RoleLevelStatus.SUPER.getCode().equals(role.getLevel())) { |
| | | throw new BusinessException("无法添加超级管理员"); |
| | | } |
| | | |
| | | |
| | |
| | | if (!clientRoleRelationService.save(clientRoleRelation)) { |
| | | throw new BusinessException("添加管理员角色失败"); |
| | | } |
| | | |
| | | /** |
| | | * 添加普通用户 日志记录开始 |
| | | */ |
| | | String content = "新注册用户:" + addClientPrarm.getClientName(); |
| | | StoreOperationRecordsUtils.storeOperationData(null, null, "添加普通用户", content); |
| | | /** |
| | | * 添加普通用户 日志记录结束 |
| | | */ |
| | | |
| | | return flag; |
| | | } |
| | |
| | | client.setLinkMan(updateClientPrarm.getLinkMan()); |
| | | client.setMobile(updateClientPrarm.getMobile()); |
| | | |
| | | /** |
| | | * 编辑普通用户 日志记录开始 |
| | | */ |
| | | String content = "编辑普通用户:" + client.getClientName() + "用户id:" + id; |
| | | StoreOperationRecordsUtils.storeOperationData(null, null, "编辑普通用户", content); |
| | | /** |
| | | * 编辑普通用户 日志记录结束 |
| | | */ |
| | | return updateById(client); |
| | | } |
| | | |
| | |
| | | return updateById(client); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean deleteClient(Long id) { |
| | | boolean flag = false; |
| | | //判断删除用户是否存在 |
| | | Client one = getById(id); |
| | | if (one == null) { |
| | | Client client = getById(id); |
| | | if (client == null) { |
| | | throw new BusinessException("该客户不存在"); |
| | | } |
| | | |
| | | //只有超管能删除 或 上级客户删除自己的下级用户 |
| | | if (!AdministratorEnums.ADMIN.getCode().equals(SecurityUtils.getUserDetails().getAdministratorType())) { |
| | | Client temp = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getSuperiorId, SecurityUtils.getUserId()).eq(Client::getId, id)); |
| | | if (temp == null) { |
| | | throw new BusinessException("删除的用户不存在或该用户不是你的下级用户"); |
| | | } |
| | | } |
| | | |
| | | //判断删除的用户有无下级用户 |
| | |
| | | throw new BusinessException("删除的用户下有下级用户,不允许删除"); |
| | | } |
| | | |
| | | return removeById(id); |
| | | //管理员角色关系表 |
| | | ClientRoleRelationService clientRoleRelationService = SpringContextHolder.getBean(ClientRoleRelationService.class); |
| | | //有用户必定有用户与角色的关系数据 |
| | | ClientRoleRelation one = clientRoleRelationService.getOne(Wrappers.lambdaQuery(ClientRoleRelation.class).eq(ClientRoleRelation::getClientId, client.getId())); |
| | | |
| | | //删用户 |
| | | if (removeById(id)) { |
| | | //删角色关系表信息 |
| | | flag = clientRoleRelationService.removeById(one); |
| | | List<Pole> poles = SpringContextHolder.getBean(PoleService.class).list(Wrappers.lambdaQuery(Pole.class).eq(Pole::getClientId, id)); |
| | | poles.forEach( |
| | | pole -> { |
| | | pole.setClientId(-1L); |
| | | pole.setUserId(-1L); |
| | | } |
| | | ); |
| | | |
| | | //删除用户后需要自动解绑该用户的设备 |
| | | |
| | | //灯杆id集合 |
| | | List<Long> collect = poles.stream().map(p -> p.getId()).collect(Collectors.toList()); |
| | | List<Long> collect1 = poles.stream().map(Pole::getId).collect(Collectors.toList()); |
| | | //删除绑定关系 |
| | | SpringContextHolder.getBean(PoleBindingService.class) |
| | | .remove(Wrappers.lambdaQuery(PoleBinding.class) |
| | | .in(PoleBinding::getPoleId, collect)); |
| | | //更改灯杆归属 |
| | | flag = SpringContextHolder.getBean(PoleService.class).updateBatchById(poles); |
| | | |
| | | |
| | | //删除失败回滚数据 |
| | | if (!flag) { |
| | | throw new BusinessException("删除用户失败!"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除普通用户 日志记录开始 |
| | | */ |
| | | String content = "删除普通用户:" + client.getClientName() + "用户id:" + id; |
| | | StoreOperationRecordsUtils.storeOperationData(null, null, "删除普通用户", content); |
| | | /** |
| | | * 删除普通用户 日志记录结束 |
| | | */ |
| | | |
| | | return flag; |
| | | } |
| | | |
| | | public List<Client> clientList(Long userId, BaseConditionVO baseConditionVO) { |
| | | return clientMapper.clientList(userId, baseConditionVO.getPageNo(), baseConditionVO.getPageSize()); |
| | | /** |
| | | * 用户列表 |
| | | * |
| | | * @param keyword |
| | | * @param order |
| | | * @param seq |
| | | * @return |
| | | */ |
| | | public List<ClientDtoNode> listLikeClient(String keyword, Integer order, Integer seq) { |
| | | |
| | | |
| | | LambdaQueryWrapper<Client> wrapper = Wrappers.lambdaQuery(Client.class); |
| | | if (AdministratorEnums.NORMAL.getCode().equals(SecurityUtils.getAdministratorIdentity())) { |
| | | wrapper.eq(Client::getSuperiorId, SecurityUtils.getUserId()).or(w -> w.eq( |
| | | Client::getId, SecurityUtils.getUserId() |
| | | )); |
| | | } |
| | | if (keyword != null && !keyword.isEmpty()) { |
| | | wrapper.like(Client::getClientName, keyword) |
| | | .or(w1 -> w1.like(Client::getMobile, keyword)) |
| | | .or(w2 -> w2.like(Client::getLinkMan, keyword)); |
| | | } |
| | | //排序字段 |
| | | String orderByResult = "id"; |
| | | //正序、倒叙 |
| | | String orderBySeq = OrderByEnums.ASC.getCode(); |
| | | if (order != null) { |
| | | switch (order) { |
| | | case 1: |
| | | orderByResult = OrderByEnums.CLIENT_CREATE_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(), orderBy); |
| | | // } |
| | | |
| | | List<Client> clientList = list(wrapper); |
| | | |
| | | List<ClientDto> clientDtoList = new ArrayList<>(); |
| | | for (Client demo : clientList) { |
| | | ClientDto clientDto = new ClientDto(); |
| | | BeanUtil.copyProperties(demo, clientDto); |
| | | ClientRoleRelation one = clientRoleRelationService.getOne(Wrappers.lambdaQuery(ClientRoleRelation.class) |
| | | .eq(ClientRoleRelation::getClientId, demo.getId())); |
| | | |
| | | if (one != null) { |
| | | clientDto.setRoleId(one.getRoleId()); |
| | | } |
| | | clientDtoList.add(clientDto); |
| | | } |
| | | |
| | | return clientDtoList.stream() |
| | | .filter(bean -> null == bean.getSuperiorId()) |
| | | .map(bean -> covertClientDtoNode(bean, clientDtoList)).collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | | * 数据整理成树状图 |
| | | * |
| | | * @param clientDto |
| | | * @param clientDtoList |
| | | * @return |
| | | */ |
| | | private ClientDtoNode covertClientDtoNode(ClientDto clientDto, List<ClientDto> clientDtoList) { |
| | | ClientDtoNode node = new ClientDtoNode(); |
| | | BeanUtils.copyProperties(clientDto, node); |
| | | List<ClientDtoNode> children = clientDtoList.stream() |
| | | .filter(subClientDto -> (subClientDto.getSuperiorId() != null && subClientDto.getSuperiorId().equals(clientDto.getId()))) |
| | | .map(subClientDto -> covertClientDtoNode(subClientDto, clientDtoList)).collect(Collectors.toList()); |
| | | node.setChildren(children); |
| | | return node; |
| | | } |
| | | |
| | | public Client findByPhone(String phone) { |
| | |
| | | |
| | | /** |
| | | * \ |
| | | * 其他类用来查找客户id使用 如果没有上级客户 这返回用户ID |
| | | * 其他类用来查找客户id使用 如果没有上级客户 返回-1 |
| | | * |
| | | * @param |
| | | * @return |
| | |
| | | if (one != null && one.getSuperiorId() != null) { |
| | | return one.getSuperiorId(); |
| | | } else { |
| | | return userId; |
| | | return -1L; |
| | | } |
| | | |
| | | } |
| | |
| | | one.setIcon(IconUrl); |
| | | return updateById(one); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户权限列表地 |
| | | */ |
| | | public List<MenuNode> getUserPermissionList() { |
| | | //通过用户UserID获取用户角色 |
| | | ClientRoleRelation one = SpringContextHolder.getBean(ClientRoleRelationService.class). |
| | | getOne(Wrappers.lambdaQuery(ClientRoleRelation.class).eq(ClientRoleRelation::getClientId, 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()); |
| | | } |
| | | |
| | | List<MenuNode> resultList = SpringContextHolder.getBean(MenuService.class).getUserPermissionListById(menuIds); |
| | | return resultList; |
| | | } |
| | | |
| | | /** |
| | | * 生成便捷登录KEY |
| | | */ |
| | | public String creatKeyByLogin(String keyValue) { |
| | | if (keyValue == null) { |
| | | throw new BusinessException("key不能为空!"); |
| | | } |
| | | |
| | | Object object = SpringContextHolder.getBean(RedisService.class).get(keyValue); |
| | | |
| | | if (object != null) { |
| | | return object.toString(); |
| | | } else { |
| | | Client client = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getUserKey, keyValue)); |
| | | if (client == null) { |
| | | throw new BusinessException("用户不存在或key过期,请联系管理员重新获取最新key!"); |
| | | } |
| | | |
| | | Collection<GrantedAuthority> grantedAuthorities = roleService.mapToGrantedAuthorities(client.getId(), false); |
| | | |
| | | LoginUserInfo loginUserInfo = new LoginUserInfo(); |
| | | loginUserInfo.setUserId(client.getId()); |
| | | loginUserInfo.setAccount(client.getClientName()); |
| | | loginUserInfo.setIp(IpUtil.getRealIp()); |
| | | loginUserInfo.setAdministratorType(AdministratorEnums.NORMAL.getCode()); |
| | | loginUserInfo.setPermission(grantedAuthorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.joining(","))); |
| | | |
| | | // 生成令牌 |
| | | String token = tokenProvider.createToken(loginUserInfo); |
| | | return token; |
| | | } |
| | | } |
| | | } |