|
|
@@ -5,76 +5,274 @@ import com.jeesite.common.lang.StringUtils;
|
|
|
import com.jeesite.common.mybatis.mapper.query.QueryType;
|
|
|
import com.jeesite.modules.mes.entity.MesLineProcess;
|
|
|
import com.jeesite.modules.mes.entity.MesProductCheckFirst;
|
|
|
-import com.jeesite.modules.mes.entity.MesShift;
|
|
|
+import com.jeesite.modules.mes.entity.MesProductRecord;
|
|
|
import com.jeesite.modules.mes.resp.MesShiftDataResp;
|
|
|
import com.jeesite.modules.mes.service.MesLineProcessService;
|
|
|
import com.jeesite.modules.mes.service.MesProductCheckFirstService;
|
|
|
+import com.jeesite.modules.mes.service.MesProductRecordService;
|
|
|
import com.jeesite.modules.mes.service.MesShiftService;
|
|
|
import com.jeesite.modules.mes.util.CommonUitl;
|
|
|
-import org.apache.poi.util.StringUtil;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
+/**
|
|
|
+ * 开班首检超时锁机任务。
|
|
|
+ *
|
|
|
+ * 规则:开班 2 小时内未完成首检的工位锁机(mes_line_process.stop=3),
|
|
|
+ * 首检合格后由首检提交接口或本任务自动解除锁机。
|
|
|
+ * 默认对 CZ060 的 OP160、OP165 生效,可通过
|
|
|
+ * mes.first.check.oprnos / mes.first.check.oprnos.{产线小写} 配置。
|
|
|
+ */
|
|
|
@Configuration
|
|
|
@EnableScheduling
|
|
|
-public class DeviceCheckFirstScheduling {//检查首件检查是否超时未检查
|
|
|
+public class DeviceCheckFirstScheduling {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(DeviceCheckFirstScheduling.class);
|
|
|
+
|
|
|
+ /** 首检超时时间:开班后 2 小时 */
|
|
|
+ private static final long FIRST_CHECK_TIMEOUT_MILLIS = 2L * 60 * 60 * 1000;
|
|
|
+
|
|
|
+ /** 首件超时停机状态 */
|
|
|
+ private static final int STOP_FIRST_CHECK_TIMEOUT = 3;
|
|
|
+
|
|
|
+ private static final String DATE_PATTERN = "yyyy-MM-dd HH:mm";
|
|
|
+
|
|
|
@Autowired
|
|
|
private MesShiftService mesShiftService;
|
|
|
+
|
|
|
@Autowired
|
|
|
private MesLineProcessService mesLineProcessService;
|
|
|
|
|
|
@Autowired
|
|
|
private MesProductCheckFirstService mesProductCheckFirstService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MesProductRecordService mesProductRecordService;
|
|
|
+
|
|
|
// 每分钟执行一次
|
|
|
-// @Scheduled(cron = "0 * * * * ?") // cron 表达式
|
|
|
+ @Scheduled(cron = "0 * * * * ?")
|
|
|
public void checkDevices() {
|
|
|
- String check = Global.getConfig("mes.first.check.timeout");
|
|
|
- if(check.equals("1")){
|
|
|
- // 定义日期时间格式
|
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
|
|
- LocalDateTime now = LocalDateTime.now();
|
|
|
- System.out.println("now:"+now.format(formatter));
|
|
|
- // 将字符串转换为 LocalDateTime
|
|
|
- LocalDateTime dateTime = LocalDateTime.parse(now.format(formatter), formatter);
|
|
|
- // 减少两个小时
|
|
|
- LocalDateTime twoHoursLater = dateTime.minusHours(2);
|
|
|
- // 格式化输出
|
|
|
- String endDate = twoHoursLater.format(formatter);
|
|
|
- System.out.println("limit:"+endDate);
|
|
|
-
|
|
|
- String startDate = "2025-10-01 08:00";
|
|
|
-
|
|
|
- //超过开班两小时,并且状态为未检查的首件检查记录
|
|
|
- MesProductCheckFirst mesProductCheckFirst = new MesProductCheckFirst();
|
|
|
- mesProductCheckFirst.setState("0");
|
|
|
- mesProductCheckFirst.setType("1"); // 开班首件检查
|
|
|
- mesProductCheckFirst.getSqlMap().getWhere().and("a.create_date", QueryType.GT,startDate);
|
|
|
- mesProductCheckFirst.getSqlMap().getWhere().and("a.create_date", QueryType.LT,endDate);
|
|
|
- List<MesProductCheckFirst> list = mesProductCheckFirstService.findList(mesProductCheckFirst);
|
|
|
- if (list.size() > 0){
|
|
|
- for (MesProductCheckFirst dtl : list) {
|
|
|
- MesLineProcess mesLineProcess1 = new MesLineProcess();
|
|
|
- mesLineProcess1.setLineSn(dtl.getLineSn());
|
|
|
- mesLineProcess1.getSqlMap().getWhere().and("a.oprno",QueryType.EQ,dtl.getOprno());
|
|
|
- List<MesLineProcess> mesLineProcesses = mesLineProcessService.findList(mesLineProcess1);
|
|
|
- if (mesLineProcesses.size() > 0){
|
|
|
- for (MesLineProcess lineProcess : mesLineProcesses) {
|
|
|
- //修改工位状态为首件检超时未处理
|
|
|
- if(lineProcess.getStop() != 3){
|
|
|
- lineProcess.setStop(3);
|
|
|
- mesLineProcessService.update(lineProcess);
|
|
|
- }
|
|
|
+ String check = Global.getConfig("mes.first.check.timeout", "1");
|
|
|
+ if (check == null || !"1".equals(check.trim())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ MesShiftDataResp currentShift = mesShiftService.getCurShift();
|
|
|
+ if (currentShift == null || StringUtils.isBlank(currentShift.getStart())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Date shiftStart = parseDate(currentShift.getStart());
|
|
|
+ Date shiftEnd = parseDate(currentShift.getEnd());
|
|
|
+ if (shiftStart == null || shiftEnd == null) {
|
|
|
+ logger.warn("首检锁机任务:班次时间格式不正确,start={}, end={}",
|
|
|
+ currentShift.getStart(), currentShift.getEnd());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Date now = new Date();
|
|
|
+ boolean timeoutReached = now.getTime() >= shiftStart.getTime() + FIRST_CHECK_TIMEOUT_MILLIS;
|
|
|
+
|
|
|
+ // 一次性取出所有启用中的工位,再按产线、首检工位组归类,避免逐工位查询数据库
|
|
|
+ MesLineProcess processQuery = new MesLineProcess();
|
|
|
+ processQuery.setStatus("0");
|
|
|
+ List<MesLineProcess> allProcesses = mesLineProcessService.findList(processQuery);
|
|
|
+ Map<String, List<MesLineProcess>> lineMap = new LinkedHashMap<>();
|
|
|
+ for (MesLineProcess process : allProcesses) {
|
|
|
+ if (StringUtils.isBlank(process.getLineSn()) || StringUtils.isBlank(process.getOprno())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ lineMap.computeIfAbsent(process.getLineSn(), k -> new ArrayList<>()).add(process);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Map.Entry<String, List<MesLineProcess>> entry : lineMap.entrySet()) {
|
|
|
+ String lineSn = entry.getKey();
|
|
|
+ String groups = CommonUitl.getFirstCheckOprnos(lineSn);
|
|
|
+ if (StringUtils.isBlank(groups)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (String rawGroup : groups.split(",")) {
|
|
|
+ String group = rawGroup.trim();
|
|
|
+ if (group.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<MesLineProcess> groupProcesses = new ArrayList<>();
|
|
|
+ for (MesLineProcess process : entry.getValue()) {
|
|
|
+ String normalized = CommonUitl.normalizeFirstCheckOprno(process.getOprno(), lineSn);
|
|
|
+ if (group.equals(normalized)) {
|
|
|
+ groupProcesses.add(process);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (groupProcesses.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<MesProductCheckFirst> records = findFirstCheckRecords(lineSn, group, shiftStart, shiftEnd);
|
|
|
+ boolean completed = false;
|
|
|
+ boolean completedOk = false;
|
|
|
+ for (MesProductCheckFirst record : records) {
|
|
|
+ if ("1".equals(record.getState())) {
|
|
|
+ completed = true;
|
|
|
+ if ("OK".equalsIgnoreCase(record.getResult())) {
|
|
|
+ completedOk = true;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ if (!timeoutReached) {
|
|
|
+ // 新班次开始后的 2 小时缓冲期内,清掉上一班遗留的超时锁机
|
|
|
+ unlockTimeout(groupProcesses, lineSn, group);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (completedOk) {
|
|
|
+ // 首检合格,恢复生产
|
|
|
+ unlockTimeout(groupProcesses, lineSn, group);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (completed) {
|
|
|
+ // 首检已做但不合格,保留不合格停机状态(stop=1/2),不再改成超时停机
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2 小时内没有完成首检:先补一条待检记录,方便操作员做首检,再锁机
|
|
|
+ if (records.isEmpty()) {
|
|
|
+ createPendingFirstCheck(lineSn, group, shiftStart, shiftEnd);
|
|
|
+ }
|
|
|
+ lockTimeout(groupProcesses, lineSn, group);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询当前班次、当前产线、当前首检工位组的首检记录。
|
|
|
+ * oprno 可能带 A/B/C 后缀,这里按工位组统一过滤。
|
|
|
+ */
|
|
|
+ private List<MesProductCheckFirst> findFirstCheckRecords(String lineSn, String group,
|
|
|
+ Date shiftStart, Date shiftEnd) {
|
|
|
+ MesProductCheckFirst query = new MesProductCheckFirst();
|
|
|
+ query.setLineSn(lineSn);
|
|
|
+ query.setType("1");
|
|
|
+ query.getSqlMap().getWhere().and("a.create_date", QueryType.GTE, shiftStart);
|
|
|
+ query.getSqlMap().getWhere().and("a.create_date", QueryType.LT, shiftEnd);
|
|
|
+ List<MesProductCheckFirst> list = mesProductCheckFirstService.findList(query);
|
|
|
+
|
|
|
+ List<MesProductCheckFirst> result = new ArrayList<>();
|
|
|
+ if (list == null) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ for (MesProductCheckFirst record : list) {
|
|
|
+ String normalized = CommonUitl.normalizeFirstCheckOprno(record.getOprno(), lineSn);
|
|
|
+ if (group.equals(normalized)) {
|
|
|
+ result.add(record);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 超时且没有任何首检记录时,取本班次该工位组的第一件产品生成待检记录。
|
|
|
+ * 若本班次还没有产品经过该工位,则只锁机,不生成无工件码的记录。
|
|
|
+ */
|
|
|
+ private void createPendingFirstCheck(String lineSn, String group, Date shiftStart, Date shiftEnd) {
|
|
|
+ MesProductRecord query = new MesProductRecord();
|
|
|
+ query.getSqlMap().getWhere().and("a.line_sn", QueryType.EQ, lineSn);
|
|
|
+ query.getSqlMap().getWhere().and("a.oprno", QueryType.RIGHT_LIKE, group);
|
|
|
+ query.getSqlMap().getWhere().and("a.create_date", QueryType.GTE, shiftStart);
|
|
|
+ query.getSqlMap().getWhere().and("a.create_date", QueryType.LT, shiftEnd);
|
|
|
+ query.getSqlMap().getOrder().setOrderBy("a.create_date ASC");
|
|
|
+ List<MesProductRecord> records = mesProductRecordService.findList(query);
|
|
|
+ if (records == null || records.isEmpty()) {
|
|
|
+ logger.info("首检锁机:产线 {} 工位 {} 本班次暂无产品记录,仅锁机不补首检记录", lineSn, group);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 优先取已经产生实际结果的首件,避免取到仅占位的 UDF 记录
|
|
|
+ MesProductRecord firstProduct = null;
|
|
|
+ for (MesProductRecord record : records) {
|
|
|
+ if (StringUtils.isBlank(record.getSn())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if ("OK".equalsIgnoreCase(record.getContent()) || "NG".equalsIgnoreCase(record.getContent())) {
|
|
|
+ firstProduct = record;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (firstProduct == null) {
|
|
|
+ for (MesProductRecord record : records) {
|
|
|
+ if (StringUtils.isNotBlank(record.getSn())) {
|
|
|
+ firstProduct = record;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (firstProduct == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ MesProductCheckFirst checkFirst = new MesProductCheckFirst();
|
|
|
+ checkFirst.setSn(firstProduct.getSn());
|
|
|
+ checkFirst.setOprno(group);
|
|
|
+ checkFirst.setLineSn(lineSn);
|
|
|
+ checkFirst.setType("1");
|
|
|
+ checkFirst.setState("0");
|
|
|
+ checkFirst.setCreateBy("system");
|
|
|
+ checkFirst.setUpdateBy("system");
|
|
|
+ mesProductCheckFirstService.save(checkFirst);
|
|
|
+ logger.info("首检超时:产线 {} 工位 {} 已补建首检记录,sn={}", lineSn, group, firstProduct.getSn());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置超时停机。已有其他停机原因(首检 NG、设备报警)时不覆盖。
|
|
|
+ */
|
|
|
+ private void lockTimeout(List<MesLineProcess> processes, String lineSn, String group) {
|
|
|
+ for (MesLineProcess process : processes) {
|
|
|
+ Integer stop = process.getStop();
|
|
|
+ if (stop == null || stop == 0 || Objects.equals(stop, STOP_FIRST_CHECK_TIMEOUT)) {
|
|
|
+ if (!Objects.equals(stop, STOP_FIRST_CHECK_TIMEOUT)) {
|
|
|
+ process.setStop(STOP_FIRST_CHECK_TIMEOUT);
|
|
|
+ mesLineProcessService.update(process);
|
|
|
+ logger.info("首检超时锁机:产线 {} 工位 {} stop=3", lineSn, process.getOprno());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解除本任务设置的超时停机,不影响其它停机原因。
|
|
|
+ */
|
|
|
+ private void unlockTimeout(List<MesLineProcess> processes, String lineSn, String group) {
|
|
|
+ for (MesLineProcess process : processes) {
|
|
|
+ if (Objects.equals(process.getStop(), STOP_FIRST_CHECK_TIMEOUT)) {
|
|
|
+ process.setStop(0);
|
|
|
+ mesLineProcessService.update(process);
|
|
|
+ logger.info("首检完成解锁:产线 {} 工位 {} stop=3 -> 0", lineSn, process.getOprno());
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
+ private Date parseDate(String value) {
|
|
|
+ if (StringUtils.isBlank(value)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return new SimpleDateFormat(DATE_PATTERN).parse(value);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|
|
|
}
|