huang
7 天以前 22e17b6db03ca58bc477a35ca067e55a09cffce7
mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java
@@ -5,17 +5,16 @@
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.EngineeringSequenceService;
import com.mes.device.service.GlassInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static java.util.stream.IntStream.range;
@@ -27,8 +26,12 @@
 * @since 2024-11-20
 */
@Slf4j
@RefreshScope
@Service("deviceGlassInfoService")
public class GlassInfoServiceImpl extends ServiceImpl<DeviceGlassInfoMapper, GlassInfo> implements GlassInfoService {
    @Autowired
    private EngineeringSequenceService engineeringSequenceService;
    @Override
    public GlassInfo getGlassInfo(String glassId) {
@@ -212,6 +215,14 @@
        }
    }
    @Value("${mes.engineering.import-url}")
    private String mesEngineeringImportUrl;
    @Override
    public String getMesEngineeringImportUrl() {
        return mesEngineeringImportUrl;
    }
    @Override
    public Map<String, Object> buildEngineerImportPayload(List<Map<String, Object>> excelRows) {
        Map<String, Object> result = new HashMap<>();
@@ -219,9 +230,8 @@
            return result;
        }
        // 工程号生成:P + yyMMdd + 序号(2位)
        AtomicInteger seq = new AtomicInteger(1);
        final String engineerId = generateEngineerId(firstValue(excelRows, "glassId"), seq.getAndIncrement());
        // 工程号生成:使用数据库自增序号,避免重复
        final String engineerId = engineeringSequenceService.generateAndSaveEngineeringId(new Date());
        final String filmsIdDefault = firstValue(excelRows, "filmsId", "白玻");
        final double thicknessDefault = parseDouble(firstValue(excelRows, "thickness"), 0d);
@@ -235,7 +245,6 @@
                    int qty = (int) parseDouble(row.getOrDefault("quantity", 1), 1);
                    if (qty <= 0) qty = 1;
                    String glassId = str(row.get("glassId"));
                    Integer orderNumber = Integer.parseInt(str(row.get("orderNumber")));
                    String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
                    String flowCardId = str(row.get("flowCardId"));
                    String productName = str(row.get("productName"));
@@ -256,7 +265,6 @@
                        m.put("glassId", finalGlassId);
                        m.put("engineerId", engineerIdFinal);
                        m.put("flowCardId", finalFlowCardId);
                        m.put("orderNumber", orderNumber);
                        m.put("productSortNumber", idx + 1);
                        m.put("hollowCombineDirection", "0");
                        m.put("width", width);
@@ -279,7 +287,7 @@
                        m.put("combine", 0);
                        m.put("markIcon", "");
                        m.put("filmRemove", 0);
                        m.put("flowCardSequence", String.valueOf(idx + 1));
                        m.put("flowCardSequence", flowCardId + "/" + (idx + 1));
                        m.put("process", "");
                        m.put("rawAngle", 0);
                        m.put("graphNo", 0);
@@ -324,7 +332,7 @@
            double height = parseDouble(row.get("height"), 0d);
            double thickness = parseDouble(row.get("thickness"), thicknessDefaultFinal);
            String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
            Integer orderNumber = Integer.parseInt(str(row.get("orderNumber")));
            String productName = str(row.get("productName"));
            String customerName = str(row.get("customerName"));
@@ -339,7 +347,6 @@
                m.put("totalLayer", 0);
                m.put("layer", 0);
                m.put("glassTotal", 1);
                m.put("orderNumber", orderNumber);
                m.put("productName", productName);
                m.put("customerName", customerName);
                flowCardMap.put(flowCardId, m);
@@ -381,47 +388,6 @@
        return result;
    }
    // 日期格式化器(线程不安全,使用ThreadLocal保证线程安全)
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyMMdd");
    // 数字匹配正则(预编译提升性能)
    private static final Pattern DIGIT_PATTERN = Pattern.compile("\\d+");
    /**
     * 生成工程师ID
     * 格式规则:P + 年月日(yyMMdd) + 两位序号
     * 序号优先从glassId中提取末尾两位数字,否则使用传入的index补零
     *
     * @param glassId 玻璃ID(可为null,用于提取数字序号)
     * @param index   备用序号(当glassId无有效数字时使用)
     * @return 格式化的工程师ID(如:P25010801)
     */
    private String generateEngineerId(Object glassId, int index) {
        // 1. 生成日期前缀(yyMMdd)
        String base = LocalDate.now().format(DATE_FORMATTER);
        // 2. 初始化序号(两位补零)
        String seq = String.format("%02d", index);
        // 3. 从glassId中提取末尾两位数字(覆盖默认序号)
        if (glassId != null) {
            String glassIdStr = glassId.toString();
            Matcher matcher = DIGIT_PATTERN.matcher(glassIdStr);
            String lastDigitStr = null;
            // 遍历匹配所有数字段,取最后一个
            while (matcher.find()) {
                lastDigitStr = matcher.group();
            }
            // 若数字段长度≥2,取最后两位;否则保留原序号
            if (lastDigitStr != null && lastDigitStr.length() >= 2) {
                seq = lastDigitStr.substring(lastDigitStr.length() - 2);
            }
        }
        return "P" + base + seq;
    }
    /**
     * 提取List中第一个Map的指定key值(默认空字符串)