package com.qianwen.smartman.modules.mdc.service.impl; import java.math.BigDecimal; import java.math.RoundingMode; import java.sql.Timestamp; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.qianwen.core.tool.utils.DateTimeUtil; import com.qianwen.core.tool.utils.DateUtil; import com.qianwen.core.tool.utils.Func; import com.qianwen.smartman.common.constant.CommonConstant; import com.qianwen.smartman.common.constant.DateConstant; import com.qianwen.smartman.common.utils.LocalDateTimeUtils; import com.qianwen.smartman.modules.smis.entity.Workstation; import com.qianwen.smartman.modules.smis.service.IWorkstationService; import com.qianwen.smartman.modules.mdc.entity.SuperAggregate; import com.qianwen.smartman.modules.mdc.entity.SuperAggregateState; import com.qianwen.smartman.modules.mdc.enums.OpenTypeEnums; import com.qianwen.smartman.modules.mdc.enums.StatisticalMethodEnum; import com.qianwen.smartman.modules.mdc.mapper.SuperAggregateStateFeedbackMapper; import com.qianwen.smartman.modules.mdc.mapper.SuperAggregateStateMapper; import com.qianwen.smartman.modules.mdc.service.ISuperAggregateStateService; import com.qianwen.smartman.modules.mdc.utils.FilterOffUtils; import com.qianwen.smartman.modules.mdc.vo.StatusTimeTopVO; import cn.hutool.core.date.LocalDateTimeUtil; @Service public class SuperAggregateStateServiceImpl implements ISuperAggregateStateService { private static final Logger log = LoggerFactory.getLogger(SuperAggregateStateServiceImpl.class); private final SuperAggregateStateMapper baseMapper; private final SuperAggregateStateFeedbackMapper aggregateStateFeedbackMapper; private final IWorkstationService workstationService; public SuperAggregateStateServiceImpl(final SuperAggregateStateMapper baseMapper, final SuperAggregateStateFeedbackMapper aggregateStateFeedbackMapper, final IWorkstationService workstationService) { this.baseMapper = baseMapper; this.aggregateStateFeedbackMapper = aggregateStateFeedbackMapper; this.workstationService = workstationService; } @Override public List getStatusData(List workstationIds, StatisticalMethodEnum statisticalMethod, LocalDate startDate, LocalDate endDate) { List statusDataList; if (StatisticalMethodEnum.SHIFT.equals(statisticalMethod) || StatisticalMethodEnum.DAY.equals(statisticalMethod) || StatisticalMethodEnum.WEEK.equals(statisticalMethod) || StatisticalMethodEnum.MONTH.equals(statisticalMethod)) { statusDataList = this.baseMapper.getStatusDataByFactoryDate(workstationIds, Integer.parseInt(LocalDateTimeUtil.format(startDate, "yyyyMMdd")), Integer.parseInt(LocalDateTimeUtil.format(endDate, "yyyyMMdd"))); } else { //statusDataList = this.baseMapper.getStatusData(workstationIds, LocalDateTimeUtil.format(startDate, DateConstant.PATTERN_DATE_TIME), LocalDateTimeUtil.format(endDate.plusDays(1L), DateConstant.PATTERN_DATE_TIME)); LocalDateTime startTime = LocalDateTime.of(startDate, LocalTime.MIN); LocalDateTime endTime = LocalDateTime.of(endDate.plusDays(1L), LocalTime.MIN); //statusDataList = this.baseMapper.getStatusData(workstationIds, startDate,endDate.plusDays(1L)); statusDataList = this.baseMapper.getStatusData(workstationIds, DateTimeUtil.toDate(startTime),DateTimeUtil.toDate(endTime)); } return buildDuration(statusDataList); } @Override public List getStatusDataWithFeedback(List workstationIds, StatisticalMethodEnum statisticalMethod, LocalDate startDate, LocalDate endDate) { //稼动率查询 List statusDataList; if (StatisticalMethodEnum.SHIFT.equals(statisticalMethod) || StatisticalMethodEnum.DAY.equals(statisticalMethod) || StatisticalMethodEnum.WEEK.equals(statisticalMethod) || StatisticalMethodEnum.MONTH.equals(statisticalMethod)) { //statusDataList = this.aggregateStateFeedbackMapper.getStatusDataByFactoryDate(workstationIds, LocalDateTimeUtil.format(startDate, "yyyyMMdd"), LocalDateTimeUtil.format(endDate, "yyyyMMdd")); int startFactoryDate = Integer.parseInt(LocalDateTimeUtil.format(startDate, "yyyyMMdd")); int endFactoryDate = Integer.parseInt(LocalDateTimeUtil.format(endDate, "yyyyMMdd")); statusDataList = this.aggregateStateFeedbackMapper.getStatusDataByFactoryDate(workstationIds, startFactoryDate, endFactoryDate); } else { //statusDataList = this.aggregateStateFeedbackMapper.getStatusData(workstationIds, LocalDateTimeUtil.format(startDate, DateConstant.PATTERN_DATE_TIME), LocalDateTimeUtil.format(endDate.plusDays(1L), DateConstant.PATTERN_DATE_TIME)); LocalDateTime startTime = LocalDateTime.of(startDate, LocalTime.MIN); LocalDateTime endTime = LocalDateTime.of(endDate.plusDays(1L), LocalTime.MIN); statusDataList = this.aggregateStateFeedbackMapper.getStatusData(workstationIds, DateTimeUtil.toDate(startTime),DateTimeUtil.toDate(endTime)); } return buildDuration(statusDataList); } @Override public List getStatusByCondition(List workstationIds, LocalDateTime startTime, LocalDateTime endTime) { //List statusDataList = this.baseMapper.getStatusData(workstationIds, LocalDateTimeUtil.format(startTime, DateConstant.PATTERN_DATE_TIME), LocalDateTimeUtil.format(endTime, DateConstant.PATTERN_DATE_TIME)); List statusDataList = this.baseMapper.getStatusData(workstationIds, DateTimeUtil.toDate(startTime), DateTimeUtil.toDate(endTime)); return buildDuration(statusDataList); } @Override public List getStatusByTimeSection(List workstationIds, LocalDateTime startTime, LocalDateTime endTime) { //List statusDataList = this.baseMapper.getStatusDataByTimeSection(workstationIds, LocalDateTimeUtil.format(startTime, DateConstant.PATTERN_DATE_TIME), LocalDateTimeUtil.format(endTime, DateConstant.PATTERN_DATE_TIME)); List statusDataList = this.baseMapper.getStatusDataByTimeSection(workstationIds, DateTimeUtil.toDate(startTime), DateTimeUtil.toDate(endTime)); return buildDuration(startTime, statusDataList); } @Override public List getStatusTimeByWcs(List workstationIds, LocalDateTime startTime, LocalDateTime endTime, Integer status, Integer top) { List voList = new ArrayList<>(); //查询除所有状态时可用的工位,转换位 工位id -> 工位 的map Map workstationMap = this.workstationService.list(Wrappers.lambdaQuery() .eq(Workstation::getStatus, CommonConstant.ENABLE)).stream().collect(Collectors.toMap(Workstation::getId, Function.identity())); // Date startTimeD = LocalDateUtil.localDateTimeToDate(startTime); //startTimeD = new Date(124,8,2); //Date endTimeD = LocalDateUtil.localDateTimeToDate(endTime); //endTimeD = new Date(124,8,3); //查询出时间段内wcs=status的所有数据 List equipmentStatusDuration = this.baseMapper.getEquipmentStatusDuration(workstationIds, DateTimeUtil.toDate(startTime), DateTimeUtil.toDate(endTime), status); List equipmentStatusDuration2 = FilterOffUtils.filterOffDay(equipmentStatusDuration, OpenTypeEnums.TIME_USED_ANALYSIS);//过滤数据,TIME_USED_ANALYSIS open =1则过滤 Map timeMap = buildDuration(startTime, equipmentStatusDuration2).stream().collect(Collectors.groupingBy(SuperAggregate::getWorkstationId, Collectors.summingLong(SuperAggregateState::getDurationCollect))).entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(top.intValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, java.util.LinkedHashMap::new)); //yangys改了buildDuration,去掉了startTime参数 //Map timeMap = buildDuration(equipmentStatusDuration2,ChronoUnit.SECONDS).stream().collect(Collectors.groupingBy(SuperAggregate::getWorkstationId, Collectors.summingLong(SuperAggregateState::getDurationCollect))).entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(top.intValue()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, java.util.LinkedHashMap::new)); /* Map timeMap = (buildDuration(startTime, FilterOffUtils.filterOffDay(equipmentStatusDuration, OpenTypeEnums.TIME_USED_ANALYSIS)).stream().collect(Collectors.groupingBy((v0) -> { return v0.getWorkstationId(); }, Collectors.summingLong((v0) -> { return v0.getDurationCollect(); })))).entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(top.intValue()).collect(Collectors.toMap((v0) -> { return v0.getKey(); }, (v0) -> { return v0.getValue(); }, (oldValue, newValue) -> { return oldValue; }, LinkedHashMap::new));*/ timeMap.forEach((k, v) -> { StatusTimeTopVO statusTimeTopVO = new StatusTimeTopVO(); statusTimeTopVO.setValue(secondToHour(v));// statusTimeTopVO.setWorkstation(workstationMap.get(k)); voList.add(statusTimeTopVO); }); return voList; } @Override public List getOeeAnalysis(List workStationIdList, LocalDateTime startTime, LocalDateTime endTime) { //Date startTimeD = LocalDateUtil.localDateTimeToDate(startTime); //Date endTimeD = LocalDateUtil.localDateTimeToDate(endTime); //List statusDataList = this.baseMapper.getEquipmentStatusDuration(workStationIdList, LocalDateTimeUtil.format(startTime, DateConstant.PATTERN_DATE_TIME), LocalDateTimeUtil.format(endTime, DateConstant.PATTERN_DATE_TIME), null); List statusDataList = this.baseMapper.getEquipmentStatusDuration(workStationIdList, DateTimeUtil.toDate(startTime), DateTimeUtil.toDate(endTime), null); return buildDuration(startTime, FilterOffUtils.filterOffDay(statusDataList, OpenTypeEnums.OEE)); } @Override public List getShiftStatusData(List workstationIdList, String factoryDate, Integer shiftIndex) { List statusDataShift = this.baseMapper.getStatusDataShift(Integer.valueOf(factoryDate.replace("-", "")), shiftIndex, workstationIdList); return buildDuration(statusDataShift); } @Override public List getStatusByFactory(List workstationIdList, String factoryDate, Integer shiftIndex) { List statusDataShift = this.baseMapper.getStatusByFactory(Integer.valueOf(Func.toInt(factoryDate.replace("-", ""))), shiftIndex, workstationIdList); return buildDuration(statusDataShift); } @Override public List listsState(List ids, LocalDate queryTime, List shiftIndex) { String factoryDate = DateUtil.format(queryTime, DateConstant.PATTERN_DATE); List statusDataShift = this.baseMapper.listsState(Integer.valueOf(Func.toInt(factoryDate.replace("-", ""))), ids, shiftIndex); return buildDuration(statusDataShift); } @Override public List getDataByWeek(List ids, Integer year, List weekList) { List data = this.baseMapper.getDataByWeek(ids, year, weekList); return buildDuration(data); } @Override public List getDataByMonth(List ids, Integer year, List monthList) { List data = this.baseMapper.getDataByMonth(ids, year, monthList); return buildDuration(data); } @Override public List crossDay(LocalDateTime start, LocalDateTime end) { String startTime = DateUtil.formatDateTime(start); String endTime = DateUtil.formatDateTime(end); List res = this.baseMapper.listCrossDay(startTime, endTime); return buildDuration(res); } /* @Override public void saveState(List collect) { Map> map = collect.stream().collect(Collectors.groupingBy(SuperAggregate::getWorkstationId)); map.forEach(this.baseMapper::saveState); } */ @Override public List getStatusDataByTime(List workstationIdList, LocalDateTime start, LocalDateTime end) { List superAggregateStateList = new ArrayList<>(); String startTime = LocalDateTimeUtil.format(start, "yyyy-MM-dd HH:mm:ss.SSS"); String endTime = LocalDateTimeUtil.format(end, "yyyy-MM-dd HH:mm:ss.SSS"); List inTime = this.baseMapper.listInTime(workstationIdList, startTime, endTime); if (Func.isNotEmpty(inTime)) { superAggregateStateList.addAll(inTime); } List tsInTime = this.baseMapper.listTsInTime(workstationIdList, startTime, endTime); if (Func.isNotEmpty(tsInTime)) { superAggregateStateList.addAll(tsInTime); } List endTimeInTime = this.baseMapper.listEndTimeInTime(workstationIdList, startTime, endTime); if (Func.isNotEmpty(endTimeInTime)) { superAggregateStateList.addAll(inTime); } List outTime = this.baseMapper.listOutTime(workstationIdList, startTime, endTime); if (Func.isNotEmpty(outTime)) { superAggregateStateList.addAll(outTime); } superAggregateStateList.stream().forEach(superAggregateState -> { if (Func.isEmpty(endTime)) { superAggregateState.setEndTime(Timestamp.valueOf(LocalDateTime.now())); } if (superAggregateState.getStartTime().before(Timestamp.valueOf(start))) { superAggregateState.setStartTime(Timestamp.valueOf(start)); } if (superAggregateState.getEndTime().after(Timestamp.valueOf(end))) { superAggregateState.setEndTime(Timestamp.valueOf(end)); } }); return superAggregateStateList; } @Override public List queryPerfByDay(Long workstationId, String startTime, String endTime, Long employeeId) { List states = this.baseMapper.queryPerfByDay(workstationId, startTime, endTime, employeeId); return buildDuration(states); } @Override public List queryPerfByWeek(Long workstationId, List weeks, Long employeeId) { List states = this.baseMapper.queryPerfByWeek(workstationId, weeks, employeeId); return buildDuration(states); } @Override public List queryPerfByMonth(Long workstationId, List months, Long employeeId) { List states = this.baseMapper.queryPerfByMonth(workstationId, months, employeeId); return buildDuration(states); } @Override public List queryPerfByEtDay(Long employeeId, String queryTime) { List states = this.baseMapper.queryPerfByEtDay(employeeId, queryTime); return buildDuration(states); } @Override public List queryPerfByEtWeek(Long employeeId, Integer week) { List states = this.baseMapper.queryPerfByEtWeek(employeeId, week); return buildDuration(states); } @Override public List queryPerfByEtMonth(Long employeeId, Integer month) { List states = this.baseMapper.queryPerfByEtMonth(employeeId, month); return buildDuration(states); } @Override public List queryPerfByWtDay(Long workstationId, String time) { List states = this.baseMapper.queryPerfByWtDay(workstationId, time); return buildDuration(states); } @Override public List queryPerfByWtWeek(Long workstationId, Integer week) { List states = this.baseMapper.queryPerfByWtWeek(workstationId, week); return buildDuration(states); } @Override public List queryPerfByWtMonth(Long workstationId, Integer month) { List states = this.baseMapper.queryPerfByWtMonth(workstationId, month); return buildDuration(states); } @Override public List getCurrMonthState(List ids, Integer month) { List states = this.baseMapper.getCurrMonthState(ids, month); return buildDuration(states); } @Override public List getCurrMonthAlarmState(List workIds, Integer month, Integer code) { List states = this.baseMapper.getCurrMonthAlarmState(workIds, month, code); return buildDuration(states); } @Override public List getCurrWeekAlarmState(List workIds, Integer week, Integer code) { List states = this.baseMapper.getCurrWeekAlarmState(workIds, week, code); return buildDuration(states); } private List buildDuration(List statusDataShift) { Date now = DateUtil.now(); statusDataShift.forEach(x -> { if (Func.isEmpty(x.getEndTime()) ) {//|| x.getEndTime().toLocalDateTime().getYear()==1970 x.setEndTime(new Timestamp(now.getTime())); } x.setDurationCollect(LocalDateTimeUtils.betweenTwoTime(x.getStartTime().toLocalDateTime(), x.getEndTime().toLocalDateTime(), ChronoUnit.MILLIS)); //System.out.println(x); }); return statusDataShift; } private List buildDuration(LocalDateTime startTime, List statusDataShift) { Date now = DateUtil.now(); statusDataShift.forEach(x -> { if (Func.isEmpty(x.getEndTime()) || x.getEndTime().toLocalDateTime().getYear()==1970) { x.setEndTime(new Timestamp(now.getTime())); } if (x.getStartTime().toLocalDateTime().isBefore(startTime)) { x.setStartTime(Timestamp.valueOf(startTime)); x.setTime(Timestamp.valueOf(startTime));//这个才起作用,getStartTime获取的是time } x.setDurationCollect(LocalDateTimeUtils.betweenTwoTime(x.getStartTime().toLocalDateTime(), x.getEndTime().toLocalDateTime(), ChronoUnit.MILLIS)); //System.out.println(x); }); return statusDataShift; } private double secondToHour(Long second) { BigDecimal bg = new BigDecimal(second.longValue()); BigDecimal bg2 = new BigDecimal("3600000"); return bg.divide(bg2, 2, RoundingMode.HALF_UP).doubleValue(); } }