From 591b785cf76754a696b5b07eab07cf587f2a4449 Mon Sep 17 00:00:00 2001
From: zhanzhiqin <895896009@qq.com>
Date: 星期日, 24 四月 2022 11:33:21 +0800
Subject: [PATCH] fix

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/ClientService.java |  257 ++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 232 insertions(+), 25 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 9feb6f9..dc4e402 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,56 +1,263 @@
 package com.sandu.ximon.admin.service;
 
+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.service.impl.BaseServiceImpl;
-import com.sandu.ximon.admin.param.ClientPrarm;
+import com.sandu.ximon.admin.param.*;
+import com.sandu.ximon.admin.security.SecurityUtils;
 import com.sandu.ximon.dao.domain.Admin;
 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.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 java.util.List;
 
 @Service
 @AllArgsConstructor
 public class ClientService extends BaseServiceImpl<ClientMapper, Client> {
 
-    public boolean addClient(ClientPrarm clientPrarm) {
-        Client client=new Client();
-        client.setClientName(clientPrarm.getClientName());
-        client.setLinkMan(clientPrarm.getLinkMan());
-        client.setMobile(clientPrarm.getMobile());
-        client.setClientSuperior(clientPrarm.getClientSuperior());
-
-        return save(client);
-    }
+    private final ClientMapper clientMapper;
+    private final PasswordEncoder passwordEncoder;
+    private final ClientRoleRelationService clientRoleRelationService;
+    private final RoleService roleService;
 
 
-    public boolean updateClient(Long id,ClientPrarm clientPrarm){
-        //Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, id));
-        Client one=getById(id);
-        if(one==null){
-            throw new BusinessException("璇ュ鎴蜂笉瀛樺湪");
+    public boolean addClient(AddClientPrarm addClientPrarm) {
+
+        if (getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, addClientPrarm.getClientName().trim())) != null) {
+            throw new BusinessException("璇ョ敤鎴峰悕宸插瓨鍦紒");
         }
-        Client client=new Client();
-        client.setId(id);
-        client.setClientName(clientPrarm.getClientName());
-        client.setLinkMan(clientPrarm.getLinkMan());
-        client.setMobile(clientPrarm.getMobile());
-        client.setClientSuperior(clientPrarm.getClientSuperior());
 
-       // update(client);
-      return updateById(client);
+        Client client = new Client();
+        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());
+                } else {
+                    throw new BusinessException("涓婄骇瀹㈡埛涓嶈兘涓轰簩绾х敤鎴�");
+                }
+            } else {
+                throw new BusinessException("涓婄骇瀹㈡埛涓嶅瓨鍦�");
+            }
+        }
+
+        Role role = roleService.getById(addClientPrarm.getRoleId());
+        if (role == null) {
+            throw new BusinessException("瑙掕壊涓嶅瓨鍦�");
+        }
+        if (!RoleLevelStatus.NORMAL.getCode().equals(role.getLevel())) {
+            throw new BusinessException("鏃犳硶娣诲姞瓒呯骇绠$悊鍛樻垨鐢ㄦ埛绠$悊鍛�");
+        }
+
+
+        client.setClientName(addClientPrarm.getClientName());
+        client.setLinkMan(addClientPrarm.getLinkMan());
+        client.setMobile(addClientPrarm.getMobile());
+        client.setClientSuperior(addClientPrarm.getClientSuperior());
+        client.setPassword(passwordEncoder.encode(addClientPrarm.getPassword()));
+        boolean flag = save(client);
+
+        ClientRoleRelation clientRoleRelation = new ClientRoleRelation();
+        clientRoleRelation.setClientId(client.getId());
+        clientRoleRelation.setRoleId(addClientPrarm.getRoleId());
+        if (!clientRoleRelationService.save(clientRoleRelation)) {
+            throw new BusinessException("娣诲姞绠$悊鍛樿鑹插け璐�");
+        }
+
+        return flag;
     }
 
-    public boolean deleteClient(Long id){
-        Client one=getById(id);
+
+    public boolean updateClient(Long id, UpdateClientPrarm updateClientPrarm) {
+        //鍒ゆ柇鐢ㄦ埛鏄惁瀛樺湪
+        Client one = getById(id);
         if (one == null) {
             throw new BusinessException("璇ュ鎴蜂笉瀛樺湪");
         }
+
+        //鍒ゆ柇鐢ㄦ埛鍚嶆槸鍚﹂噸澶�
+        Client client1 = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, updateClientPrarm.getClientName().trim()));
+        if (client1 != null && !client1.getId().equals(one.getId())) {
+            throw new BusinessException("璇ョ敤鎴峰悕宸插瓨鍦紒");
+        }
+        //鍒ゆ柇涓婄骇鐢ㄦ埛鏄惁瀛樺湪
+        Client client = new Client();
+        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())) {
+                    throw new BusinessException("涓婄骇瀹㈡埛涓烘湰璐︽埛锛�");
+                } else {
+                    client.setSuperiorId(superior.getId());
+                    client.setClientSuperior(updateClientPrarm.getClientSuperior());
+                }
+            } else {
+                throw new BusinessException("涓婄骇瀹㈡埛涓嶅瓨鍦�");
+            }
+        }
+        client.setId(id);
+        client.setClientName(updateClientPrarm.getClientName());
+        client.setLinkMan(updateClientPrarm.getLinkMan());
+        client.setMobile(updateClientPrarm.getMobile());
+
+        // update(client);
+        return updateById(client);
+    }
+
+    /**
+     * 淇敼褰撳墠鐧诲綍鐢ㄦ埛瀵嗙爜
+     *
+     * @param param
+     * @return
+     */
+    public boolean resetPassword(PwdParam param) {
+
+        Client client = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, SecurityUtils.getUserDetails().getUserId()));
+
+        if (client == null) {
+            throw new BusinessException("璇ョ敤鎴蜂笉瀛樺湪锛�");
+        }
+
+        //鍒ゆ柇鏃у瘑鐮佷笌鏁版嵁搴撴槸鍚︿竴鑷�
+        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("鐢ㄦ埛涓嶅瓨鍦�");
+        }
+
+        //鍔犲瘑鏂板瘑鐮�
+        client.setPassword(passwordEncoder.encode(param.getNewPass()));
+        return updateById(client);
+    }
+
+    public boolean deleteClient(Long id) {
+        //鍒ゆ柇鍒犻櫎鐢ㄦ埛鏄惁瀛樺湪
+        Client one = getById(id);
+        if (one == null) {
+            throw new BusinessException("璇ュ鎴蜂笉瀛樺湪");
+        }
+
+        //鍒ゆ柇鍒犻櫎鐨勭敤鎴锋湁鏃犱笅绾х敤鎴�
+        LambdaQueryWrapper<Client> lambdaQueryWrapper = Wrappers.lambdaQuery(Client.class).eq(Client::getSuperiorId, id);
+        List<Client> list = list(lambdaQueryWrapper);
+        if (list != null && list.size() != 0) {
+            throw new BusinessException("鍒犻櫎鐨勭敤鎴蜂笅鏈変笅绾х敤鎴凤紝涓嶅厑璁稿垹闄�");
+        }
+
         return removeById(id);
+    }
+
+    public List<Client> clientList(Long userId, BaseConditionVO baseConditionVO) {
+        return clientMapper.clientList(userId, baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
     }
 
     public Client findByPhone(String phone) {
         return getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getMobile, phone).last("limit 1"));
     }
+
+    /**
+     * \
+     * 鍏朵粬绫荤敤鏉ユ煡鎵惧鎴穒d浣跨敤  濡傛灉娌℃湁涓婄骇瀹㈡埛 杩欒繑鍥炵敤鎴稩D
+     *
+     * @param
+     * @return
+     */
+    public Long getClientId() {
+        Long userId = SecurityUtils.getUserId();
+        Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, userId));
+        if (one != null && one.getSuperiorId() != null) {
+            return one.getSuperiorId();
+        } else {
+            return userId;
+        }
+
+    }
+
+    /**
+     * \
+     * 鍏朵粬绫荤敤鏉ユ煡鎵惧鎴穒d浣跨敤  濡傛灉娌℃湁涓婄骇瀹㈡埛 杩欒繑鍥炵敤鎴稩D
+     *
+     * @param
+     * @return
+     */
+    public Long getClientId(Long userId) {
+        Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, userId));
+        if (one != null && one.getSuperiorId() != null) {
+            return one.getSuperiorId();
+        } else {
+            return userId;
+        }
+
+    }
+
+    /**
+     * 涓�绾у鎴疯繑鍥瀎alse  浜岀骇瀹㈡埛杩斿洖true
+     *
+     * @return
+     */
+    public boolean findClientId() {
+        Long userId = SecurityUtils.getUserId();
+        Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, userId));
+        if (one != null && one.getSuperiorId() != null) {
+            return true;
+        } else {
+            return false;
+        }
+
+    }
+
+    /**
+     * 涓�绾у鎴疯繑鍥瀎alse  浜岀骇瀹㈡埛杩斿洖true
+     *
+     * @return
+     */
+    public boolean findClientId(Long userId) {
+        Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, userId));
+        if (one != null && one.getSuperiorId() != null) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    /**
+     * 淇敼鏅�氱敤鎴峰ご鍍�
+     *
+     * @param userId
+     * @param IconUrl
+     * @return
+     */
+    public boolean updateIcon(Long userId, String IconUrl) {
+        Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, userId));
+        if (one == null) {
+            throw new BusinessException("鐢ㄦ埛涓嶅瓨鍦�");
+        }
+        one.setIcon(IconUrl);
+        return updateById(one);
+    }
 }

--
Gitblit v1.9.3