package com.qianwen.mdc.collect.service.feedback; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.qianwen.core.tool.utils.Func; import com.qianwen.core.tool.utils.SpringUtil; import com.qianwen.mdc.collect.cache.WorkstationCache; import com.qianwen.mdc.collect.dto.CalendarShiftInfoDTO; import com.qianwen.mdc.collect.dto.WorkstationFeedbackTimePointDTO; import com.qianwen.mdc.collect.dto.WorkstationFeedbackTimeQuantumDTO; import com.qianwen.mdc.collect.entity.iotdb.DeviceState; import com.qianwen.mdc.collect.entity.mgr.GlobalWcsOfRps; import com.qianwen.mdc.collect.entity.mgr.WorkstationWcsFeedback; import com.qianwen.mdc.collect.entity.mgr.WorkstationWcsFeedbackDetail; import com.qianwen.mdc.collect.enums.FeedbackDetailStatusEnum; import com.qianwen.mdc.collect.enums.FeedbackProcessStatusEnum; import com.qianwen.mdc.collect.enums.FeedbackTimePointEnum; import com.qianwen.mdc.collect.mapper.iotdb.DeviceStateMapper; import com.qianwen.mdc.collect.mapper.mgr.WorkstationWcsFeedbackMapper; import com.qianwen.mdc.collect.service.DeviceStateAggregateService; import com.qianwen.mdc.collect.service.WorkstationService; /** * 时间段反馈的处理策略 */ @Component public class NoImmediateFeedbackHandlerStrategy implements WorkstationFeedbackHandlerStrategy { private static final Logger log = LoggerFactory.getLogger(NoImmediateFeedbackHandlerStrategy.class); private WorkstationWcsFeedbackMapper feedbackMapper; private WorkstationFeedbackDetailService workstationFeedbackDetailService; //private WorkstationAggregateProducer producer; //, final WorkstationAggregateProducer producer public NoImmediateFeedbackHandlerStrategy(final WorkstationWcsFeedbackMapper feedbackMapper, final WorkstationFeedbackDetailService workstationFeedbackDetailService) { this.feedbackMapper = feedbackMapper; this.workstationFeedbackDetailService = workstationFeedbackDetailService; //this.producer = producer; } @Override public void handlerFeedback(WorkstationWcsFeedback feedback) { NoImmediateFeedbackAnalyseResult analyseResult = NoImmediateFeedbackAnalyseResultBuilder.get().buildFeedbackBaseInfo(feedback).buildFeedbackTimePointInfo().buildFeedbackTimeQuantumInfo().buildFeedbackComplementStateInfo().analyse(); DeviceStateMapper stateMapper = SpringUtil.getBean(DeviceStateMapper.class); /* List effectiveStateList = stateMapper.selectList(Wrappers.lambdaQuery() .le(WorkstationState::getTs, Long.valueOf(analyseResult.getEffectiveEndDate().getTime())) .ge(WorkstationState::getTs, Long.valueOf(analyseResult.getEffectiveStartDate().getTime())) .eq(WorkstationState::getWorkstationId, analyseResult.getWorkstationId()) .ne(WorkstationState::getIsDeleted, Boolean.TRUE) .gt(WorkstationState::getFeedbackPointType, FeedbackTimePointEnum.NO_FEED_BACK_POINT.getValue())); */ List effectiveStateList = stateMapper.workstationNoFeedbackPointStatesInTimeRange(analyseResult.getWorkstationId(),analyseResult.getEffectiveStartDate().getTime(),analyseResult.getEffectiveEndDate().getTime()); effectiveStateList = effectiveStateList.stream().map(x -> resetEffectiveWorkstationState(x)).collect(Collectors.toList()); /* List fixPointStateList = stateMapper.selectList(Wrappers.lambdaQuery() .le(WorkstationState::getTs, Long.valueOf(analyseResult.getEffectiveEndDate().getTime())) .ge(WorkstationState::getTs, Long.valueOf(analyseResult.getEffectiveStartDate().getTime())) .eq(WorkstationState::getWorkstationId, analyseResult.getWorkstationId()) .eq(WorkstationState::getIsFixPoint, Boolean.TRUE)); */ List fixPointStateList = stateMapper.workstationFixPointStatesInTimeRange(analyseResult.getWorkstationId(),analyseResult.getEffectiveStartDate().getTime(),analyseResult.getEffectiveEndDate().getTime()); List newFeedbackStateList = new ArrayList<>(); if (Func.isNotEmpty(analyseResult.getEffectiveFeedbackTimePointList())) for (WorkstationFeedbackTimePointDTO item : analyseResult.getEffectiveFeedbackTimePointList()) { newFeedbackStateList.add(convertFeedbackPointToWorkstationState(analyseResult.getWorkstationId(), item, fixPointStateList, stateMapper)); } List insertData = generateWorkstationState(newFeedbackStateList, effectiveStateList); //insertData.addAll((Collection)analyseResult.getCompensateStateList().stream().map(item -> wrapperShiftInfo(item, stateMapper)).collect(Collectors.toList())); insertData.addAll(analyseResult.getCompensateStateList().stream().map(item -> wrapperShiftInfo(item, stateMapper)).collect(Collectors.toList())); //stateMapper.batchSave(feedback.getWorkstationId(), insertData); //我们使用servcie保存 List effectiveFeedbackTimeQuantumList = analyseResult.getEffectiveFeedbackTimeQuantumList(); List detailList = (List)effectiveFeedbackTimeQuantumList.stream().map(x -> convertFeedbackTimeQuantumToDetail(analyseResult.getWorkstationId(), x)).collect(Collectors.toList()); this.workstationFeedbackDetailService.remove(Wrappers.lambdaQuery() .eq(WorkstationWcsFeedbackDetail::getFeedbackId, analyseResult.getFeedbackId()) .eq(WorkstationWcsFeedbackDetail::getStatus, FeedbackDetailStatusEnum.BE_EFFECTIVE.getValue())); this.workstationFeedbackDetailService.saveBatch(detailList); this.feedbackMapper.update(null, Wrappers.lambdaUpdate() .eq(WorkstationWcsFeedback::getId, analyseResult.getFeedbackId()) .set(WorkstationWcsFeedback::getStatus, FeedbackProcessStatusEnum.PROCESSING.getValue())); //下面是发送消息,通知消费者进行状态聚合 /* WorkstationAggregateMessage message = new WorkstationAggregateMessage(); message.setWorkStationId(feedback.getWorkstationId().toString()); this.producer.sendWorkStationAggregateMessage(Arrays.asList(new WorkstationAggregateMessage[] { message })); */ //我们直接调用了,不发消息 DeviceStateAggregateService deviceStateAggregateService = SpringUtil.getBean(DeviceStateAggregateService.class); deviceStateAggregateService.stateAggregate(feedback.getWorkstationId()); } private List generateWorkstationState(List newFeedbackStateList, List effectiveStateList) { List resultList = new ArrayList<>(); resultList.addAll(newFeedbackStateList); if (Func.isNotEmpty(effectiveStateList)) { for (DeviceState item : effectiveStateList) { long matchCount = newFeedbackStateList.stream().filter(x -> { return x.getTime().equals(item.getTime()); }).count(); if (matchCount <= 0) { resultList.add(item); } } } return resultList; } private DeviceState convertFeedbackPointToWorkstationState(Long workstationId, WorkstationFeedbackTimePointDTO timePoint, List fixPointStateList, DeviceStateMapper stateMapper) { DeviceState workstationState = new DeviceState(); workstationState.setTime(Long.valueOf(timePoint.getFeedbackTime().getTime())); workstationState.setWcs(Integer.valueOf(timePoint.getWcs())); workstationState.setValueCollect(Integer.valueOf(timePoint.getWcs())); workstationState.setWorkstationId(workstationId); workstationState.setIsDeleted(Boolean.FALSE); workstationState.setIsSync(false); workstationState.setIsFixPoint(Boolean.FALSE); workstationState.setFeedbackId(timePoint.getFeedbackId()); workstationState.setFeedbackPointType(timePoint.getPointEnum().getValue()); DeviceState matchRecord = fixPointStateList.stream().filter(x -> { return x.getTime().equals(Long.valueOf(timePoint.getFeedbackTime().getTime())); }).findFirst().orElse(null); if (Func.isNotEmpty(matchRecord)) { workstationState.setCalendarCode(matchRecord.getCalendarCode()); workstationState.setFactoryYear(matchRecord.getFactoryYear()); workstationState.setFactoryWeek(matchRecord.getFactoryWeek()); workstationState.setFactoryMonth(matchRecord.getFactoryMonth()); workstationState.setFactoryDate(matchRecord.getFactoryDate()); workstationState.setIsFixPoint(matchRecord.getIsFixPoint()); workstationState.setShiftTimeType(matchRecord.getShiftTimeType()); workstationState.setShiftIndex(matchRecord.getShiftIndex()); } else { workstationState = wrapperShiftInfo(workstationState, stateMapper); } //GlobalWcsOfRps setting = WorkstationCache.getWorkstationWcsSetting(workstationId, timePoint.getWcs()); WorkstationCache workstationCache = SpringUtil.getBean(WorkstationCache.class); GlobalWcsOfRps setting = workstationCache.getWorkstationWcsSetting(workstationId, timePoint.getWcs()); workstationState.setRps(setting.getRps()); workstationState.setIsPlan(setting.getIsPlan()); return workstationState; } private DeviceState wrapperShiftInfo(DeviceState workstationState, DeviceStateMapper stateMapper) { /* WorkstationState resultTimePoint = stateMapper.getLastWorkstationState(Wrappers.lambdaQuery() .eq(WorkstationState::getWorkstationId, workstationState.getWorkstationId()) .le(WorkstationState::getTs, workstationState.getTs()) .isNotNull(WorkstationState::getShiftIndex) .ne(WorkstationState::getIsDeleted, Boolean.TRUE)); */ DeviceState resultTimePoint = stateMapper.getLastByWorkstationIdAndLeTimeAndShiftIndexNotNull(workstationState.getWorkstationId(),workstationState.getTime()); if (Func.isNotEmpty(resultTimePoint)) { workstationState.setFactoryYear(resultTimePoint.getFactoryYear()); workstationState.setFactoryMonth(resultTimePoint.getFactoryMonth()); workstationState.setFactoryWeek(resultTimePoint.getFactoryWeek()); workstationState.setFactoryDate(resultTimePoint.getFactoryDate()); workstationState.setShiftIndex(resultTimePoint.getShiftIndex()); workstationState.setShiftTimeType(resultTimePoint.getShiftTimeType()); workstationState.setCalendarCode(resultTimePoint.getCalendarCode()); } else { WorkstationService workstationService = SpringUtil.getBean(WorkstationService.class); CalendarShiftInfoDTO calendarShiftInfo = workstationService.getCalendarShiftInfoForWorkstation(workstationState.getWorkstationId(), new Date(workstationState.getTime())); workstationState.setFactoryYear(calendarShiftInfo.getFactoryYear()); workstationState.setCalendarCode(calendarShiftInfo.getCode()); workstationState.setFactoryDate(Integer.valueOf(calendarShiftInfo.getFactoryDate())); workstationState.setFactoryWeek(calendarShiftInfo.getFactoryWeek()); workstationState.setFactoryMonth(calendarShiftInfo.getFactoryMonth()); workstationState.setShiftTimeType(calendarShiftInfo.getShiftTimeType()); workstationState.setShiftIndex(calendarShiftInfo.getShiftIndex()); } return workstationState; } private WorkstationWcsFeedbackDetail convertFeedbackTimeQuantumToDetail(Long workstationId, WorkstationFeedbackTimeQuantumDTO timeQuantumDTO) { WorkstationWcsFeedbackDetail detail = new WorkstationWcsFeedbackDetail(); detail.setWorkstationId(workstationId); detail.setFeedbackId(timeQuantumDTO.getFeedbackId()); detail.setStartTime(timeQuantumDTO.getStartTime()); detail.setEndTime(timeQuantumDTO.getEndTime()); detail.setCancel(0); detail.setStatus(FeedbackDetailStatusEnum.BE_EFFECTIVE.getValue()); detail.setWcs(timeQuantumDTO.getWcs()); return detail; } private DeviceState resetEffectiveWorkstationState(DeviceState workstationState) { if (workstationState.getIsFixPoint().booleanValue()) { workstationState.setFeedbackPointType(FeedbackTimePointEnum.NO_FEED_BACK_POINT.getValue()); workstationState.setValueCollect(0); workstationState.setWcs(0); workstationState.setIsPlan(null); workstationState.setRps(0); workstationState.setIsSync(Boolean.FALSE); workstationState.setIsDeleted(Boolean.FALSE); } else { workstationState.setIsDeleted(Boolean.TRUE); } return workstationState; } }