cloudroam
2024-12-09 96abc87da07408ef817d9086ea71ba4c287aa474
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
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.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;
 
@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() {
 
        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);
 
        if (!CollectionUtils.isEmpty(list)) {
 
            List<AppMenu> allMenuList = menuMapper.selectList(
                    new QueryWrapper<AppMenu>().lambda().eq(AppMenu::getStatus, Constants.STATUS_ACTIVE));
 
            return allMenuList;
        }
 
        return null;
    }
 
 
 
}