kaiyu
2021-04-27 19792073aeb0462088208334b4bd4241571630d4
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
package com.moral.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.moral.api.entity.ManageMenu;
import com.moral.api.entity.Menu;
import com.moral.api.mapper.MenuMapper;
import com.moral.api.pojo.dto.menu.MenuDTO;
import com.moral.api.pojo.dto.menu.MenuQueryDTO;
import com.moral.api.pojo.form.menu.MenuDeleteForm;
import com.moral.api.pojo.form.menu.MenuInsertForm;
import com.moral.api.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
 
/**
 * <p>
 * 前台菜单 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-03-09
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
 
    @Autowired
    MenuMapper menuMapper;
 
    @Override
    public MenuQueryDTO queryAllMenu() {
        //创建返回对象
        MenuQueryDTO dto = new MenuQueryDTO();
        //获取所有菜单
        List<Menu> menus = menuMapper.selectList(null);
        //组合menu父子结构
        Map<Integer, Menu> menuMap = new HashMap<>();
        for (Menu menu : menus) {
            menuMap.put(menu.getId(), menu);
            menu.setChildren(new ArrayList<>());//初始化集合
        }
        for (Menu menu : menus) {
            combinationParentChildrenMenus(menuMap, menu);
        }
        //删除非跟菜单
        menus.removeIf(new Predicate<Menu>() {
            @Override
            public boolean test(Menu menu) {
                if (menu.getParentId().equals(0))
                    return false;
                return true;
            }
        });
        //转换菜单为DTO
        List<MenuDTO> dtos = new ArrayList<>();
        for (Menu menu : menus) {
            MenuDTO menuDTO = new MenuDTO();
            menuDTO.setMenu(menu);
            dtos.add(menuDTO);
        }
        dto.setDtos(dtos);
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return dto;
    }
 
    @Override
    @Transactional
    public MenuDTO insertMenu(MenuInsertForm form) {
        //创建返回对象
        MenuDTO dto = new MenuDTO();
        //取参
        Menu menu = form.formConvertEntity();
        //检查名字是否重复
        Menu existMenu = new Menu();
        existMenu.setName(menu.getName());
        existMenu.setIsDelete(Constants.NOT_DELETE);
        QueryWrapper existWrapper = new QueryWrapper();
        existWrapper.setEntity(existMenu);
        Menu existMenuResult = menuMapper.selectOne(existWrapper);
        if (!ObjectUtils.isEmpty(existMenuResult)) {
            dto.setCode(ResponseCodeEnum.MENU_IS_EXIST.getCode());
            dto.setMsg(ResponseCodeEnum.MENU_IS_EXIST.getMsg());
            return dto;
        }
        //执行插入逻辑
        menuMapper.insert(menu);
        //封装返回对象
        dto.setMenu(menu);
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return dto;
    }
 
    @Override
    public MenuDTO deleteMenu(MenuDeleteForm form) {
        //创建返回对象
        MenuDTO dto = new MenuDTO();
        //取参
        Integer id = form.getId();
        //查询要删除的菜单
        Menu existMenu = menuMapper.selectById(id);
        if (ObjectUtils.isEmpty(existMenu)) {
            dto.setCode(ResponseCodeEnum.MENU_IS_NULL.getCode());
            dto.setMsg(ResponseCodeEnum.MENU_IS_NULL.getMsg());
            return dto;
        }
        //执行逻辑删除
        existMenu.setIsDelete(Constants.DELETE);
        menuMapper.updateById(existMenu);
        //删除所有子菜单
        List<Integer> childrenIds = getChildrenIdsByParentId(id);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.in("id",childrenIds);
        wrapper.set("is_delete",Constants.DELETE);
        menuMapper.update(null,wrapper);
        //封装返回对象
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        dto.setMenu(existMenu);
        return dto;
    }
 
    private void combinationParentChildrenMenus(Map<Integer, Menu> menuMap, Menu menu) {
        Integer parentId = menu.getParentId();
        Menu parentMenu = menuMap.get(parentId);
        if (!ObjectUtils.isEmpty(parentMenu)) {
            parentMenu.getChildren().add(menu);
        }
    }
 
    /**
    * @Description: 根据菜单的id获取封装好children的menu对象
            * @Param: [id]
            * @return: com.moral.api.entity.Menu
            * @Author: 陈凯裕
            * @Date: 2021/4/26
            */
    private Menu getMenuAndChildrenById(Integer id){
        //获取所有菜单
        List<Menu> menus = menuMapper.selectList(null);
        //组合menu父子结构
        Map<Integer, Menu> menuMap = new HashMap<>();
        for (Menu menu : menus) {
            menuMap.put(menu.getId(), menu);
            menu.setChildren(new ArrayList<>());//初始化集合
        }
        for (Menu menu : menus) {
            combinationParentChildrenMenus(menuMap, menu);
        }
        Menu menu = menuMap.get(id);
        return menu;
    }
 
    /**
    * @Description: 获取该菜单下的所有子菜单id集合
            * @Param: [id]
            * @return: java.util.List<java.lang.Integer>
            * @Author: 陈凯裕
            * @Date: 2021/4/26
            */
    private List<Integer> getChildrenIdsByParentId(Integer id){
        Menu parentMenu = getMenuAndChildrenById(id);
        List<Menu> children = parentMenu.getChildren();
        List<Integer> childrenIds = new ArrayList<>();
        for (Menu child : children) {
            recursiveAccess(child,childrenIds);
        }
        return childrenIds;
    }
 
 
    /**
    * @Description: 递归获取菜单的id放入集合中
            * @Param: [menu, ids]
            * @return: void
            * @Author: 陈凯裕
            * @Date: 2021/4/26
            */
    private void recursiveAccess(Menu menu,List<Integer> ids){
        ids.add(menu.getId());
        List<Menu> children = menu.getChildren();
        if(!ObjectUtils.isEmpty(children)){
            for (Menu child : children) {
                recursiveAccess(child,ids);
            }
        }
    }
 
}