2021与蓝度共同重构项目,服务端
liuhaonan
2022-03-09 ed037879103f9051f9f7381f02f9d93dc681ea42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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.execption.BusinessException;
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.admin.param.AddClientPrarm;
import com.sandu.ximon.admin.param.ClientPrarm;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.dao.domain.Client;
import com.sandu.ximon.dao.mapper.ClientMapper;
import lombok.AllArgsConstructor;
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> {
 
    private final ClientMapper clientMapper;
    private final PasswordEncoder passwordEncoder;
 
 
    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();
        if (addClientPrarm.getClientSuperior() != null && !"".equals(addClientPrarm.getClientSuperior())) {
            Client one = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, addClientPrarm.getClientSuperior()));
            if (one != null) {
                client.setSuperiorId(one.getId());
            } else {
                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()));
 
 
        return save(client);
    }
 
 
    public boolean updateClient(Long id, ClientPrarm clientPrarm) {
        //判断用户是否存在
        Client one = getById(id);
        if (one == null) {
            throw new BusinessException("该客户不存在");
        }
 
        //判断用户名是否重复
        Client client1 = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, clientPrarm.getClientName().trim()));
        if (client1 != null && !client1.getId().equals(one.getId())) {
            throw new BusinessException("该用户名已存在!");
        }
        //判断上级用户是否存在
        Client client = new Client();
        if (clientPrarm.getClientSuperior() != null && clientPrarm.getClientSuperior().trim().length() != 0) {
            Client superior = getOne(Wrappers.lambdaQuery(Client.class).eq(Client::getClientName, clientPrarm.getClientSuperior()));
            if (superior != null) {
                if (superior.getId().equals(one.getId())) {
                    throw new BusinessException("上级客户为本账户!");
                } else {
                    client.setSuperiorId(superior.getId());
                    client.setClientSuperior(clientPrarm.getClientSuperior());
                }
            } else {
                throw new BusinessException("上级客户不存在");
            }
        }
        client.setId(id);
        client.setClientName(clientPrarm.getClientName());
        client.setLinkMan(clientPrarm.getLinkMan());
        client.setMobile(clientPrarm.getMobile());
 
        // update(client);
        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"));
    }
 
    /**
     * \
     * 其他类用来查找客户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;
        }
 
    }
 
    /**
     * 一级客户返回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;
        }
 
    }
}