郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.gkhy.safePlatform.account.rpc.test;
import com.gkhy.safePlatform.account.model.dto.resp.DepRespDTO;
import com.gkhy.safePlatform.account.service.DepartmentService;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.MenuMetaRPCRespDTO;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.ProjectRPCRespDTO;
 
 
import com.gkhy.safePlatform.account.model.dto.req.LoginReqDTO;
import com.gkhy.safePlatform.account.model.dto.resp.MenuRespDTO;
import com.gkhy.safePlatform.account.model.dto.resp.UserLoginRespDTO;
import com.gkhy.safePlatform.account.entity.user.PermissionInfo;
import com.gkhy.safePlatform.account.entity.user.RoleInfo;
import com.gkhy.safePlatform.account.entity.user.UserInfo;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountAuthService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.*;
import com.gkhy.safePlatform.account.service.AuthService;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
@DubboService
public class AccountAuthServiceProvider implements AccountAuthService {
 
    @Autowired
    private AuthService authService;
    @Autowired
    private DepartmentService departmentService;
 
    @Override
    public String sayName(String id) {
        return "rpc call "+id+" test";
    }
 
 
 
    @Override
    public  ResultVO<List<String>> getUserPermissionByUserId (Long userId) {
        try {
            List<String> codes = new ArrayList<>();
            List<PermissionInfo> permissionInfos = authService.getUserPermissionByUserId(userId);
            if (permissionInfos.size() > 0) {
                codes = permissionInfos.stream().map(PermissionInfo::getCode).collect(Collectors.toList());
            }
            return new ResultVO<>(ResultCodes.OK, codes);
        } catch (AusinessException e) {
            return new ResultVO<>(e.getCode(), e.getMessage());
        } catch (BusinessException e) {
            return new ResultVO<>(e.getCode(), e.getMessage());
        } catch (Exception e) {
            ResultVO resultVO = new ResultVO<>();
            resultVO.setCode(ResultCodes.SERVER_ERROR);
            return resultVO;
        }
    }
 
 
    @Override
    public ResultVO<String> getUserRoleCodeByUserId(Long userId) {
        try {
            RoleInfo roleInfo = authService.getUserRoleByUserId(userId);
            return new ResultVO<>(ResultCodes.OK, roleInfo.getCode());
        } catch (AusinessException e) {
            return new ResultVO<>(e.getCode(), e.getMessage());
        } catch (BusinessException e) {
            return new ResultVO<>(e.getCode(), e.getMessage());
        } catch (Exception e) {
            ResultVO resultVO = new ResultVO<>();
            resultVO.setCode(ResultCodes.SERVER_ERROR);
            return resultVO;
        }
    }
 
 
    @Override
    public ResultVO<UserRPCRespDTO> getUserById(Long userId) {
        try {
            // 用户
            UserInfo userInfo = authService.getUserByUserId(userId);
            UserRPCRespDTO userReqDTO = new UserRPCRespDTO();
            BeanUtils.copyProperties(userInfo, userReqDTO);
            // 用户部门
            if (userInfo.getDepId() != null) {
                DepRespDTO dep = departmentService.getEnableDepartmentInfoByDepId(userId, userInfo.getDepId());
                if (dep != null) {
                    UserDepRPCRespDTO userDepRPCRespDTO = new UserDepRPCRespDTO();
                    BeanUtils.copyProperties(dep, userDepRPCRespDTO);
                    userReqDTO.setDepartment(userDepRPCRespDTO);
                }
            }
            userReqDTO.setEnterprise(new UserEnterpriseRPCRespDTO());
            return new ResultVO<>(ResultCodes.OK, userReqDTO);
        } catch (AusinessException e) {
            return new ResultVO<>(e.getCode(), e.getMessage());
        } catch (BusinessException e) {
            return new ResultVO<>(e.getCode(), e.getMessage());
        } catch (Exception e) {
            ResultVO resultVO = new ResultVO<>();
            resultVO.setCode(ResultCodes.SERVER_ERROR);
            return resultVO;
        }
 
 
    }
 
    @Override
    public ResultVO<UserLoginRPCRespDTO> authLogin(String username, String password) {
        ResultVO<UserLoginRPCRespDTO> result = new ResultVO<>(ResultCodes.OK);
        LoginReqDTO loginReqDTO = new LoginReqDTO();
        loginReqDTO.setUsername(username);
        loginReqDTO.setPassword(password);
 
        try {
            UserLoginRespDTO userOnlineRespDTO = authService.authLogin(loginReqDTO);
            UserLoginRPCRespDTO userLoginRespDTO = new UserLoginRPCRespDTO();
            BeanUtils.copyProperties(userOnlineRespDTO,userLoginRespDTO);
            result.setData(userLoginRespDTO);
        } catch (AusinessException e) {
            result.setCode(e.getCode());
            result.setMsg(e.getMessage());
        } catch (BusinessException e) {
            result.setCode(e.getCode());
            result.setMsg(e.getMessage());
        } catch (Exception e) {
            result.setCode(ResultCodes.SERVER_ERROR);
        }
 
        return result;
    }
 
    @Override
    public ResultVO<List<MenuRPCRespDTO>> getMenu(ContextCacheUser currentUser, Long projectId) {
        ResultVO<List<MenuRPCRespDTO>> result = new ResultVO<>(ResultCodes.OK);
        try {
            List<MenuRespDTO> sources = authService.getUserMenuTreeByUserIdAndProjectId(currentUser, projectId);
            // 重新赋值一遍
            result.setData(this.convertTORPCModel(sources));
        } catch (AusinessException e) {
            result.setCode(e.getCode());
            result.setMsg(e.getMessage());
        } catch (BusinessException e) {
            result.setCode(e.getCode());
            result.setMsg(e.getMessage());
        } catch (Exception e) {
            result.setCode(ResultCodes.SERVER_ERROR.getCode());
            result.setMsg(ResultCodes.SERVER_ERROR.getDesc());
        }
 
        return result;
    }
 
 
 
 
 
 
 
    private List<MenuRPCRespDTO> convertTORPCModel(List<MenuRespDTO> sources) {
        List<MenuRPCRespDTO> targets = new ArrayList<>();
        for (MenuRespDTO source : sources) {
            MenuRPCRespDTO target = new MenuRPCRespDTO();
            target.setId(source.getId());
            target.setName(source.getName());
            target.setPath(source.getPath());
            target.setPriority(source.getPriority());
            target.setDescription(source.getDescription());
            target.setParentId(source.getParentId());
            target.setRedirect(source.getRedirect());
            target.setComponent(source.getComponent());
            target.setStatus(source.getStatus());
            target.setChildren(new ArrayList<>());
            target.setPublic(source.getPublic());
            target.setMenuSuperior(source.getMenuSuperior());
 
 
        // meta
        MenuMetaRPCRespDTO meta;
        if (source.getMeta() != null) {
            meta = new MenuMetaRPCRespDTO();
            meta.setIsAffix(source.getMeta().getIsAffix());
            meta.setIsHide(source.getMeta().getIsHide());
            meta.setIcon(source.getMeta().getIcon());
            meta.setIsIframe(source.getMeta().getIsIframe());
            meta.setIsKeepAlive(source.getMeta().getIsKeepAlive());
            meta.setIsLink(source.getMeta().getIsLink());
            meta.setRoles(source.getMeta().getRoles());
            meta.setTitle(source.getMeta().getTitle());
            target.setMeta(meta);
        }
 
        // project
        if (source.getProject() != null) {
            ProjectRPCRespDTO project = new ProjectRPCRespDTO();
            project.setProjectCode(source.getProject().getProjectCode());
            project.setProjectId(source.getProject().getProjectId());
            project.setProjectName(source.getProject().getProjectName());
            project.setStatus(source.getProject().getStatus());
            target.setProject(project);
            target.setProjectId(source.getProject().getProjectId());
        }
 
 
        if (source.getChildren() != null && source.getChildren().size() > 0) {
            List<MenuRPCRespDTO> children = this.convertTORPCModel(source.getChildren());
            target.setChildren(children);
        }
 
 
            targets.add(target);
        }
        return targets;
    }
 
}