whywhyo
2023-10-07 bfe6e8b654b3a90b0d95c4aebbef872418ebeaa6
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
package com.ruoyi.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.ZAuthority;
import com.ruoyi.domain.ZInfoUser;
import com.ruoyi.domain.ZfCode;
import com.ruoyi.domain.ZfFamily;
import com.ruoyi.domain.dto.AuthorityDto;
import com.ruoyi.domain.dto.AuthorityDto2;
import com.ruoyi.mapper.ZAuthorityMapper;
import com.ruoyi.service.ZAuthorityService;
import com.ruoyi.service.ZInfoUserService;
import com.ruoyi.service.ZfCodeService;
import com.ruoyi.service.ZfFamilyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @Author Jinquan_Ou
 * @Description
 * @Date 2023-07-15 13:23
 * @Version 1.0.0
 **/
@Service
public class ZAuthorityServiceImpl extends ServiceImpl<ZAuthorityMapper, ZAuthority> implements ZAuthorityService {
 
    @Resource
    private ZfFamilyService zfFamilyService;
 
    @Resource
    private ZfCodeService zfCodeService;
 
    @Resource
    private ZAuthorityService zAuthorityService;
 
    @Resource
    private ZInfoUserService zInfoUserService;
 
    /**
     * 查询当前用户的权限
     */
    @Override
    public List<ZAuthority> getAuthority() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
 
        LambdaQueryWrapper<ZAuthority> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZAuthority::getUid,userId);
 
        return list(lqw);
    }
 
    @Override
    public AuthorityDto getByCondition(AuthorityDto authorityDto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
 
        String familyName = authorityDto.getFamilyName();
        String modelName = authorityDto.getModelName();
 
        //根据家庭的名字查出家庭的id
        Long familyId = zfFamilyService.getByName(familyName).getId();
 
        //根据模块的名字查出对应的权限码
        List<ZfCode> zfCodeList = zfCodeService.likeGetByName(modelName);
        List<Long> allCodeList = zfCodeList.stream().map(ZfCode::getCode).collect(Collectors.toList());//权限码数组
 
 
        //找到对应家庭对应模块的权限数组
        LambdaQueryWrapper<ZAuthority> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZAuthority::getFid,familyId)
                .eq(ZAuthority::getUid,userId)
                .in(ZAuthority::getAuthority,allCodeList);
        List<ZAuthority> authorityList = list(lqw);
 
        List<Long> codeList = authorityList.stream().map(ZAuthority::getAuthority).collect(Collectors.toList());//真正拥有权限的权限码数组
        List<String> nameList = zfCodeService.getNameByCode(codeList);
 
        AuthorityDto resultData = new AuthorityDto();
        nameList.forEach(name ->{
            if(name.contains("查看")){
                resultData.setSearch(1);
            }else if(name.contains("删除")){
                resultData.setDelete(1);
            }else if(name.contains("添加")){
                resultData.setInsert(1);
            }else if(name.contains("修改")){
                resultData.setUpdate(1);
            }
        });
 
        resultData.setModelName(modelName);
        resultData.setFamilyName(familyName);
 
        return resultData;
 
 
 
    }
 
    @Override
    public List<String> getAuthorityFamilyName() {
        List<ZAuthority> authorityList = getAuthority();
        List<Long> familyIds = authorityList.stream().map(ZAuthority::getFid).distinct().collect(Collectors.toList());
        List<ZfFamily> familyList = zfFamilyService.listByIds(familyIds);
        return familyList.stream().map(ZfFamily::getName).distinct().collect(Collectors.toList());
    }
 
    /**
     *
     * @param authorityDto 传入了用户id、modelName
     * @return
     */
    @Override
    @Transactional
    public AjaxResult managerAuthority(AuthorityDto2 authorityDto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        ZInfoUser myself = zInfoUserService.getById(userId);
 
        //查看当前用户是不是管理员
        if(myself.getRoleId()!=1 && myself.getRoleId()!=2){
            throw new RuntimeException("你不是家庭管理员,没有权限操作");
        }
 
 
        Long uid = authorityDto.getUid();
        Long fid = myself.getFamilyId();
        String modelName = authorityDto.getModelName();
 
        //根据模块的名字查出对应的权限码
        List<ZfCode> zfCodeList = zfCodeService.likeGetByName(modelName);
        List<Long> allCodeList = zfCodeList.stream().map(ZfCode::getCode).collect(Collectors.toList());//权限码数组
 
        //找到对应家庭对应模块的权限数组
        LambdaQueryWrapper<ZAuthority> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZAuthority::getFid,fid)
                .eq(ZAuthority::getUid,uid)
                .in(ZAuthority::getAuthority,allCodeList);
        List<ZAuthority> authorityList = list(lqw);
 
        //先删掉现在所有的权限
        List<Long> idList = authorityList.stream().map(ZAuthority::getId).collect(Collectors.toList());
        zAuthorityService.removeBatchByIds(idList);
 
        //再添加新设置的权限
        if(authorityDto.getSearch() == 1){
            //根据模块的名字查到权限码,只会有一个结果
            List<ZfCode> zfcode = zfCodeService.likeGetByName(modelName + "查看");
            ZAuthority zAuthority = new ZAuthority();
            zAuthority.setFid(fid);
            zAuthority.setUid(uid);
            zAuthority.setAuthority(zfcode.get(0).getCode());
            zAuthorityService.save(zAuthority);
        }
 
        if(authorityDto.getInsert() == 1){
            //根据模块的名字查到权限码,只会有一个结果
            List<ZfCode> zfcode = zfCodeService.likeGetByName(modelName + "添加");
            ZAuthority zAuthority = new ZAuthority();
            zAuthority.setFid(fid);
            zAuthority.setUid(uid);
            zAuthority.setAuthority(zfcode.get(0).getCode());
            zAuthorityService.save(zAuthority);
        }
 
        if(authorityDto.getUpdate() == 1){
            //根据模块的名字查到权限码,只会有一个结果
            List<ZfCode> zfcode = zfCodeService.likeGetByName(modelName + "修改");
            ZAuthority zAuthority = new ZAuthority();
            zAuthority.setFid(fid);
            zAuthority.setUid(uid);
            zAuthority.setAuthority(zfcode.get(0).getCode());
            zAuthorityService.save(zAuthority);
        }
 
        if(authorityDto.getDelete() == 1){
            //根据模块的名字查到权限码,只会有一个结果
            List<ZfCode> zfcode = zfCodeService.likeGetByName(modelName + "删除");
            ZAuthority zAuthority = new ZAuthority();
            zAuthority.setFid(fid);
            zAuthority.setUid(uid);
            zAuthority.setAuthority(zfcode.get(0).getCode());
            zAuthorityService.save(zAuthority);
        }
 
        return AjaxResult.success();
 
    }
 
}