package cn.iocoder.yudao.module.mes.service.dc.statistics; import cn.hutool.core.util.ObjUtil; import cn.iocoder.yudao.module.mes.controller.admin.dc.statistics.vo.MesDcDeviceOverviewRespVO; import cn.iocoder.yudao.module.mes.controller.admin.dc.statistics.vo.MesDcStatisticsDeviceItemVO; import cn.iocoder.yudao.module.mes.controller.admin.dc.statistics.vo.MesDcStatisticsReqVO; import cn.iocoder.yudao.module.mes.controller.admin.dc.statistics.vo.MesDcStatisticsSummaryRespVO; import cn.iocoder.yudao.module.mes.controller.admin.dc.statistics.vo.MesDcStatisticsTrendItemVO; import cn.iocoder.yudao.module.mes.dal.dataobject.dc.device.MesDcDeviceDO; import cn.iocoder.yudao.module.mes.dal.mysql.dc.device.MesDcDeviceMapper; import cn.iocoder.yudao.module.mes.dal.mysql.dc.record.MesDcRecordMapper; import cn.iocoder.yudao.module.mes.enums.dc.MesDcDeviceStatusEnum; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * MES 数据采集统计 Service 实现类 * * @author 超级管理员 */ @Service @Validated public class MesDcStatisticsServiceImpl implements MesDcStatisticsService { /** * 设备在线判定阈值(分钟):超过该时间无心跳视为离线 */ private static final int ONLINE_TIMEOUT_MINUTES = 5; @Resource private MesDcRecordMapper recordMapper; @Resource private MesDcDeviceMapper deviceMapper; @Override public MesDcStatisticsSummaryRespVO getSummary(MesDcStatisticsReqVO reqVO) { MesDcStatisticsSummaryRespVO summary = new MesDcStatisticsSummaryRespVO(); // 1. 汇总数量、重量、条数 List> totals = recordMapper.selectTotals(reqVO); Map totalRow = totals.isEmpty() ? Map.of() : totals.get(0); summary.setTotalQuantity(toLong(totalRow.get("totalQuantity"))); summary.setTotalWeight(toDecimal(totalRow.get("totalWeight"))); summary.setRecordCount(toLong(totalRow.get("recordCount"))); // 2. 按设备分组 List devices = new ArrayList<>(); for (Map row : recordMapper.selectDeviceSummary(reqVO)) { MesDcStatisticsDeviceItemVO item = new MesDcStatisticsDeviceItemVO(); item.setDeviceId(toLong(row.get("deviceId"))); item.setDeviceCode(toString(row.get("deviceCode"))); item.setDeviceName(toString(row.get("deviceName"))); item.setTotalQuantity(toLong(row.get("totalQuantity"))); item.setTotalWeight(toDecimal(row.get("totalWeight"))); item.setRecordCount(toLong(row.get("recordCount"))); devices.add(item); } summary.setDevices(devices); summary.setDeviceCount(devices.size()); return summary; } @Override public List getTrend(MesDcStatisticsReqVO reqVO) { boolean byHour = "hour".equalsIgnoreCase(reqVO.getGranularity()); List result = new ArrayList<>(); for (Map row : recordMapper.selectTrend(reqVO, byHour)) { MesDcStatisticsTrendItemVO item = new MesDcStatisticsTrendItemVO(); item.setPeriod(toString(row.get("period"))); item.setTotalQuantity(toLong(row.get("totalQuantity"))); item.setTotalWeight(toDecimal(row.get("totalWeight"))); item.setRecordCount(toLong(row.get("recordCount"))); result.add(item); } return result; } @Override public List getDeviceOverview() { List devices = deviceMapper.selectSimpleList(); // 今日按设备统计 MesDcStatisticsReqVO todayReqVO = new MesDcStatisticsReqVO(); todayReqVO.setBeginTime(LocalDate.now().atStartOfDay()); todayReqVO.setEndTime(LocalDate.now().plusDays(1).atStartOfDay()); Map> summaryByDevice = new HashMap<>(); for (Map row : recordMapper.selectDeviceSummary(todayReqVO)) { summaryByDevice.put(toLong(row.get("deviceId")), row); } // 组装概览 LocalDateTime onlineThreshold = LocalDateTime.now().minusMinutes(ONLINE_TIMEOUT_MINUTES); List result = new ArrayList<>(devices.size()); for (MesDcDeviceDO device : devices) { MesDcDeviceOverviewRespVO item = new MesDcDeviceOverviewRespVO(); item.setDeviceId(device.getId()); item.setCode(device.getCode()); item.setName(device.getName()); item.setEnabled(ObjUtil.equal(device.getStatus(), MesDcDeviceStatusEnum.ENABLE.getStatus())); item.setOnline(device.getLastHeartbeatTime() != null && device.getLastHeartbeatTime().isAfter(onlineThreshold)); item.setWorkOrderId(device.getWorkOrderId()); item.setWorkOrderCode(device.getWorkOrderCode()); Map row = summaryByDevice.get(device.getId()); item.setTodayQuantity(row == null ? 0L : toLong(row.get("totalQuantity"))); item.setTodayWeight(row == null ? BigDecimal.ZERO : toDecimal(row.get("totalWeight"))); item.setLastIngestTime(device.getLastIngestTime()); item.setLastHeartbeatTime(device.getLastHeartbeatTime()); result.add(item); } return result; } // ==================== 转换方法 ==================== private static long toLong(Object value) { if (value == null) { return 0L; } if (value instanceof Number number) { return number.longValue(); } return Long.parseLong(value.toString()); } private static BigDecimal toDecimal(Object value) { if (value == null) { return BigDecimal.ZERO; } if (value instanceof BigDecimal decimal) { return decimal; } if (value instanceof Number number) { return new BigDecimal(number.toString()); } return new BigDecimal(value.toString()); } private static String toString(Object value) { return value == null ? null : value.toString(); } }