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

320 lines
15 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.OsmoticDetailQuerySo;
import com.gunshi.project.xyt.entity.so.OsmoticQueryPageSo;
import com.gunshi.project.xyt.entity.so.OsmoticQuerySo;
import com.gunshi.project.xyt.entity.vo.*;
import com.gunshi.project.xyt.mapper.OsmoticPressRMapper;
import com.gunshi.project.xyt.mapper.OsmoticShiftRMapper;
import com.gunshi.project.xyt.model.OsmoticShiftR;
import com.gunshi.project.xyt.util.DataHandleUtil;
import com.gunshi.project.xyt.util.DateUtil;
import com.gunshi.project.xyt.util.ExcelUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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 OsmoticPressRMapper pressRMapper;
private static final String X_PREFIX = "/X";
private static final String Y_PREFIX = "/Y";
private static final String H_PREFIX = "/H";
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 = baseMapper.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 = baseMapper.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_PREFIX);
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_PREFIX);
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_PREFIX);
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;
}
public void yearStatExport(OsmoticQuerySo osmoticQuerySo, HttpServletResponse response) {
String fileName = "年度位移统计";
String sheetName = "年度位移统计";
//上方表格数据
List<OsmoticShiftVo> resList = yearStat(osmoticQuerySo);
//下方特征值数据
List<OsmoticChartVo> chartList = yearStatValue(osmoticQuerySo);
List<String> stationCodes = osmoticQuerySo.getStationCodes();
//表头信息
List<List<String>> headList = new ArrayList<>();
List<String> heads1 = new ArrayList<>();
heads1.add("序号");
heads1.add("序号");
headList.add(heads1);
List<String> heads2 = new ArrayList<>();
heads2.add("监测日期");
heads2.add("监测日期");
headList.add(heads2);
for(String code : stationCodes){
List<String> headsX = new ArrayList<>();
headsX.add(code);
headsX.add("X");
headList.add(headsX);
List<String> headsY = new ArrayList<>();
headsY.add(code);
headsY.add("Y");
headList.add(headsY);
List<String> headsH = new ArrayList<>();
headsH.add(code);
headsH.add("H");
headList.add(headsH);
}
List<Map<String, Object>> list = new ArrayList<>();
for (int j = 0;j < resList.size(); j++) {
OsmoticShiftVo vo = resList.get(j);
Map<String, Object> test = new LinkedHashMap<>();
test.put("t0",j+1);
test.put("t1", vo.getTm());
for(int i = 0;i < stationCodes.size();i++){
String code = stationCodes.get(i);
OsmoticShiftValueVo valueVo = vo.getList().stream().filter(o->code.equals(o.getStationCode())).findFirst().orElse(null);
test.put(code + X_PREFIX,valueVo != null ? valueVo.getX() : "");
test.put(code + Y_PREFIX,valueVo != null ? valueVo.getY() : "");
test.put(code + H_PREFIX,valueVo != null ? valueVo.getH() : "");
}
list.add(test);
}
Map<String, Object> max = new LinkedHashMap<>();
max.put("t0","全年度特征值统计");
max.put("t1", "最大值");
for(int i = 0;i < stationCodes.size();i++){
String code = stationCodes.get(i);
max.put(code+X_PREFIX,chartList.stream().filter(o->(code+X_PREFIX).equals(o.getStationCode())).findFirst().get().getMaxValue());
max.put(code+Y_PREFIX,chartList.stream().filter(o->(code+Y_PREFIX).equals(o.getStationCode())).findFirst().get().getMaxValue());
max.put(code+H_PREFIX,chartList.stream().filter(o->(code+H_PREFIX).equals(o.getStationCode())).findFirst().get().getMaxValue());
}
list.add(max);
Map<String, Object> maxTm = new LinkedHashMap<>();
maxTm.put("t0","全年度特征值统计");
maxTm.put("t1", "日期");
for(int i = 0;i < stationCodes.size();i++){
String code = stationCodes.get(i);
maxTm.put(code+X_PREFIX,chartList.stream().filter(o->(code+X_PREFIX).equals(o.getStationCode())).findFirst().get().getMaxTm());
maxTm.put(code+Y_PREFIX,chartList.stream().filter(o->(code+Y_PREFIX).equals(o.getStationCode())).findFirst().get().getMaxTm());
maxTm.put(code+H_PREFIX,chartList.stream().filter(o->(code+H_PREFIX).equals(o.getStationCode())).findFirst().get().getMaxTm());
}
list.add(maxTm);
Map<String, Object> min = new LinkedHashMap<>();
min.put("t0","全年度特征值统计");
min.put("t1", "最小值");
for(int i = 0;i < stationCodes.size();i++){
String code = stationCodes.get(i);
min.put(code+X_PREFIX,chartList.stream().filter(o->(code+X_PREFIX).equals(o.getStationCode())).findFirst().get().getMinValue());
min.put(code+Y_PREFIX,chartList.stream().filter(o->(code+Y_PREFIX).equals(o.getStationCode())).findFirst().get().getMinValue());
min.put(code+H_PREFIX,chartList.stream().filter(o->(code+H_PREFIX).equals(o.getStationCode())).findFirst().get().getMinValue());
}
list.add(min);
Map<String, Object> minTm = new LinkedHashMap<>();
minTm.put("t0","全年度特征值统计");
minTm.put("t1", "日期");
for(int i = 0;i < stationCodes.size();i++){
String code = stationCodes.get(i);
minTm.put(code+X_PREFIX,chartList.stream().filter(o->(code+X_PREFIX).equals(o.getStationCode())).findFirst().get().getMinTm());
minTm.put(code+Y_PREFIX,chartList.stream().filter(o->(code+Y_PREFIX).equals(o.getStationCode())).findFirst().get().getMinTm());
minTm.put(code+H_PREFIX,chartList.stream().filter(o->(code+H_PREFIX).equals(o.getStationCode())).findFirst().get().getMinTm());
}
list.add(minTm);
Map<String, Object> diff = new LinkedHashMap<>();
diff.put("t0","全年度特征值统计");
diff.put("t1", "年变幅");
for(int i = 0;i < stationCodes.size();i++){
String code = stationCodes.get(i);
diff.put(code+X_PREFIX,chartList.stream().filter(o->(code+X_PREFIX).equals(o.getStationCode())).findFirst().get().getDiff());
diff.put(code+Y_PREFIX,chartList.stream().filter(o->(code+Y_PREFIX).equals(o.getStationCode())).findFirst().get().getDiff());
diff.put(code+H_PREFIX,chartList.stream().filter(o->(code+H_PREFIX).equals(o.getStationCode())).findFirst().get().getDiff());
}
list.add(diff);
ExcelUtil.exportExcel(headList, DataHandleUtil.tableData(list),2,new int[]{0},fileName,response,sheetName);
}
public List<OsmoticShiftListVo> listValue() {
List<OsmoticShiftListVo> list = baseMapper.listValue();
OsmoticQuerySo so = new OsmoticQuerySo();
List<String> stationCodes = list.stream().map(OsmoticShiftListVo::getStationCode).collect(Collectors.toList());
String maxTm = list.stream().filter(o->o.getTm() != null).max(Comparator.comparing(OsmoticShiftListVo::getTm)).get().getTm();
String minTm = list.stream().filter(o->o.getTm() != null).min(Comparator.comparing(OsmoticShiftListVo::getTm)).get().getTm();
so.setStationCodes(stationCodes);
DateTimeRangeSo dateTimeRangeSo = new DateTimeRangeSo();
dateTimeRangeSo.setStart(DateUtil.convertStringToDate(minTm));
dateTimeRangeSo.setEnd(DateUtil.convertStringToDate(maxTm));
so.setDateTimeRangeSo(dateTimeRangeSo);
List<OsmoticShiftListVo> warnList = baseMapper.queryWarn(so);
list.stream().map(o->{
if(o.getTm() != null && DateUtil.hoursBetweenDate(DateUtil.convertStringToDate(o.getTm()), new Date()) > 48){
o.setFlag(1);
}
Boolean a = warnList.stream().filter(t->t.getStationCode().equals(o.getStationCode()) && t.getTm().equals(o.getTm()) && t.getDirection().equals("X")).findAny().isPresent();
if(a){
o.setXStatus(1);
}
Boolean b = warnList.stream().filter(t->t.getStationCode().equals(o.getStationCode()) && t.getTm().equals(o.getTm()) && t.getDirection().equals("Y")).findAny().isPresent();
if(b){
o.setYStatus(1);
}
Boolean c = warnList.stream().filter(t->t.getStationCode().equals(o.getStationCode()) && t.getTm().equals(o.getTm()) && t.getDirection().equals("H")).findAny().isPresent();
if(c){
o.setHStatus(1);
}
return o;
}).collect(Collectors.toList());
return list;
}
public List<OsmoticShiftValueVo> detailValue(OsmoticDetailQuerySo so) {
List<OsmoticShiftValueVo> list = baseMapper.detailValue(so);
OsmoticQuerySo osmoticQuerySo = new OsmoticQuerySo();
BeanUtils.copyProperties(so,osmoticQuerySo);
List<StRzVo> stRzVos = pressRMapper.queryLineRz(osmoticQuerySo);
return bindShiftDetail(list,stRzVos);
}
private List<OsmoticShiftValueVo> bindShiftDetail(List<OsmoticShiftValueVo> list, List<StRzVo> stRzVos) {
HashSet<String> strings = new HashSet<>();
list.stream().forEach(v1 -> strings.add(v1.getTm()));
stRzVos.stream().forEach(v1 -> strings.add(v1.getTm()));
List<OsmoticShiftValueVo> result = new ArrayList<>();
strings.stream().forEach(v1 ->{
OsmoticShiftValueVo v = new OsmoticShiftValueVo();
v.setTm(v1);
result.add(v);
});
List<OsmoticShiftValueVo> resList = result.stream().map(v1 -> {
stRzVos.stream().filter(v2 -> v1.getTm().equals(v2.getTm())).forEach(v2 -> {
v1.setRz(v2.getRz());
});
list.stream().filter(v2 -> v1.getTm().equals(v2.getTm())).forEach(v2 -> {
v1.setX(v2.getX());
v1.setY(v2.getY());
v1.setH(v2.getH());
});
return v1;
}).collect(Collectors.toList());
return resList.stream().sorted(Comparator.comparing(OsmoticShiftValueVo::getTm)).collect(Collectors.toList());
}
}