diff --git a/src/main/java/com/sipai/controller/watervolume/WaterVolumeLJLLController.java b/src/main/java/com/sipai/controller/watervolume/WaterVolumeLJLLController.java new file mode 100644 index 00000000..32dd36f2 --- /dev/null +++ b/src/main/java/com/sipai/controller/watervolume/WaterVolumeLJLLController.java @@ -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> sevenDaysData = waterVolumeLJLLService.getSevenDaysTotal(companyKey); + JSONArray sevenDaysArray = new JSONArray(); + if (sevenDaysData != null && !sevenDaysData.isEmpty()) { + for (Map 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(); + } +} diff --git a/src/main/java/com/sipai/dao/scada/WaterVolumeLJLLDao.java b/src/main/java/com/sipai/dao/scada/WaterVolumeLJLLDao.java new file mode 100644 index 00000000..6a6dd651 --- /dev/null +++ b/src/main/java/com/sipai/dao/scada/WaterVolumeLJLLDao.java @@ -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 { + + 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 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 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 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> selectSevenDaysTotal(String tableName) { + Map 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; + } +} diff --git a/src/main/java/com/sipai/entity/watervolume/WaterVolumeLJLL.java b/src/main/java/com/sipai/entity/watervolume/WaterVolumeLJLL.java new file mode 100644 index 00000000..c3c3a5b8 --- /dev/null +++ b/src/main/java/com/sipai/entity/watervolume/WaterVolumeLJLL.java @@ -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 + + '}'; + } +} diff --git a/src/main/java/com/sipai/mapper/scada/WaterVolumeLJLLMapper.xml b/src/main/java/com/sipai/mapper/scada/WaterVolumeLJLLMapper.xml new file mode 100644 index 00000000..64a39413 --- /dev/null +++ b/src/main/java/com/sipai/mapper/scada/WaterVolumeLJLLMapper.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + ItemID, ParmValue, MeasureDT, memotype, memo, userid, insdt + + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/sipai/service/scada/WaterVolumeLJLLService.java b/src/main/java/com/sipai/service/scada/WaterVolumeLJLLService.java new file mode 100644 index 00000000..9424927f --- /dev/null +++ b/src/main/java/com/sipai/service/scada/WaterVolumeLJLLService.java @@ -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 { + + @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> 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 selectListByWhere(String wherestr) { + return null; + } + + @Override + public int deleteByWhere(String wherestr) { + return 0; + } +}