package com.qianwen.smartman.modules.mdc.service.impl; import java.math.BigDecimal; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.WeekFields; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import javax.servlet.http.HttpServletResponse; 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.mp.support.Condition; import com.qianwen.core.mp.support.Query; import com.qianwen.core.oss.model.BladeFile; 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.MdcConstant; import com.qianwen.smartman.common.utils.LocalDateTimeUtils; import com.qianwen.smartman.common.utils.LocalDateUtil; import com.qianwen.smartman.common.utils.MessageUtils; import com.qianwen.smartman.modules.smis.convert.WorkstationConvert; import com.qianwen.smartman.modules.smis.dto.ShiftInfoDTO; import com.qianwen.smartman.modules.smis.entity.Workstation; import com.qianwen.smartman.modules.smis.enums.WorkstationTypeEnum; import com.qianwen.smartman.modules.smis.mapper.WorkstationMapper; import com.qianwen.smartman.modules.smis.service.IShiftDetailService; import com.qianwen.smartman.modules.smis.service.IWorkstationService; import com.qianwen.smartman.modules.smis.vo.WorkstationVO; import com.qianwen.smartman.modules.mdc.dto.IntervalDateDto; import com.qianwen.smartman.modules.mdc.dto.StatisticsWrapperDto; import com.qianwen.smartman.modules.mdc.entity.SuperAggregate; import com.qianwen.smartman.modules.mdc.entity.SuperAggregateState; import com.qianwen.smartman.modules.mdc.enums.ProductivityTypeEnum; import com.qianwen.smartman.modules.mdc.enums.StatisticalMethodEnum; import com.qianwen.smartman.modules.mdc.excel.EfficiencyAnalysisExport; import com.qianwen.smartman.modules.mdc.service.IEfficiencyAnalysisService; import com.qianwen.smartman.modules.mdc.service.IStatisticsService; import com.qianwen.smartman.modules.mdc.service.ISuperAggregateStateService; import com.qianwen.smartman.modules.mdc.utils.EifficiencyUtils; import com.qianwen.smartman.modules.mdc.utils.ExcelStrategyUtil; import com.qianwen.smartman.modules.mdc.vo.AnalysisFilterVO; import com.qianwen.smartman.modules.mdc.vo.ColumnInfoVO; import com.qianwen.smartman.modules.mdc.vo.QueryIntervalDateVO; import com.qianwen.smartman.modules.mdc.vo.SplitFilterListVO; import com.qianwen.smartman.modules.mdc.vo.SplitFilterVO; import com.qianwen.smartman.modules.mdc.vo.StatisticsAnalysisQueryVO; import com.qianwen.smartman.modules.mdc.vo.StatisticsVO; import com.qianwen.smartman.modules.mdc.wrapper.StatisticsWrapper; import com.qianwen.smartman.modules.resource.builder.oss.OssBuilder; import cn.hutool.core.util.StrUtil; @Service public class EfficiencyAnalysisServiceImpl implements IEfficiencyAnalysisService { private static final Logger log = LoggerFactory.getLogger(EfficiencyAnalysisServiceImpl.class); private static final String ZERO = "0.00%"; private final ISuperAggregateStateService superAggregateStateService; private final IWorkstationService workstationService; private final StatisticsWrapper wrapper; private final WorkstationMapper workstationMapper; private final OssBuilder ossBuilder; private final IShiftDetailService shiftDetailService; private final IStatisticsService statisticsService; private final Integer SHIFT1 = 1; private final Integer SHIFT2 = 2; private final Integer SHIFT3 = 3; private final Integer SHIFT4 = 4; public EfficiencyAnalysisServiceImpl(final ISuperAggregateStateService superAggregateStateService, final IWorkstationService workstationService, final StatisticsWrapper wrapper, final WorkstationMapper workstationMapper, final OssBuilder ossBuilder, final IShiftDetailService shiftDetailService, final IStatisticsService statisticsService) { this.superAggregateStateService = superAggregateStateService; this.workstationService = workstationService; this.wrapper = wrapper; this.workstationMapper = workstationMapper; this.ossBuilder = ossBuilder; this.shiftDetailService = shiftDetailService; this.statisticsService = statisticsService; } @Override public StatisticsVO efficiencyAnalysis(StatisticsAnalysisQueryVO statisticsAnalysisQueryVO, Query query) { List superAggregateStates; List workStationIdList = statisticsAnalysisQueryVO.getWorkStationIdList(); if (Func.isEmpty(workStationIdList)) { workStationIdList = this.workstationService.list(Wrappers.lambdaQuery().eq(Workstation::getType, WorkstationTypeEnum.MACHINE.getCode()).eq(Workstation::getStatus, CommonConstant.ENABLE)).stream().map(s -> Func.toStr(s.getId())).collect(Collectors.toList()); } IPage workstationVOPage = this.workstationMapper.queryWorkstationAndGroup(Condition.getPage(query), Func.toLongList(workStationIdList)); IPage workstationPage = WorkstationConvert.INSTANCE.convert(workstationVOPage); List workstationIds = workstationPage.getRecords().stream().map(Workstation::getId).collect(Collectors.toList()); List intervalDates = LocalDateTimeUtils.getIntervalDate(statisticsAnalysisQueryVO.getStartDate(), statisticsAnalysisQueryVO.getEndDate()); //都是用采集的数据(getStatusData),不用反馈的。计划内外所有状态都能设置。 if (statisticsAnalysisQueryVO.getProductivityType() == ProductivityTypeEnum.OEE) { //superAggregateStates = this.superAggregateStateService.getStatusDataWithFeedback(workstationIds, statisticsAnalysisQueryVO.getStatisticalMethod(), statisticsAnalysisQueryVO.getStartDate(), statisticsAnalysisQueryVO.getEndDate()); } else { //superAggregateStates = this.superAggregateStateService.getStatusData(workstationIds, statisticsAnalysisQueryVO.getStatisticalMethod(), statisticsAnalysisQueryVO.getStartDate(), statisticsAnalysisQueryVO.getEndDate()); } superAggregateStates = this.superAggregateStateService.getStatusData(workstationIds, statisticsAnalysisQueryVO.getStatisticalMethod(), statisticsAnalysisQueryVO.getStartDate(), statisticsAnalysisQueryVO.getEndDate()); List superAggregateList = superAggregateStates.stream().filter(x -> { return x.getWcs() > 0; }).collect(Collectors.toList()); StatisticsWrapperDto statisticsWrapperDto = new StatisticsWrapperDto(superAggregateList, statisticsAnalysisQueryVO, intervalDates, workstationPage, MdcConstant.STATE); return this.wrapper.entityVO(statisticsWrapperDto); } @Override public List getIntervalDate(QueryIntervalDateVO queryIntervalDateVO) { List intervalDate = LocalDateTimeUtils.getIntervalDate(queryIntervalDateVO.getStartDate(), queryIntervalDateVO.getEndDate()); List columnInfoVOList = new ArrayList<>(); String theMessage = MessageUtils.message("mdc.efficiency.analysis.the", new Object[0]); String weekMessage = MessageUtils.message("mdc.efficiency.analysis.week", new Object[0]); switch (queryIntervalDateVO.getStatisticalMethod()) { case MONTH: intervalDate.forEach(intervalDateDto -> { intervalDateDto.getMonthList().forEach(month -> { ColumnInfoVO columnInfoVO = new ColumnInfoVO(); columnInfoVO.setId(intervalDateDto.getYear() + "-" + month); columnInfoVO.setTitle(intervalDateDto.getYear() + "-" + month); columnInfoVOList.add(columnInfoVO); }); }); break; case WEEK: WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 1); intervalDate.forEach(intervalDateDto2 -> { intervalDateDto2.getWeekList().forEach(week -> { ColumnInfoVO columnInfoVO = new ColumnInfoVO(); columnInfoVO.setId(intervalDateDto2.getYear() + "-" + week); columnInfoVO.setTitle(intervalDateDto2.getYear() + "-" + theMessage + week + weekMessage); LocalDate localDate = LocalDate.now().withYear(intervalDateDto2.getYear().intValue()).with(weekFields.weekOfYear(), week.intValue()); columnInfoVO.setStartDate(localDate.with(weekFields.dayOfWeek(), 1L)); columnInfoVO.setEndDate(localDate.with(weekFields.dayOfWeek(), 7L)); if (columnInfoVO.getEndDate().getYear() != columnInfoVO.getStartDate().getYear()) { if (week.equals(CommonConstant.ONE)) { columnInfoVO.setStartDate(LocalDateUtil.dateToLocalDate(LocalDateTimeUtils.getFirstDayDateOfYear(LocalDateUtil.localToDate(columnInfoVO.getEndDate())))); } else { columnInfoVO.setEndDate(LocalDateUtil.dateToLocalDate(LocalDateTimeUtils.getLastDayOfYear(LocalDateUtil.localToDate(columnInfoVO.getStartDate())))); } } columnInfoVOList.add(columnInfoVO); }); }); break; default: intervalDate.get(0).getDayList().forEach(day -> { ColumnInfoVO columnInfoVO = new ColumnInfoVO(); columnInfoVO.setId(day); columnInfoVO.setTitle(day); columnInfoVOList.add(columnInfoVO); }); break; } return columnInfoVOList; } @Override public BladeFile exportEfficiencyAnalysis(StatisticsAnalysisQueryVO statisticsAnalysisQueryVO, HttpServletResponse response) { List superAggregateStates; List workStationIdList = statisticsAnalysisQueryVO.getWorkStationIdList(); if (Func.isEmpty(workStationIdList)) { workStationIdList = (List)this.workstationService.list(Wrappers.lambdaQuery().eq(Workstation::getType, WorkstationTypeEnum.MACHINE.getCode()).eq(Workstation::getStatus, CommonConstant.ENABLE)).stream().map(s -> Func.toStr(s.getId())).collect(Collectors.toList()); /* workStationIdList = (List) this.workstationService.list((Wrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> { return v0.getType(); }, WorkstationTypeEnum.MACHINE.getCode())).eq((v0) -> { return v0.getStatus(); }, CommonConstant.ENABLE)).stream().map(s -> { return Func.toStr(s.getId()); }).collect(Collectors.toList());*/ } List workstationIds = workStationIdList.stream().map(Long::valueOf).collect(Collectors.toList()); if (Func.isEmpty(workstationIds)) { return exportEmpty(statisticsAnalysisQueryVO.getStatisticalMethod(), statisticsAnalysisQueryVO.getProductivityType()); } List intervalDates = LocalDateTimeUtils.getIntervalDate(statisticsAnalysisQueryVO.getStartDate(), statisticsAnalysisQueryVO.getEndDate()); if (statisticsAnalysisQueryVO.getProductivityType() == ProductivityTypeEnum.OEE) { superAggregateStates = this.superAggregateStateService.getStatusDataWithFeedback(workstationIds, statisticsAnalysisQueryVO.getStatisticalMethod(), statisticsAnalysisQueryVO.getStartDate(), statisticsAnalysisQueryVO.getEndDate()); } else { superAggregateStates = this.superAggregateStateService.getStatusData(workstationIds, statisticsAnalysisQueryVO.getStatisticalMethod(), statisticsAnalysisQueryVO.getStartDate(), statisticsAnalysisQueryVO.getEndDate()); } List superAggregateStates2 = superAggregateStates.stream().filter(x -> { return x.getWcs().intValue() > 0; }).collect(Collectors.toList()); List workstationVOList = this.workstationMapper.queryWorkstationAndGroup(Condition.getPage(new Query().setSize(-1)), workstationIds).getRecords(); if (StatisticalMethodEnum.SHIFT.equals(statisticsAnalysisQueryVO.getStatisticalMethod())) { List shiftInfoDtoList = this.shiftDetailService.getShiftInfoDtoList(workstationIds, statisticsAnalysisQueryVO.getStartDate(), statisticsAnalysisQueryVO.getEndDate()); Map shiftInfoMap = shiftInfoDtoList.stream().collect(Collectors.toMap(s2 -> { return s2.getDate() + "-" + s2.getWorkstationId() + "-" + s2.getShiftIndex(); }, (v0) -> { return v0.getIndexName(); })); return exportShift(intervalDates, superAggregateStates2, workstationVOList, statisticsAnalysisQueryVO.getShiftIndexList(), statisticsAnalysisQueryVO.getProductivityType(), shiftInfoMap); } return export(superAggregateStates2, workstationVOList, statisticsAnalysisQueryVO); } private BladeFile exportEmpty(StatisticalMethodEnum statisticalMethod, ProductivityTypeEnum productivityTypeEnum) { String name = getName(productivityTypeEnum); if (StatisticalMethodEnum.SHIFT.equals(statisticalMethod)) { List efficiencyAnalysisExportList = new ArrayList<>(); String fileName = String.format("%s-%s.xlsx", name, DateUtil.time()); MultipartFile multipartFile = ExcelUtil.exportToMultipartFile(fileName, name, efficiencyAnalysisExportList, EfficiencyAnalysisExport.class); return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile); } List> head = new ArrayList<>(); List> data = new ArrayList<>(); String fileName2 = String.format("%s-%s.xlsx", name, DateUtil.time()); MultipartFile multipartFile2; try { multipartFile2 = ExcelStrategyUtil.customerExport(fileName2, name, head, data); return this.ossBuilder.tempTemplate().putFile(multipartFile2.getOriginalFilename(), multipartFile2); } catch (Exception e) { log.error("导出错误",e); e.printStackTrace(); throw new RuntimeException(e); } } private BladeFile export(List superAggregateList, List workstationVOList, StatisticsAnalysisQueryVO statisticsAnalysisQueryVO) { List> head = new ArrayList<>(); List> data = new ArrayList<>(); AnalysisFilterVO analysisFilterVO = new AnalysisFilterVO(); StatisticalMethodEnum statisticalMethod = statisticsAnalysisQueryVO.getStatisticalMethod(); analysisFilterVO.setStatisticalMethod(statisticalMethod); analysisFilterVO.setStartDate(statisticsAnalysisQueryVO.getStartDate()); analysisFilterVO.setEndDate(statisticsAnalysisQueryVO.getEndDate()); analysisFilterVO.setIsReverse(true); SplitFilterListVO splitFilterListVO = this.statisticsService.organizeQueryConditions(analysisFilterVO); Collection items = splitFilterListVO.getItems(); buildHead(head, items, statisticalMethod); buildData(data, workstationVOList, statisticsAnalysisQueryVO, items, superAggregateList); String name = getName(statisticsAnalysisQueryVO.getProductivityType()); String fileName = String.format("%s-%s.xlsx", name, DateUtil.time()); try { MultipartFile multipartFile = ExcelStrategyUtil.customerExport(fileName, name, head, data); return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile); } catch (Exception e) { log.error("导出错误",e); e.printStackTrace(); throw new RuntimeException(e); } } private void buildData(List> data, List workstationVOList, StatisticsAnalysisQueryVO statisticsAnalysisQueryVO, Collection items, List superAggregateList) { workstationVOList.forEach(workstationVO -> { //List keyList; //Map> map; ArrayList arrayList = new ArrayList<>(); arrayList.add(workstationVO.getGroupName()); arrayList.add(workstationVO.getCode()); arrayList.add(workstationVO.getName()); List keyList = new ArrayList<>(); Map> map = new HashMap<>(); /*new ArrayList(); new HashMap();*/ Long workstationId = workstationVO.getId(); switch (statisticsAnalysisQueryVO.getStatisticalMethod()) { case WEEK: keyList = items.stream().map(s -> { return Long.valueOf(workstationId.longValue() + s.getYear().intValue() + s.getWeek().intValue()); }).collect(Collectors.toList()); map = superAggregateList.stream().collect(Collectors.groupingBy(s2 -> { return Long.valueOf(s2.getWorkstationId().longValue() + s2.getFactoryYear().intValue() + s2.getFactoryWeek().intValue()); })); break; case DAY: keyList = items.stream().map(s3 -> { return Long.valueOf(workstationId.longValue() + Integer.parseInt(s3.getDate().replace("-", ""))); }).collect(Collectors.toList()); map = superAggregateList.stream().collect(Collectors.groupingBy(s4 -> { return Long.valueOf(s4.getWorkstationId().longValue() + s4.getFactoryDate().intValue()); })); break; default: keyList = items.stream().map(s5 -> { return Long.valueOf(workstationId.longValue() + s5.getYear().intValue() + s5.getMonth().intValue()); }).collect(Collectors.toList()); map = superAggregateList.stream().collect(Collectors.groupingBy(s6 -> { return Long.valueOf(s6.getWorkstationId().longValue() + s6.getFactoryYear().intValue() + s6.getFactoryMonth().intValue()); })); break; } Map> finalMap = map; keyList.forEach(key -> { List superAggregateStates = finalMap.get(key); if (Func.isEmpty(superAggregateStates)) { arrayList.add(ZERO); return; } Double value = EifficiencyUtils.calculationResults(superAggregateStates, statisticsAnalysisQueryVO.getProductivityType()); double v = BigDecimal.valueOf(value.doubleValue()).multiply(BigDecimal.valueOf(100L)).doubleValue(); arrayList.add(v + "%"); }); data.add(arrayList); }); } private void buildHead(List> head, Collection items, StatisticalMethodEnum statisticalMethodEnum) { String the = MessageUtils.message("mdc.efficiency.analysis.the", new Object[0]); String week = MessageUtils.message("mdc.efficiency.analysis.week", new Object[0]); head.add(Lists.newArrayList(new String[]{MessageUtils.message("mdc.excel.workstationGroup", new Object[0])})); head.add(Lists.newArrayList(new String[]{MessageUtils.message("mdc.excel.workstationCode", new Object[0])})); head.add(Lists.newArrayList(new String[]{MessageUtils.message("mdc.excel.workstationName", new Object[0])})); items.forEach(splitFilterVO -> { switch (statisticalMethodEnum) { case WEEK: head.add(Lists.newArrayList(new String[]{StrUtil.format("{}-{}{}{}", new Object[]{splitFilterVO.getYear(), the, splitFilterVO.getWeek(), week})})); return; case DAY: head.add(Lists.newArrayList(new String[]{splitFilterVO.getDate()})); return; default: head.add(Lists.newArrayList(new String[]{splitFilterVO.getTitle()})); return; } }); } public BladeFile exportShift(List intervalDates, List superAggregateList, List workstationVOList, List shiftIndexList, ProductivityTypeEnum productivityTypeEnum, Map shiftInfoMap) { String message = MessageUtils.message("mdc.not.schedule", new Object[0]); Map> map = superAggregateList.stream().collect(Collectors.groupingBy(s -> { return s.getFactoryDate() + "-" + s.getWorkstationId() + "-" + s.getShiftIndex(); })); List efficiencyAnalysisExportList = new ArrayList<>(); IntervalDateDto intervalDateDto = intervalDates.get(0); intervalDateDto.getDayList().forEach(dateString -> { workstationVOList.forEach(w -> { EfficiencyAnalysisExport efficiencyAnalysisExport = new EfficiencyAnalysisExport(); efficiencyAnalysisExport.setDate(dateString).setGroupName(w.getGroupName()).setWorkstationCode(w.getCode()).setWorkstationName(w.getName()); shiftIndexList.forEach(index -> { String stringDateKey = StrUtil.format("{}-{}-{}", new Object[]{dateString, w.getId(), index}); String intDateKey = StrUtil.format("{}-{}-{}", new Object[]{dateString.replace("-", ""), w.getId(), index}); String shiftName = (String) shiftInfoMap.get(stringDateKey); if (Func.isEmpty(shiftName)) { buildEmptyData(index, efficiencyAnalysisExport, message); return; } List superAggregateStates = (List) map.get(intDateKey); if (Func.isEmpty(superAggregateStates)) { buildData(index, efficiencyAnalysisExport, shiftName, ZERO); return; } Double value = EifficiencyUtils.calculationResults(superAggregateStates, productivityTypeEnum); String efficiency = BigDecimal.valueOf(value.doubleValue()).multiply(BigDecimal.valueOf(100L)).doubleValue() + "%"; buildData(index, efficiencyAnalysisExport, shiftName, efficiency); }); efficiencyAnalysisExportList.add(efficiencyAnalysisExport); }); }); String name = getName(productivityTypeEnum); String fileName = String.format("%s-%s.xlsx", name, DateUtil.time()); MultipartFile multipartFile = ExcelUtil.exportToMultipartFile(fileName, name, efficiencyAnalysisExportList, EfficiencyAnalysisExport.class); return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile); } private String getName(ProductivityTypeEnum productivityTypeEnum) { String name; switch (productivityTypeEnum) { case RUNNING: name = ProductivityTypeEnum.RUNNING.getMessage(); break; case ALARM: name = ProductivityTypeEnum.ALARM.getMessage(); break; default: name = ProductivityTypeEnum.OEE.getMessage(); break; } return name; } public void buildEmptyData(Integer shiftIndex, EfficiencyAnalysisExport efficiencyAnalysisExport, String message) { if (this.SHIFT1.equals(shiftIndex)) { efficiencyAnalysisExport.setShift1(message); efficiencyAnalysisExport.setEfficiency1("-"); } else if (this.SHIFT2.equals(shiftIndex)) { efficiencyAnalysisExport.setShift2(message); efficiencyAnalysisExport.setEfficiency2("-"); } else if (this.SHIFT3.equals(shiftIndex)) { efficiencyAnalysisExport.setShift3(message); efficiencyAnalysisExport.setEfficiency3("-"); } else { efficiencyAnalysisExport.setShift4(message); efficiencyAnalysisExport.setEfficiency4("-"); } } public void buildData(Integer shiftIndex, EfficiencyAnalysisExport efficiencyAnalysisExport, String shiftName, String value) { if (this.SHIFT1.equals(shiftIndex)) { efficiencyAnalysisExport.setShift1(shiftName); efficiencyAnalysisExport.setEfficiency1(value); } else if (this.SHIFT2.equals(shiftIndex)) { efficiencyAnalysisExport.setShift2(shiftName); efficiencyAnalysisExport.setEfficiency2(value); } else if (this.SHIFT3.equals(shiftIndex)) { efficiencyAnalysisExport.setShift3(shiftName); efficiencyAnalysisExport.setEfficiency3(value); } else { efficiencyAnalysisExport.setShift4(shiftName); efficiencyAnalysisExport.setEfficiency4(value); } } }