Commit 3b9df84ddafa265c1b6063f78816662f807f7e9d

Authored by 杨鸣坤
1 parent 71bd5d49

feat: 新增能耗时序状态分页查询接口

... ... @@ -177,4 +177,20 @@ public class HealthController {
177 177 @RequestParam(required = false) String endDate) {
178 178 return energySearchService.queryEnergyRuntimeDetail(dtuSn, type, startDate, endDate);
179 179 }
  180 +
  181 + /**
  182 + * 查询能耗时序状态 - 分页查询
  183 + * 获取指定日期所有设备的用电量数据,计算稼动率和总用电量
  184 + *
  185 + * @param date 查询日期 yyyy-MM-dd
  186 + * @param pageNo 页码,默认1
  187 + * @param pageSize 每页条数,默认12
  188 + */
  189 + @GetMapping("/energy/timelineStatus")
  190 + public Map<String, Object> energyTimelineStatus(
  191 + @RequestParam String date,
  192 + @RequestParam(defaultValue = "1") Integer pageNo,
  193 + @RequestParam(defaultValue = "12") Integer pageSize) {
  194 + return energySearchService.queryEnergyTimelineStatus(date, pageNo, pageSize);
  195 + }
180 196 }
... ...
... ... @@ -405,6 +405,98 @@ public class EnergySearchService {
405 405 );
406 406 }
407 407
  408 + /**
  409 + * 查询能耗时序状态 - 分页查询
  410 + * 从e_run_dtl(OEE时序)表获取指定日期的设备时序数据,每页12条
  411 + * 同时计算每个设备的稼动率和总用电量(从eq_kwh表)
  412 + *
  413 + * @param date 查询日期 yyyy-MM-dd
  414 + * @param pageNo 页码,默认1
  415 + * @param pageSize 每页条数,默认12
  416 + */
  417 + public Map<String, Object> queryEnergyTimelineStatus(String date, Integer pageNo, Integer pageSize) {
  418 + // 先查OEE时序表获取有数据的设备列表
  419 + String countSql = "SELECT COUNT(DISTINCT r.dtuSn) FROM " + eRunDtlTableName + " r "
  420 + + " INNER JOIN " + energyTableName + " e ON e.dtuSn = r.dtuSn AND e.corp_code = r.corp_code "
  421 + + " WHERE r.corp_code = ? AND r.use_date = ?";
  422 + Long total = jdbcTemplate.queryForObject(countSql, Long.class, energyCorpCode, date + " 00:00:00");
  423 +
  424 + int offset = (pageNo - 1) * pageSize;
  425 + String querySql = "SELECT r.dtuSn, e.deviceName, r.runStatus1, r.runStatus2 FROM " + eRunDtlTableName + " r "
  426 + + " INNER JOIN " + energyTableName + " e ON e.dtuSn = r.dtuSn AND e.corp_code = r.corp_code "
  427 + + " WHERE r.corp_code = ? AND r.use_date = ? "
  428 + + " ORDER BY e.deviceName ASC LIMIT ?, ?";
  429 + List<Map<String, Object>> rows = jdbcTemplate.queryForList(querySql, energyCorpCode, date + " 00:00:00", offset, pageSize);
  430 +
  431 + List<Map<String, Object>> list = new ArrayList<>();
  432 + for (Map<String, Object> row : rows) {
  433 + Map<String, Object> item = new LinkedHashMap<>();
  434 + item.put("dtuSn", row.get("dtuSn"));
  435 + item.put("deviceName", row.get("deviceName"));
  436 +
  437 + // OEE时序数据(原始返回)
  438 + Object oeeRawData = Collections.emptyList();
  439 + long totalDuration = 0;
  440 + long runDuration = 0;
  441 +
  442 + String rs1 = row.get("runStatus1") != null ? String.valueOf(row.get("runStatus1")) : "";
  443 + String rs2 = row.get("runStatus2") != null ? String.valueOf(row.get("runStatus2")) : "";
  444 + if (StringUtils.hasText(rs1) || StringUtils.hasText(rs2)) {
  445 + String jsonStr = rs1 + rs2;
  446 + try {
  447 + JSONArray dataArray = JSON.parseArray(jsonStr);
  448 + oeeRawData = dataArray;
  449 + for (int i = 0; i < dataArray.size(); i++) {
  450 + JSONObject jo = dataArray.getJSONObject(i);
  451 + Long duration = jo.getLong("duration");
  452 + Integer runStatus = jo.getInteger("runStatus");
  453 + if (duration != null && duration > 0 && runStatus != null) {
  454 + totalDuration += duration;
  455 + if (runStatus == 3) runDuration += duration; // 运行状态
  456 + }
  457 + }
  458 + } catch (Exception ignored) {}
  459 + }
  460 +
  461 + item.put("timelineList", oeeRawData);
  462 + item.put("totalDurationFormatted", formatDuration(totalDuration));
  463 + item.put("totalDurationSeconds", totalDuration);
  464 +
  465 + // 从eq_kwh表获取用电量
  466 + BigDecimal totalKwh = BigDecimal.ZERO;
  467 + try {
  468 + String kwhSql = "SELECT description FROM " + eqKwhTableName
  469 + + " WHERE corp_code = ? AND dtuSn = ? AND use_date = ?";
  470 + Map<String, Object> kwhRow = jdbcTemplate.queryForMap(kwhSql, energyCorpCode, row.get("dtuSn"), date + " 00:00:00");
  471 + if (kwhRow != null && kwhRow.get("description") != null) {
  472 + JSONArray dataArray = JSON.parseArray(String.valueOf(kwhRow.get("description")));
  473 + for (int i = 0; i < dataArray.size(); i++) {
  474 + Double value = dataArray.getJSONObject(i).getDouble("value");
  475 + if (value != null) totalKwh = totalKwh.add(BigDecimal.valueOf(value));
  476 + }
  477 + }
  478 + } catch (Exception ignored) {}
  479 +
  480 + item.put("totalKwh", totalKwh.setScale(2, RoundingMode.HALF_UP));
  481 + // 稼动率 = 运行时长 / 总时长
  482 + double utilizationRate = totalDuration > 0
  483 + ? BigDecimal.valueOf(runDuration * 100.0 / totalDuration).setScale(2, RoundingMode.HALF_UP).doubleValue()
  484 + : 0.0;
  485 + item.put("utilizationRate", utilizationRate);
  486 +
  487 + list.add(item);
  488 + }
  489 +
  490 + return Map.of(
  491 + "code", 200,
  492 + "msg", "请求成功",
  493 + "total", total != null ? total : 0,
  494 + "pageNo", pageNo,
  495 + "pageSize", pageSize,
  496 + "list", list
  497 + );
  498 + }
  499 +
408 500 private static Map<Integer, Long> initStatusMap() {
409 501 Map<Integer, Long> map = new LinkedHashMap<>();
410 502 map.put(0, 0L);
... ...