feige
2024-08-22 5e2ebcceae4bc34b46370f2c10110cd72e7f4fba
zhang-content/src/main/java/com/ruoyi/service/impl/ZfAncestorServiceImpl.java
@@ -1,19 +1,30 @@
package com.ruoyi.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.EsModel;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.domain.ZInfoUser;
import com.ruoyi.domain.ZfAncestor;
import com.ruoyi.domain.ZfClan;
import com.ruoyi.domain.dto.AncestorClan;
import com.ruoyi.mapper.ZfAncestorMapper;
import com.ruoyi.service.*;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
@Service
@@ -22,6 +33,8 @@
    @Resource
    private EsService esSer;
    @Resource
    private ZfAncestorMapper zfAncestorMapper;
    @Resource
    ZInfoUserService zInfoUserService;
@@ -38,7 +51,7 @@
    private LambdaQueryWrapper<ZfAncestor> buildCondition(ZfAncestor zfAncestor) {
        LambdaQueryWrapper<ZfAncestor> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(ZfAncestor::getId);
        lqw.eq(zfAncestor.getClanId() !=null , ZfAncestor::getClanId, zfAncestor.getClanId());
        lqw.eq(zfAncestor.getName()!=null, ZfAncestor::getName,zfAncestor.getName());
        return lqw;
    }
@@ -103,26 +116,41 @@
        return AjaxResult.success(members);
    }
    @Override
    public AjaxResult selectDataList(ZfAncestor zfAncestor, Integer pageNo, Integer pageSize) {
        QueryWrapper<Object> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(zfAncestor.getClanId() !=null , " zf_ancestor.clan_id", zfAncestor.getClanId());
        queryWrapper.eq(zfAncestor.getSysId() !=null , " zf_ancestor.sys_id ", zfAncestor.getSysId());
        Page<AncestorClan> info = zfAncestorMapper.selectInfo(new Page<>(pageNo, pageSize),queryWrapper);
        //page(pa, wrapper);
     //   List<AncestorClan> orders = page(pa, wrapper);
        List<AncestorClan>  list =info.getRecords();
        HashMap<String, Object> data = MapUtils.getResult(info, list);
        return AjaxResult.success(data);
    }
    @Override
    public Long addData(ZfAncestor zfAncestor) {
        LambdaQueryWrapper<ZfAncestor> lqw = buildCondition(zfAncestor);
        List<ZfAncestor> list = list(lqw);
        Long sysID = 0L;
        if(list.size()>2){
            throw new RuntimeException("请勿重复添加祖先");
        }
        else if (list.size() == 1){
            if (list.get(0).getSex().equals(zfAncestor.getSex())){
                throw new RuntimeException("两位祖先不能为同性");
            }
//        LambdaQueryWrapper<ZfAncestor> lqw = buildCondition(zfAncestor);
//        List<ZfAncestor> list = list(lqw);
//        Long sysID = 0L;
//        System.out.println("15dniwru/"+list);
//        if(list.size()>0){
//            throw new RuntimeException("请勿重复添加祖先");
//        }
//        else if (list.size() == 1){
//            if (list.get(0).getSex().equals(zfAncestor.getSex())){
//                throw new RuntimeException("两位祖先不能为同性");
//            }
            sysID = zInfoUserService.getInfoBysysId(list.get(0).getSysId()).getUserId();
        }
          //  sysID = zInfoUserService.getInfoBysysId(list.get(0).getSysId()).getUserId();
//        }
//
//        if (!Long.valueOf(zfClanService.getById(zfClanManage.getClanId()).getAdminId()).equals(getUserId())) {
//            throw new RuntimeException("您不是管理员,没有权力添加成员");
@@ -138,20 +166,20 @@
//        zInfoUserService.saveOrUpdate(zInfoUser);
        save(zfAncestor);
            EsModel esModel = new EsModel();
            Integer inte = zfAncestor.getClanId();
            String uuid = UUID.randomUUID().toString().replace("-","");
            esModel.setId(uuid);
            esModel.setCtId(Long.valueOf(inte));
            esModel.setCtTableName("家族管理");
            esModel.setBy1(zfAncestor.getName());
            esModel.setBy5("/zfClanManage");
//            esModel.setFid(familyId);
            esSer.insertTable(esModel);
//            EsModel esModel = new EsModel();
//            Integer inte = zfAncestor.getClanId();
//            String uuid = UUID.randomUUID().toString().replace("-","");
//            esModel.setId(uuid);
//            esModel.setCtId(Long.valueOf(inte));
//            esModel.setCtTableName("家族管理");
//            esModel.setBy1(zfAncestor.getName());
//
//            esModel.setBy5("/zfClanManage");
////            esModel.setFid(familyId);
//            esSer.insertTable(esModel);
//            System.out.println(esModel);
            return sysID;
            return 1L;
    }
@@ -203,22 +231,47 @@
    }
    @Override
    public void update(ZfAncestor zfAncestor) {
    public ZfAncestor update(ZfAncestor zfAncestor) {
        LambdaQueryWrapper<ZfAncestor> lqw = uniqueCondition(zfAncestor);
        List<ZfAncestor> list = list(lqw);
        System.out.println(list);
//        System.out.println(list);
        ZInfoUser updateDate =zInfoUserService.getInfoBysysId(zfAncestor.getSysId());
        ZfAncestor zfAncestor1 = new ZfAncestor();
        ZfAncestor zfAncestor2 = new ZfAncestor();
        zfAncestor1.setId(list.get(0).getId());
        zfAncestor1.setName(zfAncestor.getName());
        if((updateDate.getSpouseId() != null || updateDate.getSpouseId() != 0) && zfAncestor.getSex() !=null){
        System.out.println(updateDate);
        if (zfAncestor.getSex() != null){
        if(updateDate.getSpouseId() != null && updateDate.getSpouseId() != 0){
            Long spouseId=updateDate.getSpouseId();
            if (zInfoUserService.getById(spouseId).getSex().equals(zfAncestor.getSex())){
                throw new RuntimeException("祖先两位不能为同性");}
            zfAncestor.setSex(zfAncestor.getSex());}
                Long spouseSysId = zInfoUserService.getById(spouseId).getSysId();
                zfAncestor2.setSysId(spouseSysId);
                Integer spId = list(uniqueCondition(zfAncestor2)).get(0).getId();
                zfAncestor2.setId(spId);
                zfAncestor2.setSex(zfAncestor.getSex() == 0? 1: 0);
                System.out.println(zfAncestor2);
                zfAncestorService.updateById(zfAncestor2);
            }}
            zfAncestor1.setSex(zfAncestor.getSex());}
//        System.out.println("1841vdndigrrr"+zfAncestor1);
            zfAncestorService.updateById(zfAncestor1);
        return zfAncestor2;
    }
    @Override
    public ZfAncestor selectZfAncestor(ZfAncestor zfAncestor) {
        LambdaQueryWrapper<ZfAncestor>  queryWraper = new LambdaQueryWrapper<>();
        queryWraper.eq(zfAncestor.getSysId() !=null , ZfAncestor::getSysId, zfAncestor.getSysId());
        List lis = this.list(queryWraper);
        if(lis.size()>0)
            return (ZfAncestor) lis.get(0);
        else
            return null;
    }
    /**
@@ -227,47 +280,45 @@
     */
    @Override
    public void deleteMember(Long ids) {
//        LambdaQueryWrapper<ZfAncestor> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(ZfAncestor::getClanId,ids);
//        List<ZfAncestor> zfAncestors =list(lqw) ;
////        for (ZfClanManage zfClanManage : zfClanManages) {
////            if (!zfClanManages.getAdminId().equals(getUserId())) {
////                throw new RuntimeException("您不是管理员,没有权力删除该家族");
////            }
////        }
//
////        if (!Long.valueOf(zfClanService.getById(zfClanManages.get(0).getClanId()).getAdminId()).equals(getUserId())) {
////            throw new RuntimeException("您不是管理员,没有权力删除成员");
////        }
//        List<Integer> id = new ArrayList<>() ;
//        for (ZfAncestor zfAncestor : zfAncestors) {
//            ZInfoUser zInfoUser =  zInfoUserService.getById(Long.valueOf(zfAncestor.getMemberId()));
//            zInfoUser.setClanId(0);
//            System.out.println(zInfoUser);
//            zInfoUserService.saveOrUpdate(zInfoUser);
//            id.add(zfAncestor.getId());
        LambdaQueryWrapper<ZfAncestor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZfAncestor::getClanId,ids);
        List<ZfAncestor> zfAncestors =list(lqw) ;
//        for (ZfClanManage zfClanManage : zfClanManages) {
//            if (!zfClanManages.getAdminId().equals(getUserId())) {
//                throw new RuntimeException("您不是管理员,没有权力删除该家族");
//            }
//        }
        }
//        if (!Long.valueOf(zfClanService.getById(zfClanManages.get(0).getClanId()).getAdminId()).equals(getUserId())) {
//            throw new RuntimeException("您不是管理员,没有权力删除成员");
//        }
        List<Integer> id = new ArrayList<>() ;
        for (ZfAncestor zfAncestor : zfAncestors) {
            ZInfoUser zInfoUser =  zInfoUserService.getInfoBysysId(zfAncestor.getSysId());
            zInfoUser.setClanId(0);
            zInfoUserService.saveOrUpdate(zInfoUser);
            id.add(zfAncestor.getId());
//
//        }
        /**
         * 在用户信息表删除家族号
         */
        if (zfAncestorService.removeByIds(id)) {
            //删除es中的数据
            zfAncestors.stream().forEach(zfAncestor1 -> {
                EsModel esModel = esSer.findByCtId((zfAncestor1.getId().intValue()), "家族管理");
                if (esModel != null) {
                    DeleteRequest deleteRequest = new DeleteRequest("allsearchdata", esModel.getId());
                    try {
                        restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
//        if (zfAncestorService.removeByIds(id)) {
//            //删除es中的数据
//            zfAncestors.stream().forEach(zfAncestor -> {
//                EsModel esModel = esSer.findByCtId((zfAncestor.getId().intValue()), "家族管理");
//                if (esModel != null) {
//                    DeleteRequest deleteRequest = new DeleteRequest("allsearchdata", esModel.getId());
//                    try {
//                        restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
//                    } catch (IOException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//            });
//
//
//        }}
        }}}
        }