From dda268997ca8f8a364f7c19b45d7a43a50a98efe Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期二, 25 十月 2022 18:00:11 +0800
Subject: [PATCH] changes

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java |  258 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 234 insertions(+), 24 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 735a41a..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,23 +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.dao.domain.Admin;
-import com.sandu.ximon.dao.domain.AdminRoleRelation;
-import com.sandu.ximon.dao.domain.Role;
+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.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;
@@ -33,6 +42,8 @@
     private final PasswordEncoder passwordEncoder;
     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"));
@@ -42,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());
@@ -68,6 +84,15 @@
         if (!adminRoleRelationService.save(adminRoleRelation)) {
             throw new BusinessException("娣诲姞绠$悊鍛樿鑹插け璐�");
         }
+
+        /**
+         * 娣诲姞绠$悊鍛樻棩蹇楄褰曞紑濮�
+         */
+        String content = "鏂版敞鍐岀鐞嗗憳锛�" + param.getUsername();
+        StoreOperationRecordsUtils.storeOperationData(null, null, "娣诲姞绠$悊鍛�", content);
+        /**
+         * 娣诲姞绠$悊鍛樻棩蹇楄褰曠粨鏉�
+         */
         return true;
     }
 
@@ -95,7 +120,7 @@
             throw new BusinessException("缂栬緫绠$悊鍛樺け璐�");
         }
 
-        adminRoleRelationService.remove(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId,admin));
+        adminRoleRelationService.remove(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, admin.getId()));
         AdminRoleRelation adminRoleRelation = new AdminRoleRelation();
         adminRoleRelation.setAdminId(adminId);
         adminRoleRelation.setRoleId(param.getRoleId());
@@ -106,40 +131,225 @@
         return true;
     }
 
+    /**
+     * 淇敼褰撳墠鐧诲綍鐢ㄦ埛瀵嗙爜
+     *
+     * @param param
+     * @return
+     */
     public boolean updateMyPassword(PwdParam param) {
-        if (!StrUtil.equals(param.getNewPass(), param.getConfirmPass())) {
-            throw new BusinessException("涓ゆ瀵嗙爜涓嶄竴鑷�");
-        }
         Long userId = SecurityUtils.getUserId();
         Admin admin = getById(userId);
         if (admin == null) {
             throw new BusinessException("鐢ㄦ埛涓嶅瓨鍦�");
         }
-        if (!passwordEncoder.matches(param.getOldPass(), admin.getPassword())) {
-            throw new BusinessException("鏃у瘑鐮佷笉姝g‘");
+        //鍒ゆ柇鏃у瘑鐮佷笌鏁版嵁搴撴槸鍚︿竴鑷�
+        if (passwordEncoder.matches(param.getOldPass(), admin.getPassword())) {
+            //鍔犲瘑鏂板瘑鐮�
+            admin.setId(userId);
+            admin.setPassword(passwordEncoder.encode(param.getNewPass()));
+            return updateById(admin);
+        } else {
+            throw new BusinessException("鏃у瘑鐮佷笉姝g‘锛岃閲嶆柊纭瀵嗙爜锛�");
         }
-        Admin update = new Admin();
-        update.setId(userId);
-        update.setPassword(passwordEncoder.encode(param.getNewPass()));
-        return updateById(update);
     }
 
+    /**
+     * 淇敼绠$悊鍛樼殑瀵嗙爜
+     *
+     * @param param
+     * @return
+     */
+    public boolean updateAdminPassword(UserPwsParm param) {
+        Admin admin = getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getId, param.getUserid()));
+        if (admin == null) {
+            throw new BusinessException("鐢ㄦ埛涓嶅瓨鍦�");
+        }
+
+        //鍔犲瘑鏂板瘑鐮�
+        admin.setPassword(passwordEncoder.encode(param.getNewPass()));
+        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;
+    }
+
+    /**
+     * 淇敼瓒呯骇绠$悊鍛樸�佺鐞嗗憳鐢ㄦ埛澶村儚
+     *
+     * @param userId
+     * @param IconUrl
+     * @return
+     */
+    public boolean updateIcon(Long userId, String IconUrl) {
+        Admin one = getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getId, userId));
+        if (one == null) {
+            throw new BusinessException("鐢ㄦ埛涓嶅瓨鍦�");
+        }
+        one.setIcon(IconUrl);
+        return updateById(one);
+    }
+
+    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(), 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