package com.qianwen.smartman.modules.mdc.service.impl;
|
|
import java.sql.Timestamp;
|
import java.time.LocalDate;
|
import java.time.chrono.ChronoLocalDate;
|
import java.time.temporal.ChronoUnit;
|
import java.util.ArrayList;
|
import java.util.Comparator;
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Optional;
|
import java.util.Set;
|
import java.util.stream.Collectors;
|
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.google.common.collect.Lists;
|
import com.qianwen.core.excel.util.ExcelUtil;
|
import com.qianwen.core.log.exception.ServiceException;
|
import com.qianwen.core.mp.support.Condition;
|
import com.qianwen.core.mp.support.Query;
|
import com.qianwen.core.oss.model.BladeFile;
|
import com.qianwen.core.tool.support.Kv;
|
import com.qianwen.core.tool.utils.DateUtil;
|
import com.qianwen.core.tool.utils.Func;
|
import com.qianwen.core.tool.utils.StringUtil;
|
import com.qianwen.smartman.common.cache.cps.TimeSliceCache;
|
import com.qianwen.smartman.common.cache.cps.WorkstationCache;
|
import com.qianwen.smartman.common.constant.CalendarConstant;
|
import com.qianwen.smartman.common.constant.CommonConstant;
|
import com.qianwen.smartman.common.constant.DateConstant;
|
import com.qianwen.smartman.common.constant.ExcelConstant;
|
import com.qianwen.smartman.common.constant.MdcConstant;
|
import com.qianwen.smartman.common.enums.DefaultWcsEnum;
|
import com.qianwen.smartman.common.utils.LocalDateTimeUtils;
|
import com.qianwen.smartman.common.utils.MessageUtils;
|
import com.qianwen.smartman.modules.cps.entity.CommonGroup;
|
import com.qianwen.smartman.modules.cps.entity.GlobalWcs;
|
import com.qianwen.smartman.modules.cps.entity.ShiftDetail;
|
import com.qianwen.smartman.modules.cps.entity.Workstation;
|
import com.qianwen.smartman.modules.cps.enums.WorkstationTypeEnum;
|
import com.qianwen.smartman.modules.cps.mapper.CalendarMapper;
|
import com.qianwen.smartman.modules.cps.service.ICalendarService;
|
import com.qianwen.smartman.modules.cps.service.ICommonGroupService;
|
import com.qianwen.smartman.modules.cps.service.IWorkstationService;
|
import com.qianwen.smartman.modules.mdc.dto.CountStatusDTO;
|
import com.qianwen.smartman.modules.mdc.dto.GroupWorkDTO;
|
import com.qianwen.smartman.modules.mdc.dto.IntervalDateDto;
|
import com.qianwen.smartman.modules.mdc.dto.MixShiftDetailsDTO;
|
import com.qianwen.smartman.modules.mdc.dto.NameIdDTO;
|
import com.qianwen.smartman.modules.mdc.dto.ShiftIndexNameDTO;
|
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.excel.TimeUsedDateExcel;
|
import com.qianwen.smartman.modules.mdc.excel.TimeUsedExport;
|
import com.qianwen.smartman.modules.mdc.mapper.SuperAggregateStateMapper;
|
import com.qianwen.smartman.modules.mdc.service.ISuperAggregateStateService;
|
import com.qianwen.smartman.modules.mdc.service.ITimeUsedAnalysisService;
|
import com.qianwen.smartman.modules.mdc.utils.FilterOffUtils;
|
import com.qianwen.smartman.modules.mdc.vo.QueryShiftIndexNameVO;
|
import com.qianwen.smartman.modules.mdc.vo.TimeUsedAnalysisWorkstationVO;
|
import com.qianwen.smartman.modules.mdc.vo.TimeUsedStatisticsByWorkstationVO;
|
import com.qianwen.smartman.modules.mdc.vo.WorkstationBandShiftVO;
|
import com.qianwen.smartman.modules.mdc.vo.excel.TimeUsedExcelVO;
|
import com.qianwen.smartman.modules.resource.builder.oss.OssBuilder;
|
|
import cn.hutool.core.util.NumberUtil;
|
|
@Service
|
public class TimeUsedAnalysisServiceImpl implements ITimeUsedAnalysisService {
|
private static final Logger log = LoggerFactory.getLogger(TimeUsedAnalysisServiceImpl.class);
|
private final SuperAggregateStateMapper baseMapper;
|
private final IWorkstationService workstationService;
|
private final ICommonGroupService commonGroupService;
|
private final CalendarMapper calendarMapper;
|
private final ISuperAggregateStateService stateService;
|
private final OssBuilder ossBuilder;
|
private final ICalendarService calendarService;
|
|
public TimeUsedAnalysisServiceImpl(final SuperAggregateStateMapper baseMapper, final IWorkstationService workstationService, final ICommonGroupService commonGroupService, final CalendarMapper calendarMapper, final ISuperAggregateStateService stateService, final OssBuilder ossBuilder, final ICalendarService calendarService) {
|
this.baseMapper = baseMapper;
|
this.workstationService = workstationService;
|
this.commonGroupService = commonGroupService;
|
this.calendarMapper = calendarMapper;
|
this.stateService = stateService;
|
this.ossBuilder = ossBuilder;
|
this.calendarService = calendarService;
|
}
|
|
@Override // org.springblade.modules.mdc.service.ITimeUsedAnalysisService
|
public TimeUsedAnalysisWorkstationVO timeUsedStatisticsByWorkstation(TimeUsedStatisticsByWorkstationVO stationVO, Query query) {
|
Integer type = stationVO.getType();
|
if (MdcConstant.WORK_STATION.equals(type)) {
|
return buildWorkStation(query, stationVO);
|
}
|
return buildGroup(query, stationVO);
|
}
|
|
private TimeUsedAnalysisWorkstationVO buildWorkStation(Query query, TimeUsedStatisticsByWorkstationVO stationVO) {
|
TimeUsedAnalysisWorkstationVO res;
|
StatisticalMethodEnum methodEnum = stationVO.getEnums();
|
List<String> ids = stationVO.getIds();
|
LocalDate queryTime = stationVO.getQueryTime();
|
IPage<Workstation> page = this.workstationService.page(Condition.getPage(query),Wrappers.<Workstation>lambdaQuery()
|
.in(Func.isNotEmpty(ids), Workstation::getId, ids)
|
.eq(Workstation::getType, WorkstationTypeEnum.MACHINE.getCode())
|
.eq(Workstation::getStatus, CommonConstant.ENABLE)
|
.isNotNull(Workstation::getCalendarCode)
|
.orderByAsc(Workstation::getCode));
|
/*
|
IPage<Workstation> page = this.workstationService.page(Condition.getPage(query), (Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().in(Func.isNotEmpty(ids), (v0) -> {
|
return v0.getId();
|
}, ids).eq((v0) -> {
|
return v0.getType();
|
}, WorkstationTypeEnum.MACHINE.getCode())).eq((v0) -> {
|
return v0.getStatus();
|
}, CommonConstant.ENABLE)).isNotNull((v0) -> {
|
return v0.getCalendarCode();
|
})).orderByAsc((v0) -> {
|
return v0.getCode();
|
}));*/
|
long total = page.getTotal();
|
List<Workstation> workstations = page.getRecords();
|
if (Func.isEmpty(workstations)) {
|
return new TimeUsedAnalysisWorkstationVO();
|
}
|
switch (methodEnum) {
|
case DAY:
|
res = buildTimeUsedAnalysisDay(workstations, queryTime);
|
res.setTotal(Long.valueOf(total));
|
break;
|
case WEEK:
|
res = buildTimeUsedAnalysisWeek(workstations, stationVO.getYear(), stationVO.getWeek());
|
res.setTotal(Long.valueOf(total));
|
break;
|
case MONTH:
|
res = buildTimeUsedAnalysisMonth(workstations, stationVO.getYear(), stationVO.getMonth());
|
res.setTotal(Long.valueOf(total));
|
break;
|
default:
|
res = buildTimeUsedAnalysisShift(ids, queryTime, stationVO.getShiftIndex(), query);
|
break;
|
}
|
return res;
|
}
|
|
private TimeUsedAnalysisWorkstationVO buildTimeUsedAnalysisDay(List<Workstation> workstations, LocalDate queryTime) {
|
List<Long> ids = workstations.stream().map((v0) -> {
|
return v0.getId();
|
}).collect(Collectors.toList());
|
List<NameIdDTO> workStationDetails = Lists.newArrayList();
|
List<List<CountStatusDTO>> countStatus = Lists.newArrayList();
|
Set<Integer> wcsList = listGlobalWcs().stream().map(c -> {
|
return Integer.valueOf(Integer.parseInt(c.getCode()));
|
}).collect(Collectors.toSet());
|
|
List<SuperAggregateState> stateList = this.baseMapper.dateState(DateUtil.format(queryTime, "yyyyMMdd"), ids);
|
Map<Long, Map<Integer, Long>> map = buildStateMap(wcsList, stateList);
|
for (Workstation workstation : workstations) {
|
workStationDetails.add(NameIdDTO.builder().id(workstation.getId()).name(workstation.getName() + "\n" + workstation.getCode()).build());
|
Long id = workstation.getId();
|
Map<Integer, Long> countResults = mergeMap(map.get(id), wcsList);
|
log.info("day合并后:{}", countResults);
|
countStatus.add(buildCountStatus(countResults));
|
}
|
return TimeUsedAnalysisWorkstationVO.builder().countStatus(countStatus).workStationDetails(workStationDetails).build();
|
}
|
|
private TimeUsedAnalysisWorkstationVO buildTimeUsedAnalysisWeek(List<Workstation> workstations, Integer year, Integer week) {
|
List<Long> ids = workstations.stream().map((v0) -> {
|
return v0.getId();
|
}).collect(Collectors.toList());
|
List<NameIdDTO> workStationDetails = Lists.newArrayList();
|
List<List<CountStatusDTO>> countStatus = Lists.newArrayList();
|
Set<Integer> wcsList = listGlobalWcs().stream().map(c -> {
|
return Integer.valueOf(Integer.parseInt(c.getCode()));
|
}).collect(Collectors.toSet());
|
List<SuperAggregateState> stateList = this.baseMapper.weekState(ids, year, week);
|
Map<Long, Map<Integer, Long>> map = buildStateMap(wcsList, stateList);
|
for (Workstation workstation : workstations) {
|
workStationDetails.add(NameIdDTO.builder().id(workstation.getId()).name(workstation.getName() + "\n" + workstation.getCode()).build());
|
Long id = workstation.getId();
|
Map<Integer, Long> countResults = mergeMap(map.get(id), wcsList);
|
log.info("week合并后:{}", countResults);
|
countStatus.add(buildCountStatus(countResults));
|
}
|
return TimeUsedAnalysisWorkstationVO.builder().countStatus(countStatus).workStationDetails(workStationDetails).build();
|
}
|
|
private TimeUsedAnalysisWorkstationVO buildTimeUsedAnalysisMonth(List<Workstation> workstations, Integer year, Integer month) {
|
List<Long> ids = workstations.stream().map((v0) -> {
|
return v0.getId();
|
}).collect(Collectors.toList());
|
List<NameIdDTO> workStationDetails = Lists.newArrayList();
|
List<List<CountStatusDTO>> countStatus = Lists.newArrayList();
|
Set<Integer> wcsList = listGlobalWcs().stream().map(c -> {
|
return Integer.valueOf(Integer.parseInt(c.getCode()));
|
}).collect(Collectors.toSet());
|
List<SuperAggregateState> stateList = this.baseMapper.yearState(ids, year, month);
|
Map<Long, Map<Integer, Long>> map = buildStateMap(wcsList, stateList);
|
for (Workstation workstation : workstations) {
|
workStationDetails.add(NameIdDTO.builder().id(workstation.getId()).name(workstation.getName() + "\n" + workstation.getCode()).build());
|
Long id = workstation.getId();
|
Map<Integer, Long> countResults = mergeMap(map.get(id), wcsList);
|
log.info("month合并后:{}", countResults);
|
countStatus.add(buildCountStatus(countResults));
|
}
|
return TimeUsedAnalysisWorkstationVO.builder().countStatus(countStatus).workStationDetails(workStationDetails).build();
|
}
|
|
private TimeUsedAnalysisWorkstationVO buildTimeUsedAnalysisShift(List<String> ids, LocalDate queryTime, Integer shiftIndex, Query query) {
|
List<NameIdDTO> workStationDetails = Lists.newArrayList();
|
List<List<CountStatusDTO>> countStatus = Lists.newArrayList();
|
IPage<Workstation> workstationPages = this.workstationService.listWorkStationOnShiftIndex(ids, queryTime, query, shiftIndex, WorkstationTypeEnum.MACHINE.getCode());
|
Set<Integer> wcsList = listGlobalWcs().stream().map(c -> {
|
return Integer.valueOf(Integer.parseInt(c.getCode()));
|
}).collect(Collectors.toSet());
|
LinkedHashMap<String, List<Workstation>> calendarMap = workstationPages.getRecords().stream().collect(Collectors.groupingBy((v0) -> {
|
return v0.getCalendarCode();
|
}, LinkedHashMap::new, Collectors.toList()));
|
calendarMap.forEach((calendarCode, workStationsList) -> {
|
List<Long> workStationIds = workStationsList.stream().map((v0) -> {
|
return v0.getId();
|
}).collect(Collectors.toList());
|
List<SuperAggregateState> stateList = this.baseMapper.shiftState(workStationIds, DateUtil.format(queryTime, "yyyyMMdd"), shiftIndex, calendarCode);
|
Map<Long, Map<Integer, Long>> map = buildStateMap(wcsList, stateList);
|
Iterator<Workstation> it = workStationsList.iterator();
|
while (it.hasNext()) {
|
Workstation workstation = it.next();
|
workStationDetails.add(NameIdDTO.builder().id(workstation.getId()).name(workstation.getName() + "\n" + workstation.getCode()).build());
|
Long id = workstation.getId();
|
Map<Integer, Long> countResults = mergeMap(map.get(id), wcsList);
|
log.info("month合并后:{}", countResults);
|
countStatus.add(buildCountStatus(countResults));
|
}
|
});
|
return TimeUsedAnalysisWorkstationVO.builder().countStatus(countStatus).workStationDetails(workStationDetails).total(Long.valueOf(workstationPages.getTotal())).build();
|
}
|
|
private TimeUsedAnalysisWorkstationVO buildGroup(Query query, TimeUsedStatisticsByWorkstationVO stationVO) {
|
TimeUsedAnalysisWorkstationVO res;
|
StatisticalMethodEnum methodEnum = stationVO.getEnums();
|
List<String> ids = stationVO.getIds();
|
LocalDate queryTime = stationVO.getQueryTime();
|
String groupType = stationVO.getGroupType();
|
String groupCategory = stationVO.getGroupCategory();
|
long total = this.commonGroupService.count(Wrappers.<CommonGroup>lambdaQuery()
|
.eq(CommonGroup::getGroupType, groupType)
|
.eq(CommonGroup::getGroupCategory, groupCategory)
|
.in(Func.isNotEmpty(ids), CommonGroup::getId, ids));
|
/*
|
long total = this.commonGroupService.count(((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getGroupType();
|
}, groupType)).eq((v0) -> {
|
return v0.getGroupCategory();
|
}, groupCategory)).in(Func.isNotEmpty(ids), (v0) -> {
|
return v0.getId();
|
}, ids));*/
|
IPage<CommonGroup> page = this.commonGroupService.page(Condition.getPage(query), Wrappers.<CommonGroup>lambdaQuery()
|
.eq(CommonGroup::getGroupType, groupType)
|
.eq(CommonGroup::getGroupCategory, groupCategory)
|
.in(Func.isNotEmpty(ids), CommonGroup::getId, ids)
|
.orderByAsc(CommonGroup::getCode));
|
/*
|
IPage<CommonGroup> page = this.commonGroupService.page(Condition.getPage(query), (Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getGroupType();
|
}, groupType)).eq((v0) -> {
|
return v0.getGroupCategory();
|
}, groupCategory)).in(Func.isNotEmpty(ids), (v0) -> {
|
return v0.getId();
|
}, ids).orderByAsc((v0) -> {
|
return v0.getCode();
|
}));*/
|
List<CommonGroup> groups = page.getRecords();
|
if (Func.isEmpty(groups)) {
|
return new TimeUsedAnalysisWorkstationVO();
|
}
|
Map<Long, Set<Long>> groupWorkstationMap = this.commonGroupService.groupWorkstationMap(groups.stream().map(c -> {
|
return String.valueOf(c.getId());
|
}).collect(Collectors.toList()));
|
|
switch (methodEnum) {
|
case DAY:
|
res = buildTimeUsedAnalysisGroupDay(groups, groupWorkstationMap, queryTime);
|
break;
|
case WEEK:
|
res = buildTimeUsedAnalysisGroupWeek(groups, groupWorkstationMap, stationVO.getYear(), stationVO.getWeek());
|
break;
|
case MONTH:
|
res = buildTimeUsedAnalysisGroupMonth(groups, groupWorkstationMap, stationVO.getYear(), stationVO.getMonth());
|
break;
|
default:
|
res = this.buildTimeUsedAnalysisShift(ids, queryTime, stationVO.getShiftIndex(), query);
|
break;
|
|
}
|
res.setTotal(Long.valueOf(total));
|
return res;
|
}
|
|
private TimeUsedAnalysisWorkstationVO buildTimeUsedAnalysisGroupDay(List<CommonGroup> groups, Map<Long, Set<Long>> groupWorkstationMap, LocalDate queryTime) {
|
List<NameIdDTO> workStationDetails = Lists.newArrayList();
|
List<List<CountStatusDTO>> countStatus = Lists.newArrayList();
|
for (CommonGroup group : groups) {
|
workStationDetails.add(NameIdDTO.builder().id(group.getId()).name(group.getName()).build());
|
Long id = group.getId();
|
Set<Long> workstationIds = groupWorkstationMap.get(id);
|
if (Func.isEmpty(workstationIds)) {
|
countStatus.add(buildCountStatus(new HashMap<>(10)));
|
} else {
|
Set<Integer> wcsList = listGlobalWcs().stream().map(c -> {
|
return Integer.valueOf(Integer.parseInt(c.getCode()));
|
}).collect(Collectors.toSet());
|
List<SuperAggregateState> stateEndTimeNotNullList = this.baseMapper.dateState(DateUtil.format(queryTime, "yyyyMMdd"), Lists.newArrayList(workstationIds));
|
Map<Integer, Long> map = buildStateGroupMap(wcsList, FilterOffUtils.filterOffDay(stateEndTimeNotNullList, OpenTypeEnums.TIME_USED_ANALYSIS));
|
Map<Integer, Long> countResults = mergeMap(map, wcsList);
|
log.info("groupDay:{}", countResults);
|
countStatus.add(buildCountStatus(countResults));
|
}
|
}
|
return TimeUsedAnalysisWorkstationVO.builder().countStatus(countStatus).workStationDetails(workStationDetails).build();
|
}
|
|
private TimeUsedAnalysisWorkstationVO buildTimeUsedAnalysisGroupWeek(List<CommonGroup> groups, Map<Long, Set<Long>> groupWorkstationMap, Integer year, Integer week) {
|
List<NameIdDTO> workStationDetails = Lists.newArrayList();
|
List<List<CountStatusDTO>> countStatus = Lists.newArrayList();
|
for (CommonGroup group : groups) {
|
workStationDetails.add(NameIdDTO.builder().id(group.getId()).name(group.getName()).build());
|
Long id = group.getId();
|
Set<Long> workstationIds = groupWorkstationMap.get(id);
|
if (Func.isEmpty(workstationIds)) {
|
countStatus.add(buildCountStatus(new HashMap<>(10)));
|
} else {
|
Set<Integer> wcsList = listGlobalWcs().stream().map(c -> {
|
return Integer.valueOf(Integer.parseInt(c.getCode()));
|
}).collect(Collectors.toSet());
|
List<SuperAggregateState> stateEndTimeNotNullList = this.baseMapper.weekState(Lists.newArrayList(workstationIds), year, week);
|
Map<Integer, Long> map = buildStateGroupMap(wcsList, FilterOffUtils.filterOffDay(stateEndTimeNotNullList, OpenTypeEnums.TIME_USED_ANALYSIS));
|
Map<Integer, Long> countResults = mergeMap(map, wcsList);
|
log.info("groupWeek:{}", countResults);
|
countStatus.add(buildCountStatus(countResults));
|
}
|
}
|
return TimeUsedAnalysisWorkstationVO.builder().countStatus(countStatus).workStationDetails(workStationDetails).build();
|
}
|
|
private TimeUsedAnalysisWorkstationVO buildTimeUsedAnalysisGroupMonth(List<CommonGroup> groups, Map<Long, Set<Long>> groupWorkstationMap, Integer year, Integer month) {
|
List<NameIdDTO> workStationDetails = Lists.newArrayList();
|
List<List<CountStatusDTO>> countStatus = Lists.newArrayList();
|
for (CommonGroup group : groups) {
|
workStationDetails.add(NameIdDTO.builder().id(group.getId()).name(group.getName()).build());
|
Long id = group.getId();
|
Set<Long> workstationIds = groupWorkstationMap.get(id);
|
if (Func.isEmpty(workstationIds)) {
|
countStatus.add(buildCountStatus(new HashMap<>(10)));
|
} else {
|
Set<Integer> wcsList = listGlobalWcs().stream().map(c -> {
|
return Integer.valueOf(Integer.parseInt(c.getCode()));
|
}).collect(Collectors.toSet());
|
List<SuperAggregateState> stateEndTimeNotNullList = this.baseMapper.yearState(Lists.newArrayList(workstationIds), year, month);
|
Map<Integer, Long> map1 = buildStateGroupMap(wcsList, FilterOffUtils.filterOffDay(stateEndTimeNotNullList, OpenTypeEnums.TIME_USED_ANALYSIS));
|
Map<Integer, Long> countResults = mergeMap(map1, wcsList);
|
log.info("groupMonth:{}", countResults);
|
countStatus.add(buildCountStatus(countResults));
|
}
|
}
|
return TimeUsedAnalysisWorkstationVO.builder().countStatus(countStatus).workStationDetails(workStationDetails).build();
|
}
|
|
private TimeUsedAnalysisWorkstationVO buildTimeUsedAnalysisGroupShift(List<CommonGroup> groups, Map<Long, Set<Long>> groupWorkstationMap, LocalDate queryTime, Integer shiftIndex) {
|
List<NameIdDTO> workStationDetails = Lists.newArrayList();
|
List<List<CountStatusDTO>> countStatus = Lists.newArrayList();
|
List<Long> ids = groupWorkstationMap.values().stream().flatMap((v0) -> {
|
return v0.stream();
|
}).distinct().collect(Collectors.toList());
|
Map<Long, String> calendarMap = findWorkstationCalendarCode(ids);
|
Map<Long, CommonGroup> groupMap = groups.stream().collect(Collectors.toMap((v0) -> {
|
return v0.getId();
|
}, v -> {
|
return v;
|
}));
|
groupWorkstationMap.forEach((groupId, workstationIds) -> {
|
CommonGroup group = (CommonGroup) groupMap.getOrDefault(groupId, new CommonGroup());
|
workStationDetails.add(NameIdDTO.builder().id(group.getId()).name(group.getName()).build());
|
if (Func.isEmpty(workstationIds)) {
|
countStatus.add(buildCountStatus(new HashMap<>(10)));
|
return;
|
}
|
Set<Integer> wcsList = listGlobalWcs().stream().map(c -> {
|
return Integer.valueOf(Integer.parseInt(c.getCode()));
|
}).collect(Collectors.toSet());
|
List<SuperAggregateState> superAggregateStates = workstationIds.stream().map(wId -> {
|
return this.baseMapper.countStatsShift(String.valueOf(wId), DateUtil.format(queryTime, "yyyyMMdd"), shiftIndex, String.valueOf(calendarMap.get(wId)));
|
}).filter((v0) -> {
|
return Func.isNotEmpty(v0);
|
}).flatMap((v0) -> {
|
return v0.stream();
|
}).filter((v0) -> {
|
return Func.isNotEmpty(v0);
|
}).collect(Collectors.toList());
|
Map<Integer, Long> map = buildStateGroupMap(wcsList, FilterOffUtils.filterOffDay(superAggregateStates, OpenTypeEnums.TIME_USED_ANALYSIS));
|
Map<Integer, Long> resultMap = mergeMap(map, wcsList);
|
log.info("groupShift:{}", resultMap);
|
countStatus.add(buildCountStatus(resultMap));
|
});
|
return TimeUsedAnalysisWorkstationVO.builder().countStatus(countStatus).workStationDetails(workStationDetails).build();
|
}
|
|
private Map<Long, String> findWorkstationCalendarCode(List<Long> workstationIds) {
|
if (Func.isEmpty(workstationIds)) {
|
return new HashMap<>(10);
|
}
|
return this.workstationService.list(Wrappers.<Workstation>lambdaQuery()
|
.in(Workstation::getId, workstationIds))
|
.stream()
|
.collect(Collectors.toMap(Workstation::getId, Workstation::getCalendarCode));
|
/*
|
return (Map) this.workstationService.list((Wrapper) Wrappers.lambdaQuery().in((v0) -> {
|
return v0.getId();
|
}, workstationIds)).stream().collect(Collectors.toMap((v0) -> {
|
return v0.getId();
|
}, (v0) -> {
|
return v0.getCalendarCode();
|
}));*/
|
}
|
|
private List<CountStatusDTO> buildCountStatus(Map<Integer, Long> map) {
|
List<GlobalWcs> list = listGlobalWcs();
|
return list.stream().map(value -> {
|
String code = value.getCode();
|
Long count = (Long) map.getOrDefault(Integer.valueOf(Integer.parseInt(code)), 0L);
|
return CountStatusDTO.builder().count(count).statusName(value.getName()).code(code).build();
|
}).collect(Collectors.toList());
|
}
|
|
private Map<Long, Map<Integer, Long>> buildStateMap(Set<Integer> wcsList, List<SuperAggregateState> stateList) {
|
List<SuperAggregateState> stateList2 = FilterOffUtils.filterOffDay(stateList, OpenTypeEnums.TIME_USED_ANALYSIS);
|
Timestamp ts = new Timestamp(System.currentTimeMillis());
|
return stateList2.stream().peek(c -> {
|
Timestamp endTime = c.getEndTime();
|
if (endTime == null) {
|
c.setEndTime(ts);
|
}
|
c.setDurationCollect(Long.valueOf(LocalDateTimeUtils.betweenTwoTime(c.getStartTime().toLocalDateTime(), c.getEndTime().toLocalDateTime(), ChronoUnit.MILLIS)));
|
}).filter(c2 -> {
|
return (!wcsList.contains(c2.getValueCollect()) || c2.getValueCollect() == null || c2.getDurationCollect() == null) ? false : true;
|
}).collect(Collectors.groupingBy((v0) -> {
|
return v0.getWorkstationId();
|
}, Collectors.groupingBy((v0) -> {
|
return v0.getValueCollect();
|
}, Collectors.summingLong((v0) -> {
|
return v0.getDurationCollect();
|
}))));
|
}
|
|
private Map<Integer, Long> buildStateGroupMap(Set<Integer> wcsList, List<SuperAggregateState> stateList) {
|
Timestamp ts = new Timestamp(System.currentTimeMillis());
|
return stateList.stream().peek(c -> {
|
Timestamp endTime = c.getEndTime();
|
if (endTime == null) {
|
c.setEndTime(ts);
|
}
|
c.setDurationCollect(Long.valueOf(LocalDateTimeUtils.betweenTwoTime(c.getStartTime().toLocalDateTime(), c.getEndTime().toLocalDateTime(), ChronoUnit.MILLIS)));
|
}).filter(c2 -> {
|
return (!wcsList.contains(c2.getValueCollect()) || c2.getValueCollect() == null || c2.getDurationCollect() == null) ? false : true;
|
}).collect(Collectors.groupingBy((v0) -> {
|
return v0.getValueCollect();
|
}, Collectors.summingLong((v0) -> {
|
return v0.getDurationCollect();
|
})));
|
}
|
|
private Map<Integer, Long> mergeMap(Map<Integer, Long> map, Set<Integer> wcsList) {
|
Map<Integer, Long> result = new HashMap<>(wcsList.size());
|
wcsList.forEach(wcs -> {
|
Long w1 = 0L;
|
if (Func.isNotEmpty(map)) {
|
w1 = (Long) map.getOrDefault(wcs, 0L);
|
}
|
result.put(wcs, w1);
|
});
|
return result;
|
}
|
|
private List<GlobalWcs> listGlobalWcs() {
|
return WorkstationCache.getDefaultWcs();
|
}
|
|
@Override // org.springblade.modules.mdc.service.ITimeUsedAnalysisService
|
public WorkstationBandShiftVO queryWorkStationShiftIndexName(QueryShiftIndexNameVO vo) {
|
Integer type = vo.getType();
|
if (MdcConstant.WORK_STATION.equals(type)) {
|
return getWorkShiftIndexName(vo);
|
}
|
return getGroupShiftIndexName(vo);
|
}
|
|
@Override // org.springblade.modules.mdc.service.ITimeUsedAnalysisService
|
public Map<Long, List<ShiftIndexNameDTO>> queryWorkStationShiftIndex(List<Long> workstationIds, LocalDate localDate) {
|
Map<Long, List<ShiftIndexNameDTO>> idToValue = Kv.newMap();
|
workstationIds.forEach(workstationId -> {
|
Workstation workstation = this.workstationService.getOne(Wrappers.<Workstation>lambdaQuery().eq(Workstation::getId, workstationId)
|
.eq(Workstation::getType, WorkstationTypeEnum.MACHINE.getCode()).isNotNull(Workstation::getCalendarCode));
|
/*
|
Workstation workstation = (Workstation) this.workstationService.getOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getId();
|
}, workstationId)).eq((v0) -> {
|
return v0.getType();
|
}, WorkstationTypeEnum.MACHINE.getCode())).isNotNull((v0) -> {
|
return v0.getCalendarCode();
|
}));*/
|
if (Func.isEmpty(workstation)) {
|
idToValue.put(workstationId, new ArrayList<>());
|
}
|
if (workstation != null) {
|
List<ShiftIndexNameDTO> dtos = this.calendarService.queryShiftIndexName(workstation.getCalendarCode(), Integer.valueOf(localDate.getYear()), DateUtil.formatDate(localDate));
|
idToValue.put(workstationId, dtos);
|
}
|
});
|
return idToValue;
|
}
|
|
@Override // org.springblade.modules.mdc.service.ITimeUsedAnalysisService
|
public Map<Long, List<ShiftIndexNameDTO>> queryBatchWorkStationShiftIndex(List<Long> workstationIdList, LocalDate localDate) {
|
Map<Long, List<ShiftIndexNameDTO>> idToValue = Kv.newMap();
|
List<Workstation> workstations = this.workstationService.listByIds(workstationIdList);
|
Map<String, List<Workstation>> collect = workstations.stream().collect(Collectors.groupingBy((v0) -> {
|
return v0.getCalendarCode();
|
}));
|
List<String> calendarCodeList = workstations.stream().map((v0) -> {
|
return v0.getCalendarCode();
|
}).distinct().collect(Collectors.toList());
|
calendarCodeList.forEach(calendarCode -> {
|
List<ShiftIndexNameDTO> shiftIndexNameDTOList = this.calendarService.queryShiftIndexName(calendarCode, Integer.valueOf(localDate.getYear()), DateUtil.formatDate(localDate));
|
|
List<Long> workstationIds = collect.get(calendarCode).stream().map(Workstation::getId).collect(Collectors.toList());
|
/*
|
List<Long> workstationIds = collect.get(calendarCode).stream().map((v0) -> {
|
return v0.getId();
|
}).collect(Collectors.toList());
|
*/
|
workstationIds.forEach(workstationId -> {
|
List list = idToValue.put(workstationId, shiftIndexNameDTOList);
|
});
|
});
|
return idToValue;
|
}
|
|
@Override // org.springblade.modules.mdc.service.ITimeUsedAnalysisService
|
public BladeFile exportTimeUsed(TimeUsedExcelVO vo) {
|
List<Workstation> workstations;
|
StatisticalMethodEnum enums = vo.getEnums();
|
List<Long> ids = vo.getIds();
|
if (Func.isEmpty(ids)) {
|
workstations = this.workstationService.list(Wrappers.<Workstation>lambdaQuery()
|
.eq(Workstation::getType, WorkstationTypeEnum.MACHINE.getCode())
|
.eq(Workstation::getStatus, CommonConstant.ENABLE));
|
/*
|
workstations = this.workstationService.list((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getType();
|
}, WorkstationTypeEnum.MACHINE.getCode())).eq((v0) -> {
|
return v0.getStatus();
|
}, CommonConstant.ENABLE));*/
|
ids = workstations.stream().map((v0) -> {
|
return v0.getId();
|
}).collect(Collectors.toList());
|
} else {
|
workstations = this.workstationService.workStationList(ids);
|
}
|
if (Func.isEmpty(ids)) {
|
return exportEmpty(vo.getEnums());
|
}
|
Map<Long, Workstation> workstationMap = workstations.stream().collect(Collectors.toMap((v0) -> {
|
return v0.getId();
|
}, v -> {
|
return v;
|
}));
|
LocalDate startTime = vo.getStartTime();
|
LocalDate endTime = vo.getEndTime();
|
if (Func.isNull(startTime) || Func.isNull(endTime)) {
|
throw new ServiceException(MessageUtils.message("mdc.process.time.not.null", new Object[0]));
|
}
|
Map<Long, GroupWorkDTO> groupWorkMap = this.workstationService.queryGroupWorkStation(ids);
|
switch (enums) {
|
case DAY:
|
return buildExcelByDay(ids, workstationMap, groupWorkMap, startTime, endTime);
|
case WEEK:
|
return buildExcelByWeek(ids, workstationMap, groupWorkMap, vo);
|
case MONTH:
|
return buildExcelByMonth(ids, workstationMap, groupWorkMap, vo);
|
case SHIFT:
|
return getExcelByShift(ids, startTime, endTime);
|
default:
|
return null;
|
}
|
}
|
|
private BladeFile exportEmpty(StatisticalMethodEnum enums) {
|
switch (enums) {
|
case DAY:
|
case WEEK:
|
case MONTH:
|
List<TimeUsedDateExcel> timeUsedDataExports = new ArrayList<>();
|
return buildExcel(timeUsedDataExports);
|
case SHIFT:
|
List<TimeUsedExport> timeUsedExports = Lists.newArrayList();
|
String fileName = String.format("%s-%s.xlsx", ExcelConstant.TIME_USED, DateUtil.time());
|
MultipartFile multipartFile = ExcelUtil.exportToMultipartFile(fileName, ExcelConstant.TIME_USED, timeUsedExports, TimeUsedExport.class);
|
return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile);
|
default:
|
return null;
|
}
|
}
|
|
private BladeFile buildExcelByMonth(List<Long> ids, Map<Long, Workstation> workstationMap, Map<Long, GroupWorkDTO> groupWorkMap, TimeUsedExcelVO vo) {
|
List<TimeUsedDateExcel> timeUsedExports = getExcelByMonth(ids, workstationMap, groupWorkMap, vo);
|
return buildExcel(timeUsedExports);
|
}
|
|
private List<TimeUsedDateExcel> getExcelByMonth(List<Long> ids, Map<Long, Workstation> workstationMap, Map<Long, GroupWorkDTO> groupWorkMap, TimeUsedExcelVO vo) {
|
LocalDate startTime = vo.getStartTime();
|
LocalDate endTime = vo.getEndTime();
|
List<SuperAggregateState> data = Lists.newArrayList();
|
List<IntervalDateDto> intervalDate = LocalDateTimeUtils.getIntervalDate(startTime, endTime);
|
intervalDate.forEach(dto -> {
|
Integer year = dto.getYear();
|
List<Integer> monthList = dto.getMonthList();
|
data.addAll(this.stateService.getDataByMonth(ids, year, monthList));
|
});
|
List<SuperAggregateState> filterData = FilterOffUtils.filterOffDay(data, OpenTypeEnums.TIME_USED_ANALYSIS);
|
if (Func.isEmpty(data)) {
|
return Lists.newArrayList();
|
}
|
List<TimeUsedDateExcel> timeUsedExports = Lists.newArrayList();
|
Map<String, Map<Long, List<SuperAggregateState>>> stateMap = filterData.stream().sorted(Comparator.comparing(SuperAggregateState::getFactoryYear).reversed().thenComparing(SuperAggregateState::getFactoryMonth).reversed()).collect(Collectors.groupingBy(c -> {
|
return c.getFactoryYear() + "-" + c.getFactoryMonth();
|
}, Collectors.groupingBy((v0) -> {
|
return v0.getWorkstationId();
|
})));
|
stateMap.forEach((date, stats) -> {
|
stats.forEach((workstationId, statsList) -> {
|
Workstation workstation = (Workstation) workstationMap.getOrDefault(workstationId, new Workstation());
|
GroupWorkDTO groupWork = (GroupWorkDTO) groupWorkMap.getOrDefault(workstationId, new GroupWorkDTO());
|
Map<Integer, String> wcsMap = getWcsMap(listGlobalWcs().stream().map(c2 -> {
|
return Integer.valueOf(Integer.parseInt(c2.getCode()));
|
}).collect(Collectors.toSet()), statsList);
|
timeUsedExports.add(TimeUsedDateExcel.builder().date(date).groupName(groupWork.getName()).workstationCode(workstation.getCode()).workstationName(workstation.getName()).running(wcsMap.get(DefaultWcsEnum.RUNNING.getCode())).standby(wcsMap.get(DefaultWcsEnum.STANDBY.getCode())).debugging(wcsMap.get(DefaultWcsEnum.DEBUGGING.getCode())).alarm(wcsMap.get(DefaultWcsEnum.ALARM.getCode())).offline(wcsMap.get(DefaultWcsEnum.OFFLINE.getCode())).build());
|
});
|
});
|
timeUsedExports.sort(Comparator.comparing(TimeUsedDateExcel::getDate).reversed());
|
/*
|
timeUsedExports.sort(Comparator.comparing((v0) -> {
|
return v0.getDate();
|
}).reversed());*/
|
return timeUsedExports;
|
}
|
|
private BladeFile buildExcelByWeek(List<Long> ids, Map<Long, Workstation> workstationMap, Map<Long, GroupWorkDTO> groupWorkMap, TimeUsedExcelVO vo) {
|
List<TimeUsedDateExcel> timeUsedExports = getExcelByWeek(ids, workstationMap, groupWorkMap, vo);
|
return buildExcel(timeUsedExports);
|
}
|
|
private List<TimeUsedDateExcel> getExcelByWeek(List<Long> ids, Map<Long, Workstation> workstationMap, Map<Long, GroupWorkDTO> groupWorkMap, TimeUsedExcelVO vo) {
|
LocalDate startTime = vo.getStartTime();
|
LocalDate endTime = vo.getEndTime();
|
List<SuperAggregateState> data = Lists.newArrayList();
|
List<IntervalDateDto> intervalDate = LocalDateTimeUtils.getIntervalDate(startTime, endTime);
|
intervalDate.forEach(dto -> {
|
Integer year = dto.getYear();
|
List<Integer> weekList = dto.getWeekList();
|
data.addAll(this.stateService.getDataByWeek(ids, year, weekList));
|
});
|
List<SuperAggregateState> filterData = FilterOffUtils.filterOffDay(data, OpenTypeEnums.TIME_USED_ANALYSIS);
|
if (Func.isEmpty(filterData)) {
|
return Lists.newArrayList();
|
}
|
Map<String, Map<Long, List<SuperAggregateState>>> stateMap = filterData.stream().sorted(Comparator.comparing(SuperAggregateState::getFactoryYear).reversed().thenComparing((v0) -> {
|
return v0.getFactoryWeek();
|
}).reversed()).collect(Collectors.groupingBy(c -> {
|
return c.getFactoryYear() + "-" + c.getFactoryWeek();
|
}, Collectors.groupingBy((v0) -> {
|
return v0.getWorkstationId();
|
})));
|
List<TimeUsedDateExcel> timeUsedExports = Lists.newArrayList();
|
stateMap.forEach((date, stats) -> {
|
stats.forEach((workstationId, statsList) -> {
|
Workstation workstation = workstationMap.getOrDefault(workstationId, new Workstation());
|
GroupWorkDTO groupWork = groupWorkMap.getOrDefault(workstationId, new GroupWorkDTO());
|
Map<Integer, String> wcsMap = getWcsMap(listGlobalWcs().stream().map(c2 -> {
|
return Integer.valueOf(Integer.parseInt(c2.getCode()));
|
}).collect(Collectors.toSet()), statsList);
|
timeUsedExports.add(TimeUsedDateExcel.builder().date(getWeek(date)).groupName(groupWork.getName()).workstationCode(workstation.getCode()).workstationName(workstation.getName()).running(wcsMap.get(DefaultWcsEnum.RUNNING.getCode())).standby(wcsMap.get(DefaultWcsEnum.STANDBY.getCode())).debugging(wcsMap.get(DefaultWcsEnum.DEBUGGING.getCode())).alarm(wcsMap.get(DefaultWcsEnum.ALARM.getCode())).offline(wcsMap.get(DefaultWcsEnum.OFFLINE.getCode())).build());
|
});
|
});
|
timeUsedExports.sort(Comparator.comparing(TimeUsedDateExcel::getDate).reversed());
|
/*
|
timeUsedExports.sort(Comparator.comparing((v0) -> {
|
return v0.getDate();
|
}).reversed());*/
|
return timeUsedExports;
|
}
|
|
private String getWeek(String date) {
|
String[] split = date.split("-");
|
return StringUtil.format(split[0].concat("-").concat("第{}周"), new Object[]{split[1]});
|
}
|
|
private BladeFile buildExcelByDay(List<Long> ids, Map<Long, Workstation> workstationMap, Map<Long, GroupWorkDTO> groupWorkMap, LocalDate startTime, LocalDate endTime) {
|
List<TimeUsedDateExcel> timeUsedExports = getExcelByDay(ids, workstationMap, startTime, endTime, groupWorkMap);
|
return buildExcel(timeUsedExports);
|
}
|
|
private List<TimeUsedDateExcel> getExcelByDay(List<Long> ids, Map<Long, Workstation> workstationMap, LocalDate startTime, LocalDate endTime, Map<Long, GroupWorkDTO> groupWorkMap) {
|
List<SuperAggregateState> statusData = FilterOffUtils.filterOffDay(this.stateService.getStatusData(ids, StatisticalMethodEnum.DAY, startTime, endTime), OpenTypeEnums.TIME_USED_ANALYSIS);
|
if (Func.isEmpty(statusData)) {
|
return Lists.newArrayList();
|
}
|
Map<Integer, Map<Long, List<SuperAggregateState>>> map = statusData.stream().sorted(Comparator.comparing(SuperAggregateState::getFactoryDate).reversed()).collect(Collectors.groupingBy((v0) -> {
|
return v0.getFactoryDate();
|
}, Collectors.groupingBy((v0) -> {
|
return v0.getWorkstationId();
|
})));
|
List<TimeUsedDateExcel> timeUsedExports = Lists.newArrayList();
|
map.forEach((date, status) -> {
|
status.forEach((workstationId, statsList) -> {
|
Workstation workstation = workstationMap.getOrDefault(workstationId, new Workstation());
|
GroupWorkDTO groupWork = groupWorkMap.getOrDefault(workstationId, new GroupWorkDTO());
|
Map<Integer, String> wcsMap = getWcsMap(listGlobalWcs().stream().map(c -> {
|
return Integer.valueOf(Integer.parseInt(c.getCode()));
|
}).collect(Collectors.toSet()), statsList);
|
timeUsedExports.add(TimeUsedDateExcel.builder().date(getDay(date)).groupName(groupWork.getName()).workstationCode(workstation.getCode()).workstationName(workstation.getName()).running(wcsMap.get(DefaultWcsEnum.RUNNING.getCode())).standby(wcsMap.get(DefaultWcsEnum.STANDBY.getCode())).debugging(wcsMap.get(DefaultWcsEnum.DEBUGGING.getCode())).alarm(wcsMap.get(DefaultWcsEnum.ALARM.getCode())).offline(wcsMap.get(DefaultWcsEnum.OFFLINE.getCode())).build());
|
});
|
});
|
timeUsedExports.sort(Comparator.comparing(TimeUsedDateExcel::getDate).reversed());
|
/*
|
timeUsedExports.sort(Comparator.comparing((v0) -> {
|
return v0.getDate();
|
}).reversed());*/
|
return timeUsedExports;
|
}
|
|
private String getDay(Integer date) {
|
char[] chars = String.valueOf(date).toCharArray();
|
StringBuilder sb = new StringBuilder();
|
for (int i = 0; i < chars.length; i++) {
|
if (i == 3) {
|
sb.append(chars[i]).append("-");
|
} else if (i == 5) {
|
sb.append(chars[i]).append("-");
|
} else {
|
sb.append(chars[i]);
|
}
|
}
|
return sb.toString();
|
}
|
|
private BladeFile buildExcel(List<TimeUsedDateExcel> timeUsedExports) {
|
String fileName = String.format("%s-%s.xlsx", ExcelConstant.TIME_USED, DateUtil.time());
|
MultipartFile multipartFile = ExcelUtil.exportToMultipartFile(fileName, ExcelConstant.SHEET, timeUsedExports, TimeUsedDateExcel.class);
|
return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile);
|
}
|
|
private BladeFile getExcelByShift(List<Long> ids, LocalDate startTime, LocalDate endTime) {
|
List<TimeUsedExport> timeUsedExports = Lists.newArrayList();
|
while (endTime.compareTo((ChronoLocalDate) startTime) >= 0) {
|
String date = DateUtil.format(endTime, DateConstant.PATTERN_DATE);
|
List<Workstation> workstations = this.workstationService.listWorkStationOnShift(ids, endTime, MdcConstant.SHIFT_INDEX, WorkstationTypeEnum.MACHINE.getCode());
|
if (Func.isEmpty(workstations)) {
|
endTime = endTime.minusDays(1L);
|
} else {
|
List<Long> wIds = workstations.stream().map((v0) -> {
|
return v0.getId();
|
}).collect(Collectors.toList());
|
Map<Long, Workstation> workstationMap = workstations.stream().collect(Collectors.toMap((v0) -> {
|
return v0.getId();
|
}, v -> {
|
return v;
|
}));
|
Map<String, List<Workstation>> calendarCodeMap = workstations.stream().collect(Collectors.groupingBy((v0) -> {
|
return v0.getCalendarCode();
|
}));
|
LocalDate finalEndTime = endTime;
|
Map<String, Map<Integer, ShiftDetail>> shiftNameMap = new HashMap<>(calendarCodeMap.size());
|
calendarCodeMap.forEach((calendarCode, wList) -> {
|
List<ShiftDetail> timeShiftDetail = TimeSliceCache.getTimeShiftDetail(calendarCode, finalEndTime);
|
Map<Integer, ShiftDetail> map = timeShiftDetail.stream().collect(Collectors.toMap((v0) -> {
|
return v0.getShiftIndex();
|
}, v2 -> {
|
return v2;
|
}));
|
shiftNameMap.put(calendarCode, map);
|
});
|
Map<Long, GroupWorkDTO> groupWorkMap = this.workstationService.queryGroupWorkStation(wIds);
|
List<SuperAggregateState> states = this.stateService.listsState(wIds, endTime, MdcConstant.SHIFT_INDEX);
|
List<TimeUsedExport> exports = buildExportData(FilterOffUtils.filterOffDay(states, OpenTypeEnums.TIME_USED_ANALYSIS), workstationMap, shiftNameMap, groupWorkMap, date);
|
timeUsedExports.addAll(exports);
|
endTime = endTime.minusDays(1L);
|
}
|
}
|
String fileName = String.format("%s-%s.xlsx", ExcelConstant.TIME_USED, DateUtil.time());
|
MultipartFile multipartFile = ExcelUtil.exportToMultipartFile(fileName, ExcelConstant.TIME_USED, timeUsedExports, TimeUsedExport.class);
|
return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile);
|
}
|
|
private List<TimeUsedExport> buildExportData(List<SuperAggregateState> states, Map<Long, Workstation> workstationMap, Map<String, Map<Integer, ShiftDetail>> shiftNameMap, Map<Long, GroupWorkDTO> groupWorkMap, String date) {
|
if (Func.isEmpty(states)) {
|
return Lists.newArrayList();
|
}
|
Map<Long, Map<Integer, List<SuperAggregateState>>> stateMap = states.stream().filter(c -> {
|
return MdcConstant.SHIFT_INDEX.contains(c.getShiftIndex());
|
}).collect(Collectors.groupingBy((v0) -> {
|
return v0.getWorkstationId();
|
}, Collectors.groupingBy((v0) -> {
|
return v0.getShiftIndex();
|
})));
|
List<TimeUsedExport> res = Lists.newArrayList();
|
stateMap.forEach((workstationId, shiftMap) -> {
|
Workstation workstation = (Workstation) workstationMap.getOrDefault(workstationId, new Workstation());
|
GroupWorkDTO groupWorkDTO = (GroupWorkDTO) groupWorkMap.getOrDefault(workstationId, new GroupWorkDTO());
|
shiftMap.forEach((shiftIndex, stateList) -> {
|
if (MdcConstant.SHIFT_INDEX.contains(shiftIndex)) {
|
String shiftName = Optional.ofNullable(shiftNameMap.get(workstation.getCalendarCode())).map(m -> {
|
return ((ShiftDetail) m.getOrDefault(shiftIndex, new ShiftDetail())).getIndexName();
|
}).orElse(MessageUtils.message("calendar.page.shift.model.shift", new Object[0]) + shiftIndex);
|
Map<Integer, String> wcsMap = getWcsMap(listGlobalWcs().stream().map(c2 -> {
|
return Integer.valueOf(Integer.parseInt(c2.getCode()));
|
}).collect(Collectors.toSet()), stateList);
|
TimeUsedExport build = TimeUsedExport.builder().date(date).groupName(groupWorkDTO.getName()).workstationCode(workstation.getCode()).workstationName(workstation.getName()).shiftName(shiftName).running(wcsMap.get(DefaultWcsEnum.RUNNING.getCode())).standby(wcsMap.get(DefaultWcsEnum.STANDBY.getCode())).debugging(wcsMap.get(DefaultWcsEnum.DEBUGGING.getCode())).alarm(wcsMap.get(DefaultWcsEnum.ALARM.getCode())).offline(wcsMap.get(DefaultWcsEnum.OFFLINE.getCode())).build();
|
res.add(build);
|
}
|
});
|
});
|
return res;
|
}
|
|
private Map<Integer, String> getWcsMap(Set<Integer> wcsList, List<SuperAggregateState> states) {
|
Map<Integer, Long> map = states.stream().filter(c -> {
|
return (!wcsList.contains(c.getValueCollect()) || c.getValueCollect() == null || c.getDurationCollect() == null) ? false : true;
|
}).collect(Collectors.groupingBy((v0) -> {
|
return v0.getValueCollect();
|
}, Collectors.summingLong((v0) -> {
|
return v0.getDurationCollect();
|
})));
|
long sum = map.values().stream().mapToLong(c2 -> {
|
return c2.longValue();
|
}).sum();
|
Map<Integer, String> res = new HashMap<>(map.size());
|
wcsList.forEach(w -> {
|
String value;
|
Long l = 0L;
|
if (l.equals(Long.valueOf(sum))) {
|
value = "0%";
|
} else {
|
value = NumberUtil.decimalFormat(MdcConstant.NORMAL_FORMAT, ((((Long) map.getOrDefault(w, 0L)).longValue() * 1.0d) / sum) * 100.0d) + "%";
|
}
|
res.put(w, value);
|
});
|
return res;
|
}
|
|
private WorkstationBandShiftVO getWorkShiftIndexName(QueryShiftIndexNameVO vo) {
|
List<String> ids = vo.getIds();
|
LocalDate localDate = vo.getLocalDate();
|
List<Workstation> workstations = this.workstationService.list(Wrappers.<Workstation>lambdaQuery()
|
.in(Func.isNotEmpty(ids), Workstation::getId, ids)
|
.eq(Workstation::getType, WorkstationTypeEnum.MACHINE.getCode())
|
.isNotNull(Workstation::getCalendarCode)
|
.orderByAsc(Workstation::getCode));
|
/*
|
List<Workstation> workstations = this.workstationService.list((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().in(Func.isNotEmpty(ids), (v0) -> {
|
return v0.getId();
|
}, ids).eq((v0) -> {
|
return v0.getType();
|
}, WorkstationTypeEnum.MACHINE.getCode())).isNotNull((v0) -> {
|
return v0.getCalendarCode();
|
})).orderByAsc((v0) -> {
|
return v0.getCode();
|
}));*/
|
return getWorkstationBandShiftVO(localDate, workstations);
|
}
|
|
private WorkstationBandShiftVO getWorkstationBandShiftVO(LocalDate localDate, List<Workstation> workstations) {
|
Map<String, List<Workstation>> calendarCodeMap = workstations.stream().collect(Collectors.groupingBy((v0) -> {
|
return v0.getCalendarCode();
|
}));
|
if (Func.isEmpty(calendarCodeMap)) {
|
return new WorkstationBandShiftVO();
|
}
|
if (calendarCodeMap.size() == 1) {
|
List<MixShiftDetailsDTO> result = Lists.newArrayList();
|
calendarCodeMap.forEach((calendarCode, workstationList) -> {
|
List<ShiftIndexNameDTO> shiftDetails = this.calendarService.queryShiftIndexName(calendarCode, Integer.valueOf(localDate.getYear()), DateUtil.formatDate(localDate));
|
this.calendarService.queryShiftIndexName(calendarCode, Integer.valueOf(localDate.getYear()), DateUtil.formatDate(localDate));
|
for (ShiftIndexNameDTO shiftDetail : shiftDetails) {
|
result.add(MixShiftDetailsDTO.builder().indexName(shiftDetail.getIndexName()).shiftIndex(shiftDetail.getShiftIndex()).mixShift(Boolean.FALSE).build());
|
}
|
});
|
return WorkstationBandShiftVO.builder().shiftDetails(result).build();
|
}
|
List<MixShiftDetailsDTO> result2 = Lists.newArrayList();
|
List<ShiftIndexNameDTO> shiftOne = Lists.newArrayList();
|
List<ShiftIndexNameDTO> shiftTwo = Lists.newArrayList();
|
List<ShiftIndexNameDTO> shiftThree = Lists.newArrayList();
|
List<ShiftIndexNameDTO> shiftFour = Lists.newArrayList();
|
calendarCodeMap.forEach((calendarCode2, workstationList2) -> {
|
List<ShiftIndexNameDTO> shiftDetails = this.calendarService.queryShiftIndexName(calendarCode2, Integer.valueOf(localDate.getYear()), DateUtil.formatDate(localDate));
|
for (ShiftIndexNameDTO shiftDetail : shiftDetails) {
|
if (CalendarConstant.ONE.equals(shiftDetail.getShiftIndex())) {
|
shiftOne.add(shiftDetail);
|
}
|
if (CalendarConstant.TWO.equals(shiftDetail.getShiftIndex())) {
|
shiftTwo.add(shiftDetail);
|
}
|
if (CalendarConstant.THREE.equals(shiftDetail.getShiftIndex())) {
|
shiftThree.add(shiftDetail);
|
}
|
if (CalendarConstant.FOUR.equals(shiftDetail.getShiftIndex())) {
|
shiftFour.add(shiftDetail);
|
}
|
}
|
});
|
buildShiftIndexNameDTO(shiftOne, result2);
|
buildShiftIndexNameDTO(shiftTwo, result2);
|
buildShiftIndexNameDTO(shiftThree, result2);
|
buildShiftIndexNameDTO(shiftFour, result2);
|
return WorkstationBandShiftVO.builder().shiftDetails(result2).build();
|
}
|
|
private void buildShiftIndexNameDTO(List<ShiftIndexNameDTO> shiftList, List<MixShiftDetailsDTO> result) {
|
if (Func.isNotEmpty(shiftList)) {
|
log.info("班次信息:{}", shiftList);
|
long count = shiftList.stream().map((v0) -> {
|
return v0.getIndexName();
|
}).distinct().count();
|
ShiftIndexNameDTO dto = shiftList.get(0);
|
if (count > 1) {
|
result.add(MixShiftDetailsDTO.builder().indexName("").shiftIndex(dto.getShiftIndex()).mixShift(Boolean.TRUE).build());
|
} else {
|
result.add(MixShiftDetailsDTO.builder().indexName(dto.getIndexName()).shiftIndex(dto.getShiftIndex()).mixShift(Boolean.FALSE).build());
|
}
|
}
|
}
|
|
private WorkstationBandShiftVO getGroupShiftIndexName(QueryShiftIndexNameVO vo) {
|
List<String> ids = vo.getIds();
|
String groupType = vo.getGroupType();
|
String groupCategory = vo.getGroupCategory();
|
LocalDate localDate = vo.getLocalDate();
|
List<CommonGroup> commonGroups = this.commonGroupService.list(Wrappers.<CommonGroup>lambdaQuery()
|
.eq(CommonGroup::getGroupType, groupType)
|
.eq(CommonGroup::getGroupCategory, groupCategory)
|
.in(Func.isNotEmpty(ids), CommonGroup::getId, ids));
|
/*
|
List<CommonGroup> commonGroups = this.commonGroupService.list(((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
|
return v0.getGroupType();
|
}, groupType)).eq((v0) -> {
|
return v0.getGroupCategory();
|
}, groupCategory)).in(Func.isNotEmpty(ids), (v0) -> {
|
return v0.getId();
|
}, ids));*/
|
if (Func.isEmpty(commonGroups)) {
|
return new WorkstationBandShiftVO();
|
}
|
List<String> groupIds = commonGroups.stream().map(c -> {
|
return String.valueOf(c.getId());
|
}).collect(Collectors.toList());
|
List<Workstation> workstations = this.workstationService.listWorkStationByGroupId(groupIds);
|
return getWorkstationBandShiftVO(localDate, workstations);
|
}
|
}
|