cloudroam
3 天以前 3a819e4f668c15e8b77b188b322470da12bb7a43
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
package com.mzl.flower.service.system;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.system.CreateAppMenuDTO;
import com.mzl.flower.dto.request.system.UpdateAppMenuDTO;
import com.mzl.flower.dto.response.system.AppMenuDTO;
import com.mzl.flower.dto.response.system.AppMenuTreeDTO;
import com.mzl.flower.entity.system.AppMenu;
import com.mzl.flower.enums.TrueOrFalseEnum;
import com.mzl.flower.mapper.system.AppMenuMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.utils.TreeBuilderUtil;
import com.mzl.flower.utils.UUIDGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Service
@Transactional
public class AppMenuService extends BaseService {
    @Autowired
    private AppMenuMapper menuMapper;
 
    public AppMenu getMenu(String id) {
        return menuMapper.selectById(id);
    }
 
    public void addMenu(CreateAppMenuDTO dto) {
        AppMenu menu = new AppMenu();
        // 查找菜单名称是否已经存在
//        if (menuMapper.selectCount(new QueryWrapper<AppMenu>()
//                .lambda().eq(AppMenu::getMenuName, dto.getMenuName())
//                .eq(AppMenu::getStatus, Constants.STATUS_ACTIVE)
//        ) > 0) {
//            throw new ValidationException("菜单名称已经存在");
//        }
        // 查找权限标识是否已经存在
        if (menuMapper.selectCount(new QueryWrapper<AppMenu>().lambda().eq(AppMenu::getPermissionUq, dto.getPermissionUq()).eq(AppMenu::getStatus, Constants.STATUS_ACTIVE)) > 0) {
            throw new ValidationException("权限标识已经存在");
        }
 
        BeanUtils.copyProperties(dto, menu);
        menu.setId(UUIDGenerator.getUUID());
        menu.setIsVisible(Constants.Y);
        menu.setStatus(Constants.STATUS_ACTIVE);
        menu.setSubaccountAccessFlag(Constants.CUSTOM01.ONE.getDesc());
 
        menuMapper.insert(menu);
    }
 
    public List<AppMenuTreeDTO> searchOperationMenu() {
        List<AppMenuTreeDTO> treeList = menuMapper.selectListOrderBySeq();
        for (AppMenuTreeDTO appMenuTreeDTO : treeList) {
            appMenuTreeDTO.setLabel(appMenuTreeDTO.getMenuName());
            appMenuTreeDTO.setPath(appMenuTreeDTO.getMenuHref());
            appMenuTreeDTO.setName(appMenuTreeDTO.getMenuName());
        }
        treeList = (List<AppMenuTreeDTO>) TreeBuilderUtil.buildListToTree(treeList);
        return treeList;
    }
 
    public AppMenuDTO getMenuDetail(String id) {
        AppMenu menu = getMenu(id);
 
        //返回对象
        AppMenuDTO menuDTO = new AppMenuDTO();
        if (menu != null) {
            BeanUtils.copyProperties(menu, menuDTO);
        }
        return menuDTO;
    }
 
    public void updateMenu(UpdateAppMenuDTO dto) {
        AppMenu menu = getMenu(dto.getId());
        if(null==menu){
            throw new ValidationException("菜单不存在");
        }
//        if (menuMapper.selectCount(new QueryWrapper<AppMenu>().lambda().eq(AppMenu::getMenuName, dto.getMenuName()).eq(AppMenu::getStatus, Constants.STATUS_ACTIVE).ne(AppMenu::getId, menu.getId())) > 0) {
//            throw new ValidationException("菜单名称已经存在");
//        }
        // 查找权限标识是否已经存在
        if (menuMapper.selectCount(new QueryWrapper<AppMenu>().lambda().eq(AppMenu::getPermissionUq, dto.getPermissionUq()).eq(AppMenu::getStatus, Constants.STATUS_ACTIVE).ne(AppMenu::getId, menu.getId())) > 0) {
            throw new ValidationException("权限标识已经存在");
        }
        Integer subaccountAccessFlag = menu.getSubaccountAccessFlag();
        BeanUtils.copyProperties(dto, menu);
        menu.setSubaccountAccessFlag(subaccountAccessFlag);
        menuMapper.updateById(menu);
    }
 
    public void deleteMenu(String id) {
        AppMenu menu = getMenu(id);
        menu.setStatus(Constants.STATUS_DELETED);
        menuMapper.updateById(menu);
 
        List<AppMenu> menus = menuMapper.selectList(new QueryWrapper<AppMenu>().eq("PARENT_ID", id));
        if (menus != null && menus.size() > 0) {
            for (AppMenu children : menus) {
                deleteMenu(children.getId());
            }
        }
    }
 
    public void updateMenuShow(String id) {
        // 更新当前菜单及所有子菜单都变为隐藏
        List<String> menuIds = new ArrayList<>();
        findAllChildMenuIds(id, menuIds);
        menuMapper.updateMenuSubaccountAccessFlag(menuIds, Constants.CUSTOM01.ONE.getDesc());
    }
 
    public void updateMenuHidden(String id) {
        // 递归查找所有子菜单
        List<String> menuIds = new ArrayList<>();
        findAllChildMenuIds(id, menuIds);
        menuMapper.updateMenuSubaccountAccessFlag(menuIds,  Constants.CUSTOM01.ZERO.getDesc());
    }
 
 
    /**
     * 递归查找当前菜单及所有子菜单的 ID
     *
     * @param menuId     当前菜单 ID
     * @param menuIds    存储菜单 ID 的列表
     */
    private void findAllChildMenuIds(String menuId, List<String> menuIds) {
        // 首先将当前菜单 ID 加入菜单列表
        menuIds.add(menuId);
 
        // 查询当前菜单的所有子菜单
        List<String> childMenuIds = menuMapper.findChildMenuIds(menuId);
 
        // 如果有子菜单,递归查找
        if (childMenuIds != null && !childMenuIds.isEmpty()) {
            for (String childMenuId : childMenuIds) {
                findAllChildMenuIds(childMenuId, menuIds);  // 递归查找子菜单
            }
        }
    }
 
    // 获取供应商菜单信息
    public List<AppMenu> getPermissionMenu() {
 
        List<AppMenu> list = getAppMenus();
 
        if (!CollectionUtils.isEmpty(list)) {
 
            List<AppMenu> allMenuList = menuMapper.selectList(
                    new QueryWrapper<AppMenu>().lambda().eq(AppMenu::getStatus, Constants.STATUS_ACTIVE));
 
            return allMenuList;
        }
 
        return null;
    }
 
    private List<AppMenu> getAppMenus() {
        LambdaQueryWrapper<AppMenu> queryWrapper = new QueryWrapper<AppMenu>()
                .lambda()
                .eq(AppMenu::getStatus, Constants.STATUS_ACTIVE)
//                .eq(AppMenu::getSubaccountAccessFlag,Constants.CUSTOM01.ONE.getDesc())
//                .eq(AppMenu::getPermissionUq, Constants.APP_MENU_TYPE.SUPPLIER.getDesc())
                ;
        List<AppMenu> list = menuMapper.selectList(queryWrapper);
        return list;
    }
 
 
    public List<AppMenu> getSupplierPermissionMenu() {
        // 获取所有订单信息
        List<AppMenu> list = getAppMenus();
        // 遍历list,找到appMenu的permissionUq的值为supplier的菜单
        // 找到 permissionUq 为 supplier 的菜单
        List<AppMenu> supplierMenus = list.stream()
                .filter(menu -> Constants.DEFAULT_SUB_ACCOUNT_SUPPLIER.equals(menu.getPermissionUq()))
                .collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(supplierMenus)){
            AppMenu appMenu = supplierMenus.get(0);
 
            // 递归查找所有子菜单
            List<AppMenu> allChildMenus = findAllChildren(appMenu.getId(), list);
 
            return allChildMenus;
        }
 
        return new ArrayList<>();
    }
 
    public List<AppMenu> getPartnerPermissionMenu() {
        // 获取所有订单信息
        List<AppMenu> list = getAppMenus();
        // 遍历list,找到appMenu的permissionUq的值为supplier的菜单
        // 找到 permissionUq 为 supplier 的菜单
        List<AppMenu> supplierMenus = list.stream()
                .filter(menu -> Constants.DEFAULT_SUB_ACCOUNT_PARTNER.equals(menu.getPermissionUq()))
                .collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(supplierMenus)){
            AppMenu appMenu = supplierMenus.get(0);
 
            // 递归查找所有子菜单
            List<AppMenu> allChildMenus = findAllChildren(appMenu.getId(), list);
 
            return allChildMenus;
        }
 
        return new ArrayList<>();
    }
 
    /**
     * 递归查找所有子菜单
     *
     * @param parentId 父菜单 ID
     * @param allMenus 所有菜单列表
     * @return 所有子菜单
     */
    public static List<AppMenu> findAllChildren(String parentId, List<AppMenu> allMenus) {
        List<AppMenu> children = allMenus.stream()
                .filter(menu -> parentId.equals(menu.getParentId()))
                .collect(Collectors.toList());
 
        List<AppMenu> allChildren = new ArrayList<>(children);
        for (AppMenu child : children) {
            allChildren.addAll(findAllChildren(child.getId(), allMenus));
        }
 
        return allChildren;
    }
 
 
}