wzy
2022-08-14 b141df9bf9764db8567efebed48006e12ab1f657
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
package com.xzx.gc.role.service;
 
import com.xzx.gc.entity.CoreMenu;
import com.xzx.gc.role.mapper.CoreRoleMenuMapper;
import com.xzx.gc.role.mapper.MenuConsoleMapper;
import com.xzx.gc.role.rbac.tree.MenuItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
 
@Service
@Transactional
public class MenuConsoleService {
    @Autowired
    MenuConsoleMapper menuDao;
    @Autowired
    CoreRoleMenuMapper roleMenuDao;
 
    @Autowired
    CorePlatformService platformService;
    public Long saveMenu(CoreMenu menu) {
        CoreMenu query = new CoreMenu();
        query.setCode(menu.getCode());
        long queryCount = menuDao.selectCount(query);
        if (queryCount > 0) {
            //throw new PlatformException("菜单编码已存在");
            return -1L;
        }
        menuDao.insertUseGeneratedKeys(menu);
        platformService.clearMenuCache();
        return menu.getId();
    }
 
    public void deleteMenu(Long menuId) {
        deleteMenuId(menuId);
    }
 
    public void batchDeleteMenuId(List<Long> menuIds) {
        for (Long id : menuIds) {
            deleteMenuId(id);
        }
        platformService.clearMenuCache();
    }
 
    public void updateMenu(CoreMenu menu) {
        menuDao.updateByPrimaryKey(menu);
        platformService.clearMenuCache();
    }
 
    public CoreMenu getMenu(Long menuId) {
        CoreMenu menu = menuDao.selectByPrimaryKey(menuId);
        platformService.clearMenuCache();
        return menu;
    }
 
 
    private void deleteMenuId(Long menuId) {
        MenuItem root = platformService.buildMenu();
        MenuItem fun = root.findChild(menuId);
        if(fun!=null){
            List<MenuItem> all = fun.findAllItem();
            //也删除自身
            all.add(fun);
            realDeleteMenu(all);
        }else{
            this.menuDao.deleteByPrimaryKey(menuId);
        }
 
 
    }
 
    private void realDeleteMenu(List<MenuItem> all) {
        List<Long> ids = new ArrayList<>(all.size());
        for (MenuItem item : all) {
            ids.add(item.getId());
            this.menuDao.deleteByPrimaryKey(item.getId());
        }
        //删除角色和菜单的关系
        roleMenuDao.deleteRoleMenu(ids);
 
    }
 
    public CoreMenu queryById(Long id) {
        return menuDao.selectByPrimaryKey(id);
    }
}