whywhyo
2023-08-05 c3a3fd61c88e008fa7857b748c227be46e8a76e5
zhang-content/src/main/java/com/ruoyi/service/impl/ZInfoUserServiceImpl.java
@@ -15,21 +15,20 @@
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.domain.dto.*;
import com.ruoyi.mapper.ZInfoUserMapper;
import com.ruoyi.service.ZInfoUserService;
import com.ruoyi.service.ZfFamilyService;
import com.ruoyi.service.ZfRelationService;
import com.ruoyi.service.ZfRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -46,6 +45,15 @@
    @Autowired
    private ZfRelationService zfRelationService;
    @Autowired
    private ZInfoUserService zInfoUserService;
    @Resource
    private ZfFamilyService zfFamilyService;
    @Resource
    private ZfRoleService zfRoleService;
    @Override
    public AjaxResult selectInfoList(ZInfoUser zInfoUser, Integer pageNum, Integer pageSize) {
@@ -94,55 +102,24 @@
    }
    @Override
    public AjaxResult searchMyRelation(Long userId) {
    public AjaxResult searchMyRelation() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        LambdaQueryWrapper<ZfRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZfRelation::getMyId,userId);
        List<ZfRelation> relationList = zfRelationService.list(lqw);
        ArrayList<UserRelationDto> otherUserList = new ArrayList<>();
        relationList.stream().map(
                (relation)->{
                    Long otherId = relation.getOtherId();
                    ZInfoUser otherUser = getById(otherId);
                    UserRelationDto userRelationDto = new UserRelationDto();
                    BeanUtils.copyProperties(otherUser,userRelationDto);
                    userRelationDto.setRelation(relation.getRelation());
                    userRelationDto.setId(relation.getId());
                    otherUserList.add(userRelationDto);
                    return relation;
                }
        ).collect(Collectors.toList());
        return AjaxResult.success(otherUserList);
    }
    /**
     * 检查传入对方的id是否存在
     * @param zfRelation
     */
    private void checkRelation(ZfRelation zfRelation){
        LambdaQueryWrapper<ZInfoUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZInfoUser::getUserId, zfRelation.getOtherId());
        ZInfoUser zInfoUser2 = getOne(lqw);
        if (zInfoUser2==null){
            throw new RuntimeException("对方的id不存在");
        }
        List<ZfRelation> list = zfRelationService.list(lqw);
        return AjaxResult.success(list);
    }
    @Override
    @Transactional
    public AjaxResult addRelation(ZfRelation zfRelation) {
        checkRelation(zfRelation);
        //获取自己的id并传入
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        zfRelation.setMyId(userId);
        zfRelationService.save(zfRelation);
        return AjaxResult.success();
    }
@@ -150,105 +127,105 @@
    @Override
    @Transactional
    public AjaxResult updateRelation(ZfRelation zfRelation) {
        checkRelation(zfRelation);
        zfRelationService.updateById(zfRelation);
        return AjaxResult.success();
    }
    @Override
    public AjaxResult deleteRelation(Long[] ids) {
        boolean flag = zfRelationService.removeByIds(Arrays.asList(ids));
        if (flag){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
        zfRelationService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }
    /**
     * 给用户授权的方法
     * @param empowerDto
     * @return
     */
    @Override
    @Transactional
    public AjaxResult empower(EmpowerDto empowerDto) {
//    @Override
//    public AjaxResult empower(EmpowerDto empowerDto) {
//        return null;
//    }
        //得到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();
        }
    }
//    /**
//     * 给用户授权的方法
//     * @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) {
@@ -273,14 +250,14 @@
        }
    }
    private String getMyFamilyId() {
    private Long 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();
        Long familyId = zInfoUser.getFamilyId();
        return familyId;
    }
@@ -300,11 +277,65 @@
        List<ZInfoUser> allPeopleList = list();
        List<ZInfoUser> result = null;
//        try {
//              result = allPeopleList.stream().filter(people -> people.getUserId()!=1&&(people.getFatherId() == 0||people.getMomId()==0))
//                .map(people -> {
//                    if(depth>1){
//                        people.setIdentity(1);
//                        //根据配偶id查询配偶的数据,封装好一起返回
//                        Long spouseId = people.getSpouseId();
//                        ZInfoUser spouse = zInfoUserService.getById(spouseId);
//                        spouse.setIdentity(1);
//                        people.setSpouse(spouse);
//                        people.setChildList(fillChildren(people, allPeopleList, depth-1));
//                    }
//                    return people;
//                }).collect(Collectors.toList());
//        } catch (NullPointerException e) {
//            throw new RuntimeException("您在加入成员的时候没有指定该成员的父亲或者母亲");
//        }
        result = allPeopleList.stream().filter(people -> people.getUserId()!=1&&(people.getFatherId() == 0||people.getMomId()==0)&&people.getIsMyFamily()==1)
                .map(people -> {
                    if(depth>1){
                        people.setIdentity(1);
                        //根据配偶id查询配偶的数据,封装好一起返回
                        Long spouseId = people.getSpouseId();
                        ZInfoUser spouse = zInfoUserService.getById(spouseId);
                        spouse.setIdentity(1);
                        people.setSpouse(spouse);
                        people.setChildList(fillChildren(people, allPeopleList, depth-1));
                    }
                    return people;
                }).collect(Collectors.toList());
        return AjaxResult.success(result);
    }
    /**
     * 条件获取家庭成员
     * @return
     */
    public AjaxResult listWithTreeByCondition(Integer depth,Genealogy genealogy) {
        ZInfoUser zInfoUser = new ZInfoUser();
        BeanUtils.copyProperties(genealogy,zInfoUser);
        LambdaQueryWrapper<ZInfoUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(zInfoUser.getIdentity()!=null,ZInfoUser::getIdentity,zInfoUser.getIdentity());
        lqw.eq(zInfoUser.getSex()!=null,ZInfoUser::getSex,zInfoUser.getSex());
        lqw.like(StringUtils.isNotEmpty(zInfoUser.getNickName()),ZInfoUser::getNickName,zInfoUser.getNickName());
        lqw.eq(zInfoUser.getBirth()!=null,ZInfoUser::getBirth,zInfoUser.getBirth());
        List<ZInfoUser> allPeopleList = list(lqw);
        List<ZInfoUser> result = null;
        try {
            result = allPeopleList.stream().filter(people -> people.getUserId()!=1&&(people.getFatherId() == 0||people.getMomId()==0))
                    .map(people -> {
                        if(depth>1)
                        if(depth>1){
                            people.setIdentity(1);
                            people.setChildList(fillChildren(people, allPeopleList, depth-1));
                        }
                        return people;
                    }).collect(Collectors.toList());
        } catch (NullPointerException e) {
@@ -312,6 +343,157 @@
        }
        return AjaxResult.success(result);
    }
    private List<ZInfoUser> userList = new ArrayList<>();
    /**
     * 家谱信息
     * @param genealogy
     * @return
     */
    @Override
    public AjaxResult listGenealogy(Genealogy genealogy,Integer pageNum,Integer pageSize) {
        List<ZInfoUser> treeList= (List<ZInfoUser>) listWithTreeByCondition(100,genealogy).get("data");
        recursionFindUser(treeList);
        List<Genealogy> allData = userList.stream().map(
                zInfoUser -> {
                    Genealogy newGenealogy = new Genealogy();
                    BeanUtils.copyProperties(zInfoUser, newGenealogy);
                    if(zInfoUser.getSpouseId()!=null){
                        ZInfoUser spouse = getById(zInfoUser.getSpouseId());
                        newGenealogy.setSpouseName(spouse.getNickName());
                    }
                    return newGenealogy;
                }
        ).collect(Collectors.toList());
        List<Genealogy> collect = allData.stream().distinct().sorted(Comparator.comparing(Genealogy::getIdentity)).collect(Collectors.toList());
        List<Genealogy> resultData= new ArrayList<>();
        for (int i = (pageNum-1)*pageSize; i <pageNum*pageSize+pageSize; i++) {
            if(i>=collect.size()){
                break;
            }
            resultData.add(collect.get(i));
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("data",resultData);
        resultMap.put("total",collect.size());
        resultMap.put("pageNum",pageNum);
        resultMap.put("pageSize",pageSize);
        return AjaxResult.success(resultMap);
    }
    @Override
    public List<Genealogy> selectByIds(Long[] ids) {
        List<ZInfoUser> zInfoUsers = listByIds(Arrays.asList(ids));
        ArrayList<Genealogy> result = new ArrayList<>();
        for (ZInfoUser zInfoUser : zInfoUsers) {
            Genealogy genealogy = new Genealogy();
            BeanUtils.copyProperties(zInfoUser,genealogy);
            if(zInfoUser.getSpouseId()!=null){
                ZInfoUser spouse = getById(zInfoUser.getSpouseId());
                genealogy.setSpouseName(spouse.getNickName());
            }
            result.add(genealogy);
        }
        System.out.println("导出的数据为:"+result);
        return result;
    }
    @Override
    public List<Genealogy> selectDatas(List<GenealogyExportDto> params) {
        ArrayList<Long> ids = new ArrayList<>();
        for (GenealogyExportDto param : params) {
            ids.add(param.getId());
        }
        List<ZInfoUser> zInfoUsers = listByIds(ids);
        ArrayList<Genealogy> result = new ArrayList<>();
        for (int i = 0; i < zInfoUsers.size(); i++) {
            Genealogy genealogy = new Genealogy();
            BeanUtils.copyProperties(zInfoUsers.get(i),genealogy);
            genealogy.setIdentity(params.get(i).getIdentity());
            if(zInfoUsers.get(i).getSpouseId()!=null){
                ZInfoUser spouse = getById(zInfoUsers.get(i).getSpouseId());
                genealogy.setSpouseName(spouse.getNickName());
            }
            result.add(genealogy);
        }
        System.out.println("导出的数据为:"+result);
        return result;
    }
    @Override
    public ZInfoUser getInfoById(Long userId) {
        ZInfoUser user = getById(userId);
        Long familyId = user.getFamilyId();
        String familyName = zfFamilyService.getById(familyId).getName();
        String roleName = zfRoleService.getById(user.getRoleId()).getName();
        user.setFamilyName(familyName);
        user.setRoleName(roleName);
        return user;
    }
//    @Override
//    public ZInfoUser getInfoById(Long userId) {
//
//        ZInfoUser user = getById(userId);
//
//        //用逗号隔开的家庭id
//        String familyId = user.getFamilyId();
//
//        //在自己家庭中角色的id
//        Long roleId = user.getRoleId();
//
//        ArrayList<String> familyList = new ArrayList<>();
//
//        //填充所属家庭
//        if(familyId.contains(",")){
//            String[] familyIds = familyId.split(",");
//            for (String id : familyIds) {
//                familyList.add(zfFamilyService.getById(id).getName());
//            }
//        }else {
//            familyList.add(zfFamilyService.getById(familyId).getName());
//        }
//
//        user.setFamilyList(familyList);
//
//        //填充所在家庭的角色
//        user.setRole(zfRoleService.getById(roleId).getName());
//
//        return user;
//    }
    @Override
    public ZInfoUser getMyself() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long userId = user.getUserId();
        return getById(userId);
    }
    private void recursionFindUser(List<ZInfoUser> treeList){
        if (treeList==null||treeList.isEmpty()){
            return;
        }
        for (ZInfoUser zInfoUser : treeList) {
            userList.add(zInfoUser);
            if (!zInfoUser.getChildList().isEmpty()){
                recursionFindUser(zInfoUser.getChildList());
            }
        }
    }
    /**
@@ -360,6 +542,8 @@
        return AjaxResult.success(zInfoUsers);
    }
    /**
     * 递归算法
     * @param people
@@ -374,12 +558,20 @@
       // layer = layer + 1;
        System.out.println(depth);
        List<ZInfoUser> collect = allPeopleList.stream().filter(
                one -> one.getFatherId() == people.getUserId() || one.getMomId() == people.getUserId()
                one -> (one.getFatherId() == people.getUserId() || one.getMomId() == people.getUserId()) && (people.getIsMyFamily() == 1)
        ).map(
                one -> {
                    if(depth==1)
                        return one;
                    else {
                        one.setIdentity(people.getIdentity()+1);
                        //根据配偶id查询配偶的数据,封装好一起返回
                        Long spouseId = one.getSpouseId();
                        if(spouseId!=null){
                            ZInfoUser spouse = zInfoUserService.getById(spouseId);
                            spouse.setIdentity(people.getIdentity()+1);
                            one.setSpouse(spouse);
                        }
                        List<ZInfoUser> zinfo = fillChildren(one, allPeopleList, depth - 1);
                        one.setChildList(zinfo);
                        System.out.println(one);