huang
2025-11-26 792236ef78c2cdd3a989fb40a7f2e2487c4e17b6
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
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.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;
        }
    }
}