From 72046386cf8a85ad7dae79d82485fa60c2c421e6 Mon Sep 17 00:00:00 2001
From: zhanzhiqin <895896009@qq.com>
Date: 星期二, 05 七月 2022 18:25:12 +0800
Subject: [PATCH] fix

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/ClientService.java |  179 +++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 161 insertions(+), 18 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 a3ff485..3e9aecd 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
@@ -6,20 +6,23 @@
 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.AddClientPrarm;
 import com.sandu.ximon.admin.param.PwdParam;
-import com.sandu.ximon.admin.param.ResetClientPasswordPrarm;
 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.mapper.ClientMapper;
 import lombok.AllArgsConstructor;
-import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 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.List;
 
 @Service
@@ -37,13 +40,27 @@
         if (getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, addClientPrarm.getClientName().trim())) != null) {
             throw new BusinessException("璇ョ敤鎴峰悕宸插瓨鍦紒");
         }
+        Long userId = SecurityUtils.getUserId();
+        boolean clientId = findClientId();
+        //鍒ゆ柇鏄惁涓轰簩绾у鎴�  濡傛灉鏄簩绾у鎴峰垯涓嶈兘缁х画娣诲姞   涓�绾у鎴蜂负FALSE
+        if (SecurityUtils.getClientId() != null && clientId) {
+            throw new BusinessException("鏉冮檺涓嶈冻,浜岀骇瀹㈡埛涓嶈兘鏂板鐢ㄦ埛锛�");
+        }
+
 
         Client client = new Client();
-        if (addClientPrarm.getClientSuperior() != null && !"".equals(addClientPrarm.getClientSuperior())) {
+
+        if (SecurityUtils.getClientId() != null && !clientId) {
+            //涓�绾у鎴锋柊澧炵敤鎴锋椂 榛樿涓鸿嚜宸卞悕涓嬬殑浜岀骇瀹㈡埛
+            client.setSuperiorId(SecurityUtils.getUserId());
+            client.setClientSuperior(addClientPrarm.getClientSuperior());
+        } else if (addClientPrarm.getClientSuperior() != null && !"".equals(addClientPrarm.getClientSuperior())) {
+            //瓒呯
             Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, addClientPrarm.getClientSuperior()));
             if (one != null) {
                 if (one.getSuperiorId() == null) {
                     client.setSuperiorId(one.getId());
+                    client.setClientSuperior(addClientPrarm.getClientSuperior());
                 } else {
                     throw new BusinessException("涓婄骇瀹㈡埛涓嶈兘涓轰簩绾х敤鎴�");
                 }
@@ -56,15 +73,18 @@
         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("鏃犳硶娣诲姞瓒呯骇绠$悊鍛�");
         }
 
 
         client.setClientName(addClientPrarm.getClientName());
         client.setLinkMan(addClientPrarm.getLinkMan());
+//        client.setClientSuperior(addClientPrarm.getClientSuperior());
         client.setMobile(addClientPrarm.getMobile());
-        client.setClientSuperior(addClientPrarm.getClientSuperior());
+        if (SecurityUtils.getClientId() != null && clientId) {
+            client.setClientSuperior(SecurityUtils.getUsername());
+        }
         client.setPassword(passwordEncoder.encode(addClientPrarm.getPassword()));
         boolean flag = save(client);
 
@@ -74,6 +94,15 @@
         if (!clientRoleRelationService.save(clientRoleRelation)) {
             throw new BusinessException("娣诲姞绠$悊鍛樿鑹插け璐�");
         }
+
+        /**
+         * 娣诲姞鏅�氱敤鎴� 鏃ュ織璁板綍寮�濮�
+         */
+        String content = "鏂版敞鍐岀敤鎴凤細" + addClientPrarm.getClientName();
+        StoreOperationRecordsUtils.storeOperationData(null, null, "娣诲姞鏅�氱敤鎴�", content);
+        /**
+         * 娣诲姞鏅�氱敤鎴� 鏃ュ織璁板綍缁撴潫
+         */
 
         return flag;
     }
@@ -91,9 +120,25 @@
         if (client1 != null && !client1.getId().equals(one.getId())) {
             throw new BusinessException("璇ョ敤鎴峰悕宸插瓨鍦紒");
         }
+
         //鍒ゆ柇涓婄骇鐢ㄦ埛鏄惁瀛樺湪
+        boolean clientId = findClientId();
+        Long userId = SecurityUtils.getUserId();
+        //鍒ゆ柇鏇存敼鐨勭敤鎴锋槸鍚︽槸灞炰簬鑷繁鍚嶄笅鐨勪簩绾у鎴�
+        if (SecurityUtils.getClientId() != null && !clientId) {
+            if (SecurityUtils.getUserId() != one.getSuperiorId()) {
+                throw new BusinessException("鏉冮檺涓嶈冻锛屼笉鑳芥洿鏀瑰叾浠栧鎴风殑鎵�灞炲鎴蜂俊鎭紒");
+            }
+        }
+
         Client client = new Client();
-        if (updateClientPrarm.getClientSuperior() != null && updateClientPrarm.getClientSuperior().trim().length() != 0) {
+        client.setClientName(updateClientPrarm.getClientName());
+        if (SecurityUtils.getClientId() != null && clientId) {
+            //涓�绾у鎴锋柊澧炵敤鎴锋椂 榛樿涓鸿嚜宸卞悕涓嬬殑浜岀骇瀹㈡埛
+            client.setSuperiorId(SecurityUtils.getUserId());
+            client.setClientSuperior(updateClientPrarm.getClientSuperior());
+        } else if (updateClientPrarm.getClientSuperior() != null && updateClientPrarm.getClientSuperior().trim().length() != 0) {
+            //瓒呯
             Client superior = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, updateClientPrarm.getClientSuperior()));
             if (superior != null) {
                 if (superior.getId().equals(one.getId())) {
@@ -107,14 +152,27 @@
             }
         }
         client.setId(id);
-        client.setClientName(updateClientPrarm.getClientName());
+//        client.setClientName(updateClientPrarm.getClientName());
         client.setLinkMan(updateClientPrarm.getLinkMan());
         client.setMobile(updateClientPrarm.getMobile());
 
-        // update(client);
+        /**
+         * 缂栬緫鏅�氱敤鎴� 鏃ュ織璁板綍寮�濮�
+         */
+        String content = "缂栬緫鏅�氱敤鎴凤細" + client.getClientName() + "鐢ㄦ埛id锛�" + id;
+        StoreOperationRecordsUtils.storeOperationData(null, null, "缂栬緫鏅�氱敤鎴�", content);
+        /**
+         * 缂栬緫鏅�氱敤鎴� 鏃ュ織璁板綍缁撴潫
+         */
         return updateById(client);
     }
 
+    /**
+     * 淇敼褰撳墠鐧诲綍鐢ㄦ埛瀵嗙爜
+     *
+     * @param param
+     * @return
+     */
     public boolean resetPassword(PwdParam param) {
 
         Client client = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, SecurityUtils.getUserDetails().getUserId()));
@@ -123,18 +181,49 @@
             throw new BusinessException("璇ョ敤鎴蜂笉瀛樺湪锛�");
         }
 
-        PasswordEncoder pw = new BCryptPasswordEncoder();
+        //鍒ゆ柇鏃у瘑鐮佷笌鏁版嵁搴撴槸鍚︿竴鑷�
+        if (passwordEncoder.matches(param.getOldPass(), client.getPassword())) {
+            //鍔犲瘑鏂板瘑鐮�
+            String encode = passwordEncoder.encode(param.getNewPass());
+            client.setPassword(encode);
+            return updateById(client);
+        } else {
+            throw new BusinessException("鏃у瘑鐮佷笉姝g‘锛岃閲嶆柊纭瀵嗙爜锛�");
+        }
+    }
+
+    /**
+     * 淇敼绠$悊鍛樼殑瀵嗙爜
+     *
+     * @param param
+     * @return
+     */
+    public boolean updateAdminPassword(UserPwsParm param) {
+        Client client = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, param.getUserid()));
+        if (client == null) {
+            throw new BusinessException("鐢ㄦ埛涓嶅瓨鍦�");
+        }
+
         //鍔犲瘑鏂板瘑鐮�
-        String encode = pw.encode(param.getNewPass());
-        client.setPassword(encode);
+        client.setPassword(passwordEncoder.encode(param.getNewPass()));
         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("鍒犻櫎鐨勭敤鎴蜂笉瀛樺湪鎴栬鐢ㄦ埛涓嶆槸浣犵殑涓嬬骇鐢ㄦ埛");
+            }
         }
 
         //鍒ゆ柇鍒犻櫎鐨勭敤鎴锋湁鏃犱笅绾х敤鎴�
@@ -144,7 +233,31 @@
             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);
+            //鍒犻櫎澶辫触鍥炴粴鏁版嵁
+            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) {
@@ -235,4 +348,34 @@
         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;
+    }
 }

--
Gitblit v1.9.3