|
@@ -4,16 +4,31 @@ package org.jeecg.modules.productionConsume.service.impl;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.jeecg.common.util.DateUtils;
|
|
|
import org.jeecg.modules.device.mapper.DeviceInformationMapper;
|
|
|
import org.jeecg.modules.productionConsume.entity.ProductionConsume;
|
|
|
import org.jeecg.modules.productionConsume.entity.ProductionConsumeDTO;
|
|
|
+import org.jeecg.modules.productionConsume.entity.ProductionDevice;
|
|
|
import org.jeecg.modules.productionConsume.mapper.ProductionConsumeMapper;
|
|
|
+import org.jeecg.modules.productionConsume.mapper.ProductionDeviceMapper;
|
|
|
import org.jeecg.modules.productionConsume.service.IProductionConsumeService;
|
|
|
+import org.jeecg.modules.utils.PostgreSQLUtil;
|
|
|
+import org.jeecg.modules.utils.ProductPostgreSQLUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.Map;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.sql.Connection;
|
|
|
+import java.sql.PreparedStatement;
|
|
|
+import java.sql.ResultSet;
|
|
|
+import java.sql.Timestamp;
|
|
|
+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
|
|
@@ -25,6 +40,9 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
|
|
|
@Autowired
|
|
|
private ProductionConsumeMapper productionConsumeMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ProductionDeviceMapper productionDeviceMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public void addC(ProductionConsumeDTO productionConsumeDTO) {
|
|
|
try {
|
|
@@ -36,6 +54,156 @@ public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeM
|
|
|
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void updateActivePower(String createDate) {
|
|
|
+
|
|
|
+ // 解析字符串日期
|
|
|
+ LocalDate localDate = LocalDate.parse(createDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
|
|
+
|
|
|
+ // 获取当天的开始时间和结束时间
|
|
|
+ LocalDateTime startTime = localDate.atStartOfDay();
|
|
|
+ LocalDateTime endTime = localDate.plusDays(1).atStartOfDay();
|
|
|
+
|
|
|
+ Connection connection = null;
|
|
|
+ PreparedStatement statement = null;
|
|
|
+ ResultSet resultSet = null;
|
|
|
+ try {
|
|
|
+ // 建立链接
|
|
|
+ connection = ProductPostgreSQLUtil.getConnection();
|
|
|
+ boolean isClosed = connection.isClosed();
|
|
|
+ LambdaQueryWrapper<ProductionDevice> deviceLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ List<ProductionDevice> devices = productionDeviceMapper.selectList(deviceLambdaQueryWrapper);
|
|
|
+ // 遍历所有设备
|
|
|
+ for (ProductionDevice device : devices) {
|
|
|
+
|
|
|
+ // 构造查询条件
|
|
|
+ LambdaQueryWrapper<ProductionConsume> productionLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ productionLambdaQueryWrapper
|
|
|
+ .eq(ProductionConsume::getDeviceTitle, device.getDeviceTitle())
|
|
|
+ .ge(ProductionConsume::getCreateTime, startTime)
|
|
|
+ .lt(ProductionConsume::getCreateTime, endTime)
|
|
|
+ .orderByDesc(ProductionConsume::getCreateTime);
|
|
|
+
|
|
|
+ List<ProductionConsume> productionConsumeList = productionConsumeMapper.selectList(productionLambdaQueryWrapper);
|
|
|
+
|
|
|
+ // 使用 split 方法分割字符串
|
|
|
+ String[] strArray = device.getDeviceVariable().split(",");
|
|
|
+
|
|
|
+ // 将数组转换为 List
|
|
|
+ List<String> list = Arrays.asList(strArray);
|
|
|
+
|
|
|
+ // 将 List 转换为正确的 SQL IN 子句格式
|
|
|
+ String deviceSnInClause = list.stream()
|
|
|
+ .map(String::trim) // 去掉可能的空格
|
|
|
+ .map(sn -> "'" + sn + "'") // 每个值加上单引号
|
|
|
+ .collect(Collectors.joining(",")); // 用逗号拼接
|
|
|
+
|
|
|
+ for (ProductionConsume production : productionConsumeList) {
|
|
|
+
|
|
|
+ // 日期转字符串
|
|
|
+ String formattedTime = DateUtils.date2Str(production.getCreateTime(), DateUtils.datetimeFormat.get());
|
|
|
+ // 构造 SQL
|
|
|
+ String sql = String.format("select key,dbl_v,upload_time,device_sn from sys_device_delivery_t\n" +
|
|
|
+ "where device_sn in (%s) and \"key\" = 'ep' and upload_time = '%s' ", deviceSnInClause, formattedTime);
|
|
|
+ statement = connection.prepareStatement(sql);
|
|
|
+ resultSet = statement.executeQuery();
|
|
|
+
|
|
|
+ // 解析结果集
|
|
|
+ List<Map<String, Object>> resultList = new ArrayList<>();
|
|
|
+ while (resultSet.next()) {
|
|
|
+ Map<String, Object> row = new HashMap<>();
|
|
|
+ row.put("key", resultSet.getString("key"));
|
|
|
+ row.put("dbl_v", resultSet.getBigDecimal("dbl_v"));
|
|
|
+ row.put("upload_time", resultSet.getTimestamp("upload_time"));
|
|
|
+ row.put("device_sn", resultSet.getString("device_sn"));
|
|
|
+ resultList.add(row);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按 upload_time 分组
|
|
|
+ Map<Timestamp, List<Map<String, Object>>> groupedData = resultList.stream()
|
|
|
+ .collect(Collectors.groupingBy(row -> (Timestamp) row.get("upload_time")));
|
|
|
+
|
|
|
+ // 按 upload_time 升序排序
|
|
|
+ List<Map.Entry<Timestamp, List<Map<String, Object>>>> sortedEntries = groupedData.entrySet()
|
|
|
+ .stream()
|
|
|
+ .sorted(Map.Entry.comparingByKey()) // 按 Timestamp 升序排列
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 对分组后的数据进行处理
|
|
|
+ for (Map.Entry<Timestamp, List<Map<String, Object>>> entry : sortedEntries) {
|
|
|
+
|
|
|
+ List<Map<String, Object>> group = entry.getValue();
|
|
|
+
|
|
|
+ // 累加 dbl_v 的值,保留为 BigDecimal 类型
|
|
|
+ BigDecimal totalTs = group.stream()
|
|
|
+ .map(row -> (BigDecimal) row.get("dbl_v")) // 提取 dbl_v 值为 BigDecimal
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add); // 累加
|
|
|
+
|
|
|
+ // 将累加后的值设置到 activePower
|
|
|
+ production.setActivePower(totalTs.setScale(2, RoundingMode.HALF_UP));
|
|
|
+
|
|
|
+ // 更新处理生产消耗记录
|
|
|
+ productionConsumeMapper.updateById(production);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 关闭连接
|
|
|
+ resultSet.close();
|
|
|
+ statement.close();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 关闭连接
|
|
|
+ log.info(e.getMessage());
|
|
|
+ } finally {
|
|
|
+ PostgreSQLUtil.close(connection);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateConsumePower(String createDate) {
|
|
|
+ // 解析字符串日期
|
|
|
+ LocalDate localDate = LocalDate.parse(createDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
|
|
+
|
|
|
+ // 获取当天的开始时间和结束时间
|
|
|
+ LocalDateTime startTime = localDate.atStartOfDay();
|
|
|
+ LocalDateTime endTime = localDate.plusDays(1).atStartOfDay();
|
|
|
+
|
|
|
+ // 构造查询条件,查询当天数据,并按 createTime 升序排列
|
|
|
+ LambdaQueryWrapper<ProductionConsume> productionLambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ productionLambdaQueryWrapper
|
|
|
+ .ge(ProductionConsume::getCreateTime, startTime)
|
|
|
+ .lt(ProductionConsume::getCreateTime, endTime)
|
|
|
+ .orderByAsc(ProductionConsume::getCreateTime); // 按创建时间升序排列
|
|
|
+
|
|
|
+ // 查询数据
|
|
|
+ List<ProductionConsume> productionConsumeList = productionConsumeMapper.selectList(productionLambdaQueryWrapper);
|
|
|
+
|
|
|
+ // 用于存储每个 deviceTitle 最近的记录(时间更晚的记录)
|
|
|
+ Map<String, ProductionConsume> latestRecordMap = new HashMap<>();
|
|
|
+
|
|
|
+ // 遍历数据,计算 consumePower
|
|
|
+ for (ProductionConsume currentRecord : productionConsumeList) {
|
|
|
+ String deviceTitle = currentRecord.getDeviceTitle();
|
|
|
+
|
|
|
+ // 找到该设备(deviceTitle)的上一条记录
|
|
|
+ ProductionConsume prevRecord = latestRecordMap.get(deviceTitle);
|
|
|
+
|
|
|
+ if (prevRecord == null) {
|
|
|
+ // 该设备的第一条数据(没有比它时间更早的记录)
|
|
|
+ currentRecord.setConsumePower(BigDecimal.ZERO);
|
|
|
+ } else {
|
|
|
+ // 计算 consumePower = 当前 activePower - 上一条 activePower
|
|
|
+ BigDecimal consumePower = currentRecord.getActivePower().subtract(prevRecord.getActivePower());
|
|
|
+ currentRecord.setConsumePower(consumePower);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新最新记录
|
|
|
+ latestRecordMap.put(deviceTitle, currentRecord);
|
|
|
+ productionConsumeMapper.updateById(currentRecord);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private void insertProductionConsume(ProductionConsumeDTO productionConsumeDTO) {
|
|
|
|
|
|
ProductionConsume productionConsume = new ProductionConsume();
|