MesStationTimeValidateService.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.jeesite.modules.mes.service;
  2. import com.jeesite.common.lang.ObjectUtils;
  3. import com.jeesite.common.lang.StringUtils;
  4. import com.jeesite.modules.mes.dao.MesProductRecordDao;
  5. import com.jeesite.modules.mes.entity.MesDeviceTime;
  6. import com.jeesite.modules.mes.entity.MesLineProcess;
  7. import com.jeesite.modules.mes.entity.MesProductDbjRecord;
  8. import com.jeesite.modules.mes.entity.MesProductRecord;
  9. import com.jeesite.modules.mes.util.CommonUitl;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import java.util.Arrays;
  13. import java.util.Date;
  14. import java.util.HashSet;
  15. import java.util.List;
  16. import java.util.Set;
  17. /**
  18. * 工位加工时效校验
  19. */
  20. @Service
  21. public class MesStationTimeValidateService {
  22. private static final int COOLING_OP023_TO_OP030_MINUTES = 30;
  23. private static final int CROSS_OP150_TO_OP160_MINUTES = 15;
  24. private static final Set<String> CROSS_TO_OP160_OPRNOS = new HashSet<>(Arrays.asList(
  25. "OP160", "OP160A", "OP160B"
  26. ));
  27. @Autowired
  28. private MesDeviceTimeService mesDeviceTimeService;
  29. @Autowired
  30. private MesProductDbjRecordService mesProductDbjRecordService;
  31. @Autowired
  32. private MesProductRecordDao mesProductRecordDao;
  33. /**
  34. * 单部件工位进站校验(精追码)
  35. * @return 两位错误码,null 表示通过
  36. */
  37. public String validateDbjEntry(String oldOprno, String lineSn, String partSn) {
  38. if (!matchOprno(oldOprno, "OP030")) {
  39. return null;
  40. }
  41. if (StringUtils.isBlank(partSn)) {
  42. return "NE";
  43. }
  44. if (mesProductDbjRecordService.findLatestOk(partSn, oldOprno, lineSn) != null) {
  45. return "RP";
  46. }
  47. MesProductDbjRecord op023Ok = mesProductDbjRecordService.findLatestOk(partSn, "OP023", lineSn);
  48. if (op023Ok == null || op023Ok.getCreateDate() == null) {
  49. return "QD";
  50. }
  51. long elapsedMs = System.currentTimeMillis() - op023Ok.getCreateDate().getTime();
  52. if (elapsedMs < COOLING_OP023_TO_OP030_MINUTES * 60L * 1000L) {
  53. return "GH";
  54. }
  55. return null;
  56. }
  57. /**
  58. * 工位提交前校验
  59. * @return 两位错误码,null 表示通过
  60. */
  61. public String validateSubmit(String oldOprno, String lineSn, String sn, MesLineProcess mesLineProcess) {
  62. if (StringUtils.isBlank(sn) || ObjectUtils.isEmpty(mesLineProcess)) {
  63. return null;
  64. }
  65. String crossErr = validateCrossOp150ToOp160(oldOprno, lineSn, sn);
  66. if (crossErr != null) {
  67. return crossErr;
  68. }
  69. Integer maxMinutes = resolveMaxProcessMinutes(mesLineProcess);
  70. if (maxMinutes == null || maxMinutes <= 0) {
  71. return null;
  72. }
  73. return validateMaxProcessDuration(oldOprno, lineSn, sn, maxMinutes);
  74. }
  75. private String validateCrossOp150ToOp160(String oldOprno, String lineSn, String sn) {
  76. if (!isCrossToOp160(oldOprno)) {
  77. return null;
  78. }
  79. Date op150Complete = findOprnoOkCompleteTime(sn, lineSn, "OP150");
  80. if (op150Complete == null) {
  81. return "QD";
  82. }
  83. long elapsedMs = System.currentTimeMillis() - op150Complete.getTime();
  84. if (elapsedMs > CROSS_OP150_TO_OP160_MINUTES * 60L * 1000L) {
  85. return "TO";
  86. }
  87. return null;
  88. }
  89. private String validateMaxProcessDuration(String oldOprno, String lineSn, String sn, int maxMinutes) {
  90. MesDeviceTime mesDeviceTime = mesDeviceTimeService.getInfoByOprnoAndSnAndCate(oldOprno, sn, lineSn);
  91. if (ObjectUtils.isEmpty(mesDeviceTime) || mesDeviceTime.getStartDate() == null) {
  92. return "TO";
  93. }
  94. long elapsedMs = System.currentTimeMillis() - mesDeviceTime.getStartDate().getTime();
  95. if (elapsedMs > maxMinutes * 60L * 1000L) {
  96. return "TO";
  97. }
  98. return null;
  99. }
  100. /**
  101. * 仅使用工序配置 max_duration:
  102. * - &gt;0:开启本工位超时校验
  103. * - null 或 &lt;=0:明确关闭(不做本工位超时校验)
  104. */
  105. private Integer resolveMaxProcessMinutes(MesLineProcess mesLineProcess) {
  106. if (mesLineProcess.getMaxDuration() != null && mesLineProcess.getMaxDuration() > 0) {
  107. return mesLineProcess.getMaxDuration();
  108. }
  109. return null;
  110. }
  111. private Date findOprnoOkCompleteTime(String sn, String lineSn, String targetOprno) {
  112. MesProductRecord query = new MesProductRecord();
  113. query.setSn(sn);
  114. query.setLineSn(lineSn);
  115. query.setCraft("100000");
  116. query.setContent("OK");
  117. List<MesProductRecord> list = mesProductRecordDao.findList(query);
  118. String target = normalizeOprno(targetOprno);
  119. Date latest = null;
  120. for (MesProductRecord record : list) {
  121. if (!target.equals(normalizeOprno(record.getOprno()))) {
  122. continue;
  123. }
  124. Date completeDate = record.getUpdateDate() != null ? record.getUpdateDate() : record.getCreateDate();
  125. if (completeDate != null && (latest == null || completeDate.after(latest))) {
  126. latest = completeDate;
  127. }
  128. }
  129. return latest;
  130. }
  131. private boolean isCrossToOp160(String oldOprno) {
  132. return CROSS_TO_OP160_OPRNOS.contains(normalizeOprno(oldOprno));
  133. }
  134. private boolean matchOprno(String oldOprno, String target) {
  135. return normalizeOprno(oldOprno).equals(normalizeOprno(target));
  136. }
  137. private String normalizeOprno(String oprno) {
  138. if (StringUtils.isBlank(oprno)) {
  139. return "";
  140. }
  141. return CommonUitl.formatOprno(oprno.trim().toUpperCase());
  142. }
  143. }