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 public class ZfAncestorServiceImpl extends ServiceImpl implements ZfAncestorService { @Resource private EsService esSer; @Resource private ZfAncestorMapper zfAncestorMapper; @Resource ZInfoUserService zInfoUserService; @Resource private RestHighLevelClient restHighLevelClient; @Resource private ZfAncestorService zfAncestorService; @Resource private ZfClanService zfClanService; private LambdaQueryWrapper buildCondition(ZfAncestor zfAncestor) { LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.orderByDesc(ZfAncestor::getId); lqw.eq(zfAncestor.getName()!=null, ZfAncestor::getName,zfAncestor.getName()); return lqw; } private LambdaQueryWrapper uniqueCondition(ZfAncestor zfAncestor){ LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(zfAncestor.getClanId() !=null , ZfAncestor::getClanId, zfAncestor.getClanId()); lqw.eq(zfAncestor.getSysId() !=null , ZfAncestor::getSysId, zfAncestor.getSysId()); return lqw; } /** * * 获取当前用户的sysUserId * @return */ public Long getUserId(){ ZInfoUser myself = zInfoUserService.getMyself(); return zInfoUserService.getInfoBysysId( myself.getUserId()).getUserId(); } @Override public boolean count(Integer clanId){ ZfAncestor zfAncestor = new ZfAncestor(); zfAncestor.setClanId(clanId); return list(buildCondition(zfAncestor)).size() < 2; } @Override public AjaxResult selectDataList(Integer clanId, Integer pageNo, Integer pageSize) { // ZfAncestor zfAncestor = new ZfAncestor(); // zfAncestor.setClanId(clanId); // LambdaQueryWrapper lqw = buildCondition(zfAncestor); // List beanRecords = list(lqw); // List> zInfoUsers = new ArrayList<>(); // // for (ZfAncestor beanRecord:beanRecords) { // HashMap idAndName = new HashMap<>(); // String name = zInfoUserService.getById(Long.valueOf(beanRecord.getMemberId())).getNickName(); // idAndName.put("名字",name); // idAndName.put("角色类型",String.valueOf(beanRecord.getRoleId())); // zInfoUsers.add(idAndName); // } // // List> record = zInfoUsers.stream().skip((pageNo-1)*pageSize).limit(pageSize).collect(Collectors.toList()); // // int totalPage = (record.size() -1) / pageSize +1; // // Page zInfoUserPage = new Page<>(pageNo, pageSize,totalPage); // HashMap data = MapUtils.getShareResult(zInfoUserPage, record,zInfoUsers.size()); // return AjaxResult.success(data); // List beanRecords = list(lqw); List zInfoUsers = zInfoUserService.selectByClanId(clanId); HashMap members = new HashMap<>(); for (ZInfoUser zInfoUser: zInfoUsers) { members.put(zInfoUser.getNickName(),zInfoUser.getRoleId()); } return AjaxResult.success(members); } @Override public AjaxResult selectDataList(ZfAncestor zfAncestor, Integer pageNo, Integer pageSize) { QueryWrapper 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 info = zfAncestorMapper.selectInfo(new Page<>(pageNo, pageSize),queryWrapper); //page(pa, wrapper); // List orders = page(pa, wrapper); List list =info.getRecords(); HashMap data = MapUtils.getResult(info, list); return AjaxResult.success(data); } @Override public Long addData(ZfAncestor zfAncestor) { // LambdaQueryWrapper lqw = buildCondition(zfAncestor); // List 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(); // } // // if (!Long.valueOf(zfClanService.getById(zfClanManage.getClanId()).getAdminId()).equals(getUserId())) { // throw new RuntimeException("您不是管理员,没有权力添加成员"); // // } /** * 在用户信息表添加家族号 */ // ZInfoUser zInfoUser = zInfoUserService.getInfoBysysId(zfAncestor.getSysId()); // zInfoUser.setClanId(zfAncestor.getClanId()); // 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); // System.out.println(esModel); return 1L; } /** *通过id删除 */ @Override public AjaxResult deleteData(Integer clanId,Integer id) { // ZfAncestor zfAncestor1 = new ZfAncestor(); // zfAncestor1.setMemberId(id); // zfAncestor1.setClanId(clanId); // List zfAncestors =list(buildCondition(zfAncestor1)); //// 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(zfAncestors.get(0).getMemberId())); // zInfoUser.setClanId(0); // zInfoUserService.saveOrUpdate(zInfoUser); // // if (zfAncestorService.removeByIds(Arrays.asList(zfAncestors.get(0).getId()))) { // // //删除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); // } // } // }); // // return AjaxResult.success(); // } else { // return AjaxResult.error(); // } return null; } @Override public ZfAncestor update(ZfAncestor zfAncestor) { LambdaQueryWrapper lqw = uniqueCondition(zfAncestor); List list = list(lqw); // 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()); 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())){ 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 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; } /** * *通过家族id删除 */ @Override public void deleteMember(Long ids) { LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(ZfAncestor::getClanId,ids); List 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 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); } } }); }}} }