gunshi-project-ss/src/main/java/com/gunshi/project/xyt/service/OsmoticShiftRService.java

135 lines
5.9 KiB
Java

package com.gunshi.project.xyt.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.db.dto.DateTimeRangeSo;
import com.gunshi.project.xyt.entity.so.OsmoticQueryPageSo;
import com.gunshi.project.xyt.entity.so.OsmoticQuerySo;
import com.gunshi.project.xyt.entity.vo.OsmoticChartVo;
import com.gunshi.project.xyt.entity.vo.OsmoticShiftValueVo;
import com.gunshi.project.xyt.entity.vo.OsmoticShiftVo;
import com.gunshi.project.xyt.mapper.OsmoticShiftRMapper;
import com.gunshi.project.xyt.model.OsmoticShiftDeviceAutoDao;
import com.gunshi.project.xyt.model.OsmoticShiftR;
import com.gunshi.project.xyt.util.DateUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;
/**
* 描述: 位移监测记录表
* author: xusan
* date: 2024-07-08 17:30:37
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class OsmoticShiftRService extends ServiceImpl<OsmoticShiftRMapper, OsmoticShiftR>
{
@Resource
private OsmoticShiftRMapper mapper;
@Resource
private OsmoticShiftDeviceAutoDao shiftDeviceAutoDao;
public Page<OsmoticShiftR> queryPage(OsmoticQueryPageSo osmoticQueryPageSo) {
LambdaQueryWrapper<OsmoticShiftR> wrapper = Wrappers.lambdaQuery();
if(osmoticQueryPageSo.getDateTimeRangeSo() != null && osmoticQueryPageSo.getDateTimeRangeSo().getStart() != null){
wrapper.ge(OsmoticShiftR::getTm,osmoticQueryPageSo.getDateTimeRangeSo().getStart());
}
if(osmoticQueryPageSo.getDateTimeRangeSo() != null && osmoticQueryPageSo.getDateTimeRangeSo().getEnd() != null){
wrapper.le(OsmoticShiftR::getTm,osmoticQueryPageSo.getDateTimeRangeSo().getEnd());
}
if(StringUtils.isNotEmpty(osmoticQueryPageSo.getStationCode())){
wrapper.eq(OsmoticShiftR::getStationCode,osmoticQueryPageSo.getStationCode());
}
wrapper.orderByDesc(OsmoticShiftR::getTm);
return this.page(osmoticQueryPageSo.getPageSo().toPage(),wrapper);
}
public List<OsmoticShiftVo> yearStat(OsmoticQuerySo osmoticQuerySo) {
List<OsmoticShiftVo> resList = new ArrayList<>();
commonQueryHandle(osmoticQuerySo);
//查询位移监测记录
List<OsmoticShiftValueVo> valueList = mapper.queryValue(osmoticQuerySo);
List<String> dateList = DateUtil.getDatesBetween(osmoticQuerySo.getDateTimeRangeSo().getStart(), osmoticQuerySo.getDateTimeRangeSo().getEnd(), false);
//数据处理
for(String str : dateList){
OsmoticShiftVo vo = new OsmoticShiftVo();
vo.setTm(str);
vo.setList(valueList.stream().filter(o->str.equals(o.getTm())).collect(Collectors.toList()));
resList.add(vo);
}
return resList;
}
private OsmoticQuerySo commonQueryHandle(OsmoticQuerySo osmoticQuerySo){
Integer year = osmoticQuerySo.getYear();
Date start = DateUtil.convertStringToDate(year + "-01-01 00:00:00");
Date end = DateUtil.convertStringToDate(year + "-12-31 00:00:00");
DateTimeRangeSo so = new DateTimeRangeSo();
so.setStart(start);
so.setEnd(end);
osmoticQuerySo.setDateTimeRangeSo(so);
return osmoticQuerySo;
}
public List<OsmoticChartVo> yearStatValue(OsmoticQuerySo osmoticQuerySo) {
List<OsmoticChartVo> resList = new ArrayList<>();
commonQueryHandle(osmoticQuerySo);
//查询位移监测记录
List<OsmoticShiftValueVo> valueList = mapper.queryValue(osmoticQuerySo);
//按测站分组
Map<String, List<OsmoticShiftValueVo>> map = valueList.stream().collect(Collectors.groupingBy(OsmoticShiftValueVo::getStationCode));
map.entrySet().forEach(o->{
String key = o.getKey();
List<OsmoticShiftValueVo> value = o.getValue();
OsmoticChartVo xVo = new OsmoticChartVo();
OsmoticShiftValueVo xMax = value.stream().max(Comparator.comparing(OsmoticShiftValueVo::getX)).get();
OsmoticShiftValueVo xMin = value.stream().min(Comparator.comparing(OsmoticShiftValueVo::getX)).get();
xVo.setStationCode(key + "/X");
xVo.setMaxValue(xMax.getX());
xVo.setMaxTm(xMax.getTm());
xVo.setMinValue(xMin.getX());
xVo.setMinTm(xMin.getTm());
xVo.setDiff(xMax.getX().subtract(xMin.getX()));
resList.add(xVo);
OsmoticChartVo yVo = new OsmoticChartVo();
OsmoticShiftValueVo yMax = value.stream().max(Comparator.comparing(OsmoticShiftValueVo::getY)).get();
OsmoticShiftValueVo yMin = value.stream().min(Comparator.comparing(OsmoticShiftValueVo::getY)).get();
yVo.setStationCode(key + "/Y");
yVo.setMaxValue(yMax.getY());
yVo.setMaxTm(yMax.getTm());
yVo.setMinValue(yMin.getY());
yVo.setMinTm(yMin.getTm());
yVo.setDiff(yMax.getY().subtract(yMin.getY()));
resList.add(yVo);
OsmoticChartVo hVo = new OsmoticChartVo();
OsmoticShiftValueVo hMax = value.stream().max(Comparator.comparing(OsmoticShiftValueVo::getH)).get();
OsmoticShiftValueVo hMin = value.stream().min(Comparator.comparing(OsmoticShiftValueVo::getH)).get();
hVo.setStationCode(key + "/H");
hVo.setMaxValue(hMax.getH());
hVo.setMaxTm(hMax.getTm());
hVo.setMinValue(hMin.getH());
hVo.setMinTm(hMin.getTm());
hVo.setDiff(hMax.getH().subtract(hMin.getH()));
resList.add(hVo);
});
return resList;
}
}