zqy
5 天以前 b02beccf4567068cb47a3f1181a00039456c872d
zhang-content/src/main/java/com/ruoyi/service/impl/ZAuthorityServiceImpl.java
@@ -2,20 +2,25 @@
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.ZfCode;
import com.ruoyi.domain.ZfFamily;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.*;
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.List;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -33,6 +38,12 @@
    @Resource
    private ZfCodeService zfCodeService;
    @Resource
    private ZAuthorityService zAuthorityService;
    @Resource
    private ZInfoUserService zInfoUserService;
    /**
     * 查询当前用户的权限
     */
@@ -40,9 +51,9 @@
    public List<ZAuthority> getAuthority() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        ZInfoUser zInfoUser = zInfoUserService.getInfoBysysId(userId);
        LambdaQueryWrapper<ZAuthority> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZAuthority::getUid,userId);
        lqw.eq(ZAuthority::getUid,zInfoUser.getUserId());
        return list(lqw);
    }
@@ -51,12 +62,13 @@
    public AuthorityDto getByCondition(AuthorityDto authorityDto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        Integer clanId = user.getClanId();
        String familyName = authorityDto.getFamilyName();
        String modelName = authorityDto.getModelName();
        //根据家庭的名字查出家庭的id
        Long familyId = zfFamilyService.getByName(familyName).getId();
      //  Integer clanId = 0;
        Long familyId = zfFamilyService.getByName(familyName, clanId).getId();
        //根据模块的名字查出对应的权限码
        List<ZfCode> zfCodeList = zfCodeService.likeGetByName(modelName);
@@ -103,4 +115,249 @@
        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);
    }
    private LambdaQueryWrapper<ZAuthority> uniqueCondition(ZAuthority zAuthority) {
        LambdaQueryWrapper<ZAuthority> lqw = new LambdaQueryWrapper<>();
        lqw.eq(zAuthority.getUid()!=null,ZAuthority::getUid,zAuthority.getUid());
        lqw.eq(zAuthority.getFid()!=null,ZAuthority::getFid,zAuthority.getFid());
        lqw.eq(zAuthority.getAuthority()!=null,ZAuthority::getAuthority,zAuthority.getAuthority());
        return lqw;
    }
    public void addData(ZAuthority za)
    {
        //判断是否重复授权
        LambdaQueryWrapper<ZAuthority> lqw = uniqueCondition(za);
        List<ZAuthority> list = list(lqw);
        //
        if(list.size()>0){
            throw new RuntimeException("请勿新增重复数据");
        }
        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);
                System.out.println(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);
                System.out.println("11111111111111111111"+list(lqw));
                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<>();
        List<ZInfoUser> userInfo = new ArrayList<>();
        if(allUserListId.size()!=0) {
            lq.in(ZInfoUser::getUserId, allUserListId);
            userInfo = zInfoUserService.list(lq);
        }
      //  Map<Long, String> usi = userInfo.stream().collect(Collectors.toMap(ZInfoUser::getUserId,ZInfoUser::getNickName));
        return AjaxResult.success(userInfo);
    }
}