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.*; 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.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; @Service public class ZfShareDataServiceImpl extends ServiceImpl implements ZfShareDataService { @Resource private ZInfoUserService zInfoUserService; @Resource private RestHighLevelClient restHighLevelClient; @Resource private ZfClanService zfClanService; @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() { 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(); } 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 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(); } } @Override public AjaxResult setAdmin(Long userId,Long id) { List list = list(buildCondition(getSource(userId))); list.addAll(getShareId(userId)); for (ZfShareData zfShareData1:list){ zfShareData1.setMasterAccount(id.intValue()); updateById(zfShareData1); } return AjaxResult.success(); } @Override public AjaxResult allUser(Long id) { List list = list(buildCondition(getSource(id))); list.addAll(getShareId(id)); for (ZfShareData zfShareData1:list){ 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); } }