huang
2025-12-01 dad0263459b30dbfa75f06dff062a0c85183517b
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.mes.device.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mes.device.entity.GlassInfo;
import com.mes.device.mapper.DeviceGlassInfoMapper;
import com.mes.device.service.GlassInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 玻璃信息服务实现类
 * 
 * @author mes
 * @since 2024-11-20
 */
@Slf4j
@Service("deviceGlassInfoService")
public class GlassInfoServiceImpl extends ServiceImpl<DeviceGlassInfoMapper, GlassInfo> implements GlassInfoService {
 
    @Override
    public GlassInfo getGlassInfo(String glassId) {
        if (glassId == null || glassId.trim().isEmpty()) {
            return null;
        }
        try {
            return baseMapper.selectByGlassId(glassId.trim());
        } catch (Exception e) {
            log.error("查询玻璃信息失败, glassId={}", glassId, e);
            return null;
        }
    }
 
    @Override
    public Integer getGlassLength(String glassId) {
        GlassInfo glassInfo = getGlassInfo(glassId);
        return glassInfo != null ? glassInfo.getGlassLength() : null;
    }
 
    @Override
    public List<GlassInfo> getGlassInfos(List<String> glassIds) {
        if (glassIds == null || glassIds.isEmpty()) {
            return Collections.emptyList();
        }
        try {
            // 过滤空值并去重
            List<String> validIds = glassIds.stream()
                    .filter(id -> id != null && !id.trim().isEmpty())
                    .map(String::trim)
                    .distinct()
                    .collect(Collectors.toList());
            
            if (validIds.isEmpty()) {
                return Collections.emptyList();
            }
            
            return baseMapper.selectByGlassIds(validIds);
        } catch (Exception e) {
            log.error("批量查询玻璃信息失败, glassIds={}", glassIds, e);
            return Collections.emptyList();
        }
    }
 
    @Override
    public Map<String, Integer> getGlassLengthMap(List<String> glassIds) {
        Map<String, Integer> lengthMap = new HashMap<>();
        
        if (glassIds == null || glassIds.isEmpty()) {
            return lengthMap;
        }
        
        try {
            List<GlassInfo> glassInfos = getGlassInfos(glassIds);
            for (GlassInfo glassInfo : glassInfos) {
                if (glassInfo.getGlassId() != null && glassInfo.getGlassLength() != null) {
                    lengthMap.put(glassInfo.getGlassId(), glassInfo.getGlassLength());
                }
            }
        } catch (Exception e) {
            log.error("获取玻璃长度映射失败, glassIds={}", glassIds, e);
        }
        
        return lengthMap;
    }
 
    @Override
    public boolean saveOrUpdateGlassInfo(GlassInfo glassInfo) {
        if (glassInfo == null || glassInfo.getGlassId() == null) {
            return false;
        }
        try {
            // 检查是否已存在
            GlassInfo existing = baseMapper.selectByGlassId(glassInfo.getGlassId());
            if (existing != null) {
                glassInfo.setId(existing.getId());
                return updateById(glassInfo);
            } else {
                return save(glassInfo);
            }
        } catch (Exception e) {
            log.error("保存或更新玻璃信息失败, glassInfo={}", glassInfo, e);
            return false;
        }
    }
 
    @Override
    public boolean batchSaveOrUpdateGlassInfo(List<GlassInfo> glassInfos) {
        if (glassInfos == null || glassInfos.isEmpty()) {
            return true;
        }
        try {
            for (GlassInfo glassInfo : glassInfos) {
                saveOrUpdateGlassInfo(glassInfo);
            }
            return true;
        } catch (Exception e) {
            log.error("批量保存或更新玻璃信息失败", e);
            return false;
        }
    }
 
    @Override
    public List<String> getRecentScannedGlassIds(Integer minutesAgo, Integer maxCount, String workLine) {
        try {
            // 默认查询最近2分钟内的记录,最多返回20条
            int minutes = minutesAgo != null && minutesAgo > 0 ? minutesAgo : 2;
            int limit = maxCount != null && maxCount > 0 ? maxCount : 20;
            
            Date timeThreshold = new Date(System.currentTimeMillis() - minutes * 60 * 1000L);
            
            LambdaQueryWrapper<GlassInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(GlassInfo::getStatus, GlassInfo.Status.ACTIVE)
                   .ge(GlassInfo::getCreatedTime, timeThreshold)
                   .orderByDesc(GlassInfo::getCreatedTime)
                   .last("LIMIT " + limit);
            
            // 如果指定了workLine,则过滤description
            if (workLine != null && !workLine.trim().isEmpty()) {
                wrapper.like(GlassInfo::getDescription, "workLine=" + workLine);
            }
            
            List<GlassInfo> recentGlasses = baseMapper.selectList(wrapper);
            
            // 提取玻璃ID列表
            return recentGlasses.stream()
                    .map(GlassInfo::getGlassId)
                    .filter(id -> id != null && !id.trim().isEmpty())
                    .collect(Collectors.toList());
            
        } catch (Exception e) {
            log.error("查询最近扫码的玻璃ID失败, minutesAgo={}, maxCount={}, workLine={}", 
                    minutesAgo, maxCount, workLine, e);
            return Collections.emptyList();
        }
    }
}