yangys
2024-11-21 fe82f1f9a9be911d1420fe3b018ea85dd5fff1a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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;
    }
 
    
}