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/AdminService.java |  192 +++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 175 insertions(+), 17 deletions(-)

diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java
index 768d3f3..481de34 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java
@@ -1,28 +1,32 @@
 package com.sandu.ximon.admin.service;
 
 import cn.hutool.core.collection.CollectionUtil;
-import cn.hutool.core.util.StrUtil;
+import cn.hutool.core.lang.Snowflake;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.pagehelper.PageHelper;
 import com.sandu.common.enums.RoleLevelStatus;
 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.AdminParam;
 import com.sandu.ximon.admin.param.PwdParam;
 import com.sandu.ximon.admin.param.UserPwsParm;
 import com.sandu.ximon.admin.security.SecurityUtils;
+import com.sandu.ximon.admin.security.authcode.MD5Util;
+import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
 import com.sandu.ximon.dao.bo.AdminBo;
-import com.sandu.ximon.dao.domain.Admin;
-import com.sandu.ximon.dao.domain.AdminRoleRelation;
-import com.sandu.ximon.dao.domain.Client;
-import com.sandu.ximon.dao.domain.Role;
+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.AdminMapper;
 import lombok.AllArgsConstructor;
 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.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
@@ -39,6 +43,7 @@
     private final AdminRoleRelationService adminRoleRelationService;
     private final RoleService roleService;
     private AdminMapper adminMapper;
+    private Snowflake snowflake;
 
     public Admin findByUserName(String username) {
         return getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getUsername, username).last("limit 1"));
@@ -48,9 +53,14 @@
     public boolean register(AdminParam param) {
         Admin admin = findByUserName(param.getUsername());
         if (admin != null) {
-            throw new BusinessException("褰撳墠璐﹀彿" + param.getUsername() + "宸茬粡瀛樺湪");
+            throw new BusinessException("褰撳墠璐﹀彿" + param.getUsername() + "宸茬粡鍦ㄧ鐞嗗憳涓瓨鍦�");
+        }
+        Client one = SpringContextHolder.getBean(ClientService.class).getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, param.getUsername()));
+        if (one != null) {
+            throw new BusinessException("褰撳墠璐﹀彿" + param.getUsername() + "宸茬粡鍦ㄥ鎴蜂腑瀛樺湪");
         }
         Admin save = new Admin();
+        save.setId(snowflake.nextId());
         save.setUsername(param.getUsername());
         save.setPassword(passwordEncoder.encode(param.getPassword()));
         save.setNickName(param.getNickName());
@@ -74,6 +84,15 @@
         if (!adminRoleRelationService.save(adminRoleRelation)) {
             throw new BusinessException("娣诲姞绠$悊鍛樿鑹插け璐�");
         }
+
+        /**
+         * 娣诲姞绠$悊鍛樻棩蹇楄褰曞紑濮�
+         */
+        String content = "鏂版敞鍐岀鐞嗗憳锛�" + param.getUsername();
+        StoreOperationRecordsUtils.storeOperationData(null, null, "娣诲姞绠$悊鍛�", content);
+        /**
+         * 娣诲姞绠$悊鍛樻棩蹇楄褰曠粨鏉�
+         */
         return true;
     }
 
@@ -152,23 +171,50 @@
         return updateById(admin);
     }
 
+    @Transactional(rollbackFor = Exception.class)
     public boolean deleteAdmin(Long adminId) {
+        boolean flag = false;
         Admin admin = getById(adminId);
         if (admin == null) {
             throw new BusinessException("鎵句笉鍒扮鐞嗗憳");
         }
-        List<Role> roles = roleService.listByAdminId(admin.getId());
-        if (CollectionUtil.isEmpty(roles)) {
-            throw new BusinessException("褰撳墠鐢ㄦ埛娌℃湁瑙掕壊");
-        }
-        List<Integer> levels = roles.stream().map(Role::getLevel).collect(Collectors.toList());
-        int min = Collections.min(levels);
-        int maxLevel = roleService.assertLevels(min);
-        if (!RoleLevelStatus.SUPER.getCode().equals(maxLevel)) {
+        if (!AdministratorEnums.ADMIN.getCode().equals(SecurityUtils.getUserDetails().getAdministratorType())) {
             throw new BusinessException("鍙湁瓒呯骇绠$悊鍛樻墠鑳藉垹闄ょ敤鎴�");
         }
 
-        return removeById(adminId);
+        //绠$悊鍛樿鑹插叧绯昏〃
+        AdminRoleRelationService adminRoleRelationService = SpringContextHolder.getBean(AdminRoleRelationService.class);
+        //鏈夌敤鎴峰繀瀹氭湁鐢ㄦ埛涓庤鑹茬殑鍏崇郴鏁版嵁
+        AdminRoleRelation one = adminRoleRelationService.getOne(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, admin.getId()));
+        Role role = SpringContextHolder.getBean(RoleService.class).getOne(Wrappers.lambdaQuery(Role.class).eq(Role::getId, one.getRoleId()));
+        if (role == null) {
+            throw new BusinessException("鏁版嵁寮傚父锛�");
+        }
+
+        if (role.getLevel().equals(RoleLevelStatus.SUPER.getCode())) {
+            throw new BusinessException("瓒呯骇绠$悊鍛樻棤娉曞垹闄�");
+        } else {
+            //鍒犵敤鎴�
+            if (removeById(adminId)) {
+                //鍒犺鑹插叧绯昏〃淇℃伅
+                flag = adminRoleRelationService.removeById(one);
+                //鍒犻櫎澶辫触鍥炴粴鏁版嵁
+                if (!flag) {
+                    throw new BusinessException("鍒犻櫎绠$悊鍛樼敤鎴峰け璐ワ紒");
+                }
+            }
+        }
+
+        /**
+         * 鍒犻櫎绠$悊鍛� 鏃ュ織璁板綍寮�濮�
+         */
+        String content = "鍒犻櫎绠$悊鍛橈細" + admin.getUsername() + "鐢ㄦ埛id锛�" + admin;
+        StoreOperationRecordsUtils.storeOperationData(null, null, "鍒犻櫎绠$悊鍛�", content);
+        /**
+         * 鍒犻櫎绠$悊鍛� 鏃ュ織璁板綍缁撴潫
+         */
+
+        return flag;
     }
 
     /**
@@ -187,11 +233,123 @@
         return updateById(one);
     }
 
-    public List<AdminBo> listAdmin(BaseConditionVO baseConditionVO, String keyword) {
+    public List<AdminBo> listAdmin(BaseConditionVO baseConditionVO, String keyword, Integer order, Integer seq) {
+        //鎺掑簭瀛楁
+        String orderByResult = OrderByEnums.ADMIN_ID.getCode();
+        //姝e簭銆佸�掑彊
+        String orderBySeq = OrderByEnums.ASC.getCode();
+        if (order != null) {
+            switch (order) {
+                case 1:
+                    orderByResult = OrderByEnums.ADMIN_ID.getCode();
+                    break;
+                case 2:
+                    orderByResult = OrderByEnums.ADMIN_CREATE_TIME.getCode();
+                    break;
+                case 3:
+                    orderByResult = OrderByEnums.ADMIN_LOGIN_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());
+            PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize(), orderBy);
         }
 
         return adminMapper.listAdmin(keyword);
     }
+
+    /**
+     * 鑾峰彇鐢ㄦ埛鏉冮檺鍒楄〃鍦�
+     */
+    public List<MenuNode> getUserPermissionList() {
+        List<MenuNode> resultList;
+        //閫氳繃鐢ㄦ埛UserID鑾峰彇鐢ㄦ埛瑙掕壊
+        AdminRoleRelation one = SpringContextHolder.getBean(AdminRoleRelationService.class).
+                getOne(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, 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());
+        }
+
+        resultList = SpringContextHolder.getBean(MenuService.class).getUserPermissionListById(menuIds);
+
+        return resultList;
+    }
+
+
+    /**
+     * 鐢熸垚key
+     *
+     * @param userId
+     * @return
+     */
+    public String creatUserKey(Long userId, Integer type) {
+        if (userId == null) {
+            throw new BusinessException("鐢ㄦ埛ID涓嶈兘涓虹┖锛�");
+        }
+        //0锛氬厖閲嶇疆key,1锛氳幏鍙杒ey
+        if (type == null || (type != 0 && type != 1)) {
+            type = 1;
+        }
+
+        ClientService clientService = SpringContextHolder.getBean(ClientService.class);
+        Client client = clientService.getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, userId));
+        if (client == null) {
+            throw new BusinessException("鐢ㄦ埛涓嶅瓨鍦紝璇烽噸鏂扮‘璁わ紒");
+        }
+
+        /**
+         * key鎿嶄綔鏃ュ織
+         */
+        String content = "鎿嶄綔鐢ㄦ埛key锛�" + SecurityUtils.getUsername() + "  琚搷浣滅敤鎴穒d锛�" + userId;
+
+
+        //鑾峰彇
+        if (type == 1) {
+            content += "鎿嶄綔绫诲瀷:鑾峰彇key";
+            StoreOperationRecordsUtils.storeOperationData(null, null, "鎿嶄綔鐢ㄦ埛key", content);
+            return "鐢ㄦ埛鐨勪究鎹风櫥褰昸ey涓猴細" + client.getUserKey();
+        } else {
+            //鏇存柊
+            String md5 = MD5Util.md5("uesrId:" + userId + "_AdministratorType:" + 2 + "time:" + System.currentTimeMillis());
+            client.setUserKey(md5);
+            boolean flag = clientService.updateById(client);
+            if (flag) {
+                content += "鎿嶄綔绫诲瀷:鐢熸垚key,鏂扮殑key涓猴細" + md5;
+                StoreOperationRecordsUtils.storeOperationData(null, null, "鎿嶄綔鐢ㄦ埛key", content);
+                return "鐢熸垚渚挎嵎鐧诲綍key鎴愬姛锛岃鐢ㄦ埛key涓猴細" + md5 + "锛岃濡ュ杽淇濈锛�";
+            } else {
+                throw new BusinessException("鐢熸垚渚挎嵎鐧诲綍key澶辫触锛�");
+            }
+        }
+    }
 }

--
Gitblit v1.9.3