|
|
@@ -0,0 +1,125 @@
|
|
|
+package com.jeesite.modules.mes.service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import com.jeesite.common.entity.Page;
|
|
|
+import com.jeesite.common.lang.ObjectUtils;
|
|
|
+import com.jeesite.common.lang.StringUtils;
|
|
|
+import com.jeesite.common.mybatis.mapper.query.QueryType;
|
|
|
+import com.jeesite.common.service.CrudService;
|
|
|
+import com.jeesite.modules.mes.dao.MesTimeoutConfigDao;
|
|
|
+import com.jeesite.modules.mes.entity.MesDeviceTime;
|
|
|
+import com.jeesite.modules.mes.entity.MesTimeoutConfig;
|
|
|
+import com.jeesite.modules.mes.util.CommonUitl;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 工位超时配置Service
|
|
|
+ * @author mes
|
|
|
+ * @version 2026-10-30
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class MesTimeoutConfigService extends CrudService<MesTimeoutConfigDao, MesTimeoutConfig> {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MesDeviceTimeService mesDeviceTimeService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public MesTimeoutConfig get(MesTimeoutConfig mesTimeoutConfig) {
|
|
|
+ return super.get(mesTimeoutConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<MesTimeoutConfig> findPage(MesTimeoutConfig mesTimeoutConfig) {
|
|
|
+ return super.findPage(mesTimeoutConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<MesTimeoutConfig> findList(MesTimeoutConfig mesTimeoutConfig) {
|
|
|
+ return super.findList(mesTimeoutConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void save(MesTimeoutConfig mesTimeoutConfig) {
|
|
|
+ if (StringUtils.isBlank(mesTimeoutConfig.getEnableFlag())) {
|
|
|
+ mesTimeoutConfig.setEnableFlag(MesTimeoutConfig.ENABLE_YES);
|
|
|
+ }
|
|
|
+ super.save(mesTimeoutConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void delete(MesTimeoutConfig mesTimeoutConfig) {
|
|
|
+ super.delete(mesTimeoutConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 提交结果时按加工时间表校验:无启用配置则原样返回;
|
|
|
+ * 有配置且加工时长超过最大秒数则返回 NG。
|
|
|
+ */
|
|
|
+ public String applyTimeout(String oprno, String sn, String lineSn, String content) {
|
|
|
+ if (StringUtils.isBlank(oprno) || StringUtils.isBlank(sn)) {
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+ MesTimeoutConfig config = findEnabledByOprno(oprno);
|
|
|
+ if (config == null || config.getMaxSeconds() == null || config.getMaxSeconds() <= 0) {
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+
|
|
|
+ MesDeviceTime deviceTime = findDeviceTime(oprno, sn, lineSn);
|
|
|
+ if (ObjectUtils.isEmpty(deviceTime) || deviceTime.getStartDate() == null) {
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+
|
|
|
+ Date end = deviceTime.getEndDate() != null ? deviceTime.getEndDate() : new Date();
|
|
|
+ long elapsedSeconds = (end.getTime() - deviceTime.getStartDate().getTime()) / 1000L;
|
|
|
+ if (elapsedSeconds < 0) {
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+ if (elapsedSeconds > config.getMaxSeconds()) {
|
|
|
+ logger.info("工位加工超时 oprno={} sn={} lineSn={} elapsed={}s max={}s deviceResult={}",
|
|
|
+ oprno, sn, lineSn, elapsedSeconds, config.getMaxSeconds(), content);
|
|
|
+ return "NG";
|
|
|
+ }
|
|
|
+ return content;
|
|
|
+ }
|
|
|
+
|
|
|
+ public MesTimeoutConfig findEnabledByOprno(String oprno) {
|
|
|
+ Set<String> oprnos = new HashSet<>();
|
|
|
+ oprnos.add(oprno.trim());
|
|
|
+ String formatted = CommonUitl.formatOprno(oprno.trim());
|
|
|
+ if (StringUtils.isNotBlank(formatted)) {
|
|
|
+ oprnos.add(formatted);
|
|
|
+ }
|
|
|
+ MesTimeoutConfig query = new MesTimeoutConfig();
|
|
|
+ query.setEnableFlag(MesTimeoutConfig.ENABLE_YES);
|
|
|
+ query.getSqlMap().getWhere().and("oprno", QueryType.IN, oprnos.toArray());
|
|
|
+ List<MesTimeoutConfig> list = findList(query);
|
|
|
+ if (list == null || list.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return list.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ private MesDeviceTime findDeviceTime(String oprno, String sn, String lineSn) {
|
|
|
+ MesDeviceTime byCate = mesDeviceTimeService.getInfoByOprnoAndSnAndCate(oprno, sn.trim(), lineSn);
|
|
|
+ if (!ObjectUtils.isEmpty(byCate)) {
|
|
|
+ return byCate;
|
|
|
+ }
|
|
|
+ String formatted = CommonUitl.formatOprno(oprno);
|
|
|
+ if (!oprno.equals(formatted)) {
|
|
|
+ byCate = mesDeviceTimeService.getInfoByOprnoAndSnAndCate(formatted, sn.trim(), lineSn);
|
|
|
+ if (!ObjectUtils.isEmpty(byCate)) {
|
|
|
+ return byCate;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return mesDeviceTimeService.getInfoByOprnoAndSn(oprno, sn.trim());
|
|
|
+ }
|
|
|
+}
|