吴羡
26 分钟以前 3d416372d21acb1a554e4e3eb96e2f9dead6d5a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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<Map<String, Object>> totals = recordMapper.selectTotals(reqVO);
        Map<String, Object> 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<MesDcStatisticsDeviceItemVO> devices = new ArrayList<>();
        for (Map<String, Object> 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<MesDcStatisticsTrendItemVO> getTrend(MesDcStatisticsReqVO reqVO) {
        boolean byHour = "hour".equalsIgnoreCase(reqVO.getGranularity());
        List<MesDcStatisticsTrendItemVO> result = new ArrayList<>();
        for (Map<String, Object> 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<MesDcDeviceOverviewRespVO> getDeviceOverview() {
        List<MesDcDeviceDO> devices = deviceMapper.selectSimpleList();
        // 今日按设备统计
        MesDcStatisticsReqVO todayReqVO = new MesDcStatisticsReqVO();
        todayReqVO.setBeginTime(LocalDate.now().atStartOfDay());
        todayReqVO.setEndTime(LocalDate.now().plusDays(1).atStartOfDay());
        Map<Long, Map<String, Object>> summaryByDevice = new HashMap<>();
        for (Map<String, Object> row : recordMapper.selectDeviceSummary(todayReqVO)) {
            summaryByDevice.put(toLong(row.get("deviceId")), row);
        }
        // 组装概览
        LocalDateTime onlineThreshold = LocalDateTime.now().minusMinutes(ONLINE_TIMEOUT_MINUTES);
        List<MesDcDeviceOverviewRespVO> 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<String, Object> 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();
    }
 
}