gunshi-project-ss/src/main/java/com/gunshi/project/ss/service/JcskGnssRService.java

645 lines
29 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.gunshi.project.ss.service;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.ss.common.model.vo.*;
import com.gunshi.project.ss.entity.dto.ArtificialJcskGnssDeleteDto;
import com.gunshi.project.ss.common.model.so.JcskGnssRPageSo;
import com.gunshi.project.ss.common.model.so.OsmoticDetailQuerySo;
import com.gunshi.project.ss.common.model.so.OsmoticQuerySo;
import com.gunshi.project.ss.entity.vo.*;
import com.gunshi.project.ss.mapper.AttResBaseMapper;
import com.gunshi.project.ss.common.mapper.JcskGnssBMapper;
import com.gunshi.project.ss.common.mapper.JcskGnssRMapper;
import com.gunshi.project.ss.common.mapper.JcskSyRMapper;
import com.gunshi.project.ss.common.model.JcskGnssR;
import com.gunshi.project.ss.util.DataHandleUtil;
import com.gunshi.project.ss.util.DateUtil;
import com.gunshi.project.ss.util.ExcelUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class JcskGnssRService extends ServiceImpl<JcskGnssRMapper, JcskGnssR> {
@Resource
private AttResBaseMapper attResBaseMapper;
@Autowired
private JcskSyRMapper jcskSyRMapper;
@Autowired
private JcskGnssBMapper jcskGnssBMapper;
private static final String X_PREFIX = "/E";
private static final String Y_PREFIX = "/N";
private static final String H_PREFIX = "/U";
public Page<JcskGnssR> pageQuery(JcskGnssRPageSo page) {
Page<JcskGnssR> res = this.baseMapper.pageQuery(page.getPageSo().toPage(), page);
return res;
}
public String getStcd() {
List<AttResBaseVo> attResBaseVos = attResBaseMapper.queryList();
AttResBaseVo attResBaseVo = attResBaseVos.get(0);
return attResBaseVo == null ? "":attResBaseVo.getStcd();
}
public List<JcskGnessListVo> listValue() {
List<JcskGnessListVo> list = baseMapper.listValue();
list.stream().forEach(o ->{
o.setStationCode(o.getCd());
});
if(CollectionUtils.isEmpty(list)){
return list;
}
OsmoticQuerySo so = new OsmoticQuerySo();
List<String> stationCodes = list.stream().map(JcskGnessListVo::getCd).collect(Collectors.toList());
String maxTm = list.stream().filter(o->o.getTm() != null).max(Comparator.comparing(JcskGnessListVo::getTm)).get().getTm();
String minTm = list.stream().filter(o->o.getTm() != null).min(Comparator.comparing(JcskGnessListVo::getTm)).get().getTm();
so.setStationCodes(stationCodes);
DateTimeRangeSo dateTimeRangeSo = new DateTimeRangeSo();
dateTimeRangeSo.setStart(DateUtil.convertStringToDate(minTm));
dateTimeRangeSo.setEnd(DateUtil.convertStringToDate(maxTm));
so.setDateTimeRangeSo(dateTimeRangeSo);
List<OsmoticShiftListVo2> 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.getCd().equals(o.getCd()) && t.getTm().equals(o.getTm()) && t.getDirection().toUpperCase().equals("X")).findAny().isPresent();
if(a){
o.setXStatus(1);
}
Boolean b = warnList.stream().filter(t->t.getCd().equals(o.getCd()) && t.getTm().equals(o.getTm()) && t.getDirection().toUpperCase().equals("Y")).findAny().isPresent();
if(b){
o.setYStatus(1);
}
Boolean c = warnList.stream().filter(t->t.getCd().equals(o.getCd()) && t.getTm().equals(o.getTm()) && t.getDirection().toUpperCase().equals("H")).findAny().isPresent();
if(c){
o.setHStatus(1);
}
return o;
}).collect(Collectors.toList());
return list;
}
public List<OsmoticShiftVo2> yearStat(OsmoticQuerySo osmoticQuerySo) {
List<OsmoticShiftVo2> resList = new ArrayList<>();
commonQueryHandle(osmoticQuerySo);
//查询位移监测记录
List<OsmoticShiftValueVo2> valueList = baseMapper.queryReorganizeValue(osmoticQuerySo);
List<String> dateList = DateUtil.getDatesBetween(osmoticQuerySo.getDateTimeRangeSo().getStart(), osmoticQuerySo.getDateTimeRangeSo().getEnd(), false);
//数据处理
for(String str : dateList){
OsmoticShiftVo2 vo = new OsmoticShiftVo2();
vo.setTm(str);
// 筛选出该日期8点的数据
List<OsmoticShiftValueVo2> collect = valueList.stream()
.filter(o -> {
if (o.getTm() != null && o.getTm().startsWith(str)) {
// 提取时间部分检查是否为08:00:00
String timePart = o.getTm().substring(11, 13); // 获取 HH:mm:ss 部分
return "08".equals(timePart);
}
return false;
})
.peek(o->o.setTm(str))
.collect(Collectors.toList());
vo.setList(collect);
resList.add(vo);
}
return resList;
}
public List<OsmoticShiftVo2> rangeStat(OsmoticQuerySo osmoticQuerySo) {
List<OsmoticShiftVo2> resList = new ArrayList<>();
//查询位移监测记录
List<OsmoticShiftValueVo2> valueList = baseMapper.queryReorganizeValue(osmoticQuerySo);
List<String> dateList = DateUtil.getDatesBetween(osmoticQuerySo.getDateTimeRangeSo().getStart(), osmoticQuerySo.getDateTimeRangeSo().getEnd(), false);
//数据处理
for(String str : dateList){
OsmoticShiftVo2 vo = new OsmoticShiftVo2();
vo.setTm(str);
// 筛选出该日期8点的数据
List<OsmoticShiftValueVo2> collect = valueList.stream()
.filter(o -> {
if (o.getTm() != null && o.getTm().startsWith(str)) {
// 提取时间部分检查是否为08:00:00
String timePart = o.getTm().substring(11, 13); // 获取 HH:mm:ss 部分
return "08".equals(timePart);
}
return false;
})
.peek(o->o.setTm(str))
.collect(Collectors.toList());
vo.setList(collect);
resList.add(vo);
}
return resList;
}
// 新增:生成指定年份的所有日期(确保只包含当年日期)
private List<String> generateYearDates(Integer year) {
List<String> dates = new ArrayList<>();
LocalDate start = LocalDate.of(year, 1, 1);
LocalDate end = LocalDate.of(year, 12, 31);
LocalDate current = start;
while (!current.isAfter(end)) {
dates.add(current.toString()); // 格式为yyyy-MM-dd
current = current.plusDays(1);
}
return dates;
}
private OsmoticQuerySo commonQueryHandle(OsmoticQuerySo osmoticQuerySo){
String year = osmoticQuerySo.getYear();
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date start = sdf.parse(year + "-01-01 00:00:00");
Date end = sdf.parse(year + "-12-31 23:59:59"); // 建议使用 23:59:59
DateTimeRangeSo so = new DateTimeRangeSo();
so.setStart(start);
so.setEnd(end);
osmoticQuerySo.setDateTimeRangeSo(so);
return osmoticQuerySo;
} catch (Exception e) {
throw new IllegalArgumentException("日期格式错误: " + year, e);
}
}
public List<OsmoticChartVo2> yearStatValue(OsmoticQuerySo osmoticQuerySo) {
// 定义时间格式化器
List<OsmoticChartVo2> resList = new ArrayList<>();
try {
commonQueryHandle(osmoticQuerySo);
//查询位移监测记录
List<OsmoticShiftValueVo2> valueList = baseMapper.queryValue(osmoticQuerySo);
//按测站分组
Map<String, List<OsmoticShiftValueVo2>> map = valueList.stream().collect(Collectors.groupingBy(OsmoticShiftValueVo2::getCd));
map.entrySet().forEach(o->{
String key = o.getKey();
List<OsmoticShiftValueVo2> value = o.getValue();
OsmoticChartVo2 xVo = new OsmoticChartVo2();
OsmoticShiftValueVo2 xMax = value.stream().max(Comparator.comparing(OsmoticShiftValueVo2::getDe)).get();
OsmoticShiftValueVo2 xMin = value.stream().min(Comparator.comparing(OsmoticShiftValueVo2::getDe)).get();
xVo.setCd(key + X_PREFIX);
xVo.setMaxValue(xMax.getDe());
xVo.setMaxTm(xMax.getTm());
xVo.setMinValue(xMin.getDe());
xVo.setMinTm(xMin.getTm());
xVo.setDiff(xMax.getDe().subtract(xMin.getDe()));
resList.add(xVo);
OsmoticChartVo2 yVo = new OsmoticChartVo2();
OsmoticShiftValueVo2 yMax = value.stream().max(Comparator.comparing(OsmoticShiftValueVo2::getDn)).get();
OsmoticShiftValueVo2 yMin = value.stream().min(Comparator.comparing(OsmoticShiftValueVo2::getDn)).get();
yVo.setCd(key + Y_PREFIX);
yVo.setMaxValue(yMax.getDn());
yVo.setMaxTm(yMax.getTm());
yVo.setMinValue(yMin.getDn());
yVo.setMinTm(yMin.getTm());
yVo.setDiff(yMax.getDn().subtract(yMin.getDn()));
resList.add(yVo);
OsmoticChartVo2 hVo = new OsmoticChartVo2();
OsmoticShiftValueVo2 hMax = value.stream().max(Comparator.comparing(OsmoticShiftValueVo2::getDu)).get();
OsmoticShiftValueVo2 hMin = value.stream().min(Comparator.comparing(OsmoticShiftValueVo2::getDu)).get();
hVo.setCd(key + H_PREFIX);
hVo.setMaxValue(hMax.getDu());
hVo.setMaxTm(hMax.getTm());
hVo.setMinValue(hMin.getDu());
hVo.setMinTm(hMin.getTm());
hVo.setDiff(hMax.getDu().subtract(hMin.getDu()));
resList.add(hVo);
});
} catch (Exception e) {
log.info("year/state/value报错");
e.printStackTrace();
throw new IllegalArgumentException("报错");
}
return resList;
}
public List<OsmoticChartVo2> rangeStatValue(OsmoticQuerySo osmoticQuerySo) {
// 定义时间格式化器
List<OsmoticChartVo2> resList = new ArrayList<>();
try {
//查询位移监测记录
List<OsmoticShiftValueVo2> valueList = baseMapper.queryValue(osmoticQuerySo);
//按测站分组
Map<String, List<OsmoticShiftValueVo2>> map = valueList.stream().collect(Collectors.groupingBy(OsmoticShiftValueVo2::getCd));
map.entrySet().forEach(o->{
String key = o.getKey();
List<OsmoticShiftValueVo2> value = o.getValue();
OsmoticChartVo2 xVo = new OsmoticChartVo2();
OsmoticShiftValueVo2 xMax = value.stream().max(Comparator.comparing(OsmoticShiftValueVo2::getDe)).get();
OsmoticShiftValueVo2 xMin = value.stream().min(Comparator.comparing(OsmoticShiftValueVo2::getDe)).get();
xVo.setCd(key + X_PREFIX);
xVo.setMaxValue(xMax.getDe());
xVo.setMaxTm(xMax.getTm());
xVo.setMinValue(xMin.getDe());
xVo.setMinTm(xMin.getTm());
xVo.setDiff(xMax.getDe().subtract(xMin.getDe()));
resList.add(xVo);
OsmoticChartVo2 yVo = new OsmoticChartVo2();
OsmoticShiftValueVo2 yMax = value.stream().max(Comparator.comparing(OsmoticShiftValueVo2::getDn)).get();
OsmoticShiftValueVo2 yMin = value.stream().min(Comparator.comparing(OsmoticShiftValueVo2::getDn)).get();
yVo.setCd(key + Y_PREFIX);
yVo.setMaxValue(yMax.getDn());
yVo.setMaxTm(yMax.getTm());
yVo.setMinValue(yMin.getDn());
yVo.setMinTm(yMin.getTm());
yVo.setDiff(yMax.getDn().subtract(yMin.getDn()));
resList.add(yVo);
OsmoticChartVo2 hVo = new OsmoticChartVo2();
OsmoticShiftValueVo2 hMax = value.stream().max(Comparator.comparing(OsmoticShiftValueVo2::getDu)).get();
OsmoticShiftValueVo2 hMin = value.stream().min(Comparator.comparing(OsmoticShiftValueVo2::getDu)).get();
hVo.setCd(key + H_PREFIX);
hVo.setMaxValue(hMax.getDu());
hVo.setMaxTm(hMax.getTm());
hVo.setMinValue(hMin.getDu());
hVo.setMinTm(hMin.getTm());
hVo.setDiff(hMax.getDu().subtract(hMin.getDu()));
resList.add(hVo);
});
} catch (Exception e) {
log.info("year/state/value报错");
e.printStackTrace();
throw new IllegalArgumentException("报错");
}
return resList;
}
public void yearStatExport(OsmoticQuerySo osmoticQuerySo, HttpServletResponse response) {
String fileName = "年度位移统计";
String sheetName = "年度位移统计";
//上方表格数据
List<OsmoticShiftVo2> resList = yearStat(osmoticQuerySo);
//下方特征值数据
List<OsmoticChartVo2> 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++) {
OsmoticShiftVo2 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);
OsmoticShiftValueVo2 valueVo = vo.getList().stream().filter(o->code.equals(o.getCd())).findFirst().orElse(null);
test.put(code + X_PREFIX,valueVo != null ? valueVo.getDe() : "");
test.put(code + Y_PREFIX,valueVo != null ? valueVo.getDn() : "");
test.put(code + H_PREFIX,valueVo != null ? valueVo.getDu() : "");
}
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.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).getMaxValue());
max.put(code+Y_PREFIX,chartList.stream().filter(o->(code+Y_PREFIX).equals(o.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).getMaxValue());
max.put(code+H_PREFIX,chartList.stream().filter(o->(code+H_PREFIX).equals(o.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).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.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).getMaxTm());
maxTm.put(code+Y_PREFIX,chartList.stream().filter(o->(code+Y_PREFIX).equals(o.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).getMaxTm());
maxTm.put(code+H_PREFIX,chartList.stream().filter(o->(code+H_PREFIX).equals(o.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).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.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).getMinValue());
min.put(code+Y_PREFIX,chartList.stream().filter(o->(code+Y_PREFIX).equals(o.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).getMinValue());
min.put(code+H_PREFIX,chartList.stream().filter(o->(code+H_PREFIX).equals(o.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).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.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).getMinTm());
minTm.put(code+Y_PREFIX,chartList.stream().filter(o->(code+Y_PREFIX).equals(o.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).getMinTm());
minTm.put(code+H_PREFIX,chartList.stream().filter(o->(code+H_PREFIX).equals(o.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).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.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).getDiff());
diff.put(code+Y_PREFIX,chartList.stream().filter(o->(code+Y_PREFIX).equals(o.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).getDiff());
diff.put(code+H_PREFIX,chartList.stream().filter(o->(code+H_PREFIX).equals(o.getCd())).findFirst().orElseGet(OsmoticChartVo2::new).getDiff());
}
list.add(diff);
ExcelUtil.exportExcel(headList, DataHandleUtil.tableData(list),2,new int[]{0},fileName,response,sheetName);
}
public List<OsmoticShiftValueVo2> detailValue(OsmoticDetailQuerySo so) {
List<OsmoticShiftValueVo2> list = baseMapper.detailValue(so);
OsmoticQuerySo osmoticQuerySo = new OsmoticQuerySo();
BeanUtils.copyProperties(so,osmoticQuerySo);
List<StRzVo> stRzVos = jcskSyRMapper.queryLineRz(osmoticQuerySo,getStcd());
return bindShiftDetail(list,stRzVos);
}
private String getHourTime(String timeStr) {
try {
if (timeStr == null || timeStr.length() < 16) {
return timeStr;
}
// 假设时间格式为 "yyyy-MM-dd HH:mm:ss"
// 取到小时部分分钟和秒设为00
return timeStr.substring(0, 14) + "00:00";
} catch (Exception e) {
return timeStr;
}
}
private List<OsmoticShiftValueVo2> bindShiftDetail(List<OsmoticShiftValueVo2> list, List<StRzVo> stRzVos) {
// 分离人工录入数据和自动采集数据
List<OsmoticShiftValueVo2> manualData = list.stream()
.filter(v -> v.getIsArtificial() != null && v.getIsArtificial() == 1)
.collect(Collectors.toList());
List<OsmoticShiftValueVo2> autoData = list.stream()
.filter(v -> v.getIsArtificial() == null || v.getIsArtificial() == 0)
.collect(Collectors.toList());
// 如果是人工录入数据,直接使用原始数据,不进行时间合并
if (!manualData.isEmpty()) {
return manualData.stream()
.map(manual -> {
// 为人工录入数据匹配整点的库水位数据
String hourTime = getHourTime(manual.getTm());
stRzVos.stream()
.filter(rz -> hourTime.equals(rz.getTm()))
.findFirst()
.ifPresent(rz -> manual.setRz(rz.getRz()));
return manual;
})
.sorted(Comparator.comparing(OsmoticShiftValueVo2::getTm).reversed())
.collect(Collectors.toList());
}
// 如果是自动采集数据,保持原有逻辑
HashSet<String> timeSet = new HashSet<>();
autoData.forEach(v -> timeSet.add(v.getTm()));
stRzVos.forEach(v -> timeSet.add(v.getTm()));
List<OsmoticShiftValueVo2> result = new ArrayList<>();
timeSet.forEach(time -> {
OsmoticShiftValueVo2 vo = new OsmoticShiftValueVo2();
vo.setTm(time);
result.add(vo);
});
List<OsmoticShiftValueVo2> resList = result.stream().map(vo -> {
// 匹配库水位数据 - 自动数据也匹配整点库水位
String hourTime = getHourTime(vo.getTm());
stRzVos.stream()
.filter(rz -> hourTime.equals(rz.getTm()))
.findFirst()
.ifPresent(rz -> vo.setRz(rz.getRz()));
// 匹配位移数据
autoData.stream()
.filter(shift -> vo.getTm().equals(shift.getTm()))
.findFirst()
.ifPresent(shift -> {
vo.setDe(shift.getDe());
vo.setDn(shift.getDn());
vo.setDu(shift.getDu());
vo.setIsArtificial(shift.getIsArtificial());
});
return vo;
}).collect(Collectors.toList());
// 过滤掉没有监测数据的记录(即位移数据为空的记录)
List<OsmoticShiftValueVo2> filteredList = resList.stream()
.filter(vo -> vo.getDe() != null || vo.getDn() != null || vo.getDu() != null)
.collect(Collectors.toList());
return filteredList.stream()
.sorted(Comparator.comparing(OsmoticShiftValueVo2::getTm).reversed())
.collect(Collectors.toList());
}
public List<GnssCdAndCdVo> getChAndCd() {
List<String> chs = jcskGnssBMapper.selectCH();
List<GnssCdAndCdVo> res = new ArrayList<>();
for (String ch : chs) {
GnssCdAndCdVo vo = new GnssCdAndCdVo();
vo.setCh(ch);
List<String> cds = jcskGnssBMapper.selectCDbyCh(ch);
vo.setChildrens(cds);
res.add(vo);
}
return res;
}
public Page<JcskGnssRHisVo> historyPage(JcskGnssRPageSo page) {
return this.baseMapper.historyPage(page.getPageSo().toPage(),page);
}
public Page<JcskGnssR> artificialPage(JcskGnssRPageSo page) {
Page<JcskGnssR> res = this.baseMapper.artificialPage(page.getPageSo().toPage(),page);
return res;
}
/**
* 将Date转为String
* @param date
* @return
*/
public String transforDateToString(Date date) {
// 将Date转换为LocalDateTime
if(date == null){
return null;
}
LocalDateTime localDateTime = date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = localDateTime.format(formatter);
return formattedDate;
}
public void artificialExport(JcskGnssRPageSo page, HttpServletResponse response) {
Page<JcskGnssR> res = this.baseMapper.artificialPage(page.getPageSo().toPage(), page);
List<JcskGnssR> records = res.getRecords();
// 创建导出数据列表
List<Map<String, Object>> exportData = new ArrayList<>();
for (JcskGnssR record : records) {
Map<String, Object> row = new HashMap<>();
row.put("cd",record.getCd());
row.put("isArtificial", record.getIsArtificial() != null && record.getIsArtificial() == 1 ? "人工录入" : "自动采集");
row.put("updateTm", record.getUpdateTm().toString());
row.put("x",record.getDe());
row.put("y",record.getDn());
row.put("h",record.getDu());
row.put("tm",record.getTm().toString());
exportData.add(row);
}
// 通过工具类创建writer
ExcelWriter writer = cn.hutool.poi.excel.ExcelUtil.getWriter();
// 自定义标题去掉水库代码、水工建筑物编号、stcd、mpcd
writer.addHeaderAlias("cd", "测点编号");
writer.addHeaderAlias("tm", "监测时间");
writer.addHeaderAlias("x", "X方向形变值(mm)");
writer.addHeaderAlias("y", "Y方向形变值(mm)");
writer.addHeaderAlias("h", "H方向形变值(mm)");
writer.addHeaderAlias("isArtificial","录入方式");
writer.addHeaderAlias("updateTm","更新时间");
// 只写出设置了别名的字段
writer.setOnlyAlias(true);
// 一次性写出内容
writer.write(exportData, true);
// 设置响应内容类型
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
try {
// 设置响应头信息
String fileName = URLEncoder.encode("位移人工录入数据", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
// 将writer对象刷新到响应输出流中
writer.flush(response.getOutputStream(), true);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("导出Excel失败", e);
} finally {
// 关闭writer释放内存
writer.close();
}
}
public boolean saveData(JcskGnssR dto) {
dto.setIsArtificial(1);
dto.setUpdateTm(LocalDateTime.now());
int insert = this.baseMapper.insert(dto);
return insert > 0;
}
public boolean updateData(JcskGnssR dto) {
dto.setUpdateTm(LocalDateTime.now());
LocalDateTime oldTm = dto.getTm();
LambdaQueryWrapper<JcskGnssR> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(JcskGnssR::getIsArtificial, 1);
queryWrapper.eq(JcskGnssR::getTm, oldTm);
queryWrapper.eq(JcskGnssR::getCd, dto.getCd());
dto.setCd(dto.getNewUpdateCd());
dto.setTm(dto.getNewUpdateTm());
int update = this.baseMapper.update(dto, queryWrapper);
return update > 0;
}
public boolean deleteData(ArtificialJcskGnssDeleteDto dto) {
LambdaQueryWrapper<JcskGnssR> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(JcskGnssR::getIsArtificial, 1);
queryWrapper.eq(JcskGnssR::getTm, dto.getTm());
queryWrapper.eq(JcskGnssR::getCd, dto.getCd());
int delete = this.baseMapper.delete(queryWrapper);
return delete > 0;
}
public JcskGnssR queryByCDNM(String stationCode) {
return this.baseMapper.queryByCDNM(stationCode);
}
}