package com.ruoyi.service.impl; 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.EsModel; import com.ruoyi.domain.*; import com.ruoyi.mapper.ZfShareDataMapper; import com.ruoyi.service.EsService; import com.ruoyi.service.ZInfoUserService; import com.ruoyi.service.ZfClanService; import com.ruoyi.service.ZfShareDataService; 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 ZfShareDataServiceImpl extends ServiceImpl implements ZfShareDataService { @Resource private ZInfoUserService zInfoUserService; @Resource private RestHighLevelClient restHighLevelClient; @Resource private EsService esSer; private LambdaQueryWrapper buildCondition(Long userId) { LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.orderByDesc(ZfShareData::getId); lqw.eq(ZfShareData::getUserId,userId); return lqw; } private LambdaQueryWrapper uniqueCondition(ZfShareData zfShareData) { LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.orderByDesc(ZfShareData::getId); lqw.eq(ZfShareData::getSharedId,zfShareData.getSharedId()); return lqw; } @Override public List selectList(ZfShareData zfShareData) { LambdaQueryWrapper lqw = buildCondition(getUserId()); return list(lqw); } @Override public List selectByIds(Long[] ids) { List list = new ArrayList<>(); if(ids.length!=0) list = listByIds(Arrays.asList(ids)); else list = list(); return list; } public Long getUserId(){ ZInfoUser myself = zInfoUserService.getMyself(); return myself.getUserId(); } @Override public Long getSource(Long userId){ LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.orderByDesc(ZfShareData::getId); lqw.eq(ZfShareData::getSharedId,userId); if (list(lqw).size() == 0){ return userId; } return getSource(list(lqw).get(0).getUserId().longValue()); } @Override public Long getEnd(Long userId){ LambdaQueryWrapper lqw1 = new LambdaQueryWrapper<>(); lqw1.eq(ZfShareData::getUserId,userId); if (list(lqw1).size() == 0){ return userId; } return getEnd(list(lqw1).get(list(lqw1).size()-1).getSharedId().longValue()); } @Override public AjaxResult addData(Long[] userIds) { for (Long userId : userIds) { ZfShareData zfShareData = new ZfShareData(); zfShareData.setUserId(getUserId().intValue()); zfShareData.setSharedId(userId.intValue()); zfShareData.setMasterAccount(0); for(ZfShareData zfShareData1:list(buildCondition(getUserId()))){ System.out.println(zfShareData1); } if (list(uniqueCondition(zfShareData)).size() == 0) save(zfShareData); else return AjaxResult.error("请勿重复共享"); } return AjaxResult.success("数据共享成功"); } @Override public AjaxResult deleteData(Long[] ids) { List meetings = listByIds(Arrays.asList(ids)); if (removeByIds(Arrays.asList(ids))) { //删除es中的数据 meetings.forEach(meeting -> { EsModel esModel = esSer.findByCtId((meeting.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(); } } private LambdaQueryWrapper selectByUserId(List userId) { LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.in(ZfShareData::getSharedId,userId); return lqw; } private LambdaQueryWrapper selectBySharedId(List userId) { LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.in(ZfShareData::getUserId,userId); return lqw; } @Override public List getAllPerson(List userIds){ List lists = new ArrayList<>(list(selectByUserId(userIds))); lists.addAll(list(selectBySharedId(userIds))); if (userIds.contains(getEnd(getSource(userIds.get(0))))){ return userIds; } return getAllPerson(lists.stream().map(ZfShareData::getSharedId).map(Integer::longValue).collect(Collectors.toList())); } @Override public AjaxResult setAdmin(Long userId,Long id) { Long source = getSource(userId); List all = getAllPerson(Collections.singletonList(source)); List collect = all.stream().distinct().collect(Collectors.toList()); collect.add(source); List list = list(selectByUserId(collect)); list.addAll(list(selectBySharedId(collect))); List collect1 = list.stream().distinct().collect(Collectors.toList()); for (ZfShareData zfShareData1:collect1){ zfShareData1.setMasterAccount(id.intValue()); updateById(zfShareData1); } return AjaxResult.success(); } @Override public AjaxResult allUser(Long id) { Long source = getSource(id); List all = getAllPerson(Collections.singletonList(source)); List collect = all.stream().distinct().collect(Collectors.toList()); collect.add(source); List list = list(selectByUserId(collect)); list.addAll(list(selectBySharedId(collect))); List collect1 = list.stream().distinct().collect(Collectors.toList()); for (ZfShareData zfShareData1:collect1){ zfShareData1.setMasterAccount(0); updateById(zfShareData1); } return AjaxResult.success(); } @Override public List getShareId(Long id) { LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.orderByDesc(ZfShareData::getId); lqw.eq(ZfShareData::getSharedId,id); return list(lqw); } @Override public List getByShareId(List id) { LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.orderByDesc(ZfShareData::getId); lqw.in(ZfShareData::getSharedId,id); return list(lqw); } }