gunshi-project-ss/src/main/java/com/gunshi/project/hsz/service/ProjectEventsService.java

237 lines
9.8 KiB
Java

package com.gunshi.project.hsz.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gunshi.project.hsz.entity.so.CommonDataPageSo;
import com.gunshi.project.hsz.entity.vo.ProjectEventsVo;
import com.gunshi.project.hsz.mapper.ProjectEventsMapper;
import com.gunshi.project.hsz.model.FileAssociations;
import com.gunshi.project.hsz.model.ProjectEvents;
import com.gunshi.project.hsz.model.TermiteSurvey;
import com.gunshi.project.hsz.util.DataHandleUtil;
import com.gunshi.project.hsz.util.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
/**
* 描述: 工程大事记
* author: wanyan
* date: 2024-08-21 10:40:37
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ProjectEventsService extends ServiceImpl<ProjectEventsMapper, ProjectEvents> {
@Autowired
private FileAssociationsService fileService;
public Page<ProjectEvents> pageQuery(CommonDataPageSo page) {
LambdaQueryWrapper<ProjectEvents> query = Wrappers.lambdaQuery();
if (ObjectUtils.isNotNull(page.getName())) {
query.like(ProjectEvents::getName, page.getName());
}
if (page.getDateSo() != null && page.getDateSo().getStart() != null) {
query.ge(ProjectEvents::getEventsDate, page.getDateSo().getStart());
}
if (page.getDateSo() != null && page.getDateSo().getEnd() != null) {
query.le(ProjectEvents::getEventsDate, page.getDateSo().getEnd());
}
query.orderByDesc(ProjectEvents::getEventsDate);
Page<ProjectEvents> res = this.page(page.getPageSo().toPage(), query);
if (res.getRecords() != null) {
fillAttach(res.getRecords());
}
return res;
}
private void fillAttach(List<ProjectEvents> ret) {
for (ProjectEvents record : ret) {
record.setFiles(fileService.getFiles(getGroupId(), String.valueOf(record.getId())));
}
}
public String getGroupId() {
return "ProjectEvents";
}
public ProjectEvents saveData(ProjectEvents dto) {
dto.setId(IdWorker.getId());
boolean result = this.save(dto);
if (result) {
fileService.saveFile(dto.getFiles(), getGroupId(), String.valueOf(dto.getId()));
}
return dto;
}
public ProjectEvents updateData(ProjectEvents dto) {
if (Objects.isNull(this.getById(dto.getId()))) {
throw new IllegalArgumentException("当前数据不存在");
}
boolean result = this.updateById(dto);
if (result) {
fileService.saveFile(dto.getFiles(), getGroupId(), String.valueOf(dto.getId()));
}
return dto;
}
public Boolean delData(Serializable id) {
if (Objects.isNull(this.getById(id))) {
throw new IllegalArgumentException("当前数据不存在");
}
boolean data = this.removeById(id);
if (data) {
fileService.deleteFile(getGroupId(), id.toString());
}
return data;
}
public List<ProjectEvents> queryList(CommonDataPageSo page) {
LambdaQueryWrapper<ProjectEvents> query = Wrappers.lambdaQuery();
if (ObjectUtils.isNotNull(page.getName())) {
query.like(ProjectEvents::getName, page.getName());
}
if (page.getDateSo() != null && page.getDateSo().getStart() != null) {
query.ge(ProjectEvents::getEventsDate, page.getDateSo().getStart());
}
if (page.getDateSo() != null && page.getDateSo().getEnd() != null) {
query.le(ProjectEvents::getEventsDate, page.getDateSo().getEnd());
}
query.orderByDesc(ProjectEvents::getEventsDate);
List<ProjectEvents> list = this.list(query);
fillAttach(list);
return list;
}
public Page<ProjectEventsVo> filePage(CommonDataPageSo page) {
Page<ProjectEventsVo> res = new Page<>();
List<ProjectEventsVo> list = new ArrayList<>();
list = queryData(page,list);
if (CollectionUtils.isNotEmpty(list)) {
fillFile(list.stream().filter(o->o.getType() != 6).collect(Collectors.toList()));
list = list.stream().sorted(Comparator.comparing(ProjectEventsVo::getEventsDate).reversed()).collect(Collectors.toList());
List<ProjectEventsVo> paginate = DataHandleUtil.paginate(list, page.getPageSo().getPageNumber(), page.getPageSo().getPageSize());
res.setRecords(paginate);
}
res.setCurrent(page.getPageSo().getPageNumber());
res.setTotal(list.size());
return res;
}
private List<ProjectEventsVo> queryData(CommonDataPageSo page,List<ProjectEventsVo> list) {
List<Integer> types = page.getTypes();
if(CollectionUtils.isEmpty(types) || (CollectionUtils.isNotEmpty(types)) && types.contains(1)){
List<ProjectEventsVo> projectEventsVos = this.baseMapper.eventList(page);
list.addAll(projectEventsVos);
}
if(CollectionUtils.isEmpty(types) || (CollectionUtils.isNotEmpty(types)) && types.contains(2)){
List<ProjectEventsVo> projectEventsVos = this.baseMapper.dispatchList(page);
list.addAll(projectEventsVos);
}
if(CollectionUtils.isEmpty(types) || (CollectionUtils.isNotEmpty(types)) && types.contains(3)){
List<ProjectEventsVo> projectEventsVos = this.baseMapper.maintainList(page);
list.addAll(projectEventsVos);
}
if(CollectionUtils.isEmpty(types) || (CollectionUtils.isNotEmpty(types)) && types.contains(4)){
List<ProjectEventsVo> projectEventsVos = this.baseMapper.safetyList(page);
list.addAll(projectEventsVos);
}
if(CollectionUtils.isEmpty(types) || (CollectionUtils.isNotEmpty(types)) && types.contains(5)){
List<ProjectEventsVo> projectEventsVos = this.baseMapper.reinforceList(page);
list.addAll(projectEventsVos);
}
// if(CollectionUtils.isEmpty(types) || (CollectionUtils.isNotEmpty(types)) && types.contains(6)){
// List<TermiteSurvey> termiteVos = this.baseMapper.termiteList(page);
// List<ProjectEventsVo> projectEventsVos = this.handelTermite(termiteVos);
// list.addAll(projectEventsVos);
// }
return list;
}
private List<ProjectEventsVo> handelTermite(List<TermiteSurvey> termiteVos) {
List<ProjectEventsVo> list = new ArrayList<>();
if(CollectionUtils.isEmpty(termiteVos)){
return list;
}
for (TermiteSurvey vo : termiteVos){
Integer surveyWay = vo.getSurveyWay();
String way = "";
switch (surveyWay){
case 1 :
way = "人工排查法";
break;
case 2 :
way = "引诱法";
break;
case 3 :
way = "仪器探测法";
break;
case 4 :
way = "其它";
break;
}
Integer surveyType = vo.getSurveyType();
String type = "";
switch (surveyType){
case 1 :
type = "日常检查排查";
break;
case 2 :
type = "定期普查";
break;
case 3 :
type = "专项调查";
break;
}
String result = vo.getHarmNum() > 0 ?"有危害":"无危害";
String content = "采用"+way+"进行"+type+",结果:"+result+";白蚁危害处数"+vo.getHarmNum()+"处;已处理处数"+vo.getHandleNum()+"处。";
ProjectEventsVo eventsVo = new ProjectEventsVo();
eventsVo.setId(vo.getId());
eventsVo.setType(6);
eventsVo.setEventsDate(vo.getReportDate());
eventsVo.setEventsDesc(content);
List<Long> ids = this.baseMapper.queryTermiteDetail(vo.getId());
List<FileAssociations> files = fileService.getFilesByIds(ids.stream().map(Objects::toString).collect(Collectors.toList()));
eventsVo.setFiles(files);
list.add(eventsVo);
}
return list;
}
private void fillFile(List<ProjectEventsVo> list) {
if(CollectionUtils.isEmpty(list)){
return;
}
List<String> ids = list.stream().map(ProjectEventsVo::getId).map(Objects::toString).collect(Collectors.toList());
List<FileAssociations> files = fileService.getFilesByIds(ids);
Map<String, List<FileAssociations>> map = files.stream().collect(Collectors.groupingBy(FileAssociations::getBusinessId));
for (ProjectEventsVo vo : list){
vo.setFiles(map.get(vo.getId().toString()));
}
}
public void export(CommonDataPageSo page, HttpServletResponse response) {
List<ProjectEventsVo> list = new ArrayList<>();
list = queryData(page,list);
if (CollectionUtils.isNotEmpty(list)) {
list = list.stream().sorted(Comparator.comparing(ProjectEventsVo::getEventsDate).reversed()).collect(Collectors.toList());
}
ExcelUtil.exportExcel(list,"全周期档案", ProjectEventsVo.class,response,"全周期档案");
}
}