| | |
| | | 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.admin.utils.StoreOperationRecordsUtils; |
| | | import com.sandu.ximon.admin.utils.StringUtil; |
| | | 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; |
| | |
| | | @AllArgsConstructor |
| | | public class ClientService extends BaseServiceImpl<ClientMapper, Client> { |
| | | |
| | | private final ClientMapper clientMapper; |
| | | private final PasswordEncoder passwordEncoder; |
| | | private final ClientRoleRelationService clientRoleRelationService; |
| | | private final RoleService roleService; |
| | |
| | | public boolean addClient(AddClientPrarm addClientPrarm) { |
| | | |
| | | if (getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, addClientPrarm.getClientName().trim())) != null) { |
| | | throw new BusinessException("该用户名已存在!"); |
| | | throw new BusinessException("该用户名已在客户中存在!"); |
| | | } |
| | | Admin admin = SpringContextHolder.getBean(AdminService.class).findByUserName(addClientPrarm.getClientName()); |
| | | if (admin != null) { |
| | | throw new BusinessException("当前账号" + addClientPrarm.getClientName() + "已经在管理员中存在"); |
| | | } |
| | | Long userId = SecurityUtils.getUserId(); |
| | | boolean clientId = findClientId(); |
| | |
| | | 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("删除用户失败!"); |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |
| | |
| | | } else { |
| | | Client client = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getUserKey, keyValue)); |
| | | if (client == null) { |
| | | throw new BusinessException("用户不存在,获取信息异常!"); |
| | | throw new BusinessException("用户不存在或key过期,请联系管理员重新获取最新key!"); |
| | | } |
| | | |
| | | Collection<GrantedAuthority> grantedAuthorities = roleService.mapToGrantedAuthorities(client.getId(), false); |