动态查询点位数据,最大值最小平均当前值

This commit is contained in:
Timer
2026-03-08 23:11:58 +08:00
parent 2c1a99269f
commit 3a50d35f2c
9 changed files with 582 additions and 6 deletions

View File

@ -0,0 +1,216 @@
package com.sipai.controller.mpoint;
import com.sipai.entity.scada.MPointData;
import com.sipai.service.scada.MPointDataService;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
/**
* 测点数据控制器
* 提供测点数据统计接口,支持动态表名查询
* 表名规则tb_mp_{mPointKey}
*/
@Controller
@RequestMapping("/mpoint/data")
public class MPointDataController {
@Resource
private MPointDataService mPointDataService;
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 获取测点统计数据
*
* @param mPointKey 测点编码DO1必填
* @param companyKey 公司编码可选用于筛选userid字段
* @param startTimeStr 开始时间可选格式yyyy-MM-dd HH:mm:ss
* @param endTimeStr 结束时间可选格式yyyy-MM-dd HH:mm:ss
* @return 统计数据JSON
*
* 返回内容:
* - currentValue: 当前值(最新一条数据)
* - currentMeasureTime: 当前值的测量时间
* - avgValue: 平均值
* - maxValue: 最高值
* - minValue: 最低值
* - totalCount: 数据总条数
*
* 示例请求:/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")
public ModelAndView getStatistics(HttpServletRequest request, Model model,
@RequestParam(value = "mPointKey") String mPointKey,
@RequestParam(value = "companyKey", required = false) String companyKey,
@RequestParam(value = "startTime", required = false) String startTimeStr,
@RequestParam(value = "endTime", required = false) 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");
}
mPointKey = mPointKey.trim();
if (companyKey != null) {
companyKey = companyKey.trim();
}
// 解析时间参数
Date startTime = null;
Date endTime = null;
if (startTimeStr != null && !startTimeStr.trim().isEmpty()) {
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");
}
}
if (endTimeStr != null && !endTimeStr.trim().isEmpty()) {
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 != null && endTime != null && 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");
}
// 获取最新数据(当前值)
MPointData latestData = mPointDataService.getLatestData(mPointKey, companyKey, startTime, endTime);
if (latestData != null) {
result.put("currentValue", formatAmount(latestData.getParmValue()));
result.put("currentMeasureTime", formatDate(latestData.getMeasureDt()));
} else {
result.put("currentValue", null);
result.put("currentMeasureTime", null);
}
// 获取统计数据
Map<String, Object> stats = mPointDataService.getStatistics(mPointKey, companyKey, startTime, endTime);
if (stats != null) {
Object avgObj = stats.get("avgValue");
Object maxObj = stats.get("maxValue");
Object minObj = stats.get("minValue");
Object countObj = stats.get("totalCount");
if (avgObj instanceof BigDecimal) {
result.put("avgValue", formatAmount((BigDecimal) avgObj));
} else if (avgObj instanceof Number) {
result.put("avgValue", formatAmount(new BigDecimal(avgObj.toString())));
} else {
result.put("avgValue", "0.00");
}
if (maxObj instanceof BigDecimal) {
result.put("maxValue", formatAmount((BigDecimal) maxObj));
} else if (maxObj instanceof Number) {
result.put("maxValue", formatAmount(new BigDecimal(maxObj.toString())));
} else {
result.put("maxValue", "0.00");
}
if (minObj instanceof BigDecimal) {
result.put("minValue", formatAmount((BigDecimal) minObj));
} else if (minObj instanceof Number) {
result.put("minValue", formatAmount(new BigDecimal(minObj.toString())));
} else {
result.put("minValue", "0.00");
}
if (countObj instanceof Number) {
result.put("totalCount", ((Number) countObj).intValue());
} else {
result.put("totalCount", 0);
}
} else {
result.put("avgValue", "0.00");
result.put("maxValue", "0.00");
result.put("minValue", "0.00");
result.put("totalCount", 0);
}
result.put("success", true);
result.put("mPointKey", mPointKey);
result.put("tableName", mPointDataService.buildTableName(mPointKey));
if (companyKey != null && !companyKey.isEmpty()) {
result.put("companyKey", companyKey);
}
if (startTime != null) {
result.put("startTime", formatDate(startTime));
}
if (endTime != null) {
result.put("endTime", formatDate(endTime));
}
} catch (Exception e) {
result.put("success", false);
result.put("message", "获取统计数据失败:" + e.getMessage());
}
model.addAttribute("result", result.toString());
return new ModelAndView("result");
}
/**
* 格式化数值保留2位小数
*/
private String formatAmount(BigDecimal amount) {
if (amount == null) {
return "0.00";
}
return amount.setScale(2, RoundingMode.HALF_UP).toPlainString();
}
/**
* 格式化日期
*/
private String formatDate(Date date) {
if (date == null) {
return null;
}
return DATE_FORMAT.format(date);
}
}

View File

@ -0,0 +1,64 @@
package com.sipai.dao.scada;
import com.sipai.dao.base.CommDaoImpl;
import com.sipai.entity.scada.MPointData;
import org.springframework.stereotype.Repository;
import java.util.Map;
@Repository
public class MPointDataDao extends CommDaoImpl<MPointData> {
private static final String MAPPER_NAMESPACE = "scada.MPointDataMapper";
public MPointDataDao() {
super();
this.setMappernamespace(MAPPER_NAMESPACE);
}
/**
* 获取最新一条数据(当前值)
* @param tableName 表名
* @param companyKey 公司编码(可选)
* @param startTime 开始时间(可选)
* @param endTime 结束时间(可选)
* @return 最新一条数据
*/
public MPointData selectLatest(String tableName, String companyKey,
java.util.Date startTime, java.util.Date endTime) {
Map<String, Object> params = new java.util.HashMap<>();
params.put("tableName", tableName);
params.put("companyKey", companyKey);
params.put("startTime", startTime);
params.put("endTime", endTime);
return this.getSqlSession().selectOne(MAPPER_NAMESPACE + ".selectLatest", params);
}
/**
* 获取统计数据
* @param tableName 表名
* @param companyKey 公司编码(可选)
* @param startTime 开始时间(可选)
* @param endTime 结束时间(可选)
* @return 统计数据avgValue, maxValue, minValue, totalCount
*/
public Map<String, Object> selectStatistics(String tableName, String companyKey,
java.util.Date startTime, java.util.Date endTime) {
Map<String, Object> params = new java.util.HashMap<>();
params.put("tableName", tableName);
params.put("companyKey", companyKey);
params.put("startTime", startTime);
params.put("endTime", endTime);
return this.getSqlSession().selectOne(MAPPER_NAMESPACE + ".selectStatistics", params);
}
/**
* 检查表是否存在
* @param tableName 表名
* @return 是否存在
*/
public boolean checkTableExists(String tableName) {
Integer count = this.getSqlSession().selectOne(MAPPER_NAMESPACE + ".checkTableExists", tableName);
return count != null && count > 0;
}
}

View File

@ -1,7 +1,7 @@
package com.sipai.dao.scada;
import com.sipai.dao.base.CommDaoImpl;
import com.sipai.entity.watervolume.WaterVolumeLJLL;
import com.sipai.entity.scada.WaterVolumeLJLL;
import org.springframework.stereotype.Repository;
import java.math.BigDecimal;
@ -12,7 +12,7 @@ import java.util.Map;
@Repository
public class WaterVolumeLJLLDao extends CommDaoImpl<WaterVolumeLJLL> {
private static final String MAPPER_NAMESPACE = "watervolume.WaterVolumeLJLLMapper";
private static final String MAPPER_NAMESPACE = "scada.WaterVolumeLJLLMapper";
public WaterVolumeLJLLDao() {
super();

View File

@ -0,0 +1,135 @@
package com.sipai.entity.scada;
import com.sipai.entity.base.SQLAdapter;
import javax.persistence.Column;
import javax.persistence.Id;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
/**
* 测点数据实体类
* 表名规则tb_mp_{mPointKey}
*/
public class MPointData extends SQLAdapter implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 自增主键
*/
@Id
@Column(name = "ItemID")
private Long itemId;
/**
* 测量值
*/
@Column(name = "ParmValue", precision = 18, scale = 4)
private BigDecimal parmValue;
/**
* 测量时间
*/
@Column(name = "MeasureDT")
private Date measureDt;
/**
* 备注类型
*/
@Column(name = "memotype", length = 100)
private String memotype;
/**
* 备注
*/
@Column(name = "memo", length = 50)
private String memo;
/**
* 用户ID公司标识
*/
@Column(name = "userid", length = 50)
private String userid;
/**
* 入库时间
*/
@Column(name = "insdt")
private Date insdt;
// 构造方法
public MPointData() {
}
// Getters 和 Setters
public Long getItemId() {
return itemId;
}
public void setItemId(Long itemId) {
this.itemId = itemId;
}
public BigDecimal getParmValue() {
return parmValue;
}
public void setParmValue(BigDecimal parmValue) {
this.parmValue = parmValue;
}
public Date getMeasureDt() {
return measureDt;
}
public void setMeasureDt(Date measureDt) {
this.measureDt = measureDt;
}
public String getMemotype() {
return memotype;
}
public void setMemotype(String memotype) {
this.memotype = memotype;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public Date getInsdt() {
return insdt;
}
public void setInsdt(Date insdt) {
this.insdt = insdt;
}
@Override
public String toString() {
return "MPointData{" +
"itemId=" + itemId +
", parmValue=" + parmValue +
", measureDt=" + measureDt +
", memotype='" + memotype + '\'' +
", memo='" + memo + '\'' +
", userid='" + userid + '\'' +
", insdt=" + insdt +
'}';
}
}

View File

@ -1,4 +1,4 @@
package com.sipai.entity.watervolume;
package com.sipai.entity.scada;
import com.sipai.entity.base.SQLAdapter;

View File

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sipai.mapper.scada.MPointDataMapper">
<resultMap id="BaseResultMap" type="com.sipai.entity.scada.MPointData" >
<id column="ItemID" property="itemId" jdbcType="BIGINT" />
<result column="ParmValue" property="parmValue" jdbcType="DECIMAL" />
<result column="MeasureDT" property="measureDt" jdbcType="TIMESTAMP" />
<result column="memotype" property="memotype" jdbcType="VARCHAR" />
<result column="memo" property="memo" jdbcType="VARCHAR" />
<result column="userid" property="userid" jdbcType="VARCHAR" />
<result column="insdt" property="insdt" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
ItemID, ParmValue, MeasureDT, memotype, memo, userid, insdt
</sql>
<!-- 获取最新一条数据(当前值) -->
<select id="selectLatest" resultMap="BaseResultMap">
SELECT TOP 1 <include refid="Base_Column_List" />
FROM ${tableName}
<where>
<if test="companyKey != null and companyKey != ''">
AND userid = #{companyKey}
</if>
<if test="startTime != null">
AND MeasureDT >= #{startTime}
</if>
<if test="endTime != null">
AND MeasureDT &lt;= #{endTime}
</if>
</where>
ORDER BY MeasureDT DESC
</select>
<!-- 获取统计数据:平均值、最高值、最低值 -->
<select id="selectStatistics" resultType="java.util.Map">
SELECT
ISNULL(AVG(ParmValue), 0) as avgValue,
ISNULL(MAX(ParmValue), 0) as maxValue,
ISNULL(MIN(ParmValue), 0) as minValue,
COUNT(*) as totalCount
FROM ${tableName}
<where>
<if test="companyKey != null and companyKey != ''">
AND userid = #{companyKey}
</if>
<if test="startTime != null">
AND MeasureDT >= #{startTime}
</if>
<if test="endTime != null">
AND MeasureDT &lt;= #{endTime}
</if>
</where>
</select>
<!-- 检查表是否存在 -->
<select id="checkTableExists" resultType="int">
SELECT COUNT(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = #{tableName}
</select>
</mapper>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="watervolume.WaterVolumeLJLLMapper">
<resultMap id="BaseResultMap" type="com.sipai.entity.watervolume.WaterVolumeLJLL" >
<mapper namespace="com.sipai.mapper.scada.WaterVolumeLJLLMapper">
<resultMap id="BaseResultMap" type="com.sipai.entity.scada.WaterVolumeLJLL" >
<id column="ItemID" property="itemId" jdbcType="BIGINT" />
<result column="ParmValue" property="parmValue" jdbcType="DECIMAL" />
<result column="MeasureDT" property="measureDt" jdbcType="TIMESTAMP" />

View File

@ -0,0 +1,98 @@
package com.sipai.service.scada;
import com.sipai.dao.scada.MPointDataDao;
import com.sipai.entity.scada.MPointData;
import com.sipai.tools.CommService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class MPointDataService implements CommService<MPointData> {
@Resource
private MPointDataDao mPointDataDao;
/**
* 构建表名
* @param mPointKey 测点编码DO1
* @return 完整表名tb_mp_DO1
*/
public String buildTableName(String mPointKey) {
if (mPointKey == null || mPointKey.isEmpty()) {
throw new IllegalArgumentException("mPointKey cannot be null or empty");
}
return "tb_mp_" + mPointKey;
}
/**
* 检查表是否存在
* @param mPointKey 测点编码
* @return 是否存在
*/
public boolean checkTableExists(String mPointKey) {
String tableName = buildTableName(mPointKey);
return mPointDataDao.checkTableExists(tableName);
}
/**
* 获取最新一条数据(当前值)
* @param mPointKey 测点编码
* @param companyKey 公司编码(可选)
* @param startTime 开始时间(可选)
* @param endTime 结束时间(可选)
* @return 最新一条数据
*/
public MPointData getLatestData(String mPointKey, String companyKey,
Date startTime, Date endTime) {
String tableName = buildTableName(mPointKey);
return mPointDataDao.selectLatest(tableName, companyKey, startTime, endTime);
}
/**
* 获取统计数据
* @param mPointKey 测点编码
* @param companyKey 公司编码(可选)
* @param startTime 开始时间(可选)
* @param endTime 结束时间(可选)
* @return 统计数据
*/
public Map<String, Object> getStatistics(String mPointKey, String companyKey,
Date startTime, Date endTime) {
String tableName = buildTableName(mPointKey);
return mPointDataDao.selectStatistics(tableName, companyKey, startTime, endTime);
}
@Override
public MPointData selectById(String id) {
return null;
}
@Override
public int deleteById(String id) {
return 0;
}
@Override
public int save(MPointData entity) {
return 0;
}
@Override
public int update(MPointData entity) {
return 0;
}
@Override
public List<MPointData> selectListByWhere(String wherestr) {
return null;
}
@Override
public int deleteByWhere(String wherestr) {
return 0;
}
}

View File

@ -1,7 +1,7 @@
package com.sipai.service.scada;
import com.sipai.dao.scada.WaterVolumeLJLLDao;
import com.sipai.entity.watervolume.WaterVolumeLJLL;
import com.sipai.entity.scada.WaterVolumeLJLL;
import com.sipai.tools.CommService;
import org.springframework.stereotype.Service;