huang
10 小时以前 8ec0064cd95292f14027006a8be47c1a71f69af9
mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/EngineeringSequenceServiceImpl.java
@@ -5,7 +5,6 @@
import com.mes.device.mapper.EngineeringSequenceMapper;
import com.mes.device.service.EngineeringSequenceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -57,27 +56,45 @@
            String sequenceStr = engineeringId.substring(engineeringId.length() - 2);
            int sequence = Integer.parseInt(sequenceStr);
            // 检查是否已存在
            EngineeringSequence existing = baseMapper.selectByEngineeringId(engineeringId);
            EngineeringSequence engineeringSequence = new EngineeringSequence();
            engineeringSequence.setEngineeringId(engineeringId);
            engineeringSequence.setDate(date);
            engineeringSequence.setSequence(sequence);
            engineeringSequence.setCreatedTime(new Date());
            engineeringSequence.setUpdatedTime(new Date());
            engineeringSequence.setCreatedBy("system");
            engineeringSequence.setUpdatedBy("system");
            boolean result = save(engineeringSequence);
            if (result) {
                log.info("保存工程号成功: engineeringId={}, date={}, sequence={}", engineeringId, date, sequence);
            boolean result;
            if (existing != null) {
                // 如果已存在,则更新
                engineeringSequence.setId(existing.getId());
                // 保留原始创建信息
                engineeringSequence.setCreatedTime(existing.getCreatedTime());
                engineeringSequence.setCreatedBy(existing.getCreatedBy());
                // 更新为当前时间
                engineeringSequence.setUpdatedTime(new Date());
                engineeringSequence.setUpdatedBy("system");
                result = updateById(engineeringSequence);
                if (result) {
                    log.info("更新工程号成功: engineeringId={}, date={}, sequence={}", engineeringId, date, sequence);
                } else {
                    log.error("更新工程号失败: engineeringId={}, date={}, sequence={}", engineeringId, date, sequence);
                }
            } else {
                log.error("保存工程号失败: engineeringId={}, date={}, sequence={}", engineeringId, date, sequence);
                // 如果不存在,则插入
                engineeringSequence.setCreatedTime(new Date());
                engineeringSequence.setUpdatedTime(new Date());
                engineeringSequence.setCreatedBy("system");
                engineeringSequence.setUpdatedBy("system");
                result = save(engineeringSequence);
                if (result) {
                    log.info("保存工程号成功: engineeringId={}, date={}, sequence={}", engineeringId, date, sequence);
                } else {
                    log.error("保存工程号失败: engineeringId={}, date={}, sequence={}", engineeringId, date, sequence);
                }
            }
            return result;
        } catch (DuplicateKeyException dup) {
            log.error("保存工程号唯一键冲突: date={}, engineeringId={}", date, engineeringId, dup);
            throw new RuntimeException("保存工程号失败", dup);
        } catch (Exception e) {
            log.error("保存工程号失败, date={}, engineeringId={}", date, engineeringId, e);
            throw new RuntimeException("保存工程号失败", e);