|
|
@@ -0,0 +1,254 @@
|
|
|
+package com.jeesite.modules.tianHong.service;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.LinkedHashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import com.jeesite.common.entity.Page;
|
|
|
+import com.jeesite.common.lang.StringUtils;
|
|
|
+import com.jeesite.common.service.CrudService;
|
|
|
+import com.jeesite.modules.tianHong.dao.MachineAttrHistoryDao;
|
|
|
+import com.jeesite.modules.tianHong.entity.MachineAttrHistory;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 设备历史属性Service
|
|
|
+ * @author ljr
|
|
|
+ * @version 2026-07-07
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Transactional(readOnly = true)
|
|
|
+public class MachineAttrHistoryService extends CrudService<MachineAttrHistoryDao, MachineAttrHistory> {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页:必须先选设备;按 event_time 倒序,每页默认 20;关联表按「秒」补齐
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Page<MachineAttrHistory> findPage(MachineAttrHistory machineAttrHistory) {
|
|
|
+ Page<MachineAttrHistory> page = machineAttrHistory.getPage();
|
|
|
+ if (page == null) {
|
|
|
+ page = new Page<>();
|
|
|
+ machineAttrHistory.setPage(page);
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(machineAttrHistory.getEqpId())) {
|
|
|
+ page.setList(Collections.emptyList());
|
|
|
+ page.setCount(0);
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+ int pageSize = page.getPageSize() > 0 ? page.getPageSize() : 20;
|
|
|
+ int pageNo = page.getPageNo() > 0 ? page.getPageNo() : 1;
|
|
|
+ if (pageSize > 100) {
|
|
|
+ pageSize = 100;
|
|
|
+ }
|
|
|
+ page.setPageSize(pageSize);
|
|
|
+ page.setPageNo(pageNo);
|
|
|
+
|
|
|
+ machineAttrHistory.setPage(null);
|
|
|
+ Long count = dao.findHistoryCount(machineAttrHistory);
|
|
|
+ page.setCount(count == null ? 0L : count);
|
|
|
+
|
|
|
+ machineAttrHistory.setLimit(pageSize);
|
|
|
+ machineAttrHistory.setOffset((pageNo - 1) * pageSize);
|
|
|
+ List<MachineAttrHistory> list = dao.findList(machineAttrHistory);
|
|
|
+ fillRelatedBySecond(machineAttrHistory.getEqpId(), list);
|
|
|
+ page.setList(list);
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询列表(必须带设备号)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<MachineAttrHistory> findList(MachineAttrHistory machineAttrHistory) {
|
|
|
+ if (StringUtils.isBlank(machineAttrHistory.getEqpId())) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ if (machineAttrHistory.getLimit() == null) {
|
|
|
+ machineAttrHistory.setLimit(20);
|
|
|
+ }
|
|
|
+ if (machineAttrHistory.getOffset() == null) {
|
|
|
+ machineAttrHistory.setOffset(0);
|
|
|
+ }
|
|
|
+ List<MachineAttrHistory> list = dao.findList(machineAttrHistory);
|
|
|
+ fillRelatedBySecond(machineAttrHistory.getEqpId(), list);
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用本页时间点截断到秒,按索引范围补齐温压电量(避免大表按秒 JOIN)
|
|
|
+ */
|
|
|
+ private void fillRelatedBySecond(String eqpId, List<MachineAttrHistory> list) {
|
|
|
+ if (StringUtils.isBlank(eqpId) || list == null || list.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Set<Date> secSet = new LinkedHashSet<>();
|
|
|
+ for (MachineAttrHistory row : list) {
|
|
|
+ if (row.getAttrEventTime() != null) {
|
|
|
+ secSet.add(truncateToSecond(row.getAttrEventTime()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (secSet.isEmpty()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<Date> secList = new ArrayList<>(secSet);
|
|
|
+
|
|
|
+ Map<Long, MachineAttrHistory> cruMap = latestBySec(dao.findCruBySecList(eqpId, secList));
|
|
|
+ Map<Long, MachineAttrHistory> aluMap = latestBySec(dao.findAluBySecList(eqpId, secList));
|
|
|
+ Map<Long, MachineAttrHistory> mouldMap = latestBySec(dao.findMouldBySecList(eqpId, secList));
|
|
|
+ Map<Long, MachineAttrHistory> pressMap = latestBySec(dao.findPressureBySecList(eqpId, secList));
|
|
|
+ Map<Long, MachineAttrHistory> eleMap = latestBySec(dao.findEleBySecList(eqpId, secList));
|
|
|
+
|
|
|
+ for (MachineAttrHistory row : list) {
|
|
|
+ if (row.getAttrEventTime() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Long key = secKey(row.getAttrEventTime());
|
|
|
+ MachineAttrHistory cru = cruMap.get(key);
|
|
|
+ if (cru != null) {
|
|
|
+ row.setCruTemp(cru.getCruTemp());
|
|
|
+ row.setCruTempTime(cru.getCruTempTime());
|
|
|
+ }
|
|
|
+ MachineAttrHistory alu = aluMap.get(key);
|
|
|
+ if (alu != null) {
|
|
|
+ row.setAluTemp(alu.getAluTemp());
|
|
|
+ row.setAluTempTime(alu.getAluTempTime());
|
|
|
+ }
|
|
|
+ MachineAttrHistory mould = mouldMap.get(key);
|
|
|
+ if (mould != null) {
|
|
|
+ row.setMouldInfo(mould.getMouldInfo());
|
|
|
+ row.setMouldTempTime(mould.getMouldTempTime());
|
|
|
+ }
|
|
|
+ MachineAttrHistory press = pressMap.get(key);
|
|
|
+ if (press != null) {
|
|
|
+ row.setTheoryPressure(press.getTheoryPressure());
|
|
|
+ row.setRealPressure(press.getRealPressure());
|
|
|
+ row.setPressureLevel(press.getPressureLevel());
|
|
|
+ row.setPressureTime(press.getPressureTime());
|
|
|
+ }
|
|
|
+ MachineAttrHistory ele = eleMap.get(key);
|
|
|
+ if (ele != null) {
|
|
|
+ row.setEleConsume(ele.getEleConsume());
|
|
|
+ row.setEleConsumeTime(ele.getEleConsumeTime());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 同一秒多条时保留 EventTime 最新的一条 */
|
|
|
+ private Map<Long, MachineAttrHistory> latestBySec(List<MachineAttrHistory> rows) {
|
|
|
+ Map<Long, MachineAttrHistory> map = new HashMap<>();
|
|
|
+ if (rows == null) {
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ for (MachineAttrHistory row : rows) {
|
|
|
+ Date t = row.getEventTime();
|
|
|
+ if (t == null) {
|
|
|
+ t = row.getCruTempTime();
|
|
|
+ }
|
|
|
+ if (t == null) {
|
|
|
+ t = row.getAluTempTime();
|
|
|
+ }
|
|
|
+ if (t == null) {
|
|
|
+ t = row.getMouldTempTime();
|
|
|
+ }
|
|
|
+ if (t == null) {
|
|
|
+ t = row.getPressureTime();
|
|
|
+ }
|
|
|
+ if (t == null) {
|
|
|
+ t = row.getEleConsumeTime();
|
|
|
+ }
|
|
|
+ if (t == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Long key = secKey(t);
|
|
|
+ MachineAttrHistory old = map.get(key);
|
|
|
+ if (old == null || compareTime(t, resolveTime(old)) > 0) {
|
|
|
+ if (row.getEventTime() == null) {
|
|
|
+ row.setEventTime(t);
|
|
|
+ }
|
|
|
+ map.put(key, row);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Date resolveTime(MachineAttrHistory row) {
|
|
|
+ if (row.getEventTime() != null) {
|
|
|
+ return row.getEventTime();
|
|
|
+ }
|
|
|
+ if (row.getCruTempTime() != null) {
|
|
|
+ return row.getCruTempTime();
|
|
|
+ }
|
|
|
+ if (row.getAluTempTime() != null) {
|
|
|
+ return row.getAluTempTime();
|
|
|
+ }
|
|
|
+ if (row.getMouldTempTime() != null) {
|
|
|
+ return row.getMouldTempTime();
|
|
|
+ }
|
|
|
+ if (row.getPressureTime() != null) {
|
|
|
+ return row.getPressureTime();
|
|
|
+ }
|
|
|
+ return row.getEleConsumeTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ private int compareTime(Date a, Date b) {
|
|
|
+ if (a == null && b == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (a == null) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if (b == null) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return a.compareTo(b);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Date truncateToSecond(Date date) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ return cal.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 用 epoch 秒做 map key,避免 Date 毫秒差异 */
|
|
|
+ private Long secKey(Date date) {
|
|
|
+ return date.getTime() / 1000L;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询指定设备的所有属性明细(用于详情展示)
|
|
|
+ */
|
|
|
+ public List<MachineAttrHistory> findAttrList(MachineAttrHistory machineAttrHistory) {
|
|
|
+ if (StringUtils.isBlank(machineAttrHistory.getEqpId())) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return dao.findAttrList(machineAttrHistory);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取单条数据(按 eqp_id 查询属性明细)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public MachineAttrHistory get(MachineAttrHistory machineAttrHistory) {
|
|
|
+ if (StringUtils.isBlank(machineAttrHistory.getEqpId())) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ MachineAttrHistory entity = new MachineAttrHistory();
|
|
|
+ entity.setEqpId(machineAttrHistory.getEqpId());
|
|
|
+ MachineAttrHistory queryParam = new MachineAttrHistory();
|
|
|
+ queryParam.setEqpId(machineAttrHistory.getEqpId());
|
|
|
+ queryParam.setBeginTime(machineAttrHistory.getBeginTime());
|
|
|
+ queryParam.setEndTime(machineAttrHistory.getEndTime());
|
|
|
+ entity.setAttrList(dao.findAttrList(queryParam));
|
|
|
+ return entity;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|