package com.qianwen.smartman.modules.mdc.service.impl; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; 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.extension.plugins.pagination.Page; 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.CollectionUtil; import com.qianwen.core.tool.utils.DateUtil; import com.qianwen.core.tool.utils.Func; import com.qianwen.smartman.common.constant.DateConstant; import com.qianwen.smartman.common.constant.ExcelConstant; 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.cps.dto.ShiftInfoDTO; import com.qianwen.smartman.modules.cps.mapper.WorkstationMapper; import com.qianwen.smartman.modules.cps.service.IShiftDetailService; import com.qianwen.smartman.modules.cps.vo.WorkstationVO; import com.qianwen.smartman.modules.mdc.convert.AlarmConvert; import com.qianwen.smartman.modules.mdc.entity.SuperAggregate; import com.qianwen.smartman.modules.mdc.entity.SuperAlarm; import com.qianwen.smartman.modules.mdc.enums.OpenTypeEnums; import com.qianwen.smartman.modules.mdc.enums.StatisticalMethodEnum; import com.qianwen.smartman.modules.mdc.excel.AlarmDataSheetExcel; import com.qianwen.smartman.modules.mdc.excel.AlarmDataSheetExcelForOther; import com.qianwen.smartman.modules.mdc.mapper.SuperAlarmMapper; import com.qianwen.smartman.modules.mdc.service.IAlarmService; import com.qianwen.smartman.modules.mdc.utils.FilterOffUtils; import com.qianwen.smartman.modules.mdc.vo.AlarmDataSheetShiftVO; import com.qianwen.smartman.modules.mdc.vo.AlarmDataSheetVO; import com.qianwen.smartman.modules.mdc.vo.AlarmShiftSearchExcelVO; import com.qianwen.smartman.modules.mdc.vo.AlarmShiftSearchVO; import com.qianwen.smartman.modules.mdc.vo.AlarmSummaryGraphShiftVO; import com.qianwen.smartman.modules.mdc.vo.AlarmSummaryGraphVO; import com.qianwen.smartman.modules.mdc.vo.SplitFilterListVO; import com.qianwen.smartman.modules.resource.builder.oss.OssBuilder; import cn.hutool.core.util.StrUtil; @Service public class AlarmServiceImpl implements IAlarmService { private static final Logger log = LoggerFactory.getLogger(AlarmServiceImpl.class); private final SuperAlarmMapper alarmMapper; private final OssBuilder ossBuilder; private final IShiftDetailService shiftDetailService; private final WorkstationMapper workstationMapper; public final Integer LENGTH_1 = 1; public AlarmServiceImpl(final SuperAlarmMapper alarmMapper, final OssBuilder ossBuilder, final IShiftDetailService shiftDetailService, final WorkstationMapper workstationMapper) { this.alarmMapper = alarmMapper; this.ossBuilder = ossBuilder; this.shiftDetailService = shiftDetailService; this.workstationMapper = workstationMapper; } @Override // org.springblade.modules.mdc.service.IAlarmService public IPage pageAlarm(Query query, AlarmShiftSearchVO vo) { if (Func.isNull(vo.getWorkstationId())) { return new Page<>(); } LocalDate queryTime = vo.getQueryTime(); List list = this.alarmMapper.selectListAlarm(vo, DateUtil.format(queryTime, "yyyyMMdd")); Map> map = FilterOffUtils.filterOffDay(list, OpenTypeEnums.ALARM_ANALYSIS).stream().collect(Collectors.groupingBy(c -> { return c.getCode() + c.getMessage(); })); Integer size = query.getSize(); Integer current = query.getCurrent(); Page page = new Page<>(current.intValue(), size.intValue(), map.size()); if (map.size() == 0) { return page; } List alarms = this.alarmMapper.selectListAlarm(vo, DateUtil.format(queryTime, "yyyyMMdd")); List res = AlarmConvert.INSTANCE.convert(FilterOffUtils.filterOffDay(alarms, OpenTypeEnums.ALARM_ANALYSIS)); List alarmDataSheetVOS = new ArrayList<>(); Map> resMap = res.stream().collect(Collectors.groupingBy(c2 -> { return c2.getAlarmCode() + c2.getAlarmMsg(); })); resMap.values().forEach(alarmDataSheetVOList -> { ((AlarmDataSheetVO) alarmDataSheetVOList.get(0)).setCount(Integer.valueOf(alarmDataSheetVOList.size())); alarmDataSheetVOS.add(alarmDataSheetVOList.get(0)); }); if (query.getSize().intValue() == -1) { page.setRecords(alarmDataSheetVOS); } else if (alarmDataSheetVOS.size() < ((query.getCurrent().intValue() - 1) * query.getSize().intValue()) + query.getSize().intValue()) { page.setRecords(alarmDataSheetVOS.subList((query.getCurrent().intValue() - 1) * query.getSize().intValue(), alarmDataSheetVOS.size())); } else { page.setRecords(alarmDataSheetVOS.subList((query.getCurrent().intValue() - 1) * query.getSize().intValue(), ((query.getCurrent().intValue() - 1) * query.getSize().intValue()) + query.getSize().intValue())); } return page; } @Override // org.springblade.modules.mdc.service.IAlarmService public BladeFile exportAlarm(AlarmShiftSearchExcelVO vo) { BladeFile bladeFile; List superAlarms = this.alarmMapper.selectListAlarmByShift(LocalDateTimeUtils.formatTimeLocalDate(vo.getStartTime(), "yyyyMMdd"), LocalDateTimeUtils.formatTimeLocalDate(vo.getEndTime(), "yyyyMMdd"), vo.getWorkstationId()); superAlarms = FilterOffUtils.filterOffDay(superAlarms, OpenTypeEnums.ALARM_ANALYSIS); superAlarms.sort(Comparator.comparing(SuperAggregate::getFactoryDate)); Collections.reverse(superAlarms); //superAlarms = superAlarms.stream().collect(Collectors.toList()); /* List superAlarms = FilterOffUtils.filterOffDay(this.alarmMapper.selectListAlarmByShift(LocalDateTimeUtils.formatTimeLocalDate(vo.getStartTime(), "yyyyMMdd"), LocalDateTimeUtils.formatTimeLocalDate(vo.getEndTime(), "yyyyMMdd"), vo.getWorkstationId()), OpenTypeEnums.ALARM_ANALYSIS).stream().sorted(Comparator.comparing((v0) -> { return v0.getFactoryDate(); }).reversed()).collect(Collectors.toList());*/ WorkstationVO workstationVO = (WorkstationVO) this.workstationMapper.queryWorkstationAndGroup(Condition.getPage(new Query().setSize(-1)), Lists.newArrayList(new Long[]{vo.getWorkstationId()})).getRecords().get(0); if (vo.getEnums().equals(StatisticalMethodEnum.SHIFT)) { List superAlarms2 = superAlarms.stream().filter(s -> { return s.getShiftIndex().intValue() > 0; }).collect(Collectors.toList()); List shiftInfoDtoList = this.shiftDetailService.getShiftInfoDtoList(Lists.newArrayList(new Long[]{vo.getWorkstationId()}), vo.getStartTime(), vo.getEndTime()); bladeFile = exportShift(superAlarms2, workstationVO, shiftInfoDtoList); } else { bladeFile = export(superAlarms, workstationVO, vo.getEnums()); } return bladeFile; } private BladeFile exportShift(List superAlarms, WorkstationVO workstationVO, List shiftInfoDtoList) { Map> mapGroupByShift = superAlarms.stream().collect(Collectors.groupingBy(s -> { return s.getFactoryDate() + "-" + s.getShiftIndex(); })); List alarmDataSheetExcelList = new ArrayList<>(); String shift = MessageUtils.message("mdc.excel.alarm.shift", new Object[0]); Map shiftIndexToName = shiftInfoDtoList.stream().collect(Collectors.toMap(s2 -> { return s2.getDate().replace("-", "") + "-" + s2.getShiftIndex(); }, (v0) -> { return v0.getIndexName(); })); Map> result = new LinkedHashMap<>(); mapGroupByShift.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())).forEachOrdered(x -> { List list = result.put(x.getKey(), x.getValue()); }); result.forEach((k, v) -> { Map> mapGroupByAlarmCodeAndMessage = v.stream().collect(Collectors.groupingBy(s3 -> { return s3.getCode() + "-" + s3.getMessage(); })); mapGroupByAlarmCodeAndMessage.forEach((key, value) -> { String alarmMessage; AlarmDataSheetExcel alarmDataSheetExcel = new AlarmDataSheetExcel(); String[] split = k.split("-"); alarmDataSheetExcel.setDate(LocalDateTimeUtils.formatTimeLocalDate(LocalDate.parse(split[0], DateTimeFormatter.ofPattern("yyyyMMdd")), DateConstant.PATTERN_DATE)); String indexName = (String) shiftIndexToName.get(k); alarmDataSheetExcel.setShift(Func.isNotEmpty(indexName) ? indexName : StrUtil.format("{}{}", new Object[]{shift, split[1]})); alarmDataSheetExcel.setWorkstationCode(workstationVO.getCode()); alarmDataSheetExcel.setWorkstationName(workstationVO.getName()); alarmDataSheetExcel.setWorkstationGroup(workstationVO.getGroupName()); String[] splitByMessage = key.split("-"); String alarmCode = splitByMessage[0]; if (splitByMessage.length > this.LENGTH_1.intValue()) { alarmMessage = splitByMessage[1]; } else { alarmMessage = ""; } alarmDataSheetExcel.setAlarmCode(alarmCode); alarmDataSheetExcel.setAlarmMsg(alarmMessage); alarmDataSheetExcel.setCount(String.valueOf(value.size())); alarmDataSheetExcelList.add(alarmDataSheetExcel); }); }); String fileName = String.format("%s-%s.xlsx", ExcelConstant.ALARM, DateUtil.time()); MultipartFile multipartFile = ExcelUtil.exportToMultipartFile(fileName, ExcelConstant.ALARM, alarmDataSheetExcelList, AlarmDataSheetExcel.class); return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile); } private BladeFile export(List superAlarms, WorkstationVO workstationVO, StatisticalMethodEnum statisticalMethodEnum) { Map> map; String the = MessageUtils.message("mdc.efficiency.analysis.the", new Object[0]); String week = MessageUtils.message("mdc.efficiency.analysis.week", new Object[0]); if (statisticalMethodEnum.equals(StatisticalMethodEnum.DAY)) { map = superAlarms.stream().collect(Collectors.groupingBy(s -> { return String.valueOf(s.getFactoryDate()); })); } else if (statisticalMethodEnum.equals(StatisticalMethodEnum.WEEK)) { map = superAlarms.stream().collect(Collectors.groupingBy(s2 -> { return s2.getFactoryYear() + "-" + s2.getFactoryWeek(); })); } else { map = superAlarms.stream().collect(Collectors.groupingBy(s3 -> { return s3.getFactoryYear() + "-" + s3.getFactoryMonth(); })); } List alarmDataSheetExcelForOtherList = new ArrayList<>(); Map> result = new LinkedHashMap<>(); map.entrySet().stream().sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())).forEachOrdered(x -> { List list = (List) result.put(x.getKey(), x.getValue()); }); result.forEach((k, v) -> { Map> mapGroupByAlarmCodeAndMessage = v.stream().collect(Collectors.groupingBy(s4 -> { return s4.getCode() + "-" + s4.getMessage(); })); mapGroupByAlarmCodeAndMessage.forEach((key, value) -> { String alarmMessage; String[] split = k.split("-"); AlarmDataSheetExcelForOther alarmDataSheetExcelForOther = new AlarmDataSheetExcelForOther(); if (statisticalMethodEnum.equals(StatisticalMethodEnum.DAY)) { alarmDataSheetExcelForOther.setDate(LocalDateTimeUtils.formatTimeLocalDate(LocalDate.parse(k, DateTimeFormatter.ofPattern("yyyyMMdd")), DateConstant.PATTERN_DATE)); } else if (statisticalMethodEnum.equals(StatisticalMethodEnum.WEEK)) { alarmDataSheetExcelForOther.setDate(StrUtil.format("{}-{}", new Object[]{split[0], StrUtil.format("{}{}{}", new Object[]{the, split[1], week})})); } else { alarmDataSheetExcelForOther.setDate(StrUtil.format("{}-{}", new Object[]{split[0], split[1]})); } alarmDataSheetExcelForOther.setWorkstationCode(workstationVO.getCode()); alarmDataSheetExcelForOther.setWorkstationName(workstationVO.getName()); alarmDataSheetExcelForOther.setWorkstationGroup(workstationVO.getGroupName()); String[] splitByMessage = key.split("-"); String alarmCode = splitByMessage[0]; if (splitByMessage.length > this.LENGTH_1.intValue()) { alarmMessage = splitByMessage[1]; } else { alarmMessage = ""; } alarmDataSheetExcelForOther.setAlarmCode(alarmCode); alarmDataSheetExcelForOther.setAlarmMsg(alarmMessage); alarmDataSheetExcelForOther.setCount(String.valueOf(value.size())); alarmDataSheetExcelForOtherList.add(alarmDataSheetExcelForOther); }); }); String fileName = String.format("%s-%s.xlsx", ExcelConstant.ALARM, DateUtil.time()); MultipartFile multipartFile = ExcelUtil.exportToMultipartFile(fileName, ExcelConstant.ALARM, alarmDataSheetExcelForOtherList, AlarmDataSheetExcelForOther.class); return this.ossBuilder.tempTemplate().putFile(multipartFile.getOriginalFilename(), multipartFile); } @Override // org.springblade.modules.mdc.service.IAlarmService public AlarmSummaryGraphVO summaryGraph(AlarmShiftSearchVO vo) { if (Func.isNull(vo.getWorkstationId())) { return new AlarmSummaryGraphVO(); } List alarms = this.alarmMapper.queryAlarm(null, vo, DateUtil.format(vo.getQueryTime(), "yyyyMMdd")); Map map = FilterOffUtils.filterOffDay(alarms, OpenTypeEnums.ALARM_ANALYSIS).stream().collect(Collectors.groupingBy((v0) -> { return v0.getCode(); }, Collectors.reducing(0, e -> { return 1; }, (v0, v1) -> { return Integer.sum(v0, v1); }))); return AlarmSummaryGraphVO.builder().res(map).build(); } @Override // org.springblade.modules.mdc.service.IAlarmService public List listShiftAlarm(AlarmShiftSearchVO vo) { String shift = MessageUtils.message("mdc.excel.alarm.shift", new Object[0]); List shiftInfoDtoList = this.shiftDetailService.getShiftInfoDtoList(Lists.newArrayList(new Long[]{vo.getWorkstationId()}), vo.getQueryTime(), vo.getQueryTime()); if (Func.isEmpty(shiftInfoDtoList)) { return new ArrayList(); } return shiftInfoDtoList.stream().map(shiftInfoDTO -> { AlarmDataSheetShiftVO alarmDataSheetShiftVO = new AlarmDataSheetShiftVO(); vo.setShiftIndex(shiftInfoDTO.getShiftIndex()); Query query = new Query(); query.setSize(-1); query.setCurrent(1); List alarmDataSheetVOList = pageAlarm(query, vo).getRecords(); alarmDataSheetShiftVO.setShiftIndex(shiftInfoDTO.getShiftIndex()); alarmDataSheetShiftVO.setShiftName(Func.isNotEmpty(shiftInfoDTO.getIndexName()) ? shiftInfoDTO.getIndexName() : StrUtil.format("{}{}", new Object[]{shift, shiftInfoDTO.getShiftIndex()})); alarmDataSheetShiftVO.setAlarmDataSheetVOList(alarmDataSheetVOList); return alarmDataSheetShiftVO; }).collect(Collectors.toList()); } @Override // org.springblade.modules.mdc.service.IAlarmService public List summaryShiftGraph(AlarmShiftSearchVO vo) { String shift = MessageUtils.message("mdc.excel.alarm.shift", new Object[0]); List shiftInfoDtoList = this.shiftDetailService.getShiftInfoDtoList(Lists.newArrayList(new Long[]{vo.getWorkstationId()}), vo.getQueryTime(), vo.getQueryTime()); if (Func.isEmpty(shiftInfoDtoList)) { return new ArrayList<>(); } return shiftInfoDtoList.stream().map(shiftInfoDTO -> { AlarmSummaryGraphShiftVO alarmSummaryGraphShiftVO = new AlarmSummaryGraphShiftVO(); alarmSummaryGraphShiftVO.setShiftIndex(shiftInfoDTO.getShiftIndex()); alarmSummaryGraphShiftVO.setShiftIndexName(Func.isNotEmpty(shiftInfoDTO.getIndexName()) ? shiftInfoDTO.getIndexName() : StrUtil.format("{}{}", new Object[]{shift, shiftInfoDTO.getShiftIndex()})); vo.setShiftIndex(shiftInfoDTO.getShiftIndex()); AlarmSummaryGraphVO alarmSummaryGraphVO = summaryGraph(vo); alarmSummaryGraphShiftVO.setRes(alarmSummaryGraphVO.getRes()); return alarmSummaryGraphShiftVO; }).collect(Collectors.toList()); } private List getDayData(AlarmShiftSearchExcelVO vo, WorkstationVO workstationVO) { List list = this.alarmMapper.selectAllAlarm(vo, DateUtil.format(vo.getStartTime(), "yyyyMMdd"), DateUtil.format(vo.getEndTime(), "yyyyMMdd"), null, null); List tempList = new ArrayList<>(); Set set = new HashSet<>(); list.forEach(i -> { if (!set.contains(i.getCode() + i.getMessage() + i.getFactoryDate())) { tempList.add(i); set.add(i.getCode() + i.getMessage() + i.getFactoryDate()); } }); Map> countMap = tempList.stream().collect(Collectors.groupingBy(c -> { return c.getFactoryDate() + c.getCode(); })); List result = new ArrayList<>(); for (Map.Entry> entry : countMap.entrySet()) { if (CollectionUtil.isNotEmpty(entry.getValue())) { AlarmDataSheetExcelForOther alarmDataSheetExcelForOther = new AlarmDataSheetExcelForOther(); LocalDate localDate = LocalDateUtil.stringToLocalDate(String.valueOf(entry.getValue().get(0).getFactoryDate()), "yyyyMMdd"); alarmDataSheetExcelForOther.setDate(localDate.format(DateTimeFormatter.ofPattern(DateConstant.PATTERN_DATE))); alarmDataSheetExcelForOther.setWorkstationGroup(workstationVO.getGroupName()); alarmDataSheetExcelForOther.setWorkstationCode(workstationVO.getCode()); alarmDataSheetExcelForOther.setWorkstationName(workstationVO.getName()); alarmDataSheetExcelForOther.setAlarmCode(entry.getValue().get(0).getCode()); alarmDataSheetExcelForOther.setAlarmMsg(entry.getValue().get(0).getMessage()); alarmDataSheetExcelForOther.setCount(String.valueOf(entry.getValue().size())); result.add(alarmDataSheetExcelForOther); } } return result; } /* JADX WARN: Multi-variable type inference failed */ private List getMonthOrWeekDate(Map> map, AlarmShiftSearchExcelVO vo, WorkstationVO workstationVO) { List list = new ArrayList<>(); for (Map.Entry> entry : map.entrySet()) { list.addAll(this.alarmMapper.selectAllAlarm(vo, null, null, entry.getKey(), entry.getValue())); } List tempList = new ArrayList<>(); Set set = new HashSet<>(); list.forEach(i -> { if (!set.contains(i.getCode() + i.getMessage() + i.getFactoryDate())) { tempList.add(i); set.add(i.getCode() + i.getMessage() + i.getFactoryDate()); } }); Map> countMap = new HashMap<>(); if (StatisticalMethodEnum.WEEK.equals(vo.getEnums())) { countMap = tempList.stream().collect(Collectors.groupingBy(c -> { return c.getFactoryYear() + "" + c.getFactoryWeek() + c.getCode(); })); } if (StatisticalMethodEnum.MONTH.equals(vo.getEnums())) { countMap = tempList.stream().collect(Collectors.groupingBy(c2 -> { return c2.getFactoryYear() + "" + c2.getFactoryMonth() + c2.getCode(); })); } List result = new ArrayList<>(); for (Map.Entry> entry2 : countMap.entrySet()) { if (CollectionUtil.isNotEmpty(entry2.getValue())) { AlarmDataSheetExcelForOther alarmDataSheetExcelForOther = new AlarmDataSheetExcelForOther(); if (StatisticalMethodEnum.WEEK.equals(vo.getEnums())) { alarmDataSheetExcelForOther.setDate(StrUtil.format("{}-第{}周", new Object[]{entry2.getValue().get(0).getFactoryYear(), entry2.getValue().get(0).getFactoryWeek()})); } if (StatisticalMethodEnum.MONTH.equals(vo.getEnums())) { alarmDataSheetExcelForOther.setDate(StrUtil.format("{}-{}", new Object[]{entry2.getValue().get(0).getFactoryYear(), entry2.getValue().get(0).getFactoryMonth()})); } alarmDataSheetExcelForOther.setWorkstationGroup(workstationVO.getGroupName()); alarmDataSheetExcelForOther.setWorkstationCode(workstationVO.getCode()); alarmDataSheetExcelForOther.setWorkstationName(workstationVO.getName()); alarmDataSheetExcelForOther.setAlarmCode(entry2.getValue().get(0).getCode()); alarmDataSheetExcelForOther.setAlarmMsg(entry2.getValue().get(0).getMessage()); alarmDataSheetExcelForOther.setCount(String.valueOf(entry2.getValue().size())); result.add(alarmDataSheetExcelForOther); } } return result; } Map> organizeQuery(SplitFilterListVO splitFilterListVO, AlarmShiftSearchExcelVO vo) { Map> map = new HashMap<>(); splitFilterListVO.getItems().forEach(i -> { if (map.containsKey(i.getYear())) { if (StatisticalMethodEnum.WEEK.equals(vo.getEnums())) { List week = map.get(i.getYear()); week.add(i.getWeek()); map.put(i.getYear(), week); } if (StatisticalMethodEnum.MONTH.equals(vo.getEnums())) { List month = map.get(i.getYear()); month.add(i.getMonth()); map.put(i.getYear(), month); return; } return; } if (StatisticalMethodEnum.WEEK.equals(vo.getEnums())) { List arrayList = new ArrayList<>(); arrayList.add(i.getWeek()); map.put(i.getYear(), arrayList); } if (StatisticalMethodEnum.MONTH.equals(vo.getEnums())) { List arrayList2 = new ArrayList<>(); arrayList2.add(i.getMonth()); map.put(i.getYear(), arrayList2); } }); return map; } }