package com.jeesite.modules.mes.service; import com.jeesite.common.lang.ObjectUtils; import com.jeesite.common.lang.StringUtils; import com.jeesite.modules.mes.dao.MesProductRecordDao; import com.jeesite.modules.mes.entity.MesDeviceTime; import com.jeesite.modules.mes.entity.MesLineProcess; import com.jeesite.modules.mes.entity.MesProductDbjRecord; import com.jeesite.modules.mes.entity.MesProductRecord; import com.jeesite.modules.mes.util.CommonUitl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Arrays; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; /** * 工位加工时效校验 */ @Service public class MesStationTimeValidateService { private static final int COOLING_OP023_TO_OP030_MINUTES = 30; private static final int CROSS_OP150_TO_OP160_MINUTES = 15; private static final Set CROSS_TO_OP160_OPRNOS = new HashSet<>(Arrays.asList( "OP160", "OP160A", "OP160B" )); @Autowired private MesDeviceTimeService mesDeviceTimeService; @Autowired private MesProductDbjRecordService mesProductDbjRecordService; @Autowired private MesProductRecordDao mesProductRecordDao; /** * 单部件工位进站校验(精追码) * @return 两位错误码,null 表示通过 */ public String validateDbjEntry(String oldOprno, String lineSn, String partSn) { if (!matchOprno(oldOprno, "OP030")) { return null; } if (StringUtils.isBlank(partSn)) { return "NE"; } if (mesProductDbjRecordService.findLatestOk(partSn, oldOprno, lineSn) != null) { return "RP"; } MesProductDbjRecord op023Ok = mesProductDbjRecordService.findLatestOk(partSn, "OP023", lineSn); if (op023Ok == null || op023Ok.getCreateDate() == null) { return "QD"; } long elapsedMs = System.currentTimeMillis() - op023Ok.getCreateDate().getTime(); if (elapsedMs < COOLING_OP023_TO_OP030_MINUTES * 60L * 1000L) { return "GH"; } return null; } /** * 工位提交前校验 * @return 两位错误码,null 表示通过 */ public String validateSubmit(String oldOprno, String lineSn, String sn, MesLineProcess mesLineProcess) { if (StringUtils.isBlank(sn) || ObjectUtils.isEmpty(mesLineProcess)) { return null; } String crossErr = validateCrossOp150ToOp160(oldOprno, lineSn, sn); if (crossErr != null) { return crossErr; } Integer maxMinutes = resolveMaxProcessMinutes(mesLineProcess); if (maxMinutes == null || maxMinutes <= 0) { return null; } return validateMaxProcessDuration(oldOprno, lineSn, sn, maxMinutes); } private String validateCrossOp150ToOp160(String oldOprno, String lineSn, String sn) { if (!isCrossToOp160(oldOprno)) { return null; } Date op150Complete = findOprnoOkCompleteTime(sn, lineSn, "OP150"); if (op150Complete == null) { return "QD"; } long elapsedMs = System.currentTimeMillis() - op150Complete.getTime(); if (elapsedMs > CROSS_OP150_TO_OP160_MINUTES * 60L * 1000L) { return "TO"; } return null; } private String validateMaxProcessDuration(String oldOprno, String lineSn, String sn, int maxMinutes) { MesDeviceTime mesDeviceTime = mesDeviceTimeService.getInfoByOprnoAndSnAndCate(oldOprno, sn, lineSn); if (ObjectUtils.isEmpty(mesDeviceTime) || mesDeviceTime.getStartDate() == null) { return "TO"; } long elapsedMs = System.currentTimeMillis() - mesDeviceTime.getStartDate().getTime(); if (elapsedMs > maxMinutes * 60L * 1000L) { return "TO"; } return null; } /** * 仅使用工序配置 max_duration: * - >0:开启本工位超时校验 * - null 或 <=0:明确关闭(不做本工位超时校验) */ private Integer resolveMaxProcessMinutes(MesLineProcess mesLineProcess) { if (mesLineProcess.getMaxDuration() != null && mesLineProcess.getMaxDuration() > 0) { return mesLineProcess.getMaxDuration(); } return null; } private Date findOprnoOkCompleteTime(String sn, String lineSn, String targetOprno) { MesProductRecord query = new MesProductRecord(); query.setSn(sn); query.setLineSn(lineSn); query.setCraft("100000"); query.setContent("OK"); List list = mesProductRecordDao.findList(query); String target = normalizeOprno(targetOprno); Date latest = null; for (MesProductRecord record : list) { if (!target.equals(normalizeOprno(record.getOprno()))) { continue; } Date completeDate = record.getUpdateDate() != null ? record.getUpdateDate() : record.getCreateDate(); if (completeDate != null && (latest == null || completeDate.after(latest))) { latest = completeDate; } } return latest; } private boolean isCrossToOp160(String oldOprno) { return CROSS_TO_OP160_OPRNOS.contains(normalizeOprno(oldOprno)); } private boolean matchOprno(String oldOprno, String target) { return normalizeOprno(oldOprno).equals(normalizeOprno(target)); } private String normalizeOprno(String oprno) { if (StringUtils.isBlank(oprno)) { return ""; } return CommonUitl.formatOprno(oprno.trim().toUpperCase()); } }