2021与蓝度共同重构项目,服务端
MercuryZ
2022-03-31 ef270bd1855b5ce3f398c5df024840a498e33a8f
ximon-admin/src/main/java/com/sandu/ximon/admin/service/ClientService.java
@@ -1,56 +1,225 @@
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.dao.domain.Admin;
import com.sandu.ximon.admin.param.AddClientPrarm;
import com.sandu.ximon.admin.param.ResetClientPasswordPrarm;
import com.sandu.ximon.admin.param.UpdateClientPrarm;
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.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);
    }
    public boolean resetPassword(ResetClientPasswordPrarm resetClientPasswordPrarm) {
        Client client = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getId, resetClientPasswordPrarm.getId()));
        if (client == null) {
            throw new BusinessException("该用户不存在!");
        }
        PasswordEncoder pw = new BCryptPasswordEncoder();
        //判断旧密码与数据库是否一致
        if (pw.matches(resetClientPasswordPrarm.getOldPassword(), client.getPassword())) {
            //加密新密码
            String encode = pw.encode(resetClientPasswordPrarm.getNewPassword());
            client.setPassword(encode);
            return updateById(client);
        } else {
            throw new BusinessException("旧密码不正确,请重新确认密码!");
        }
    }
    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"));
    }
    /**
     * \
     * 其他类用来查找客户id使用  如果没有上级客户 这返回用户ID
     *
     * @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;
        }
    }
    /**
     * \
     * 其他类用来查找客户id使用  如果没有上级客户 这返回用户ID
     *
     * @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;
        }
    }
    /**
     * 一级客户返回false  二级客户返回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;
        }
    }
    /**
     * 一级客户返回false  二级客户返回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;
        }
    }
}