获取某点位,某段时间内,每天的总量汇总

获取某点位,最新的一条数据量
This commit is contained in:
Timer
2026-03-10 20:44:14 +08:00
parent 0a9726fed8
commit 061ededef9
4 changed files with 259 additions and 33 deletions

View File

@ -2,7 +2,11 @@ package com.sipai.controller.mpoint;
import com.sipai.entity.scada.MPointData;
import com.sipai.service.scada.MPointDataService;
import com.sipai.tools.CommString;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@ -16,6 +20,7 @@ import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
@ -27,11 +32,43 @@ import java.util.Map;
@RequestMapping("/mpoint/data")
public class MPointDataController {
@Resource
private RedissonClient redissonClient;
@Resource
private MPointDataService mPointDataService;
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@RequestMapping("/getCurrentValue.do")
public ModelAndView getStatistics(HttpServletRequest request, Model model,
@RequestParam(value = "mPointKey") String mPointKey) {
JSONObject result = new JSONObject();
// 参数校验
if (mPointKey == null || mPointKey.trim().isEmpty()) {
result.put("success", false);
result.put("message", "mPointKey参数不能为空");
model.addAttribute("result", result.toString());
return new ModelAndView("result");
}
// 获取最新数据(当前值)
int num = mPointKey.hashCode() % 25;
RMapCache<String, String> map_redis_data = redissonClient.getMapCache(CommString.RedisMpointFlag + num);
if (map_redis_data.get(mPointKey) != null && !"".equals(map_redis_data.get(mPointKey))) {
String[] str = map_redis_data.get(mPointKey).split(";");
if (str.length >= 3 && str[1] != null && !str[1].isEmpty() && !"null".equals(str[1])) {
result.put("currentValue", formatAmount(new BigDecimal(str[1])));
result.put("currentMeasureTime", str[2]);
} else {
result.put("currentValue", BigDecimal.ZERO);
result.put("currentMeasureTime", null);
}
}
model.addAttribute("result", result.toString());
return new ModelAndView("result");
}
/**
* 获取测点统计数据
*
@ -40,7 +77,7 @@ public class MPointDataController {
* @param startTimeStr 开始时间可选格式yyyy-MM-dd HH:mm:ss
* @param endTimeStr 结束时间可选格式yyyy-MM-dd HH:mm:ss
* @return 统计数据JSON
*
* <p>
* 返回内容:
* - currentValue: 当前值(最新一条数据)
* - currentMeasureTime: 当前值的测量时间
@ -48,7 +85,7 @@ public class MPointDataController {
* - maxValue: 最高值
* - minValue: 最低值
* - totalCount: 数据总条数
*
* <p>
* 示例请求:/mpoint/data/getStatistics.do?mPointKey=DO1&companyKey=DEV022&startTime=2026-03-01 00:00:00&endTime=2026-03-08 23:59:59
*/
@RequestMapping("/getStatistics.do")
@ -194,6 +231,145 @@ public class MPointDataController {
return new ModelAndView("result");
}
/**
* 获取测点每日汇总数据
*
* @param mPointKey 测点编码DO1必填
* @param startTimeStr 开始时间格式yyyy-MM-dd HH:mm:ss
* @param endTimeStr 结束时间格式yyyy-MM-dd HH:mm:ss
* @return 每日汇总数据JSON
* <p>
* 返回内容:
* - dailyData: 每日汇总数组
* - dateStr: 日期yyyy-MM-dd
* - totalValue: 当日ParamValue汇总值
* - dataCount: 当日数据条数
* - grandTotal: 总汇总值
* - totalDays: 总天数
* <p>
* 示例请求:/mpoint/data/getDailyAggregation.do?mPointKey=DO1&startTime=2026-03-01 00:00:00&endTime=2026-03-08 23:59:59
*/
@RequestMapping("/getDailyAggregation.do")
public ModelAndView getDailyAggregation(HttpServletRequest request, Model model,
@RequestParam(value = "mPointKey") String mPointKey,
@RequestParam(value = "startTime") String startTimeStr,
@RequestParam(value = "endTime") String endTimeStr) {
JSONObject result = new JSONObject();
try {
// 参数校验
if (mPointKey == null || mPointKey.trim().isEmpty()) {
result.put("success", false);
result.put("message", "mPointKey参数不能为空");
model.addAttribute("result", result.toString());
return new ModelAndView("result");
}
if (startTimeStr == null || startTimeStr.trim().isEmpty()) {
result.put("success", false);
result.put("message", "startTime参数不能为空");
model.addAttribute("result", result.toString());
return new ModelAndView("result");
}
if (endTimeStr == null || endTimeStr.trim().isEmpty()) {
result.put("success", false);
result.put("message", "endTime参数不能为空");
model.addAttribute("result", result.toString());
return new ModelAndView("result");
}
mPointKey = mPointKey.trim();
// 解析时间参数
Date startTime;
Date endTime;
try {
startTime = DATE_FORMAT.parse(startTimeStr.trim());
} catch (ParseException e) {
result.put("success", false);
result.put("message", "startTime格式错误正确格式yyyy-MM-dd HH:mm:ss");
model.addAttribute("result", result.toString());
return new ModelAndView("result");
}
try {
endTime = DATE_FORMAT.parse(endTimeStr.trim());
} catch (ParseException e) {
result.put("success", false);
result.put("message", "endTime格式错误正确格式yyyy-MM-dd HH:mm:ss");
model.addAttribute("result", result.toString());
return new ModelAndView("result");
}
// 检查时间范围有效性
if (startTime.after(endTime)) {
result.put("success", false);
result.put("message", "开始时间不能晚于结束时间");
model.addAttribute("result", result.toString());
return new ModelAndView("result");
}
// 检查表是否存在
if (!mPointDataService.checkTableExists(mPointKey)) {
result.put("success", false);
result.put("message", "未找到测点[" + mPointKey + "]对应的数据表");
result.put("tableName", mPointDataService.buildTableName(mPointKey));
model.addAttribute("result", result.toString());
return new ModelAndView("result");
}
// 获取每日汇总数据
List<Map<String, Object>> dailyList = mPointDataService.getDailyAggregation(mPointKey, startTime, endTime);
JSONArray dailyData = new JSONArray();
BigDecimal grandTotal = BigDecimal.ZERO;
int totalDays = 0;
if (dailyList != null && !dailyList.isEmpty()) {
for (Map<String, Object> dayItem : dailyList) {
JSONObject dayObj = new JSONObject();
dayObj.put("dateStr", dayItem.get("dateStr"));
dayObj.put("dataCount", dayItem.get("dataCount"));
Object totalValueObj = dayItem.get("totalValue");
BigDecimal dayTotal;
if (totalValueObj instanceof BigDecimal) {
dayTotal = (BigDecimal) totalValueObj;
} else if (totalValueObj instanceof Number) {
dayTotal = new BigDecimal(totalValueObj.toString());
} else {
dayTotal = BigDecimal.ZERO;
}
dayObj.put("totalValue", formatAmount(dayTotal));
dailyData.add(dayObj);
grandTotal = grandTotal.add(dayTotal);
totalDays++;
}
}
result.put("success", true);
result.put("mPointKey", mPointKey);
result.put("tableName", mPointDataService.buildTableName(mPointKey));
result.put("startTime", formatDate(startTime));
result.put("endTime", formatDate(endTime));
result.put("dailyData", dailyData);
result.put("grandTotal", formatAmount(grandTotal));
result.put("totalDays", totalDays);
} catch (Exception e) {
result.put("success", false);
result.put("message", "获取每日汇总数据失败:" + e.getMessage());
}
model.addAttribute("result", result.toString());
return new ModelAndView("result");
}
/**
* 格式化数值保留2位小数
*/

View File

@ -4,6 +4,7 @@ import com.sipai.dao.base.CommDaoImpl;
import com.sipai.entity.scada.MPointData;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
@ -61,4 +62,21 @@ public class MPointDataDao extends CommDaoImpl<MPointData> {
Integer count = this.getSqlSession().selectOne(MAPPER_NAMESPACE + ".checkTableExists", tableName);
return count != null && count > 0;
}
/**
* 按天汇总统计
* @param tableName 表名
* @param startTime 开始时间
* @param endTime 结束时间
* @return 每天汇总数据列表dateStr, totalValue, dataCount
*/
public List<Map<String, Object>> selectDailyAggregation(String tableName,
java.util.Date startTime,
java.util.Date endTime) {
Map<String, Object> params = new java.util.HashMap<>();
params.put("tableName", tableName);
params.put("startTime", startTime);
params.put("endTime", endTime);
return this.getSqlSession().selectList(MAPPER_NAMESPACE + ".selectDailyAggregation", params);
}
}

View File

@ -60,4 +60,23 @@
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = #{tableName}
</select>
<!-- 按天汇总统计每天ParamValue的总量 -->
<select id="selectDailyAggregation" resultType="java.util.Map">
SELECT
CONVERT(VARCHAR(10), MeasureDT, 120) as dateStr,
ISNULL(SUM(ParmValue), 0) as totalValue,
COUNT(*) as dataCount
FROM ${tableName}
<where>
<if test="startTime != null">
AND MeasureDT >= #{startTime}
</if>
<if test="endTime != null">
AND MeasureDT &lt;= #{endTime}
</if>
</where>
GROUP BY CONVERT(VARCHAR(10), MeasureDT, 120)
ORDER BY dateStr ASC
</select>
</mapper>

View File

@ -66,6 +66,19 @@ public class MPointDataService implements CommService<MPointData> {
return mPointDataDao.selectStatistics(tableName, companyKey, startTime, endTime);
}
/**
* 获取每日汇总数据
* @param mPointKey 测点编码
* @param startTime 开始时间
* @param endTime 结束时间
* @return 每天汇总数据列表
*/
public List<Map<String, Object>> getDailyAggregation(String mPointKey,
Date startTime, Date endTime) {
String tableName = buildTableName(mPointKey);
return mPointDataDao.selectDailyAggregation(tableName, startTime, endTime);
}
@Override
public MPointData selectById(String id) {
return null;