yangys
2024-09-27 26f8e5990686bdba2119024a260d986266506757
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
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<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()));
        
        //stateMapper.batchSave(feedback.getWorkstationId(), insertData);
        //我们使用servcie保存
        
        List<WorkstationFeedbackTimeQuantumDTO> effectiveFeedbackTimeQuantumList = analyseResult.getEffectiveFeedbackTimeQuantumList();
        List<WorkstationWcsFeedbackDetail> detailList = (List<WorkstationWcsFeedbackDetail>)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());
    }
 
    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;
    }
 
    private DeviceState convertFeedbackPointToWorkstationState(Long workstationId, WorkstationFeedbackTimePointDTO timePoint, List<DeviceState> 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.<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;
    }
 
    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;
    }
}