package com.qianwen.mdc.collect.service;
|
|
import java.time.LocalDateTime;
|
import java.time.ZoneId;
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.Collections;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Map.Entry;
|
import java.util.stream.Collectors;
|
|
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
|
import org.apache.iotdb.tsfile.write.record.Tablet;
|
import org.apache.iotdb.tsfile.write.schema.MeasurementSchema;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import com.google.common.collect.Lists;
|
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.config.IotDBSessionConfig;
|
import com.qianwen.mdc.collect.constants.CommonConstant;
|
import com.qianwen.mdc.collect.constants.IOTDBConstant;
|
import com.qianwen.mdc.collect.dto.CalendarShiftInfoDTO;
|
import com.qianwen.mdc.collect.dto.WorkstationDTO;
|
import com.qianwen.mdc.collect.entity.iotdb.AggregateStateWithFeedback;
|
import com.qianwen.mdc.collect.entity.iotdb.DeviceState;
|
import com.qianwen.mdc.collect.enums.FeedbackTimePointEnum;
|
import com.qianwen.mdc.collect.mapper.mgr.CalendarMapper;
|
import com.qianwen.mdc.collect.utils.LocalDateTimeUtils;
|
|
import cn.hutool.core.date.DatePattern;
|
import cn.hutool.core.date.DateTime;
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
/**
|
* 设备状态普通服务
|
*/
|
@Service
|
public class AggregateStateWithFeedbackService{
|
private static final Logger log = LoggerFactory.getLogger(AggregateStateWithFeedbackService.class);
|
|
@Autowired
|
private IotDBCommonService iotDBCommonService;
|
@Autowired
|
private IotDBSessionConfig iotdbConfig;
|
|
static final List<MeasurementSchema> schemas;
|
|
static {
|
schemas = new ArrayList<>();
|
|
schemas.add(new MeasurementSchema("workstation_id", TSDataType.INT64));
|
schemas.add(new MeasurementSchema("value_collect", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("end_time", TSDataType.INT64));
|
schemas.add(new MeasurementSchema("duration_collect", TSDataType.INT64));
|
|
schemas.add(new MeasurementSchema("calendar_code", TSDataType.TEXT));
|
schemas.add(new MeasurementSchema("factory_year", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("factory_month", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("factory_week", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("factory_date", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("shift_index", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("shift_time_type", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("wcs", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("rps", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("is_plan", TSDataType.INT32));
|
schemas.add(new MeasurementSchema("feedback_id", TSDataType.INT64));
|
schemas.add(new MeasurementSchema("is_deleted", TSDataType.BOOLEAN));
|
|
}
|
|
/**
|
* 保存状态固定点数据(state_{workstationId})
|
* @param stateList
|
*/
|
public void saveStatesWithFeedback(List<AggregateStateWithFeedback> stateFbList) {
|
//将数据按照工位id分组
|
Map<Long,List<AggregateStateWithFeedback>> maps = stateFbList.stream().collect(Collectors.groupingBy(AggregateStateWithFeedback::getWorkstationId));
|
String deviceId;
|
Long wid;
|
|
List<AggregateStateWithFeedback> statesFb;
|
for(Entry<Long, List<AggregateStateWithFeedback>> entry: maps.entrySet()) {
|
wid = entry.getKey();
|
|
deviceId = IOTDBConstant.DB_PREFIX+"aggregate_state_with_feedback_"+wid;
|
iotDBCommonService.setTemmplateIfNotSet(IOTDBConstant.TEMPLATE_AGGREGATESTATE_WITH_FEEDBACK, deviceId);//挂载模板
|
|
Tablet tablet = new Tablet(deviceId, schemas);
|
|
statesFb = entry.getValue();
|
tablet.rowSize = statesFb.size();
|
AggregateStateWithFeedback aggStateFB;
|
for(int i=0;i<statesFb.size();i++) {
|
aggStateFB = statesFb.get(i);
|
tablet.addTimestamp(i, aggStateFB.getTime());
|
tablet.addValue("workstation_id", i, aggStateFB.getWorkstationId());
|
tablet.addValue("value_collect", i, aggStateFB.getValueCollect());
|
tablet.addValue("end_time", i, aggStateFB.getEndTime());
|
tablet.addValue("duration_collect", i, aggStateFB.getDurationCollect());
|
tablet.addValue("calendar_code", i, aggStateFB.getCalendarCode());
|
tablet.addValue("factory_year", i, aggStateFB.getFactoryYear());
|
tablet.addValue("factory_month", i, aggStateFB.getFactoryMonth());
|
tablet.addValue("factory_week", i, aggStateFB.getFactoryWeek());
|
tablet.addValue("factory_date", i, aggStateFB.getFactoryDate());
|
tablet.addValue("shift_index", i, aggStateFB.getShiftIndex());
|
tablet.addValue("shift_time_type", i, aggStateFB.getShiftTimeType());
|
tablet.addValue("wcs", i, aggStateFB.getWcs());
|
tablet.addValue("rps", i, aggStateFB.getRps());
|
|
tablet.addValue("is_plan", i, aggStateFB.getIsPlan());
|
tablet.addValue("feedback_id", i, aggStateFB.getFeedbackId());
|
tablet.addValue("is_deleted", i, aggStateFB.getIsDeleted());
|
|
}
|
try {
|
//每个工位批量插入一次数据
|
this.iotdbConfig.getSessionPool().insertAlignedTablet(tablet);
|
} catch (Exception e) {
|
log.error("保存固定点数据异常",e);
|
}
|
|
}
|
}
|
|
}
|