累计水量统计接口
This commit is contained in:
@ -0,0 +1,125 @@
|
||||
package com.sipai.controller.watervolume;
|
||||
|
||||
import com.sipai.service.scada.WaterVolumeLJLLService;
|
||||
import net.sf.json.JSONArray;
|
||||
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.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 水量统计控制器
|
||||
* 提供水量统计接口,支持动态表名查询
|
||||
* 表名规则:tb_mp_{companyKey}_LJLL
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/watervolume")
|
||||
public class WaterVolumeLJLLController {
|
||||
|
||||
@Resource
|
||||
private WaterVolumeLJLLService waterVolumeLJLLService;
|
||||
|
||||
/**
|
||||
* 获取水量统计数据
|
||||
*
|
||||
* @param companyKey 公司编码(如:DEV022),必填
|
||||
* @return 统计数据JSON
|
||||
*
|
||||
* 返回内容:
|
||||
* - todayTotal: 今日处理总量
|
||||
* - yesterdayTotal: 昨日处理总量
|
||||
* - monthTotal: 本月累计总量
|
||||
* - sevenDaysTotal: 七日水量处理总量(按日期分组)
|
||||
*
|
||||
* 示例请求:/watervolume/getStatistics.do?companyKey=DEV022
|
||||
*/
|
||||
@RequestMapping("/getStatistics.do")
|
||||
public ModelAndView getStatistics(HttpServletRequest request, Model model,
|
||||
@RequestParam(value = "companyKey") String companyKey) {
|
||||
|
||||
JSONObject result = new JSONObject();
|
||||
|
||||
try {
|
||||
// 参数校验
|
||||
if (companyKey == null || companyKey.trim().isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "companyKey参数不能为空");
|
||||
model.addAttribute("result", result.toString());
|
||||
return new ModelAndView("result");
|
||||
}
|
||||
|
||||
companyKey = companyKey.trim();
|
||||
|
||||
// 检查表是否存在
|
||||
if (!waterVolumeLJLLService.checkTableExists(companyKey)) {
|
||||
result.put("success", false);
|
||||
result.put("message", "未找到公司[" + companyKey + "]对应的水量数据表");
|
||||
result.put("tableName", waterVolumeLJLLService.buildTableName(companyKey));
|
||||
model.addAttribute("result", result.toString());
|
||||
return new ModelAndView("result");
|
||||
}
|
||||
|
||||
// 今日处理总量
|
||||
BigDecimal todayTotal = waterVolumeLJLLService.getTodayTotal(companyKey);
|
||||
result.put("todayTotal", formatAmount(todayTotal));
|
||||
|
||||
// 昨日处理总量
|
||||
BigDecimal yesterdayTotal = waterVolumeLJLLService.getYesterdayTotal(companyKey);
|
||||
result.put("yesterdayTotal", formatAmount(yesterdayTotal));
|
||||
|
||||
// 本月累计总量
|
||||
BigDecimal monthTotal = waterVolumeLJLLService.getMonthTotal(companyKey);
|
||||
result.put("monthTotal", formatAmount(monthTotal));
|
||||
|
||||
// 七日水量处理总量
|
||||
List<Map<String, Object>> sevenDaysData = waterVolumeLJLLService.getSevenDaysTotal(companyKey);
|
||||
JSONArray sevenDaysArray = new JSONArray();
|
||||
if (sevenDaysData != null && !sevenDaysData.isEmpty()) {
|
||||
for (Map<String, Object> dayData : sevenDaysData) {
|
||||
JSONObject dayJson = new JSONObject();
|
||||
dayJson.put("date", dayData.get("date"));
|
||||
Object totalObj = dayData.get("total");
|
||||
if (totalObj instanceof BigDecimal) {
|
||||
dayJson.put("total", formatAmount((BigDecimal) totalObj));
|
||||
} else if (totalObj instanceof Number) {
|
||||
dayJson.put("total", formatAmount(new BigDecimal(totalObj.toString())));
|
||||
} else {
|
||||
dayJson.put("total", "0.00");
|
||||
}
|
||||
sevenDaysArray.add(dayJson);
|
||||
}
|
||||
}
|
||||
result.put("sevenDaysTotal", sevenDaysArray);
|
||||
|
||||
result.put("success", true);
|
||||
result.put("companyKey", companyKey);
|
||||
result.put("tableName", waterVolumeLJLLService.buildTableName(companyKey));
|
||||
|
||||
} 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();
|
||||
}
|
||||
}
|
||||
78
src/main/java/com/sipai/dao/scada/WaterVolumeLJLLDao.java
Normal file
78
src/main/java/com/sipai/dao/scada/WaterVolumeLJLLDao.java
Normal file
@ -0,0 +1,78 @@
|
||||
package com.sipai.dao.scada;
|
||||
|
||||
import com.sipai.dao.base.CommDaoImpl;
|
||||
import com.sipai.entity.watervolume.WaterVolumeLJLL;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Repository
|
||||
public class WaterVolumeLJLLDao extends CommDaoImpl<WaterVolumeLJLL> {
|
||||
|
||||
private static final String MAPPER_NAMESPACE = "watervolume.WaterVolumeLJLLMapper";
|
||||
|
||||
public WaterVolumeLJLLDao() {
|
||||
super();
|
||||
this.setMappernamespace(MAPPER_NAMESPACE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取今日处理总量
|
||||
* @param tableName 表名(如:tb_mp_DEV022_LJLL)
|
||||
* @return 今日处理总量
|
||||
*/
|
||||
public BigDecimal selectTodayTotal(String tableName) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("tableName", tableName);
|
||||
BigDecimal result = this.getSqlSession().selectOne(MAPPER_NAMESPACE + ".selectTodayTotal", params);
|
||||
return result != null ? result : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取昨日处理总量
|
||||
* @param tableName 表名
|
||||
* @return 昨日处理总量
|
||||
*/
|
||||
public BigDecimal selectYesterdayTotal(String tableName) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("tableName", tableName);
|
||||
BigDecimal result = this.getSqlSession().selectOne(MAPPER_NAMESPACE + ".selectYesterdayTotal", params);
|
||||
return result != null ? result : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本月累计总量
|
||||
* @param tableName 表名
|
||||
* @return 本月累计总量
|
||||
*/
|
||||
public BigDecimal selectMonthTotal(String tableName) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("tableName", tableName);
|
||||
BigDecimal result = this.getSqlSession().selectOne(MAPPER_NAMESPACE + ".selectMonthTotal", params);
|
||||
return result != null ? result : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取七日水量处理总量
|
||||
* @param tableName 表名
|
||||
* @return 七日水量列表
|
||||
*/
|
||||
public List<Map<String, Object>> selectSevenDaysTotal(String tableName) {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("tableName", tableName);
|
||||
return this.getSqlSession().selectList(MAPPER_NAMESPACE + ".selectSevenDaysTotal", params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查表是否存在
|
||||
* @param tableName 表名
|
||||
* @return 是否存在
|
||||
*/
|
||||
public boolean checkTableExists(String tableName) {
|
||||
Integer count = this.getSqlSession().selectOne(MAPPER_NAMESPACE + ".checkTableExists", tableName);
|
||||
return count != null && count > 0;
|
||||
}
|
||||
}
|
||||
135
src/main/java/com/sipai/entity/watervolume/WaterVolumeLJLL.java
Normal file
135
src/main/java/com/sipai/entity/watervolume/WaterVolumeLJLL.java
Normal file
@ -0,0 +1,135 @@
|
||||
package com.sipai.entity.watervolume;
|
||||
|
||||
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_{companyKey}_LJLL
|
||||
*/
|
||||
public class WaterVolumeLJLL 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 WaterVolumeLJLL() {
|
||||
}
|
||||
|
||||
// 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 "WaterVolumeLJLL{" +
|
||||
"itemId=" + itemId +
|
||||
", parmValue=" + parmValue +
|
||||
", measureDt=" + measureDt +
|
||||
", memotype='" + memotype + '\'' +
|
||||
", memo='" + memo + '\'' +
|
||||
", userid='" + userid + '\'' +
|
||||
", insdt=" + insdt +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,58 @@
|
||||
<?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" >
|
||||
<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="selectTodayTotal" resultType="java.math.BigDecimal">
|
||||
SELECT ISNULL(SUM(ParmValue), 0)
|
||||
FROM ${tableName}
|
||||
WHERE CONVERT(VARCHAR(10), MeasureDT, 120) = CONVERT(VARCHAR(10), GETDATE(), 120)
|
||||
</select>
|
||||
|
||||
<!-- 昨日处理总量 -->
|
||||
<select id="selectYesterdayTotal" resultType="java.math.BigDecimal">
|
||||
SELECT ISNULL(SUM(ParmValue), 0)
|
||||
FROM ${tableName}
|
||||
WHERE CONVERT(VARCHAR(10), MeasureDT, 120) = CONVERT(VARCHAR(10), DATEADD(day, -1, GETDATE()), 120)
|
||||
</select>
|
||||
|
||||
<!-- 本月累计总量 -->
|
||||
<select id="selectMonthTotal" resultType="java.math.BigDecimal">
|
||||
SELECT ISNULL(SUM(ParmValue), 0)
|
||||
FROM ${tableName}
|
||||
WHERE YEAR(MeasureDT) = YEAR(GETDATE())
|
||||
AND MONTH(MeasureDT) = MONTH(GETDATE())
|
||||
</select>
|
||||
|
||||
<!-- 七日水量处理总量 -->
|
||||
<select id="selectSevenDaysTotal" resultType="java.util.Map">
|
||||
SELECT
|
||||
CONVERT(VARCHAR(10), MeasureDT, 120) as date,
|
||||
ISNULL(SUM(ParmValue), 0) as total
|
||||
FROM ${tableName}
|
||||
WHERE MeasureDT >= DATEADD(day, -6, CONVERT(VARCHAR(10), GETDATE(), 120))
|
||||
AND MeasureDT < DATEADD(day, 1, CONVERT(VARCHAR(10), GETDATE(), 120))
|
||||
GROUP BY CONVERT(VARCHAR(10), MeasureDT, 120)
|
||||
ORDER BY date ASC
|
||||
</select>
|
||||
|
||||
<!-- 检查表是否存在 -->
|
||||
<select id="checkTableExists" resultType="int">
|
||||
SELECT COUNT(*)
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE TABLE_NAME = #{tableName}
|
||||
</select>
|
||||
</mapper>
|
||||
@ -0,0 +1,110 @@
|
||||
package com.sipai.service.scada;
|
||||
|
||||
import com.sipai.dao.scada.WaterVolumeLJLLDao;
|
||||
import com.sipai.entity.watervolume.WaterVolumeLJLL;
|
||||
import com.sipai.tools.CommService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class WaterVolumeLJLLService implements CommService<WaterVolumeLJLL> {
|
||||
|
||||
@Resource
|
||||
private WaterVolumeLJLLDao waterVolumeLJLLDao;
|
||||
|
||||
/**
|
||||
* 构建表名
|
||||
* @param companyKey 公司编码(如:DEV022)
|
||||
* @return 完整表名(如:tb_mp_DEV022_LJLL)
|
||||
*/
|
||||
public String buildTableName(String companyKey) {
|
||||
if (companyKey == null || companyKey.isEmpty()) {
|
||||
throw new IllegalArgumentException("companyKey cannot be null or empty");
|
||||
}
|
||||
return "tb_mp_" + companyKey + "_LJLL";
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查表是否存在
|
||||
* @param companyKey 公司编码
|
||||
* @return 是否存在
|
||||
*/
|
||||
public boolean checkTableExists(String companyKey) {
|
||||
String tableName = buildTableName(companyKey);
|
||||
return waterVolumeLJLLDao.checkTableExists(tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取今日处理总量
|
||||
* @param companyKey 公司编码
|
||||
* @return 今日处理总量
|
||||
*/
|
||||
public BigDecimal getTodayTotal(String companyKey) {
|
||||
String tableName = buildTableName(companyKey);
|
||||
return waterVolumeLJLLDao.selectTodayTotal(tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取昨日处理总量
|
||||
* @param companyKey 公司编码
|
||||
* @return 昨日处理总量
|
||||
*/
|
||||
public BigDecimal getYesterdayTotal(String companyKey) {
|
||||
String tableName = buildTableName(companyKey);
|
||||
return waterVolumeLJLLDao.selectYesterdayTotal(tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本月累计总量
|
||||
* @param companyKey 公司编码
|
||||
* @return 本月累计总量
|
||||
*/
|
||||
public BigDecimal getMonthTotal(String companyKey) {
|
||||
String tableName = buildTableName(companyKey);
|
||||
return waterVolumeLJLLDao.selectMonthTotal(tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取七日水量处理总量
|
||||
* @param companyKey 公司编码
|
||||
* @return 七日水量列表
|
||||
*/
|
||||
public List<Map<String, Object>> getSevenDaysTotal(String companyKey) {
|
||||
String tableName = buildTableName(companyKey);
|
||||
return waterVolumeLJLLDao.selectSevenDaysTotal(tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WaterVolumeLJLL selectById(String id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deleteById(String id) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int save(WaterVolumeLJLL entity) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int update(WaterVolumeLJLL entity) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<WaterVolumeLJLL> selectListByWhere(String wherestr) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int deleteByWhere(String wherestr) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user