郑永安
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.gkhy.safePlatform.account.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gkhy.safePlatform.account.model.dto.req.MenuMetaReqDTO;
import com.gkhy.safePlatform.account.model.dto.req.MenuModReqDTO;
import com.gkhy.safePlatform.account.entity.user.*;
import com.gkhy.safePlatform.account.enums.MenuStatusEnum;
 
import java.util.ArrayList;
import java.util.Date;
 
import com.gkhy.safePlatform.account.model.dto.req.MenuAddReqDTO;
import com.gkhy.safePlatform.account.service.MenuService;
import com.gkhy.safePlatform.account.service.RedisService;
import com.gkhy.safePlatform.account.service.baseService.*;
import com.gkhy.safePlatform.account.utils.RedisUtils;
import com.gkhy.safePlatform.account.utils.TokenUtil;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.enums.RedisKeyEnum;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.concurrent.TimeUnit;
 
@Service("menuService")
public class MenuServiceImpl implements MenuService {
 
 
    @Autowired
    private MenuInfoService menuInfoService;
    @Autowired
    private ProjectInfoService projectInfoService;
    @Autowired
    private RoleInfoService roleInfoService;
    @Autowired
    private ProjectMenuInfoService projectMenuInfoService;
    @Autowired
    private RoleProjectInfoService roleProjectInfoService;
    @Autowired
    private RoleMenuInfoService roleMenuInfoService;
    @Autowired
    private TokenUtil tokenConfig;
    @Autowired
    private RedisService redisService;
 
    @Override
    @Transactional
    public void addMenu(MenuAddReqDTO menuAddDto, ContextCacheUser currentUser) {
 
        MenuMetaReqDTO meta = menuAddDto.getMeta();
        if (meta == null) {
            throw new AusinessException(E.DATA_PARAM_NULL,"属性为空");
        }
        if (StringUtils.isBlank(meta.getTitle())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "菜单名称不能为空");
        }
        if (StringUtils.isBlank(menuAddDto.getName())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "路由名称不能为空");
        }
        if (menuAddDto.getPriority() == null) {
            throw new AusinessException(E.DATA_PARAM_NULL, "优先级不能为空");
        }
        if (StringUtils.isBlank(menuAddDto.getPath())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "路由不能为空");
        }
        if (StringUtils.isBlank(menuAddDto.getComponent())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "组件不能为空");
        }
        MenuInfo menuInfo;
        // name 路由判断
        String name = menuAddDto.getName().trim();
        menuInfo = menuInfoService.getMenuInfoByName(name);
        if (menuInfo != null) {
            throw new AusinessException(E.DATA_DATABASE_EXIST, "路由已存在");
        }
        //  path 路由地址判断
        String path = menuAddDto.getPath().trim();
        menuInfo = menuInfoService.getMenuInfoByPath(path);
        if (menuInfo != null) {
            throw new AusinessException(E.DATA_DATABASE_EXIST, "路由地址已存在");
        }
        //  component 组件判断(组件根菜单可以重复,子菜单不可以重复)
        String component = menuAddDto.getComponent().trim();
        menuInfo = menuInfoService.getMenuInfoByComponent(component);
        if (menuInfo != null) {
            throw new AusinessException(E.DATA_DATABASE_EXIST, "组件已存在");
        }
 
        List<String> roleCodes = meta.getRoles();
        // 校验角色是否存在
        List<RoleInfo> roleInfos = roleInfoService.getRoleInfoByCodes(roleCodes);
 
        Long projectId = menuAddDto.getProjectId();
        ProjectInfo project = projectInfoService.getProjectById(projectId);
        if (project == null) {
            throw new BusinessException(ResultCodes.CLIENT_PROJECT_NOT_EXIST);
        }
 
        // 角色和项目是否绑定
        for (RoleInfo role : roleInfos) {
            RoleProjectInfo roleProjectInfo = roleProjectInfoService.getRoleProjectInfo(role.getId(), project.getId());
            if (roleProjectInfo == null) {
                throw new BusinessException(ResultCodes.CLIENT_ROLE_HAS_NO_PROJECT);
            }
        }
        // 1.菜单插入
        MenuInfo menuEntity = new MenuInfo();
        menuEntity.setName(name);
        menuEntity.setRedirect(menuAddDto.getRedirect());
        menuEntity.setComponent(component);
        menuEntity.setPath(path);
        menuEntity.setPriority(menuAddDto.getPriority());
        menuEntity.setDescription(menuAddDto.getDescription());
        menuEntity.setTitle(meta.getTitle());
        menuEntity.setLinkEnable(meta.getIsLink());
        menuEntity.setHideEnable(meta.getIsHide());
        menuEntity.setKeepAliveEnable(meta.getIsKeepAlive());
        menuEntity.setAffixEnable(meta.getIsAffix());
        menuEntity.setIframeEnable(meta.getIsIframe());
        menuEntity.setIcon(meta.getIcon());
        menuEntity.setStatus(MenuStatusEnum.ENABLED.getCode());
 
        // 判断是否有父菜单
        if (menuAddDto.getParentId() != null) {
            MenuInfo parentMenu = menuInfoService.getMenuById(menuAddDto.getParentId());
            if (parentMenu == null) {
                throw new BusinessException(ResultCodes.CLIENT_MENU_PARENT_NOT_EXIST);
            }
        }
        menuEntity.setParentId(menuAddDto.getParentId());
        menuEntity.setPublicEnable(Boolean.FALSE);
        menuEntity.setGmtCreate(new Date());
        menuInfoService.saveMenu(menuEntity);
 
        // 2.项目和菜单
        ProjectMenuInfo projectMenuInfo = new ProjectMenuInfo();
        projectMenuInfo.setProjectId(projectId);
        projectMenuInfo.setMenuId(menuEntity.getId());
        projectMenuInfoService.save(projectMenuInfo);
 
        // 3.角色和菜单
        List<RoleMenuInfo> roleMenus = new ArrayList<>();
        RoleMenuInfo roleMenuInfo;
        for (RoleInfo roleInfo : roleInfos) {
            roleMenuInfo = new RoleMenuInfo();
            roleMenuInfo.setRoleId(roleInfo.getId());
            roleMenuInfo.setMenuId(menuEntity.getId());
            roleMenus.add(roleMenuInfo);
        }
        roleMenuInfoService.saveRoleMenus(roleMenus);
 
        // 4.redis更新所有菜单
        List<MenuInfoDO> allMenu = menuInfoService.getAllMenu();
        redisService.setCacheMenuAndExpireTime(RedisKeyEnum.AUTH_MENU.getKey(), allMenu, tokenConfig.getExpiration());
    }
 
 
 
    @Override
    @Transactional
    public void modMenu(MenuModReqDTO menuModDto, ContextCacheUser currentUser) {
 
        if (menuModDto.getId() == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        MenuInfo menu = menuInfoService.getMenuById(menuModDto.getId());
        if (menu == null) {
            throw new BusinessException(ResultCodes.CLIENT_MENU_NOT_EXIST);
        }
 
        MenuMetaReqDTO meta = menuModDto.getMeta();
        if (meta == null) {
            throw new AusinessException(E.DATA_PARAM_NULL,"属性为空");
        }
        if (StringUtils.isBlank(meta.getTitle())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "菜单名称不能为空");
        }
        if (menuModDto.getPriority() == null) {
            throw new AusinessException(E.DATA_PARAM_NULL, "优先级不能为空");
        }
        if (StringUtils.isBlank(menuModDto.getName())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "路由名称不能为空");
        }
        if (StringUtils.isBlank(menuModDto.getPath())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "路由不能为空");
        }
        if (StringUtils.isBlank(menuModDto.getComponent())) {
            throw new AusinessException(E.DATA_PARAM_NULL, "组件不能为空");
        }
 
        MenuInfo menuInfo;
        // name 路由判断
        String name = menuModDto.getName().trim();
        menuInfo = menuInfoService.getMenuInfoByName(name);
        if (menuInfo != null && !menuInfo.getId().equals(menu.getId())) {
            throw new AusinessException(E.DATA_DATABASE_EXIST, "路由已存在");
        }
        //  path 路由地址判断
        String path = menuModDto.getPath().trim();
        menuInfo = menuInfoService.getMenuInfoByPath(path);
        if (menuInfo != null && !menuInfo.getId().equals(menu.getId())) {
            throw new AusinessException(E.DATA_DATABASE_EXIST, "路由地址已存在");
        }
        //  component 组件判断(组件根菜单可以重复,子菜单不可以重复)
        String component = menuModDto.getComponent().trim();
        menuInfo = menuInfoService.getMenuInfoByComponent(component);
        if (menuInfo != null && !menuInfo.getId().equals(menu.getId())) {
            throw new AusinessException(E.DATA_DATABASE_EXIST, "组件已存在");
        }
 
 
        // 判断是否有父菜单
        if (menuModDto.getParentId() != null) {
            MenuInfo parentMenu = menuInfoService.getMenuById(menuModDto.getParentId());
            if (parentMenu == null) {
                throw new BusinessException(ResultCodes.CLIENT_MENU_PARENT_NOT_EXIST);
            }
        }
        List<String> roleCodes = meta.getRoles();
        // 校验角色是否存在
        List<RoleInfo> roleInfos = roleInfoService.getRoleInfoByCodes(roleCodes);
 
        Long projectId = menuModDto.getProjectId();
        ProjectInfo project = projectInfoService.getProjectById(projectId);
        if (project == null) {
            throw new BusinessException(ResultCodes.CLIENT_PROJECT_NOT_EXIST);
        }
        // 角色和项目是否绑定
        for (RoleInfo role : roleInfos) {
            RoleProjectInfo roleProjectInfo = roleProjectInfoService.getRoleProjectInfo(role.getId(), project.getId());
            if (roleProjectInfo == null) {
                throw new BusinessException(ResultCodes.CLIENT_ROLE_HAS_NO_PROJECT);
            }
        }
        // 1.更新菜单
        MenuInfo menuEntity = new MenuInfo();
        menuEntity.setId(menu.getId());
        menuEntity.setName(menuModDto.getName());
        menuEntity.setRedirect(menuModDto.getRedirect());
        menuEntity.setComponent(menuModDto.getComponent());
        menuEntity.setPriority(menuModDto.getPriority());
        menuEntity.setDescription(menuModDto.getDescription());
        // meta
        menuEntity.setTitle(meta.getTitle());
        menuEntity.setLinkEnable(meta.getIsLink());
        menuEntity.setHideEnable(meta.getIsHide());
        menuEntity.setKeepAliveEnable(meta.getIsKeepAlive());
        menuEntity.setAffixEnable(meta.getIsAffix());
        menuEntity.setIframeEnable(meta.getIsIframe());
        menuEntity.setIcon(meta.getIcon());
        menuEntity.setPath(menuModDto.getPath());
        menuEntity.setParentId(menuModDto.getParentId());
        menuEntity.setGmtModified(new Date());
        menuInfoService.updateMenu(menuEntity);
 
        // 2.菜单项目
        // 2.1解绑该菜单的所有项目
        projectMenuInfoService.unbindProjectByMenuId(menu.getId());
        // 2.2绑定当前菜单的所有角色
        projectMenuInfoService.bindProjectWithMenu(menu.getId(), projectId);
 
        // 3.角色菜单
        // 3.1解绑菜单的角色
        roleMenuInfoService.unbindRoleByMenuId(menu.getId());
        // 3.2绑定菜单和角色
        List<RoleMenuInfo> roleMenus = new ArrayList<>();
        RoleMenuInfo roleMenuInfo;
        for (RoleInfo roleInfo : roleInfos) {
            roleMenuInfo = new RoleMenuInfo();
            roleMenuInfo.setRoleId(roleInfo.getId());
            roleMenuInfo.setMenuId(menu.getId());
            roleMenus.add(roleMenuInfo);
        }
        roleMenuInfoService.saveRoleMenus(roleMenus);
 
        // 4.redis更新所有菜单
        List<MenuInfoDO> allMenu = menuInfoService.getAllMenu();
        redisService.setCacheMenuAndExpireTime(RedisKeyEnum.AUTH_MENU.getKey(), allMenu, tokenConfig.getExpiration());
    }
 
    @Override
    @Transactional
    public void delMenu(Long id,ContextCacheUser currentUser) {
        ;
        if (id == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        MenuInfo menu = menuInfoService.getMenuById(id);
        if (menu == null) {
            throw new BusinessException(ResultCodes.CLIENT_MENU_NOT_EXIST);
        }
        assert menu.getStatus() != null;
        MenuStatusEnum status = MenuStatusEnum.parse(menu.getStatus());
        if (status != MenuStatusEnum.ENABLED) {
            throw new BusinessException(ResultCodes.CLIENT_MENU_NOT_EXIST);
        }
        // 判断是否有子菜单,有不能删除,否则删除
        Long subMenuCount = menuInfoService.countByParentId(menu.getId());
        if (subMenuCount > 0) {
            throw new AusinessException(E.NOT_DELETE, "当前菜单存在子菜单,请先删除子菜单");
        }
        // 1.删除菜单
        MenuInfo menuEntity = new MenuInfo();
        menuEntity.setId(id);
        menuEntity.setStatus(MenuStatusEnum.ABANDONED.getCode());
        menuEntity.setGmtModified(new Date());
        menuInfoService.updateById(menuEntity);
        // 2.项目和菜单解绑
        projectMenuInfoService.unbindProjectByMenuId(id);
        // 3.角色和菜单解绑
        roleMenuInfoService.unbindRoleByMenuId(id);
        // 4.redis更新所有菜单
        List<MenuInfoDO> allMenu = menuInfoService.getAllMenu();
        redisService.setCacheMenuAndExpireTime(RedisKeyEnum.AUTH_MENU.getKey(), allMenu, tokenConfig.getExpiration());
    }
 
 
}