郑永安
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
package com.gkhy.safePlatform.account.service.baseService.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.safePlatform.account.entity.user.MenuInfo;
import com.gkhy.safePlatform.account.entity.user.MenuInfoDO;
import com.gkhy.safePlatform.account.enums.MenuStatusEnum;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.account.repository.MenuInfoRepository;
import com.gkhy.safePlatform.account.service.baseService.MenuInfoService;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
 
@Service("menuInfoService")
public class MenuInfoServiceImpl extends ServiceImpl<MenuInfoRepository, MenuInfo> implements MenuInfoService {
 
    @Autowired
    private MenuInfoRepository menuInfoRepository;
 
 
    @Override
    public List<MenuInfoDO> getAllMenu() {
        return menuInfoRepository.getAllMenu();
    }
 
 
 
 
 
    @Override
    public void saveMenu(MenuInfo menu) {
        int i = menuInfoRepository.insertMenu(menu);
        if (i != 1) {
            throw new BusinessException(ResultCodes.SERVER_ADD_ERROR);
        }
    }
 
    @Override
    public void updateMenu(MenuInfo menu) {
        if (menu.getId() == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        int i = menuInfoRepository.updateMenu(menu);
        if (i == 0) {
            throw new BusinessException(ResultCodes.SERVER_UPDATE_DATA_NO_CHANGE);
        }
        if (i > 1) {
            throw new BusinessException(ResultCodes.SERVER_ADD_ERROR);
        }
    }
 
    @Override
    public MenuInfo getMenuById(Long id) {
        if (id == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        return menuInfoRepository.getMenuById(id);
    }
 
    @Override
    public Long countByParentId(Long parentMenuId) {
        if (parentMenuId == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        return menuInfoRepository.selectCount(new LambdaQueryWrapper<MenuInfo>()
                // 父id = parentMenuId
                .eq(MenuInfo::getParentId,parentMenuId)
                // 状态
                .eq(MenuInfo::getStatus, MenuStatusEnum.ENABLED.getCode()));
    }
 
    @Override
    public MenuInfo getMenuInfoByName(String name) {
        if (StringUtils.isBlank(name)) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        List<MenuInfo> menuInfos = menuInfoRepository.selectList(new LambdaQueryWrapper<MenuInfo>()
                // name
                .eq(MenuInfo::getName, name)
                // 状态
                .eq(MenuInfo::getStatus, MenuStatusEnum.ENABLED.getCode()));
        if (menuInfos.size() == 0) {
            return null;
        }
        if (menuInfos.size() > 1) {
            throw new BusinessException(ResultCodes.SERVER_DATABASE_DATA_DUPLICATED);
        }
        return menuInfos.get(0);
    }
 
    @Override
    public MenuInfo getMenuInfoByPath(String path) {
        if (StringUtils.isBlank(path)) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        List<MenuInfo> menuInfos = menuInfoRepository.selectList(new LambdaQueryWrapper<MenuInfo>()
                // path
                .eq(MenuInfo::getPath, path)
                // 状态
                .eq(MenuInfo::getStatus, MenuStatusEnum.ENABLED.getCode()));
        if (menuInfos.size() == 0) {
            return null;
        }
        if (menuInfos.size() > 1) {
            throw new BusinessException(ResultCodes.SERVER_DATABASE_DATA_DUPLICATED);
        }
        return menuInfos.get(0);
    }
 
    @Override
    public MenuInfo getMenuInfoByComponent(String component) {
        if (StringUtils.isBlank(component)) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        List<MenuInfo> menuInfos = menuInfoRepository.selectList(new LambdaQueryWrapper<MenuInfo>()
                // path
                .eq(MenuInfo::getComponent, component)
                // 状态
                .eq(MenuInfo::getStatus, MenuStatusEnum.ENABLED.getCode())
                // 父id 不为空
                .isNotNull(MenuInfo::getParentId));
        if (menuInfos.size() == 0) {
            return null;
        }
        if (menuInfos.size() > 1) {
            throw new BusinessException(ResultCodes.SERVER_DATABASE_DATA_DUPLICATED);
        }
        return menuInfos.get(0);
    }
 
 
}