jinquanOu
2024-06-11 5d7b0a0bbcae6844e2296ef53c3f4c88293dacfe
zhang-content/src/main/java/com/ruoyi/service/impl/ZAuthorityServiceImpl.java
@@ -11,6 +11,8 @@
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;
@@ -20,7 +22,7 @@
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -196,4 +198,151 @@
    }
    @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);
    }
}