package com.qianwen.smartman.modules.mdc.service.impl; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; 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.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.ExcelConstant; import com.qianwen.smartman.common.constant.MdcConstant; import com.qianwen.smartman.common.constant.ShiftConstant; import com.qianwen.smartman.common.utils.LocalDateTimeUtils; 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.SuperAggregateOutput; import com.qianwen.smartman.modules.mdc.enums.OpenTypeEnums; import com.qianwen.smartman.modules.mdc.enums.StatisticalMethodEnum; import com.qianwen.smartman.modules.mdc.excel.OutputShiftIndexSheetExcel; import com.qianwen.smartman.modules.mdc.service.IOutputStatisticsService; import com.qianwen.smartman.modules.mdc.service.IStatisticsService; import com.qianwen.smartman.modules.mdc.service.ISuperAggregateOutputService; import com.qianwen.smartman.modules.mdc.utils.ExcelStrategyUtil; import com.qianwen.smartman.modules.mdc.utils.FilterOffUtils; import com.qianwen.smartman.modules.mdc.vo.AnalysisFilterVO; 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 OutputStatisticsServiceImpl implements IOutputStatisticsService { private static final String ZERO = "0"; private final IWorkstationService workstationService; private final ISuperAggregateOutputService superAggregateOutputService; private final StatisticsWrapper wrapper; private final WorkstationMapper workstationMapper; private final IShiftDetailService shiftDetailService; private final OssBuilder ossBuilder; private final IStatisticsService statisticsService; private static final Logger log = LoggerFactory.getLogger(OutputStatisticsServiceImpl.class); private static final String FILENAME = String.format("%s-%s.xlsx", ExcelConstant.OUTPUT_ACCOUNT, DateUtil.time()); public OutputStatisticsServiceImpl(final IWorkstationService workstationService, final ISuperAggregateOutputService superAggregateOutputService, final StatisticsWrapper wrapper, final WorkstationMapper workstationMapper, final IShiftDetailService shiftDetailService, final OssBuilder ossBuilder, final IStatisticsService statisticsService) { this.workstationService = workstationService; this.superAggregateOutputService = superAggregateOutputService; this.wrapper = wrapper; this.workstationMapper = workstationMapper; this.shiftDetailService = shiftDetailService; this.ossBuilder = ossBuilder; this.statisticsService = statisticsService; } @Override public StatisticsVO outputStatistics(StatisticsAnalysisQueryVO statisticsAnalysisQueryVO, Query query) { 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((v0) -> { return v0.getId(); }).collect(Collectors.toList()); List intervalDates = LocalDateTimeUtils.getIntervalDate(statisticsAnalysisQueryVO.getStartDate(), statisticsAnalysisQueryVO.getEndDate()); List superAggregateOutputByNaturalDate = this.superAggregateOutputService.getOutputData(workstationIds, statisticsAnalysisQueryVO.getStatisticalMethod(), statisticsAnalysisQueryVO.getStartDate(), statisticsAnalysisQueryVO.getEndDate()); List superAggregateList = FilterOffUtils.filterOffDay(superAggregateOutputByNaturalDate, OpenTypeEnums.OUT_PUT); StatisticsWrapperDto statisticsWrapperDto = new StatisticsWrapperDto(superAggregateList, statisticsAnalysisQueryVO, intervalDates, workstationPage, MdcConstant.OUTPUT); StatisticsVO result = this.wrapper.entityVO(statisticsWrapperDto); return result; } @Override public BladeFile export(StatisticsAnalysisQueryVO analysisQueryVO) { StatisticalMethodEnum statisticalMethod = analysisQueryVO.getStatisticalMethod(); List workStationIdList = analysisQueryVO.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(statisticalMethod); } List intervalDates = LocalDateTimeUtils.getIntervalDate(analysisQueryVO.getStartDate(), analysisQueryVO.getEndDate()); List superAggregateOutputByNaturalDate = this.superAggregateOutputService.getOutputData(workstationIds, analysisQueryVO.getStatisticalMethod(), analysisQueryVO.getStartDate(), analysisQueryVO.getEndDate()); List superAggregateList = new ArrayList<>(superAggregateOutputByNaturalDate); List workstationVOList = this.workstationMapper.queryWorkstationAndGroup(Condition.getPage(new Query().setSize(-1)), workstationIds).getRecords(); if (StatisticalMethodEnum.SHIFT.equals(statisticalMethod)) { List shiftInfoDtoList = this.shiftDetailService.getShiftInfoDtoList(workstationIds, analysisQueryVO.getStartDate(), analysisQueryVO.getEndDate()); Map shiftInfoMap = shiftInfoDtoList.stream().collect(Collectors.toMap(s2 -> { return s2.getDate() + "-" + s2.getWorkstationId() + "-" + s2.getShiftIndex(); }, (v0) -> { return v0.getIndexName(); })); return exportOutputShift(intervalDates, superAggregateList, workstationVOList, analysisQueryVO.getShiftIndexList(), shiftInfoMap); } return exportOutputs(superAggregateOutputByNaturalDate, workstationVOList, analysisQueryVO); } private BladeFile exportEmpty(StatisticalMethodEnum statisticalMethodEnum) { if (statisticalMethodEnum == StatisticalMethodEnum.SHIFT) { List sheetExcels = new ArrayList<>(); MultipartFile shiftMultipartFile = ExcelUtil.exportToMultipartFile(FILENAME, ExcelConstant.OUTPUT_ACCOUNT, sheetExcels, OutputShiftIndexSheetExcel.class); return this.ossBuilder.tempTemplate().putFile(shiftMultipartFile.getOriginalFilename(), shiftMultipartFile); } List> heads = new ArrayList<>(); List> columnData = new ArrayList<>(); MultipartFile multipartFile; try { multipartFile = ExcelStrategyUtil.customerExport(FILENAME, ExcelConstant.OUTPUT_ACCOUNT, heads, columnData); return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile); } catch (Exception e) { log.error("导出错误",e); throw new RuntimeException(e); } } private BladeFile exportOutputs(List aggregateOutputs, List workstationVOList, StatisticsAnalysisQueryVO statisticsAnalysisQueryVO) { List> heads = new ArrayList<>(); List> columnData = 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(); buildExcelHeads(heads, items, statisticalMethod); buildExcelData(columnData, workstationVOList, statisticsAnalysisQueryVO, items, aggregateOutputs); try { MultipartFile multipartFile = ExcelStrategyUtil.customerExport(FILENAME, ExcelConstant.OUTPUT_ACCOUNT, heads, columnData); return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile); } catch (Exception e) { log.error("导出错误",e); throw new RuntimeException(e); } } public BladeFile exportOutputShift(List intervalDates, List superAggregates, List workstationVOList, List shiftIndexList, Map shiftInfoMap) { Map> map =superAggregates.stream().collect(Collectors.groupingBy(s -> { return s.getFactoryDate() + "-" + s.getWorkstationId() + "-" + s.getShiftIndex(); })); List sheetExcels = new ArrayList<>(); IntervalDateDto intervalDateDto = intervalDates.get(0); intervalDateDto.getDayList().forEach(dayString -> { workstationVOList.forEach(wId -> { OutputShiftIndexSheetExcel excel = new OutputShiftIndexSheetExcel(); excel.setDate(dayString).setGroupName(wId.getGroupName()).setWorkstationCode(wId.getCode()).setWorkstationName(wId.getName()); shiftIndexList.forEach(index -> { String stringDayKey = StrUtil.format("{}-{}-{}", new Object[]{dayString, wId.getId(), index}); String intDayKey = StrUtil.format("{}-{}-{}", new Object[]{dayString.replace("-", ""), wId.getId(), index}); String shiftName = (String) shiftInfoMap.get(stringDayKey); if (Func.isBlank(shiftName)) { buildEmptyData(index, excel, MessageUtils.message("mdc.not.schedule", new Object[0])); return; } List superAggregateList = map.get(intDayKey); if (Func.isEmpty(superAggregateList)) { buildData(index, excel, shiftName, ZERO); return; } String value = this.wrapper.getValue(superAggregateList, MdcConstant.OUTPUT, null); buildData(index, excel, shiftName, value); }); sheetExcels.add(excel); }); }); MultipartFile multipartFile = ExcelUtil.exportToMultipartFile(FILENAME, ExcelConstant.OUTPUT_ACCOUNT, sheetExcels, OutputShiftIndexSheetExcel.class); return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile); } private void buildExcelData(List> data, List workstationVOList, StatisticsAnalysisQueryVO statisticsAnalysisQueryVO, Collection items, List aggregateOutputs) { StatisticalMethodEnum statisticalMethod = statisticsAnalysisQueryVO.getStatisticalMethod(); workstationVOList.forEach(workstationVO -> { List keyList; Map> map; List arrayList = new ArrayList<>(); arrayList.add(workstationVO.getGroupName()); arrayList.add(workstationVO.getCode()); arrayList.add(workstationVO.getName()); Long workstationId = workstationVO.getId(); switch (statisticalMethod) { case DAY: keyList = items.stream().map(s -> { return Long.valueOf(workstationId.longValue() + Integer.parseInt(s.getDate().replace("-", ""))); }).collect(Collectors.toList()); map = aggregateOutputs.stream().collect(Collectors.groupingBy(s2 -> { return Long.valueOf(s2.getWorkstationId().longValue() + s2.getFactoryDate().intValue()); })); break; case WEEK: keyList = items.stream().map(s3 -> { return Long.valueOf(workstationId.longValue() + s3.getYear().intValue() + s3.getWeek().intValue()); }).collect(Collectors.toList()); map = aggregateOutputs.stream().collect(Collectors.groupingBy(s4 -> { return Long.valueOf(s4.getWorkstationId().longValue() + s4.getFactoryYear().intValue() + s4.getFactoryWeek().intValue()); })); break; default: keyList = items.stream().map(s5 -> { return Long.valueOf(workstationId.longValue() + s5.getYear().intValue() + s5.getMonth().intValue()); }).collect(Collectors.toList()); map = aggregateOutputs.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 superAggregateOutputs = finalMap.get(key); if (Func.isEmpty(superAggregateOutputs)) { arrayList.add(ZERO); } else { arrayList.add(Func.toStr(this.wrapper.calculateOutput(superAggregateOutputs))); } }); data.add(arrayList); }); } private void buildExcelHeads(List> head, Collection items, StatisticalMethodEnum statisticalMethod) { 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 (statisticalMethod) { case DAY: head.add(Lists.newArrayList(new String[]{splitFilterVO.getDate()})); return; case WEEK: head.add(Lists.newArrayList(new String[]{StrUtil.format("{}-{}{}{}", new Object[]{splitFilterVO.getYear(), the, splitFilterVO.getWeek(), week})})); return; default: head.add(Lists.newArrayList(new String[]{splitFilterVO.getTitle()})); return; } }); } private void buildEmptyData(Integer shiftIndex, OutputShiftIndexSheetExcel excel, String message) { if (ShiftConstant.SHIFT1.equals(shiftIndex)) { excel.setShift1(message); excel.setYield1("-"); } else if (ShiftConstant.SHIFT2.equals(shiftIndex)) { excel.setShift2(message); excel.setYield2("-"); } else if (ShiftConstant.SHIFT3.equals(shiftIndex)) { excel.setShift3(message); excel.setYield3("-"); } else { excel.setShift4(message); excel.setYield4("-"); } } private void buildData(Integer shiftIndex, OutputShiftIndexSheetExcel excel, String shiftName, String yield) { if (ShiftConstant.SHIFT1.equals(shiftIndex)) { excel.setShift1(shiftName); excel.setYield1(yield); } else if (ShiftConstant.SHIFT2.equals(shiftIndex)) { excel.setShift2(shiftName); excel.setYield2(yield); } else if (ShiftConstant.SHIFT3.equals(shiftIndex)) { excel.setShift3(shiftName); excel.setYield3(yield); } else { excel.setShift4(shiftName); excel.setYield4(yield); } } }