yangys
2024-10-09 7ef593e1e3c35aaeecf9318f0b3941230d3ed002
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
package com.qianwen.mdc.collect.utils;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qianwen.core.tool.utils.BeanUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.mdc.collect.entity.iotdb.AggregateState;
import com.qianwen.mdc.collect.entity.iotdb.DeviceState;
import com.qianwen.mdc.collect.enums.FeedbackTimePointEnum;
 
/**
 * 无反馈得状态聚合 util
 * 
 */
public class WorkstationStateAggregateNoFeedbackUtils {
    private static final Logger log = LoggerFactory.getLogger(WorkstationStateAggregateNoFeedbackUtils.class);
 
    
 
    public static List<AggregateState> buildAggregateList(List<DeviceState> notSyncWorkstationStates) {
        List<AggregateState> workstationAggregateStateList = new ArrayList<>();
        List<DeviceState> notSyncWorkstationStates2 = rebuildNotSyncWorkstationStates(notSyncWorkstationStates);
        if (Func.isNotEmpty(notSyncWorkstationStates2)) {
            int resultCount = notSyncWorkstationStates2.size();
            for (int i = 0; i < notSyncWorkstationStates2.size() - 1; i++) {
                DeviceState current = notSyncWorkstationStates2.get(i);
                DeviceState nextState = notSyncWorkstationStates2.get(i + 1);
                //使用ts的差,构建durationCollect,其余使用current的属性
                AggregateState aggState =new AggregateState();
                //workstationAggregateStateList.add(AggregateState.builder().ts(current.getTs()).workstationId(current.getWorkstationId()).isDeleted(Boolean.FALSE).valueCollect(current.getValueCollect()).rps(current.getRps()).wcs(current.getWcs()).isPlan(current.getIsPlan()).shiftIndex(current.getShiftIndex()).shiftTimeType(current.getShiftTimeType()).calendarCode(current.getCalendarCode()).factoryDate(current.getFactoryDate()).factoryMonth(current.getFactoryMonth()).factoryWeek(current.getFactoryWeek()).factoryYear(current.getFactoryYear()).durationCollect(Long.valueOf(TimeUnit.MILLISECONDS.toSeconds(nextState.getTs().longValue() - current.getTs().longValue()))).employeeId(current.getEmployeeId()).endTime(nextState.getTs()).build());
                aggState.setTime(current.getTime());
                aggState.setWorkstationId(current.getWorkstationId());
                aggState.setIsDeleted(false);
                aggState.setValueCollect(current.getValueCollect());
                aggState.setRps(current.getRps());
                aggState.setWcs(current.getWcs());
                aggState.setIsPlan(current.getIsPlan());
                aggState.setShiftIndex(current.getShiftIndex());
                aggState.setShiftTimeType(current.getShiftTimeType());
                aggState.setCalendarCode(current.getCalendarCode());
                aggState.setFactoryDate(current.getFactoryDate());
                aggState.setFactoryMonth(current.getFactoryMonth());
                aggState.setFactoryWeek(current.getFactoryWeek());
                aggState.setFactoryYear(current.getFactoryYear());
                aggState.setDurationCollect(Long.valueOf(TimeUnit.MILLISECONDS.toSeconds(nextState.getTime().longValue() - current.getTime().longValue())));
                aggState.setEmployeeId(current.getEmployeeId());
                aggState.setEndTime(nextState.getTime());
                
                workstationAggregateStateList.add(aggState);
            }
            
            //构建最后一条,无durationCollect
            DeviceState lastNoSyncWorkstationState = notSyncWorkstationStates2.get(resultCount - 1);
            
            AggregateState lastAggState = new AggregateState();
            //workstationAggregateStateList.add(AggregateState.builder().ts(lastNoSyncWorkstationState.getTime()).workstationId(lastNoSyncWorkstationState.getWorkstationId()).isDeleted(Boolean.FALSE).valueCollect(lastNoSyncWorkstationState.getValueCollect()).rps(lastNoSyncWorkstationState.getRps()).wcs(lastNoSyncWorkstationState.getWcs()).isPlan(lastNoSyncWorkstationState.getIsPlan()).shiftIndex(lastNoSyncWorkstationState.getShiftIndex()).shiftTimeType(lastNoSyncWorkstationState.getShiftTimeType()).calendarCode(lastNoSyncWorkstationState.getCalendarCode()).factoryDate(lastNoSyncWorkstationState.getFactoryDate()).factoryMonth(lastNoSyncWorkstationState.getFactoryMonth()).factoryWeek(lastNoSyncWorkstationState.getFactoryWeek()).factoryYear(lastNoSyncWorkstationState.getFactoryYear()).employeeId(lastNoSyncWorkstationState.getEmployeeId()).build());
            lastAggState.setTime(lastNoSyncWorkstationState.getTime());
            lastAggState.setWorkstationId(lastNoSyncWorkstationState.getWorkstationId());
            lastAggState.setIsDeleted(false);
            lastAggState.setValueCollect(lastNoSyncWorkstationState.getValueCollect());
            lastAggState.setRps(lastNoSyncWorkstationState.getRps());
            lastAggState.setWcs(lastNoSyncWorkstationState.getWcs());
            lastAggState.setIsPlan(lastNoSyncWorkstationState.getIsPlan());
            lastAggState.setShiftIndex(lastNoSyncWorkstationState.getShiftIndex());
            lastAggState.setShiftTimeType(lastNoSyncWorkstationState.getShiftTimeType());
            lastAggState.setCalendarCode(lastNoSyncWorkstationState.getCalendarCode());
            lastAggState.setFactoryDate(lastNoSyncWorkstationState.getFactoryDate());
            lastAggState.setFactoryMonth(lastNoSyncWorkstationState.getFactoryMonth());
            lastAggState.setFactoryWeek(lastNoSyncWorkstationState.getFactoryWeek());
            lastAggState.setFactoryYear(lastNoSyncWorkstationState.getFactoryYear());
            lastAggState.setEmployeeId(lastNoSyncWorkstationState.getEmployeeId());
            
            
            workstationAggregateStateList.add(lastAggState);
        }
        return workstationAggregateStateList;
    }
 
    /**
     * 重新构建未同步状态列表数据
     * @param originalDeviceStates
     * @return
     */
    private static List<DeviceState> rebuildNotSyncWorkstationStates(List<DeviceState> originalDeviceStates) {
        List<DeviceState> stateList = new ArrayList<>();
        if (Func.isNotEmpty(originalDeviceStates)) {
            List<DeviceState> notSyncWorkstationStates = originalDeviceStates.stream().filter(x -> {
                //含义未选取fixpoint 或者非反馈点,其他类型的采集数据不处理了,我们自己就不用过滤了,因为都是NO_FEED_BACK_POINT,且isFixPoint=false
                return Boolean.TRUE.equals(x.getIsFixPoint()) || x.getFeedbackPointType() == null || x.getFeedbackPointType().equals(FeedbackTimePointEnum.NO_FEED_BACK_POINT.getValue());
            }).map(x2 -> {
                //固定点,设置wcs,rps=0
                if (x2.getIsFixPoint().booleanValue()) {
                    x2.setWcs(0);
                    x2.setRps(0);
                    x2.setIsPlan(null);
                }
                return x2;
            }).collect(Collectors.toList()).stream().sorted(Comparator.comparing(DeviceState::getTime)).collect(Collectors.toList());
            
            notSyncWorkstationStates.forEach(x3 -> {
                if (x3.getIsFixPoint().booleanValue()) {
                    //是固定点,找出不晚于该固定点的最后一个点,加入stateList;如果没有找到,则将该固定点加入stateList
                    List<DeviceState> collect = notSyncWorkstationStates.stream().filter(y -> {
                        //在列表中找出时间不晚于(<=)该固定点(x3)的非固定点(为了获取最后一条的数据)
                        return !y.getIsFixPoint().booleanValue() && y.getTime().longValue() <= x3.getTime().longValue();
                    }).collect(Collectors.toList());
                    
                    if (Func.isNotEmpty(collect)) {//除非之前没有过数据,否则这个条件(isNotEmpty)都成立
                        DeviceState temp = (DeviceState) BeanUtil.copy(x3, DeviceState.class);
                        temp.setValueCollect(collect.get(collect.size() - 1).getValueCollect());
                        temp.setWcs(collect.get(collect.size() - 1).getWcs());
                        temp.setRps(collect.get(collect.size() - 1).getRps());
                        temp.setIsPlan(collect.get(collect.size() - 1).getIsPlan());
                        stateList.add(temp);
                        return;
                    }
                    stateList.add(x3);
                    return;
                }
                //非固定点,直接加入列表
                stateList.add(x3);
            });
        }
        return stateList;
    }
    
    public static List<AggregateState> getReallyEffectWorkstationStateAggregateNoFeedbackList(List<AggregateState> insertData, List<AggregateState> deleteData) {
        HashMap<Long, AggregateState> workstationStateMap = new HashMap<>();
        if (Func.isNotEmpty(insertData)) {
            insertData.forEach(item -> {
                workstationStateMap.put(item.getTime(), item);
            });
        }
        if (Func.isNotEmpty(deleteData)) {
            for (AggregateState state : deleteData) {
                if (!workstationStateMap.containsKey(state.getTime())) {
                    workstationStateMap.put(state.getTime(), state);
                }
            }
        }
        return workstationStateMap.values().stream().collect(Collectors.toList());
    }
}