diff --git a/src/main/java/com/sipai/controller/mpoint/MPointDataController.java b/src/main/java/com/sipai/controller/mpoint/MPointDataController.java new file mode 100644 index 00000000..533cc49f --- /dev/null +++ b/src/main/java/com/sipai/controller/mpoint/MPointDataController.java @@ -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 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); + } +} diff --git a/src/main/java/com/sipai/dao/scada/MPointDataDao.java b/src/main/java/com/sipai/dao/scada/MPointDataDao.java new file mode 100644 index 00000000..201c6825 --- /dev/null +++ b/src/main/java/com/sipai/dao/scada/MPointDataDao.java @@ -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 { + + 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 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 selectStatistics(String tableName, String companyKey, + java.util.Date startTime, java.util.Date endTime) { + Map 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; + } +} diff --git a/src/main/java/com/sipai/dao/scada/WaterVolumeLJLLDao.java b/src/main/java/com/sipai/dao/scada/WaterVolumeLJLLDao.java index 6a6dd651..67885b2c 100644 --- a/src/main/java/com/sipai/dao/scada/WaterVolumeLJLLDao.java +++ b/src/main/java/com/sipai/dao/scada/WaterVolumeLJLLDao.java @@ -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 { - private static final String MAPPER_NAMESPACE = "watervolume.WaterVolumeLJLLMapper"; + private static final String MAPPER_NAMESPACE = "scada.WaterVolumeLJLLMapper"; public WaterVolumeLJLLDao() { super(); diff --git a/src/main/java/com/sipai/entity/scada/MPointData.java b/src/main/java/com/sipai/entity/scada/MPointData.java new file mode 100644 index 00000000..28e4139e --- /dev/null +++ b/src/main/java/com/sipai/entity/scada/MPointData.java @@ -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 + + '}'; + } +} diff --git a/src/main/java/com/sipai/entity/watervolume/WaterVolumeLJLL.java b/src/main/java/com/sipai/entity/scada/WaterVolumeLJLL.java similarity index 98% rename from src/main/java/com/sipai/entity/watervolume/WaterVolumeLJLL.java rename to src/main/java/com/sipai/entity/scada/WaterVolumeLJLL.java index c3c3a5b8..504a38df 100644 --- a/src/main/java/com/sipai/entity/watervolume/WaterVolumeLJLL.java +++ b/src/main/java/com/sipai/entity/scada/WaterVolumeLJLL.java @@ -1,4 +1,4 @@ -package com.sipai.entity.watervolume; +package com.sipai.entity.scada; import com.sipai.entity.base.SQLAdapter; diff --git a/src/main/java/com/sipai/mapper/scada/MPointDataMapper.xml b/src/main/java/com/sipai/mapper/scada/MPointDataMapper.xml new file mode 100644 index 00000000..3cc52165 --- /dev/null +++ b/src/main/java/com/sipai/mapper/scada/MPointDataMapper.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + ItemID, ParmValue, MeasureDT, memotype, memo, userid, insdt + + + + + + + + + + + diff --git a/src/main/java/com/sipai/mapper/scada/WaterVolumeLJLLMapper.xml b/src/main/java/com/sipai/mapper/scada/WaterVolumeLJLLMapper.xml index 64a39413..7d95f1ce 100644 --- a/src/main/java/com/sipai/mapper/scada/WaterVolumeLJLLMapper.xml +++ b/src/main/java/com/sipai/mapper/scada/WaterVolumeLJLLMapper.xml @@ -1,7 +1,7 @@ - - + + diff --git a/src/main/java/com/sipai/service/scada/MPointDataService.java b/src/main/java/com/sipai/service/scada/MPointDataService.java new file mode 100644 index 00000000..fce7eb25 --- /dev/null +++ b/src/main/java/com/sipai/service/scada/MPointDataService.java @@ -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 { + + @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 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 selectListByWhere(String wherestr) { + return null; + } + + @Override + public int deleteByWhere(String wherestr) { + return 0; + } +} diff --git a/src/main/java/com/sipai/service/scada/WaterVolumeLJLLService.java b/src/main/java/com/sipai/service/scada/WaterVolumeLJLLService.java index 9424927f..f10b150b 100644 --- a/src/main/java/com/sipai/service/scada/WaterVolumeLJLLService.java +++ b/src/main/java/com/sipai/service/scada/WaterVolumeLJLLService.java @@ -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;