huang
2025-12-02 628aa6a42e587e9f337e213f87f922fc2ab2af02
mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java
@@ -1,14 +1,17 @@
package com.mes.device.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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 org.springframework.util.CollectionUtils;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -99,8 +102,35 @@
            GlassInfo existing = baseMapper.selectByGlassId(glassInfo.getGlassId());
            if (existing != null) {
                glassInfo.setId(existing.getId());
                // 保留原始创建信息
                if (glassInfo.getCreatedTime() == null) {
                    glassInfo.setCreatedTime(existing.getCreatedTime());
                }
                if (glassInfo.getCreatedBy() == null) {
                    glassInfo.setCreatedBy(existing.getCreatedBy());
                }
                // 更新为当前时间
                if (glassInfo.getUpdatedTime() == null) {
                    glassInfo.setUpdatedTime(new Date());
                }
                if (glassInfo.getUpdatedBy() == null) {
                    glassInfo.setUpdatedBy("system");
                }
                return updateById(glassInfo);
            } else {
                Date now = new Date();
                if (glassInfo.getCreatedTime() == null) {
                    glassInfo.setCreatedTime(now);
                }
                if (glassInfo.getUpdatedTime() == null) {
                    glassInfo.setUpdatedTime(now);
                }
                if (glassInfo.getCreatedBy() == null) {
                    glassInfo.setCreatedBy("system");
                }
                if (glassInfo.getUpdatedBy() == null) {
                    glassInfo.setUpdatedBy("system");
                }
                return save(glassInfo);
            }
        } catch (Exception e) {
@@ -124,5 +154,59 @@
            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.PENDING)
                   .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();
        }
    }
    @Override
    public boolean updateGlassStatus(List<String> glassIds, String status) {
        if (CollectionUtils.isEmpty(glassIds) || status == null) {
            return true;
        }
        try {
            LambdaUpdateWrapper<GlassInfo> wrapper = new LambdaUpdateWrapper<>();
            wrapper.in(GlassInfo::getGlassId, glassIds);
            GlassInfo update = new GlassInfo();
            update.setStatus(status);
            update.setUpdatedTime(new Date());
            update.setUpdatedBy("system");
            return this.update(update, wrapper);
        } catch (Exception e) {
            log.error("批量更新玻璃状态失败, glassIds={}, status={}", glassIds, status, e);
            return false;
        }
    }
}