yangys
2025-11-05 10c5247722995e571b3fd4dbffb178964a9bd6ee
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
package com.qianwen.smartman.modules.tdengine.service.impl;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.qianwen.core.tool.utils.DateUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.core.tool.utils.SpringUtil;
import com.qianwen.smartman.common.cache.cps.TimeSliceCache;
import com.qianwen.smartman.common.enums.DynamicCalculationWcsDataType;
import com.qianwen.smartman.common.enums.WcsDataTypeEnums;
import com.qianwen.smartman.common.utils.DelayList;
import com.qianwen.smartman.common.utils.Lambda;
import com.qianwen.smartman.modules.smis.dto.CalendarShiftTimeSlicesDTO;
import com.qianwen.smartman.modules.smis.entity.Workstation;
import com.qianwen.smartman.modules.smis.entity.WorkstationWcs;
import com.qianwen.smartman.modules.smis.entity.WorkstationWcsUsage;
import com.qianwen.smartman.modules.smis.service.IWorkstationService;
import com.qianwen.smartman.modules.smis.service.IWorkstationWcsService;
import com.qianwen.smartman.modules.smis.service.IWorkstationWcsUsageService;
import com.qianwen.smartman.modules.mdc.entity.SuperAggregateOutput;
import com.qianwen.smartman.modules.tdengine.dto.RemoveTableDTO;
import com.qianwen.smartman.modules.tdengine.mapper.WorkstationDynamicCollectMapper;
import com.qianwen.smartman.modules.tdengine.service.IWorkstationDynamicCollectService;
import com.qianwen.smartman.modules.visual.dto.DynamicCountDateAggregateDTO;
import com.qianwen.smartman.modules.visual.dto.VisualTimeDTO;
import com.qianwen.smartman.modules.visual.utils.VisualUtils;
import com.qianwen.smartman.modules.visual.vo.VisualUsagePolicyEnum;
 
import cn.hutool.core.date.LocalDateTimeUtil;
 
@Service
public class WorkstationDynamicCollectServiceImpl implements IWorkstationDynamicCollectService {
    private static final Logger log = LoggerFactory.getLogger(WorkstationDynamicCollectServiceImpl.class);
    @Resource
    @Lazy
    private IWorkstationService workstationService;
    @Resource
    @Lazy
    private IWorkstationWcsService workstationWcsService;
    @Resource
    @Lazy
    private IWorkstationWcsUsageService workstationWcsUsageService;
    private DelayList<RemoveTableDTO> delayHandlerQueue = new DelayList<RemoveTableDTO>(5) { // from class: org.springblade.modules.tdengine.service.impl.WorkstationDynamicCollectServiceImpl.1
        {
            setConsumer(data -> {
                data.forEach(item -> {
                    Optional<DynamicCalculationWcsDataType> ofNullable = Optional.ofNullable(DynamicCalculationWcsDataType.of(item.getCollectType().intValue()));
                    WorkstationDynamicCollectServiceImpl workstationDynamicCollectServiceImpl = WorkstationDynamicCollectServiceImpl.this;
                    ofNullable.map(x$0 -> {
                        return workstationDynamicCollectServiceImpl.getMapper(x$0);
                    }).ifPresent(mapper -> {
                        mapper.deleteSuperTable(item.getWorkstationId(), item.getUsageCode());
                        mapper.deleteTable(item.getWorkstationId(), item.getUsageCode());
                        WorkstationDynamicCollectServiceImpl.log.info("删除采集项表,工位:{}, 标签:{}, {},{}", item.getWorkstationId(), item.getUsageCode());
                    });
                });
            });
        }
    };
 
    private WorkstationDynamicCollectMapper<?> getMapper(@NotNull DynamicCalculationWcsDataType dynamicCalculationWcsDataType) {
        return SpringUtil.getBean(dynamicCalculationWcsDataType.getOperationMapper());
    }
 
    @Override 
    public void removeTableUpdateWcs(List<WorkstationWcs> updateWcsList) {
        Map<Long, WorkstationWcs> current = new HashMap<>();
        //删除时序数据库中的表
        updateWcsList.stream().filter(it -> Func.isNotEmpty(it.getUsageId())).peek(it -> current.put(it.getWorkstationId(),it)).map(it ->{
            return this.workstationWcsService.getOne(Lambda.<WorkstationWcs>eq(WorkstationWcs::getWorkstationId, it.getWorkstationId()).eq(WorkstationWcs::getCollectSettingItem, it.getCollectSettingItem()).ne(WorkstationWcs::getDataType, WcsDataTypeEnums.WcsDataType.OTHER.getCode()));
        }) .filter(Func::notNull)
        .filter(old -> !old.getUsageId().equals(((WorkstationWcs)current.get(old.getWorkstationId())).getUsageId()))
        .forEach(this::removeTable);
        
        /*
        Map<Long, WorkstationWcs> current = new HashMap<>();
        updateWcsList.stream().filter(it -> {
            return Func.isNotEmpty(it.getUsageId());
        }).peek(it2 -> {
            WorkstationWcs workstationWcs = (WorkstationWcs) current.put(it2.getWorkstationId(), it2);
        }).map(it3 -> {
            return (WorkstationWcs) this.workstationWcsService.getOne((Wrapper) ((ExtraLambdaQueryWrapper) Lambda.eq((v0) -> {
                return v0.getWorkstationId();
            }, it3.getWorkstationId()).eq((v0) -> {
                return v0.getCollectSettingItem();
            }, it3.getCollectSettingItem())).ne((v0) -> {
                return v0.getDataType();
            }, WcsDataTypeEnums.WcsDataType.OTHER.getCode()));
        }).filter((v0) -> {
            return Func.notNull(v0);
        }).filter(old -> {
            return !old.getUsageId().equals(((WorkstationWcs) current.get(old.getWorkstationId())).getUsageId());
        }).forEach(this::removeTable);
        */
    }
 
    @Override 
    public void removeTableDeleteWcs(final List<WorkstationWcs> deleteWcsList) {
        deleteWcsList.forEach(this::removeTable);
    }
 
    private void removeTable(final WorkstationWcs oldWorkstationWcs) {
        Optional.ofNullable(this.workstationWcsUsageService.getById(oldWorkstationWcs.getUsageId())).ifPresent(usage -> {
            removeTable(oldWorkstationWcs.getWorkstationId(), usage.getUsageCode(), usage.getCollectType());
        });
    }
 
    @Override 
    public void removeTable(final WorkstationWcsUsage workstationWcsUsage) {
        this.workstationWcsService.list(Lambda.eq(WorkstationWcs::getUsageId, workstationWcsUsage.getId())).forEach(it -> {
            removeTable(it.getWorkstationId(), workstationWcsUsage.getUsageCode(), workstationWcsUsage.getCollectType());
        });
    }
 
    @Async
    public void removeTable(final Long workstationId, final String usageCode, final Integer collectType) {
        this.delayHandlerQueue.add(new RemoveTableDTO(workstationId, usageCode, collectType));
    }
 
    @Override 
    public void removeTableByDelWorkstations(final List<Long> workstationIds) {
        //new ExtraLambdaQueryWrapper<WorkstationWcs>().eq(WorkstationWcs::getWorkstationId, workstationId).isNotNull(WorkstationWcs::getUsageId).select(new SFunction[] { WorkstationWcs::getUsageId }).stream().map(WorkstationWcs::getUsageId).map(this.workstationWcsUsageService::getById).forEach(()));
        //workstationIds.forEach(workstationId -> this.workstationWcsService.list((Wrapper)((ExtraLambdaQueryWrapper)Lambda.eq(WorkstationWcs::getWorkstationId, workstationId).isNotNull(WorkstationWcs::getUsageId)).select(new SFunction[] { WorkstationWcs::getUsageId })).stream().map(WorkstationWcs::getUsageId).map(this.workstationWcsUsageService::getById).forEach(()));
        
        workstationIds.forEach(workstationId -> {
            Stream<Long> map = this.workstationWcsService.list(Lambda.<WorkstationWcs>eq(WorkstationWcs::getWorkstationId, workstationId).isNotNull(WorkstationWcs::getUsageId)
                    .select(WorkstationWcs::getUsageId )).stream().map(WorkstationWcs::getUsageId);
            ////.map(WorkstationWcs::getUsageId);
            
            map.map((usageId) -> {
                return this.workstationWcsUsageService.getById(usageId);
            }).forEach(it -> {
                removeTable(workstationId, it.getUsageCode(), it.getCollectType());
            });
        });
    }
 
    @Override 
    public List<Long> getWorkstationIdsByUsageId(final Long usageId) {
        
        return this.workstationWcsService.list(Lambda.<WorkstationWcs>eq(WorkstationWcs::getUsageId, usageId).select(WorkstationWcs::getWorkstationId)).stream()
                  .filter(Func::notNull)
                  .map(WorkstationWcs::getWorkstationId).collect(Collectors.toList());
        
    }
 
    @Override 
    public DynamicCountDateAggregateDTO getOutputDataByWorkstationAndFactoryDate(List<Long> workstationIds, String timeLevel, VisualUsagePolicyEnum policyEnum, WorkstationWcsUsage workstationWcsUsage) {
         DynamicCalculationWcsDataType dynamicCalculationWcsDataType = DynamicCalculationWcsDataType.of(workstationWcsUsage.getCollectType().intValue());
            List<Workstation> workstationList = this.workstationService.list(Lambda.<Workstation>create().orderByAsc(Workstation::getId, workstationIds)
                .in(Workstation::getId, workstationIds));
            VisualTimeDTO visualTimeDTO = policyEnum.equals(VisualUsagePolicyEnum.HOUR) ? VisualUtils.getTimeSectionByLevel(timeLevel) : VisualUtils.getTimeSectionDayByLevel(timeLevel);
            List<SuperAggregateOutput> aggregateOutputs = (List<SuperAggregateOutput>)workstationIds.stream().flatMap(it -> getMapper(dynamicCalculationWcsDataType).getOutputDataByWorkstationAndFactoryDate(it, LocalDateTimeUtil.format(visualTimeDTO.getStartTime(), "yyyy-MM-dd HH:mm:ss"), LocalDateTimeUtil.format(visualTimeDTO.getEndTime(), "yyyy-MM-dd HH:mm:ss"), workstationWcsUsage.getUsageCode()).stream()).collect(Collectors.toList());
            return new DynamicCountDateAggregateDTO(aggregateOutputs, visualTimeDTO.getStartTime(), visualTimeDTO.getEndTime(), workstationList);
        /*
        DynamicCalculationWcsDataType dynamicCalculationWcsDataType = DynamicCalculationWcsDataType.of(workstationWcsUsage.getCollectType().intValue());
        List<Workstation> workstationList = this.workstationService.list((Wrapper) Lambda.create().orderByAsc((v0) -> {
            return v0.getId();
        }, workstationIds).in((v0) -> {
            return v0.getId();
        }, workstationIds));
        VisualTimeDTO visualTimeDTO = policyEnum.equals(VisualUsagePolicyEnum.HOUR) ? VisualUtils.getTimeSectionByLevel(timeLevel) : VisualUtils.getTimeSectionDayByLevel(timeLevel);
        List<SuperAggregateOutput> aggregateOutputs = (List) workstationIds.stream().flatMap(it -> {
            return getMapper(dynamicCalculationWcsDataType).getOutputDataByWorkstationAndFactoryDate(it, LocalDateTimeUtil.format(visualTimeDTO.getStartTime(), DateConstant.PATTERN_DATE_TIME), LocalDateTimeUtil.format(visualTimeDTO.getEndTime(), DateConstant.PATTERN_DATE_TIME), workstationWcsUsage.getUsageCode()).stream();
        }).collect(Collectors.toList());
        return new DynamicCountDateAggregateDTO(aggregateOutputs, visualTimeDTO.getStartTime(), visualTimeDTO.getEndTime(), workstationList);
        */
    }
 
    @Override 
    public DynamicCountDateAggregateDTO getShiftOutput(final List<Long> workstationIds, final WorkstationWcsUsage workstationWcsUsage) {
        DynamicCalculationWcsDataType dynamicCalculationWcsDataType = DynamicCalculationWcsDataType.of(workstationWcsUsage.getCollectType().intValue());
        List<Workstation> workstationList = this.workstationService.list(Wrappers.<Workstation>lambdaQuery().in(Workstation::getId, workstationIds));
        
        List<SuperAggregateOutput> aggregateOutputs = workstationList.stream().flatMap(workstation -> {
            //it.getCalendarCode()
            CalendarShiftTimeSlicesDTO timeSliceShift = TimeSliceCache.getTimeSliceShift(workstation.getCalendarCode(), DateUtil.now());
            if (timeSliceShift == null) {
                return Stream.empty();
            }else {
                return getMapper(dynamicCalculationWcsDataType).getShiftOutput(Lists.newArrayList(new Long[]{workstation.getId()}), timeSliceShift.getShiftIndex(), Integer.valueOf(Func.toInt(timeSliceShift.getFactoryDate().replaceAll("-", ""))), workstationWcsUsage.getUsageCode()).stream();
            }
           
          }).collect(Collectors.toList());
        
        return new DynamicCountDateAggregateDTO(aggregateOutputs, null, null, workstationList);
    }
}