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.DeviceStateService;
|
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<WorkstationState> effectiveStateList = stateMapper.selectList(Wrappers.<WorkstationState>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<DeviceState> effectiveStateList = stateMapper.workstationNoFeedbackPointStatesInTimeRange(analyseResult.getWorkstationId(),analyseResult.getEffectiveStartDate().getTime(),analyseResult.getEffectiveEndDate().getTime());
|
|
effectiveStateList = effectiveStateList.stream().map(x -> resetEffectiveWorkstationState(x)).collect(Collectors.toList());
|
/*
|
List<WorkstationState> fixPointStateList = stateMapper.selectList(Wrappers.<WorkstationState>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<DeviceState> fixPointStateList = stateMapper.workstationFixPointStatesInTimeRange(analyseResult.getWorkstationId(),analyseResult.getEffectiveStartDate().getTime(),analyseResult.getEffectiveEndDate().getTime());
|
List<DeviceState> newFeedbackStateList = new ArrayList<>();
|
if (Func.isNotEmpty(analyseResult.getEffectiveFeedbackTimePointList())) {
|
for (WorkstationFeedbackTimePointDTO item : analyseResult.getEffectiveFeedbackTimePointList()) {
|
newFeedbackStateList.add(convertFeedbackPointToWorkstationState(analyseResult.getWorkstationId(), item, fixPointStateList, stateMapper));
|
}
|
}
|
List<DeviceState> insertData = generateWorkstationState(newFeedbackStateList, effectiveStateList);
|
|
//insertData.addAll((Collection<? extends WorkstationState>)analyseResult.getCompensateStateList().stream().map(item -> wrapperShiftInfo(item, stateMapper)).collect(Collectors.toList()));
|
insertData.addAll(analyseResult.getCompensateStateList().stream().map(item -> wrapperShiftInfo(item, stateMapper)).collect(Collectors.toList()));
|
insertData.forEach(s -> {s.setWorkstationId(feedback.getWorkstationId());});
|
//stateMapper.batchSave(feedback.getWorkstationId(), insertData);
|
//我们使用servcie保存
|
DeviceStateService stateService = SpringUtil.getBean(DeviceStateService.class);
|
stateService.saveDeviceStates(insertData);
|
|
List<WorkstationFeedbackTimeQuantumDTO> effectiveFeedbackTimeQuantumList = analyseResult.getEffectiveFeedbackTimeQuantumList();
|
List<WorkstationWcsFeedbackDetail> detailList = effectiveFeedbackTimeQuantumList.stream().map(x -> convertFeedbackTimeQuantumToDetail(analyseResult.getWorkstationId(), x)).collect(Collectors.toList());
|
|
this.workstationFeedbackDetailService.remove(Wrappers.<WorkstationWcsFeedbackDetail>lambdaQuery()
|
.eq(WorkstationWcsFeedbackDetail::getFeedbackId, analyseResult.getFeedbackId())
|
.eq(WorkstationWcsFeedbackDetail::getStatus, FeedbackDetailStatusEnum.BE_EFFECTIVE.getValue()));
|
this.workstationFeedbackDetailService.saveBatch(detailList);
|
this.feedbackMapper.update(null, Wrappers.<WorkstationWcsFeedback>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());
|
}
|
|
/**
|
* 非固定点改为删除状态;固定点设置为初始状态
|
* @param deviceState
|
* @return
|
*/
|
private DeviceState resetEffectiveWorkstationState(DeviceState deviceState) {
|
if (deviceState.getIsFixPoint()) {
|
deviceState.setFeedbackPointType(FeedbackTimePointEnum.NO_FEED_BACK_POINT.getValue());
|
deviceState.setValueCollect(0);
|
deviceState.setWcs(0);
|
deviceState.setIsPlan(null);
|
deviceState.setRps(0);
|
deviceState.setIsSync(false);
|
deviceState.setIsDeleted(false);
|
} else {
|
deviceState.setIsDeleted(true);
|
}
|
return deviceState;
|
}
|
|
/**
|
* 以newFeedbackStateList为基础,如果effectiveStateList中的点与newFeedbackStateList中的数据时间不相等,则将effectiveStateList中的点加入
|
* @param newFeedbackStateList 根据反馈转换出来的状态数据
|
* @param effectiveStateList 反馈时间段内的非反馈点
|
* @return
|
*/
|
private List<DeviceState> generateWorkstationState(List<DeviceState> newFeedbackStateList, List<DeviceState> effectiveStateList) {
|
List<DeviceState> 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;
|
}
|
|
/**
|
* 将反馈的时间点转换为设备状态
|
* @param workstationId
|
* @param timePoint
|
* @param fixPointStateList
|
* @param stateMapper
|
* @return
|
*/
|
private DeviceState convertFeedbackPointToWorkstationState(Long workstationId, WorkstationFeedbackTimePointDTO timePoint, List<DeviceState> fixPointStateList, DeviceStateMapper stateMapper) {
|
DeviceState state = new DeviceState();
|
state.setTime(timePoint.getFeedbackTime().getTime());
|
state.setWcs(Integer.valueOf(timePoint.getWcs()));
|
state.setValueCollect(Integer.valueOf(timePoint.getWcs()));
|
state.setWorkstationId(workstationId);
|
state.setIsDeleted(false);
|
state.setIsSync(false);
|
state.setIsFixPoint(false);
|
state.setFeedbackId(timePoint.getFeedbackId());
|
state.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)) {
|
state.setCalendarCode(matchRecord.getCalendarCode());
|
state.setFactoryYear(matchRecord.getFactoryYear());
|
state.setFactoryWeek(matchRecord.getFactoryWeek());
|
state.setFactoryMonth(matchRecord.getFactoryMonth());
|
state.setFactoryDate(matchRecord.getFactoryDate());
|
state.setIsFixPoint(matchRecord.getIsFixPoint());
|
state.setShiftTimeType(matchRecord.getShiftTimeType());
|
state.setShiftIndex(matchRecord.getShiftIndex());
|
} else {
|
state = wrapperShiftInfo(state, stateMapper);
|
}
|
|
//GlobalWcsOfRps setting = WorkstationCache.getWorkstationWcsSetting(workstationId, timePoint.getWcs());
|
WorkstationCache workstationCache = SpringUtil.getBean(WorkstationCache.class);
|
GlobalWcsOfRps setting = workstationCache.getWorkstationWcsSetting(workstationId, timePoint.getWcs());
|
state.setRps(setting.getRps());
|
state.setIsPlan(setting.getIsPlan());
|
return state;
|
}
|
|
private DeviceState wrapperShiftInfo(DeviceState workstationState, DeviceStateMapper stateMapper) {
|
/*
|
WorkstationState resultTimePoint = stateMapper.getLastWorkstationState(Wrappers.<WorkstationState>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;
|
}
|
|
|
}
|