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.core.domain.entity.SysUser;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.*;
import com.ruoyi.mapper.ZSelfNoteMapper;
import com.ruoyi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 服务实现类
*
*
* @author ojq
* @since 2023-03-14
*/
@Service
public class ZSelfNoteServiceImpl extends ServiceImpl implements ZSelfNoteService {
@Autowired
ZSelfNoteService zSelfNoteService;
@Resource
ZfShareDataService zfShareDataService;
@Resource
private EsService esSer;
@Resource
private ZInfoUserService zInfoUserService;
@Resource
private ZfClanService zfClanService;
private LambdaQueryWrapper uniqueCondition(ZSelfNote zSelfNote) {
LambdaQueryWrapper lqw = new LambdaQueryWrapper<>();
lqw.eq(StringUtils.isNotEmpty(zSelfNote.getAddress()), ZSelfNote::getAddress, zSelfNote.getAddress())
.eq(StringUtils.isNotEmpty(zSelfNote.getPeople()), ZSelfNote::getPeople, zSelfNote.getPeople())
.eq(zSelfNote.getHappenTime() != null, ZSelfNote::getHappenTime, zSelfNote.getHappenTime())
.eq(StringUtils.isNotEmpty(zSelfNote.getTitle()), ZSelfNote::getTitle, zSelfNote.getTitle())
.eq(StringUtils.isNotEmpty(zSelfNote.getRemark()), ZSelfNote::getRemark, zSelfNote.getRemark())
.eq(zSelfNote.getUid() != null, ZSelfNote::getUid, zSelfNote.getUid());
return lqw;
}
private LambdaQueryWrapper buildCondition(ZSelfNote zSelfNote) {
LambdaQueryWrapper lqw = new LambdaQueryWrapper<>();
lqw.in(ZSelfNote::getUid, getAll())
.like(StringUtils.isNotEmpty(zSelfNote.getPeople()), ZSelfNote::getPeople, zSelfNote.getPeople())
.like(StringUtils.isNotEmpty(zSelfNote.getTitle()), ZSelfNote::getTitle, zSelfNote.getTitle())
.like(StringUtils.isNotEmpty(zSelfNote.getAddress()),ZSelfNote::getAddress,zSelfNote.getAddress())
.like(StringUtils.isNotEmpty(zSelfNote.getRemark()),ZSelfNote::getRemark,zSelfNote.getRemark())
.between(zSelfNote.getHappenStartTime() != null && zSelfNote.getHappenEndTime() != null, ZSelfNote::getHappenTime, zSelfNote.getHappenStartTime(), zSelfNote.getHappenEndTime())
.orderByDesc(ZSelfNote::getCreateTime);
return lqw;
}
private LambdaQueryWrapper buildConditionSec(ZSelfNote zSelfNote,List userIds) {
LambdaQueryWrapper lqw = new LambdaQueryWrapper<>();
// lqw.in(ZSelfNote::getUid, getAll())
lqw.in(ZSelfNote::getUid, userIds)
.like(StringUtils.isNotEmpty(zSelfNote.getPeople()), ZSelfNote::getPeople, zSelfNote.getPeople())
.like(StringUtils.isNotEmpty(zSelfNote.getTitle()), ZSelfNote::getTitle, zSelfNote.getTitle())
.like(StringUtils.isNotEmpty(zSelfNote.getAddress()),ZSelfNote::getAddress,zSelfNote.getAddress())
.like(StringUtils.isNotEmpty(zSelfNote.getRemark()),ZSelfNote::getRemark,zSelfNote.getRemark())
.between(zSelfNote.getHappenStartTime() != null && zSelfNote.getHappenEndTime() != null, ZSelfNote::getHappenTime, zSelfNote.getHappenStartTime(), zSelfNote.getHappenEndTime())
.orderByDesc(ZSelfNote::getCreateTime);
return lqw;
}
// public List getAllByShareId(ZfShareData zfShareData1){
// SysUser user = SecurityUtils.getLoginUser().getUser();
// Long userId = user.getUserId();
//
// ZfShareData zfShareData = new ZfShareData();
// zfShareData.setUserId(userId.intValue());
// List zfShareDataList = zfShareDataService.selectList(zfShareData);
// zfShareData.setUserId(zfShareData1.getUserId());
// zfShareDataList.addAll(zfShareDataService.selectList(zfShareData));
// return zfShareDataList;
// }
public List getAll(){
SysUser user = SecurityUtils.getLoginUser().getUser();
Long userId = user.getUserId();
List allList = new ArrayList<>();
Long source = zfShareDataService.getSource(userId);
allList.add(source);
allList.addAll(zfShareDataService.getAllPerson(Collections.singletonList(source)));
List collect = allList.stream().distinct().collect(Collectors.toList());
List all = new ArrayList<>();
all.add(userId);
if(collect.size() > 1 ) {
Integer masterAccount = zfShareDataService.getByShareId(collect).get(0).getMasterAccount();
if (masterAccount != 0 || masterAccount != userId.intValue()) {
all.clear();
}
all.addAll(collect);
}
return all;
}
/**
* 分页查找
*/
@Override
public AjaxResult selectDataList(ZSelfNote zSelfNote, Integer pageNum, Integer pageSize) {
SysUser user = SecurityUtils.getLoginUser().getUser();
Long userId = user.getUserId();
List all = getAll();
if (all.contains(userId)) {
List byShareId = zfShareDataService.getByShareId(all);
Integer masterAccount = 0;
if(byShareId.size()>0){
masterAccount = byShareId.get(0).getMasterAccount();
}
ZInfoUser infoBysysId = zInfoUserService.getInfoBysysId(masterAccount.longValue());
if (infoBysysId !=null)
{ ZfClan byId = zfClanService.getById(infoBysysId.getClanId());
return AjaxResult.success("请前往家族名称为"+byId.getClanName()+",用户名为"+infoBysysId.getNickName()+"的用户账号下查看");
}
}
// LambdaQueryWrapper lqw = buildCondition(zSelfNote);
//根据userId查询到infouser的uaid
ZInfoUser zInfoUser = zInfoUserService.getInfoBysysId(userId);
//拿到所有的sysid
List fms = zInfoUserService.findByUaidToFaid(zInfoUser.getUaid()).stream().map(ZInfoUser::getSysId).collect(Collectors.toList());
LambdaQueryWrapper lqw;
if(!fms.isEmpty())
lqw = buildConditionSec(zSelfNote, fms);
else
lqw = buildCondition(zSelfNote);
Page pageBean = new Page<>(pageNum, pageSize);
Page pageResult = page(pageBean, lqw);
List beanRecords = pageResult.getRecords();//得到查询出来的数据
HashMap data = MapUtils.getResult(pageResult, beanRecords);
return AjaxResult.success(data);
}
@Override
public List selectByIds(Long[] ids) {
List list = new ArrayList<>();
if (ids.length != 0)
list = listByIds(Arrays.asList(ids));
else
list = list();
return list;
}
@Override
public AjaxResult mySave(ZSelfNote zSelfNote) {
SysUser user = SecurityUtils.getLoginUser().getUser();
Long userId = user.getUserId();
zSelfNote.setUid(userId);
//检查是否有重复数据插入
LambdaQueryWrapper lqw = uniqueCondition(zSelfNote);
List list = list(lqw);
if (list.size() > 0) {
throw new RuntimeException("请勿新增重复数据");
}
if (save(zSelfNote)) {
return AjaxResult.success();
} else {
return AjaxResult.error();
}
}
@Override
public List selectByCondition() {
SysUser user = SecurityUtils.getLoginUser().getUser();
Long userId = user.getUserId();
ZSelfNote zSelfNote = new ZSelfNote();
LambdaQueryWrapper lqw = buildCondition(zSelfNote);
return list(lqw);
}
@Override
@Transactional
public AjaxResult importExcel(MultipartFile file) {
ExcelUtil util = new ExcelUtil<>(ZSelfNote.class);
List dataList = null;
try {
dataList = util.importExcel(file.getInputStream());
} catch (Exception e) {
throw new RuntimeException("没有按照规则导入数据");
}
assert dataList != null;
for (ZSelfNote zSelfNote : dataList) {
if (zSelfNote.getTitle().length() != 0 && zSelfNote.getPeople().length() !=0){
mySave(zSelfNote);}
else {
throw new RuntimeException("有数据的标题或人物为空");
}
}
return AjaxResult.success();
}
}