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);
|
}
|
}
|