zqy
2024-07-07 780fa6d4016c6e616bbb4b3d29d33dbf3a40cbd6
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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.domain.dto.AuthorityDtoWithName;
import com.ruoyi.domain.dto.EmpowerDto;
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.*;
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();
 
    }
 
    @Override
    public AjaxResult getOtherAuthority(AuthorityDto 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 fid = myself.getFamilyId();
 
 
        //根据家庭的名字查出家庭的id
//        ZfFamily family = zfFamilyService.getByName(authorityDto.getFamilyName());
//        Long fid = family.getId();
 
        //得到要搜索的模块名字
        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).in(ZAuthority::getAuthority,allCodeList);
        List<ZAuthority> resultData = zAuthorityService.list(lqw);
 
 
        //拿到有关用户的id和对应的权限码数组
        HashMap<Long, ArrayList<Long>> userAuthMap = new HashMap<>();
        resultData.forEach(authority->{
           if(userAuthMap.get(authority.getUid()) == null){
               ArrayList<Long> codeList = new ArrayList<>();
               codeList.add(authority.getAuthority());
               userAuthMap.put(authority.getUid(),codeList);
           }else {
               userAuthMap.get(authority.getUid()).add(authority.getAuthority());
           }
        });
 
 
        List<AuthorityDtoWithName> returnData = new ArrayList<>();
 
        for (Long uid : userAuthMap.keySet()) {
            ArrayList<Long> authCodeList = userAuthMap.get(uid);
            List<String> authNameList = zfCodeService.getNameByCode(authCodeList);
 
            AuthorityDto oneAuth = new AuthorityDto();
            authNameList.forEach(name ->{
                if(name.contains("查看")){
                    oneAuth.setSearch(1);
                }else if(name.contains("删除")){
                    oneAuth.setDelete(1);
                }else if(name.contains("添加")){
                    oneAuth.setInsert(1);
                }else if(name.contains("修改")){
                    oneAuth.setUpdate(1);
                }
            });
            oneAuth.setModelName(modelName);
            oneAuth.setFamilyName(zfFamilyService.getById(fid).getName());
 
            AuthorityDtoWithName authorityDtoWithName = new AuthorityDtoWithName();
            authorityDtoWithName.setName(zInfoUserService.getInfoById(uid).getNickName());
            authorityDtoWithName.setUserId(uid);
            authorityDtoWithName.setAuthorityInfo(oneAuth);
            returnData.add(authorityDtoWithName);
        }
 
        return AjaxResult.success(returnData);
 
    }
 
    public void addData(ZAuthority za)
    {
        //判断是否重复授权
 
        //
        zAuthorityService.save(za);
 
    }
    public AjaxResult saveZa(EmpowerDto zAuthority)
    {
      //  boolean bl = zAuthorityService.saveOrUpdate(zAuthority);
        Long [] uids = zAuthority.getUids();
        Long [] authoritys =  zAuthority.getAuthoritys();
        for(Long uid : uids)
            for(Long auri: authoritys)
            {
                ZAuthority za = new ZAuthority();
                za.setAuthority(auri);
                za.setFid(zAuthority.getFid());
                za.setUid(uid);
                addData(za);
            }
      //  Long []
     //  if(bl)
         return AjaxResult.success("权限新增成功!");
       // else
        //    return  AjaxResult.error("权限新增失败!");
 
    }
 
    @Override
    public AjaxResult deleteZa(EmpowerDto empowerDto) {
        Long [] uids = empowerDto.getUids();
        Long [] authoritys =  empowerDto.getAuthoritys();
        for(Long uid : uids)
            for(Long auri: authoritys)
            {
 
                LambdaQueryWrapper<ZAuthority> lqw = new LambdaQueryWrapper<>();
                lqw.eq(ZAuthority::getFid,empowerDto.getFid())
                        .eq(ZAuthority::getAuthority, auri)
                        .eq(ZAuthority::getUid, uid);
 
                zAuthorityService.remove(lqw);
             //   addData(za);
            }
     return AjaxResult.success("权限收回成功!");
 
    }
 
    @Override
    public AjaxResult listByFidAid(ZAuthority zAuthority) {
        //找到对应家庭以及权限模块对应的用户
        LambdaQueryWrapper<ZAuthority> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZAuthority::getFid,zAuthority.getFid())
                .eq(ZAuthority::getAuthority, zAuthority.getAuthority());
 
        List<ZAuthority> authorityList = list(lqw);
 
        List<Long> allUserListId = authorityList.stream().map(ZAuthority::getUid).collect(Collectors.toList());//授权用户数组数组
 
 
        LambdaQueryWrapper<ZInfoUser> lq = new LambdaQueryWrapper<>();
        lq.in(ZInfoUser::getUserId, allUserListId);
 
        List<ZInfoUser> userInfo = zInfoUserService.list(lq);
      //  Map<Long, String> usi = userInfo.stream().collect(Collectors.toMap(ZInfoUser::getUserId,ZInfoUser::getNickName));
        return AjaxResult.success(userInfo);
    }
}