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.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 implements ZAuthorityService { @Resource private ZfFamilyService zfFamilyService; @Resource private ZfCodeService zfCodeService; @Resource private ZAuthorityService zAuthorityService; @Resource private ZInfoUserService zInfoUserService; /** * 查询当前用户的权限 */ @Override public List getAuthority() { SysUser user = SecurityUtils.getLoginUser().getUser(); Long userId = user.getUserId(); LambdaQueryWrapper 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 zfCodeList = zfCodeService.likeGetByName(modelName); List allCodeList = zfCodeList.stream().map(ZfCode::getCode).collect(Collectors.toList());//权限码数组 //找到对应家庭对应模块的权限数组 LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(ZAuthority::getFid,familyId) .eq(ZAuthority::getUid,userId) .in(ZAuthority::getAuthority,allCodeList); List authorityList = list(lqw); List codeList = authorityList.stream().map(ZAuthority::getAuthority).collect(Collectors.toList());//真正拥有权限的权限码数组 List 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 getAuthorityFamilyName() { List authorityList = getAuthority(); List familyIds = authorityList.stream().map(ZAuthority::getFid).distinct().collect(Collectors.toList()); List 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 zfCodeList = zfCodeService.likeGetByName(modelName); List allCodeList = zfCodeList.stream().map(ZfCode::getCode).collect(Collectors.toList());//权限码数组 //找到对应家庭对应模块的权限数组 LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(ZAuthority::getFid,fid) .eq(ZAuthority::getUid,uid) .in(ZAuthority::getAuthority,allCodeList); List authorityList = list(lqw); //先删掉现在所有的权限 List idList = authorityList.stream().map(ZAuthority::getId).collect(Collectors.toList()); zAuthorityService.removeBatchByIds(idList); //再添加新设置的权限 if(authorityDto.getSearch() == 1){ //根据模块的名字查到权限码,只会有一个结果 List 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 = 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 = 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 = 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 zfCodeList = zfCodeService.likeGetByName(modelName); List allCodeList = zfCodeList.stream().map(ZfCode::getCode).collect(Collectors.toList());//权限码数组 //查找对应家庭和对应模块的数据 LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(ZAuthority::getFid,fid).in(ZAuthority::getAuthority,allCodeList); List resultData = zAuthorityService.list(lqw); //拿到有关用户的id和对应的权限码数组 HashMap> userAuthMap = new HashMap<>(); resultData.forEach(authority->{ if(userAuthMap.get(authority.getUid()) == null){ ArrayList codeList = new ArrayList<>(); codeList.add(authority.getAuthority()); userAuthMap.put(authority.getUid(),codeList); }else { userAuthMap.get(authority.getUid()).add(authority.getAuthority()); } }); List returnData = new ArrayList<>(); for (Long uid : userAuthMap.keySet()) { ArrayList authCodeList = userAuthMap.get(uid); List 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); } }