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();
|
}
|
}
|
}
|