From f214709cf1d896e7c9743f206b7a708f020e5322 Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期五, 21 十月 2022 17:54:03 +0800
Subject: [PATCH] changes
---
ximon-admin/src/main/java/com/sandu/ximon/admin/service/ClientService.java | 226 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 211 insertions(+), 15 deletions(-)
diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/ClientService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/ClientService.java
index 9938ac1..8d6d8b7 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/ClientService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/ClientService.java
@@ -1,12 +1,18 @@
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;
@@ -15,14 +21,18 @@
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.mapper.AdminMapper;
+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;
@@ -30,16 +40,20 @@
@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 (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();
@@ -74,8 +88,8 @@
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("鏃犳硶娣诲姞瓒呯骇绠$悊鍛�");
}
@@ -97,12 +111,12 @@
}
/**
- * 娣诲姞绠$悊鍛樻棩蹇楄褰曞紑濮�
+ * 娣诲姞鏅�氱敤鎴� 鏃ュ織璁板綍寮�濮�
*/
String content = "鏂版敞鍐岀敤鎴凤細" + addClientPrarm.getClientName();
StoreOperationRecordsUtils.storeOperationData(null, null, "娣诲姞鏅�氱敤鎴�", content);
/**
- * 娣诲姞绠$悊鍛樻棩蹇楄褰曠粨鏉�
+ * 娣诲姞鏅�氱敤鎴� 鏃ュ織璁板綍缁撴潫
*/
return flag;
@@ -157,6 +171,14 @@
client.setLinkMan(updateClientPrarm.getLinkMan());
client.setMobile(updateClientPrarm.getMobile());
+ /**
+ * 缂栬緫鏅�氱敤鎴� 鏃ュ織璁板綍寮�濮�
+ */
+ String content = "缂栬緫鏅�氱敤鎴凤細" + client.getClientName() + "鐢ㄦ埛id锛�" + id;
+ StoreOperationRecordsUtils.storeOperationData(null, null, "缂栬緫鏅�氱敤鎴�", content);
+ /**
+ * 缂栬緫鏅�氱敤鎴� 鏃ュ織璁板綍缁撴潫
+ */
return updateById(client);
}
@@ -202,11 +224,21 @@
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("鍒犻櫎鐨勭敤鎴蜂笉瀛樺湪鎴栬鐢ㄦ埛涓嶆槸浣犵殑涓嬬骇鐢ㄦ埛");
+ }
}
//鍒ゆ柇鍒犻櫎鐨勭敤鎴锋湁鏃犱笅绾х敤鎴�
@@ -216,11 +248,142 @@
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";
+ //姝e簭銆佸�掑彊
+ 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) {
@@ -247,7 +410,7 @@
/**
* \
- * 鍏朵粬绫荤敤鏉ユ煡鎵惧鎴穒d浣跨敤 濡傛灉娌℃湁涓婄骇瀹㈡埛 杩欒繑鍥炵敤鎴稩D
+ * 鍏朵粬绫荤敤鏉ユ煡鎵惧鎴穒d浣跨敤 濡傛灉娌℃湁涓婄骇瀹㈡埛 杩斿洖-1
*
* @param
* @return
@@ -257,7 +420,7 @@
if (one != null && one.getSuperiorId() != null) {
return one.getSuperiorId();
} else {
- return userId;
+ return -1L;
}
}
@@ -337,4 +500,37 @@
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杩囨湡,璇疯仈绯荤鐞嗗憳閲嶆柊鑾峰彇鏈�鏂発ey锛�");
+ }
+
+ 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;
+ }
+ }
}
--
Gitblit v1.9.3