kaiyu
2021-03-25 247f19f6c3f17c2ac2f37b55c7d0550731f31ffe
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
package com.moral.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.ManageRoleMenu;
import com.moral.api.mapper.ManageRoleMenuMapper;
import com.moral.api.service.ManageRoleMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.ResponseCodeEnum;
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.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 后台菜单角色关系表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-03-09
 */
@Service
public class ManageRoleMenuServiceImpl extends ServiceImpl<ManageRoleMenuMapper, ManageRoleMenu> implements ManageRoleMenuService {
 
    @Autowired(required = false)
    private ManageRoleMenuMapper manageRoleMenuMapper;
 
    @Override
    @Transactional
    public Map<String, Object> updateRoleMenu(List list, int id) {
        Map<String,Object> resultMap = new HashMap<>();
        if (list.size()==0){
            resultMap.put("code",ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode());
            resultMap.put("msg",ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            return resultMap;
        }
        QueryWrapper<ManageRoleMenu> wrapper = new QueryWrapper();
        wrapper.eq("role_id",id);
        wrapper.eq("is_delete",0);
        List<Map> result = manageRoleMenuMapper.getDataByMenuIds(list,id);
        List<Map> result1 = manageRoleMenuMapper.getDataByRoleId(id);
        if (manageRoleMenuMapper.selectCount(wrapper)==0 || result.size()==0){
            List<Map> insertList = new ArrayList<>();
            for (Object temp:list) {
                Map<String,Object> map = new HashMap<>();
                map.put("role_id",id);
                map.put("menu_id",Integer.parseInt(temp.toString()));
                insertList.add(map);
            }
            manageRoleMenuMapper.insertBatch(insertList);
        }
        List updateList = new ArrayList();
        for (Map manageRoleMenu:result1) {
            int have = 1;
            for (Object temp:list){
                if (manageRoleMenu.get("menu_id").equals(Integer.parseInt(temp.toString()))){
                    have = 2;
                    continue;
                }
            }
            if (have == 2){
                continue;
            }
            updateList.add(manageRoleMenu.get("menu_id"));
        }
        List insertList = new ArrayList();
        for (Object temp:list) {
            int have = 1;
            for (Map manageRoleMenu:result1){
                if (manageRoleMenu.get("menu_id").equals(Integer.parseInt(temp.toString()))){
                    have = 2;
                    continue;
                }
            }
            if (have == 2){
                continue;
            }
            insertList.add(temp);
        }
        if (updateList.size()>0){
            manageRoleMenuMapper.updateDeleteStateByRoleIdMenuIds(updateList,id);
        }
        if (insertList.size()>0){
            List mapList = new ArrayList();
            for (Object temp:insertList) {
                Map<String,Integer> insertMap = new HashMap<>();
                insertMap.put("role_id",id);
                insertMap.put("menu_id",Integer.parseInt(temp.toString()));
                mapList.add(insertMap);
            }
            manageRoleMenuMapper.insertBatch(mapList);
        }
        resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode());
        resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg());
        return resultMap;
    }
}