gunshi-project-ss/src/main/java/com/gunshi/project/hsz/timetask/WaterRTask.java

149 lines
5.6 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.hsz.timetask;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gunshi.project.hsz.model.StWaterR;
import com.gunshi.project.hsz.model.StWaterRReorganize;
import com.gunshi.project.hsz.service.StWaterRReorganizeService;
import com.gunshi.project.hsz.service.StWaterRService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* 供水整编定时任务
*/
@Component
@Slf4j
//@EnableScheduling
//@Profile({"prod","dev","local"})
public class WaterRTask {
@Autowired
private StWaterRService stWaterRService;
@Autowired
private StWaterRReorganizeService stWaterRReorganizeService;
//每70分钟执行一次
//@Scheduled(fixedRate = 70 , timeUnit = TimeUnit.MINUTES)
public void syncDataToReorganize() {
LambdaQueryWrapper<StWaterRReorganize> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(StWaterRReorganize::getTm)
.last("limit 1");
StWaterRReorganize stWaterRReorganize = stWaterRReorganizeService.getBaseMapper().selectOne(queryWrapper);
// 获取整编表数据中最新的时间
String newDate = stWaterRReorganize.getTm();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
Date newDateObj;
try {
newDateObj = sdf.parse(newDate);
} catch (ParseException e) {
log.error("时间解析失败{}",e.getMessage());
throw new RuntimeException(e);
}
// 在 newDateObj 基础上加一个小时
Calendar calendar = Calendar.getInstance();
calendar.setTime(newDateObj);
calendar.add(Calendar.HOUR_OF_DAY, 1); // 加一个小时
Date newDatePlusOneHour = calendar.getTime();
// 站点配置
//TODO 站点编号待定。
String ecoStcd = "1114";
String stcd1 = "1112";
String stcd2 = "1113";
List<String> stcds = new ArrayList<>(Arrays.asList(ecoStcd, stcd1, stcd2));
// 查询新数据
LambdaQueryWrapper<StWaterR> stWaterRLambdaQueryWrapper = new LambdaQueryWrapper<>();
stWaterRLambdaQueryWrapper.in(StWaterR::getStcd, stcds)
.ge(StWaterR::getTm, newDatePlusOneHour)
.orderByAsc(StWaterR::getTm); // 按时间排序便于处理
List<StWaterR> stWaterRS = stWaterRService.getBaseMapper().selectList(stWaterRLambdaQueryWrapper);
// 使用 DateTimeFormatter线程安全
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
Map<String, List<StWaterR>> groupedByTime = stWaterRS.stream()
.collect(Collectors.groupingBy(waterR -> {
try {
Instant instant = waterR.getTm().toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
return localDateTime.format(formatter);
} catch (Exception e) {
return "未知时间";
}
}));
// 组装数据并保存
List<StWaterRReorganize> reorganizeList = new ArrayList<>();
for (Map.Entry<String, List<StWaterR>> entry : groupedByTime.entrySet()) {
String timeKey = entry.getKey();
List<StWaterR> timeData = entry.getValue();
StWaterRReorganize reorganize = new StWaterRReorganize();
reorganize.setTm(timeKey);
// 初始化各字段为0
reorganize.setEcologyQ(BigDecimal.ZERO);
reorganize.setEcologyV(BigDecimal.ZERO);
reorganize.setMci1Q(BigDecimal.ZERO);
reorganize.setMci1V(BigDecimal.ZERO);
reorganize.setMci2Q(BigDecimal.ZERO);
reorganize.setMci2V(BigDecimal.ZERO);
reorganize.setSumV(BigDecimal.ZERO);
// 按站点类型处理数据
for (StWaterR waterR : timeData) {
String stcd = waterR.getStcd();
BigDecimal q = waterR.getQ();
BigDecimal v = waterR.getV();
if (ecoStcd.equals(stcd)) {
// 生态供水数据
reorganize.setEcologyQ(q);
reorganize.setEcologyV(v);
} else if (stcd1.equals(stcd)) {
// 干渠灌溉1数据
reorganize.setMci1Q(q);
reorganize.setMci1V(v);
} else if (stcd2.equals(stcd)) {
// 干渠灌溉2数据
reorganize.setMci2Q(q);
reorganize.setMci2V(v);
}
}
// 计算水量小计
BigDecimal sumV = reorganize.getEcologyV()
.add(reorganize.getMci1V())
.add(reorganize.getMci2V());
reorganize.setSumV(sumV);
reorganizeList.add(reorganize);
}
// 批量保存到重组表
if (!reorganizeList.isEmpty()) {
stWaterRReorganizeService.saveBatch(reorganizeList);
System.out.println("成功同步 " + reorganizeList.size() + " 条数据到重组表");
}
}
}