|
@@ -1,6 +1,5 @@
|
|
|
package com.kym.admin.jobs;
|
|
package com.kym.admin.jobs;
|
|
|
|
|
|
|
|
-import cn.hutool.core.date.DateUtil;
|
|
|
|
|
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
import com.kym.common.utils.CommUtil;
|
|
import com.kym.common.utils.CommUtil;
|
|
|
import com.kym.entity.admin.ConnectorInfo;
|
|
import com.kym.entity.admin.ConnectorInfo;
|
|
@@ -19,6 +18,7 @@ import org.springframework.stereotype.Component;
|
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
import java.time.LocalTime;
|
|
|
|
|
+import java.time.YearMonth;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.time.temporal.TemporalAdjusters;
|
|
import java.time.temporal.TemporalAdjusters;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
@@ -61,8 +61,8 @@ public class StationStatJob {
|
|
|
var startTime = LocalDateTime.of(statDay.toLocalDate(), LocalTime.MIN);
|
|
var startTime = LocalDateTime.of(statDay.toLocalDate(), LocalTime.MIN);
|
|
|
var endTime = LocalDateTime.of(statDay.toLocalDate(), LocalTime.MAX);
|
|
var endTime = LocalDateTime.of(statDay.toLocalDate(), LocalTime.MAX);
|
|
|
var chargeOrderList = getChargeOrders(startTime, endTime);
|
|
var chargeOrderList = getChargeOrders(startTime, endTime);
|
|
|
- dayService.replaceBatch((Collection<StationStatDay>) getStationStat(chargeOrderList, true));
|
|
|
|
|
- dayService.saveBatch((Collection<StationStatDay>) getStationStat(chargeOrderList, true));
|
|
|
|
|
|
|
+ dayService.replaceBatch((Collection<StationStatDay>) getStationStat(chargeOrderList, true, LocalDate.now().minusDays(1)));
|
|
|
|
|
+ dayService.saveBatch((Collection<StationStatDay>) getStationStat(chargeOrderList, true, LocalDate.now().minusDays(1)));
|
|
|
log.info("执行站点日统计定时任务-结束");
|
|
log.info("执行站点日统计定时任务-结束");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -77,11 +77,30 @@ public class StationStatJob {
|
|
|
var startTime = statMonth.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
|
|
var startTime = statMonth.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
|
|
|
var endTime = statMonth.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
|
|
var endTime = statMonth.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
|
|
|
var chargeOrderList = getChargeOrders(startTime, endTime);
|
|
var chargeOrderList = getChargeOrders(startTime, endTime);
|
|
|
- monthService.replaceBatch((Collection<StationStatMonth>) getStationStat(chargeOrderList, false));
|
|
|
|
|
-// monthService.saveBatch((Collection<StationStatMonth>) getStationStat(chargeOrderList, false));
|
|
|
|
|
|
|
+ monthService.replaceBatch((Collection<StationStatMonth>) getStationStat(chargeOrderList, false, LocalDate.now().minusMonths(1)));
|
|
|
|
|
+// monthService.saveBatch((Collection<StationStatMonth>) getStationStat(chargeOrderList, false, LocalDate.now().minusMonths(1)));
|
|
|
log.info("执行站点月统计定时任务-结束");
|
|
log.info("执行站点月统计定时任务-结束");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 手动重新同步指定月份的站点统计数据
|
|
|
|
|
+ * @param yearMonth 格式: "yyyy-MM"
|
|
|
|
|
+ * @return 生成的记录数
|
|
|
|
|
+ */
|
|
|
|
|
+ public int resyncMonth(String yearMonth) {
|
|
|
|
|
+ var ym = YearMonth.parse(yearMonth, DateTimeFormatter.ofPattern("yyyy-MM"));
|
|
|
|
|
+ if (!ym.isBefore(YearMonth.now())) {
|
|
|
|
|
+ throw new IllegalArgumentException("只允许同步本月之前的数据");
|
|
|
|
|
+ }
|
|
|
|
|
+ var startTime = ym.atDay(1).atTime(LocalTime.MIN);
|
|
|
|
|
+ var endTime = ym.atEndOfMonth().atTime(LocalTime.MAX);
|
|
|
|
|
+ var targetDate = ym.atDay(1);
|
|
|
|
|
+ var chargeOrderList = getChargeOrders(startTime, endTime);
|
|
|
|
|
+ var stats = (Collection<StationStatMonth>) getStationStat(chargeOrderList, false, targetDate);
|
|
|
|
|
+ monthService.replaceBatch(stats);
|
|
|
|
|
+ return stats.size();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 只执行一次
|
|
// 只执行一次
|
|
|
// @PostConstruct
|
|
// @PostConstruct
|
|
|
private void init() {
|
|
private void init() {
|
|
@@ -107,7 +126,7 @@ public class StationStatJob {
|
|
|
log.info("执行站点初始化定时任务-结束");
|
|
log.info("执行站点初始化定时任务-结束");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private List<ChargeOrder> getChargeOrders(LocalDateTime startTime, LocalDateTime endTime) {
|
|
|
|
|
|
|
+ List<ChargeOrder> getChargeOrders(LocalDateTime startTime, LocalDateTime endTime) {
|
|
|
// 通过charge_app.t_charge_order表统计start_time为前一天且充电金额大于0的有效充电订单
|
|
// 通过charge_app.t_charge_order表统计start_time为前一天且充电金额大于0的有效充电订单
|
|
|
DynamicDataSourceContextHolder.push("db-miniapp");
|
|
DynamicDataSourceContextHolder.push("db-miniapp");
|
|
|
var res = chargeOrderService.lambdaQuery()
|
|
var res = chargeOrderService.lambdaQuery()
|
|
@@ -119,7 +138,7 @@ public class StationStatJob {
|
|
|
return res;
|
|
return res;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private Collection<?> getStationStat(List<ChargeOrder> chargeOrderList, boolean isDayStat) {
|
|
|
|
|
|
|
+ Collection<?> getStationStat(List<ChargeOrder> chargeOrderList, boolean isDayStat, LocalDate targetDate) {
|
|
|
// 统计每个站点的connector_id数量
|
|
// 统计每个站点的connector_id数量
|
|
|
var stationConnectorCountMap = connectorInfoService.list().stream().collect(Collectors.groupingBy(ConnectorInfo::getStationId, Collectors.counting()));
|
|
var stationConnectorCountMap = connectorInfoService.list().stream().collect(Collectors.groupingBy(ConnectorInfo::getStationId, Collectors.counting()));
|
|
|
// 将chargeOrderList按照stationId分组
|
|
// 将chargeOrderList按照stationId分组
|
|
@@ -145,7 +164,7 @@ public class StationStatJob {
|
|
|
return isDayStat
|
|
return isDayStat
|
|
|
? new StationStatDay()
|
|
? new StationStatDay()
|
|
|
.setStationId(entry.getKey())
|
|
.setStationId(entry.getKey())
|
|
|
- .setStatDay(DateUtil.format(LocalDateTime.now().minusDays(1), "yyyy-MM-dd"))
|
|
|
|
|
|
|
+ .setStatDay(targetDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
|
|
|
.setChargeUsers(chargeUsers)
|
|
.setChargeUsers(chargeUsers)
|
|
|
.setValidOrders(chargeOrders.size())
|
|
.setValidOrders(chargeOrders.size())
|
|
|
.setTotalMoney(totalMoney)
|
|
.setTotalMoney(totalMoney)
|
|
@@ -162,7 +181,7 @@ public class StationStatJob {
|
|
|
|
|
|
|
|
: new StationStatMonth()
|
|
: new StationStatMonth()
|
|
|
.setStationId(entry.getKey())
|
|
.setStationId(entry.getKey())
|
|
|
- .setStatMonth(DateUtil.format(LocalDateTime.now().minusMonths(1), "yyyy-MM"))
|
|
|
|
|
|
|
+ .setStatMonth(targetDate.format(DateTimeFormatter.ofPattern("yyyy-MM")))
|
|
|
.setChargeUsers(chargeUsers)
|
|
.setChargeUsers(chargeUsers)
|
|
|
.setValidOrders(chargeOrders.size())
|
|
.setValidOrders(chargeOrders.size())
|
|
|
.setTotalMoney(totalMoney)
|
|
.setTotalMoney(totalMoney)
|
|
@@ -173,7 +192,7 @@ public class StationStatJob {
|
|
|
.setTotalPower(totalPower)
|
|
.setTotalPower(totalPower)
|
|
|
.setAvgOrderElec(avgPower)
|
|
.setAvgOrderElec(avgPower)
|
|
|
.setAvgOrderMoney((int) avgOrderMoney)
|
|
.setAvgOrderMoney((int) avgOrderMoney)
|
|
|
- .setAvgConnectorElec(totalPower / (stationConnectorCountMap.get(entry.getKey()) * (LocalDate.now().minusMonths(1).lengthOfMonth())));
|
|
|
|
|
|
|
+ .setAvgConnectorElec(totalPower / (stationConnectorCountMap.get(entry.getKey()) * (targetDate.lengthOfMonth())));
|
|
|
})).values();
|
|
})).values();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|