zqy
2024-07-25 c8a96c5e4ce5d5013a5ec508e2422f40ecdc311e
zhang-content/src/main/java/com/ruoyi/service/impl/ZfClanManageServiceImpl.java
@@ -1,28 +1,239 @@
package com.ruoyi.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.ZfClanManage;
import com.ruoyi.mapper.ZfClanManageMapper;
import com.ruoyi.service.ZfClanManageService;
import com.ruoyi.service.*;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class ZfClanManageServiceImpl extends ServiceImpl<ZfClanManageMapper, ZfClanManage> implements ZfClanManageService {
    @Resource
    private EsService esSer;
    @Resource
    ZInfoUserService zInfoUserService;
    @Resource
    private RestHighLevelClient restHighLevelClient;
    @Resource
    private ZfClanManageService zfClanManageService;
    @Resource
    private  ZfClanService zfClanService;
//    @Resource
//    private ISysUserService iSysUserService;
    private LambdaQueryWrapper<ZfClanManage> buildCondition(ZfClanManage zfClanManage) {
        LambdaQueryWrapper<ZfClanManage> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(ZfClanManage::getId);
        lqw.like(zfClanManage.getClanId() !=null , ZfClanManage::getClanId, zfClanManage.getClanId())
                .like(zfClanManage.getMemberId() !=null, ZfClanManage::getClanId, zfClanManage.getClanId());
        return lqw;
    }
    private LambdaQueryWrapper<ZfClanManage> uniqueCondition(ZfClanManage zfClanManage){
        LambdaQueryWrapper<ZfClanManage> lqw = new LambdaQueryWrapper<>();
        lqw.eq(zfClanManage.getClanId() !=null , ZfClanManage::getClanId, zfClanManage.getClanId());
        lqw.eq(zfClanManage.getMemberId() !=null, ZfClanManage::getMemberId, zfClanManage.getMemberId());
        return lqw;
    }
    /**
     *
     * 获取当前用户的sysUserId
     * @return
     */
    public Long getUserId(){
        ZInfoUser myself = zInfoUserService.getMyself();
        return zInfoUserService.getInfoBysysId( myself.getUserId()).getUserId();
    }
    @Override
    public AjaxResult selectDataList(ZfClanManage zfClanManage, Integer pageNum, Integer pageSize) {
        return null;
    public AjaxResult selectDataList(Integer clanId, Integer pageNo, Integer pageSize) {
        ZfClanManage zfClanManage = new ZfClanManage();
        zfClanManage.setClanId(clanId);
        LambdaQueryWrapper<ZfClanManage> lqw = buildCondition(zfClanManage);
        List<ZfClanManage> beanRecords = list(lqw);
        List<HashMap<Integer, String>> zInfoUsers = new ArrayList<>();
        for (ZfClanManage beanRecord:beanRecords) {
            HashMap<Integer,String> idAndName = new HashMap<>();
            String name = zInfoUserService.getById(Long.valueOf(beanRecord.getMemberId())).getNickName();
            idAndName.put(beanRecord.getMemberId(),name);
            zInfoUsers.add(idAndName);
        }
        List<HashMap<Integer, String>>  record =  zInfoUsers.stream().skip((pageNo-1)*pageSize).limit(pageSize).collect(Collectors.toList());
        int totalPage = (record.size() -1) / pageSize +1;
        Page<String> zInfoUserPage = new Page<>(pageNo, pageSize,totalPage);
        HashMap<String, Object> data = MapUtils.getShareResult(zInfoUserPage, record,zInfoUsers.size());
        return AjaxResult.success(data);
    }
    @Override
    public void addA(Integer clanId,Integer memberId,Integer roleId){
        ZfClanManage zfClanManage = new ZfClanManage();
        zfClanManage.setClanId(clanId);
        zfClanManage.setMemberId(memberId);
        zfClanManage.setRoleId(roleId);
        addData(zfClanManage);
    }
    @Override
    public AjaxResult addData(ZfClanManage zfClanManage) {
        return null;
//        LambdaQueryWrapper<ZfClanManage> lqw = uniqueCondition(zfClanManage);
//        List<ZfClanManage> list = list(lqw);
//        if(list.size()>0){
//            throw new RuntimeException("请勿重复添加同一成员");
//        }
//        if (!Long.valueOf(zfClanService.getById(zfClanManage.getClanId()).getAdminId()).equals(getUserId())) {
//            throw new RuntimeException("您不是管理员,没有权力添加成员");
//
//            }
        /**
         * 在用户信息表添加家族号
         */
        ZInfoUser zInfoUser =  zInfoUserService.getById(Long.valueOf(zfClanManage.getMemberId()));
        zInfoUser.setClanId(zfClanManage.getClanId());
        zInfoUserService.saveOrUpdate(zInfoUser);
        if (save(zfClanManage)) {
            EsModel esModel = new EsModel();
            Integer inte =zfClanManage.getClanId();
            String uuid = UUID.randomUUID().toString().replace("-","");
            esModel.setId(uuid);
            esModel.setCtId(Long.valueOf(inte));
            esModel.setCtTableName("家族管理");
            esModel.setBy10(zfClanManage.getMemberId());
            esModel.setBy5("/zfClanManage");
//            esModel.setFid(familyId);
            esSer.insertTable(esModel);
//            System.out.println(esModel);
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
    /**
     *通过id删除
     */
    @Override
    public AjaxResult deleteData(Long[] ids) {
        return null;
    public AjaxResult deleteData(Integer clanId,Integer id) {
        ZfClanManage zfClanManage1 = new ZfClanManage();
        zfClanManage1.setMemberId(id);
        zfClanManage1.setClanId(clanId);
        List<ZfClanManage> zfClanManages =list(buildCondition(zfClanManage1));
//        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("您不是管理员,没有权力删除成员");
//            }
        /**
         * 在用户信息表删除家族号
         */
        ZInfoUser zInfoUser =  zInfoUserService.getById(Long.valueOf(zfClanManages.get(0).getMemberId()));
        zInfoUser.setClanId(0);
        zInfoUserService.saveOrUpdate(zInfoUser);
        if (zfClanManageService.removeByIds(Arrays.asList(zfClanManages.get(0).getId()))) {
            //删除es中的数据
            zfClanManages.stream().forEach(zfClanManage -> {
                EsModel esModel = esSer.findByCtId((zfClanManage.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);
                    }
                }
            });
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
}
    /**
     *通过家族id删除
     */
    @Override
    public void deleteMember(Long ids) {
        LambdaQueryWrapper<ZfClanManage> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ZfClanManage::getClanId,ids);
        List<ZfClanManage> zfClanManages =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 (ZfClanManage zfClanManage: zfClanManages) {
            ZInfoUser zInfoUser =  zInfoUserService.getById(Long.valueOf(zfClanManage.getMemberId()));
            zInfoUser.setClanId(0);
            System.out.println(zInfoUser);
            zInfoUserService.saveOrUpdate(zInfoUser);
            id.add(zfClanManage.getId());
        }
        /**
         * 在用户信息表删除家族号
         */
        if (zfClanManageService.removeByIds(id)) {
            //删除es中的数据
            zfClanManages.stream().forEach(zfClanManage -> {
                EsModel esModel = esSer.findByCtId((zfClanManage.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);
                    }
                }
            });
        }}}