Jinquan_Ou
2023-04-13 9aff69d38fa75981560ce21807be4b30122ef8f4
zhang-content/src/main/java/com/ruoyi/service/impl/ZInfoUserServiceImpl.java
@@ -3,6 +3,8 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
@@ -13,6 +15,7 @@
import com.ruoyi.domain.ZInfoUser;
import com.ruoyi.domain.ZfEvent;
import com.ruoyi.domain.ZfRelation;
import com.ruoyi.domain.dto.EmpowerDto;
import com.ruoyi.domain.dto.UserRelationDto;
import com.ruoyi.mapper.ZInfoUserMapper;
import com.ruoyi.service.ZInfoUserService;
@@ -162,5 +165,131 @@
        }
    }
    /**
     * 给用户授权的方法
     * @param empowerDto
     * @return
     */
    @Override
    @Transactional
    public AjaxResult empower(EmpowerDto empowerDto) {
        //得到dto中的信息
        String otherUserId = empowerDto.getUserId();
        String destinationFamilyId = empowerDto.getFamilyId();
        List<String> authorityList = empowerDto.getAuthorityList();
        String familyId = getMyFamilyId();
        checkAuthorization(familyId, destinationFamilyId,true);
        //授权  //3{2007 1988 1004}
        String finalStr = getFinalStr(destinationFamilyId, authorityList);
        //更新被授权用户的权限
        LambdaQueryWrapper<ZInfoUser> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(ZInfoUser::getUserId,otherUserId);
        ZInfoUser otherUser = getOne(lqw2);
        String secondFamilyId = otherUser.getSecondFamilyId();
        String replaceFamilyId=null;
        boolean save=true;
        if (StringUtils.isNotEmpty(secondFamilyId)){
            //如果原来就已经有被赋予权限了,那么需要判断是不是跟这个家庭号有关的权限
            //这个secondFamilyId是当前用户的完整权限字段
            if(secondFamilyId.contains(",")){
                String[] split = secondFamilyId.split(",");
                for (int i = 0; i < split.length; i++) {
                    String fid = split[i].substring(0, split[i].indexOf("{"));//从权限块中拿到被授权的家庭号
                    if(fid.equals(finalStr.substring(0,finalStr.indexOf("{")))){
                        //如果匹配到了对应的家庭号,那么就替换掉对应的权限块就可以了,不需要追加
                        save=false;
                        split[i]=finalStr;
                        break;
                    }
                }
                List<String> finalList = Arrays.asList(split);
                StringBuilder stringBuilder = new StringBuilder();
                for (String s : finalList) {
                    stringBuilder.append(s).append(",");
                }
                stringBuilder.deleteCharAt(stringBuilder.length()-1);
                replaceFamilyId = stringBuilder.toString();
            }else {
                String fid = secondFamilyId.substring(0, secondFamilyId.indexOf("{"));//从权限块中拿到被授权的家庭号
                if(fid.equals(finalStr.substring(0,finalStr.indexOf("{")))){
                    //如果匹配到了对应的家庭号,那么就替换掉对应的权限块就可以了,不需要追加
                    save=false;
                    replaceFamilyId=finalStr;
                }
            }
            if(save){
                //需要追加,没有匹配到对应的家庭号
                replaceFamilyId=secondFamilyId+","+finalStr;
            }else {
                //不需要追加
            }
        }else {
            //原来就没有权限了
            replaceFamilyId=finalStr;
        }
        LambdaUpdateWrapper<ZInfoUser> uw = new LambdaUpdateWrapper<>();
        uw.eq(ZInfoUser::getUserId,otherUserId).set(ZInfoUser::getSecondFamilyId,replaceFamilyId);
        if (update(null,uw)) {
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
    private void checkAuthorization(String familyId, String destinationFamilyId,boolean flag) {
        String text=null;
        if(flag){
            text="授权";
        }else {
            text="取消授权";
        }
        //对本人的家庭号进行处理
        if(familyId.contains(",")){
            String[] familyIds = familyId.split(",");
            List<String> idList = Arrays.asList(familyIds);
            if (!idList.contains(destinationFamilyId)) {
                throw new RuntimeException("不能"+text+"不属于自己家庭的数据");
            }
        }else {
            if (!familyId.equals(destinationFamilyId)){
                throw new RuntimeException("不能"+text+"不属于自己家庭的数据");
            }
        }
    }
    private String getMyFamilyId() {
        //得到自己的家庭id信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long myUserId = user.getUserId();
        LambdaQueryWrapper<ZInfoUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZInfoUser::getUserId,myUserId);
        ZInfoUser zInfoUser = getOne(lqw);
        String familyId = zInfoUser.getFamilyId();
        return familyId;
    }
    private String getFinalStr(String destinationFamilyId, List<String> authorityList) {
        String authorityListStr = authorityList.stream().collect(Collectors.joining(" ", "{", "}"));
        String finalStr= destinationFamilyId +authorityListStr;  //3{2007 1988 1004}
        return finalStr;
    }
}