package com.qianwen.smartman.modules.report.service.impl; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.util.NumberUtil; import cn.hutool.json.JSONObject; import com.baomidou.mybatisplus.core.metadata.IPage; import java.sql.Timestamp; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; import org.jetbrains.annotations.NotNull; import com.qianwen.smartman.common.constant.DateConstant; import com.qianwen.smartman.common.utils.LocalDateTimeUtils; import com.qianwen.smartman.common.utils.LocalDateUtil; import com.qianwen.core.tool.utils.Func; import com.qianwen.smartman.modules.cps.dto.ShiftInfoDTO; import com.qianwen.smartman.modules.cps.mapper.CalendarMapper; import com.qianwen.smartman.modules.cps.mapper.WorkstationMapper; import com.qianwen.smartman.modules.mdc.dto.IntervalDateDto; 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.mapper.SuperAggregateStateMapper; import com.qianwen.smartman.modules.mdc.utils.EifficiencyUtils; import com.qianwen.smartman.modules.report.dto.WorkstationDataDTO; import com.qianwen.smartman.modules.report.dto.WorkstationShiftDTO; import com.qianwen.smartman.modules.report.mapper.CommonMapper; import com.qianwen.smartman.modules.report.service.IWorkstationEfficiencyService; import com.qianwen.smartman.modules.report.utils.CommonUtil; import com.qianwen.smartman.modules.report.utils.DateCycleUtil; import com.qianwen.smartman.modules.report.utils.ResultUtil; import com.qianwen.smartman.modules.report.vo.WorkstationEfficiencyQueryVO; import com.qianwen.smartman.modules.report.vo.WorkstationEfficiencySelectVO; import com.qianwen.smartman.modules.report.vo.WorkstationEfficiencyVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service /* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/report/service/impl/WorkstationEfficiencyServiceImpl.class */ public class WorkstationEfficiencyServiceImpl implements IWorkstationEfficiencyService { @Autowired private WorkstationMapper workstationMapper; @Autowired private SuperAggregateStateMapper superAggregateStateMapper; @Autowired private CalendarMapper calendarMapper; @Autowired private CommonMapper commonMapper; private static final String TIME_CONDITION = "time"; private static final String TABLE_NAME = "aggregate_state"; @Override // org.springblade.modules.report.service.IWorkstationEfficiencyService public JSONObject byTime(WorkstationEfficiencySelectVO workstationEfficiencySelectVO) { workstationEfficiencySelectVO.setStartTime(DateCycleUtil.setTimeCondition(TIME_CONDITION, Boolean.TRUE)); workstationEfficiencySelectVO.setEndTime(DateCycleUtil.setTimeCondition(TIME_CONDITION, Boolean.FALSE)); DateTime parse = DateUtil.parse(workstationEfficiencySelectVO.getStartTime(), DateConstant.PATTERN_DATE); DateTime parse2 = DateUtil.parse(workstationEfficiencySelectVO.getEndTime(), DateConstant.PATTERN_DATE); String workstationKeyWord = workstationEfficiencySelectVO.getName(); List workstationIdsParam = workstationEfficiencySelectVO.getWorkstationIds(); Integer pageSize = workstationEfficiencySelectVO.getPageSize(); Integer pageNo = workstationEfficiencySelectVO.getPageNo(); Integer dateCycle = workstationEfficiencySelectVO.getDateCycle(); List productivityTypes = workstationEfficiencySelectVO.getProductivityTypes(); if (Func.isEmpty(productivityTypes)) { productivityTypes = ProductivityTypeEnum.codes(); } IPage workstationVOPage = this.workstationMapper.getWorkstationData(CommonUtil.getIPage(WorkstationDataDTO.class, pageSize, pageNo, this.workstationMapper), workstationKeyWord, workstationIdsParam); long total = workstationVOPage.getTotal(); if (Func.isEmpty(workstationVOPage.getRecords())) { return ResultUtil.dataToJsonObject(null, Long.valueOf(total), pageSize); } CommonUtil.fillWorkStationGroup(workstationVOPage); List workstationVOS = workstationVOPage.getRecords(); List workstationIds = workstationVOS.stream().map((v0) -> { return v0.getWorkstationId(); }).distinct().collect(Collectors.toList()); List superAggregateStates = getStatusDataByFactoryDate(workstationIds, LocalDateTimeUtil.format(LocalDateUtil.dateToLocalDate(parse), "yyyyMMdd"), LocalDateTimeUtil.format(LocalDateUtil.dateToLocalDate(parse2), "yyyyMMdd")); List vos = buildByTimeVO(workstationVOS, superAggregateStates, productivityTypes, dateCycle, parse, parse2); return ResultUtil.dataToJsonObject(vos, Long.valueOf(total), pageSize); } @Override // org.springblade.modules.report.service.IWorkstationEfficiencyService public JSONObject byShift(@NotNull WorkstationEfficiencySelectVO workstationEfficiencySelectVO) { IPage iPage; workstationEfficiencySelectVO.setStartTime(DateCycleUtil.setTimeCondition(TIME_CONDITION, Boolean.TRUE)); workstationEfficiencySelectVO.setEndTime(DateCycleUtil.setTimeCondition(TIME_CONDITION, Boolean.FALSE)); String startTime = workstationEfficiencySelectVO.getStartTime(); String endTime = workstationEfficiencySelectVO.getEndTime(); List productivityTypes = workstationEfficiencySelectVO.getProductivityTypes(); if (Func.isEmpty(productivityTypes)) { productivityTypes = ProductivityTypeEnum.codes(); } String workstationKeyWord = workstationEfficiencySelectVO.getName(); List workstationIdsParam = workstationEfficiencySelectVO.getWorkstationIds(); String shiftIndexKeyWord = workstationEfficiencySelectVO.getShiftIndex(); Integer pageNo = workstationEfficiencySelectVO.getPageNo(); Integer pageSize = workstationEfficiencySelectVO.getPageSize(); IPage iPage2 = CommonUtil.getIPage(WorkstationDataDTO.class, pageSize, pageNo, this.workstationMapper); StatisticalMethodEnum anEnum = StatisticalMethodEnum.DAY; List fillDateCol = DateCycleUtil.fillDateCol(startTime, endTime, anEnum); if (Func.isEmpty(shiftIndexKeyWord)) { iPage = this.workstationMapper.getWorkstationData(iPage2, workstationKeyWord, workstationIdsParam); } else { iPage = this.workstationMapper.getWorkstationDataWithIndexName(iPage2, workstationKeyWord, shiftIndexKeyWord, startTime, endTime, workstationIdsParam); } CommonUtil.fillWorkStationGroup(iPage); List workstationData = iPage.getRecords(); if (Func.isEmpty(iPage.getRecords())) { return ResultUtil.dataToJsonObject(null, Long.valueOf(iPage.getTotal()), pageSize); } List ids = (List) workstationData.parallelStream().map((v0) -> { return v0.getWorkstationId(); }).collect(Collectors.toList()); List shiftInfoDtoList = this.calendarMapper.listShiftInfo(ids, LocalDate.parse(startTime), LocalDate.parse(endTime)); List workstationShifts = CommonUtil.dataMerge(workstationData, shiftInfoDtoList); List filter = (List) workstationShifts.parallelStream().filter(CommonUtil.conditionFilter(shiftIndexKeyWord, null)).collect(Collectors.toList()); List result = new ArrayList<>(); List finalProductivityTypes = productivityTypes; fillDateCol.forEach(f -> { filter.forEach(shift -> { Iterator it = finalProductivityTypes.iterator(); while (it.hasNext()) { Integer code = (Integer) it.next(); WorkstationEfficiencyVO resultVO = new WorkstationEfficiencyVO(); resultVO.setWorkstationId(shift.getWorkstationId()); resultVO.setWorkstationCode(shift.getWorkstationCode()); resultVO.setWorkstationName(shift.getWorkstationName()); resultVO.setWorkstationNameGroupName(shift.getWorkstationGroup()); resultVO.setShiftIndex(shift.getShiftIndex()); resultVO.setShiftName(shift.getShiftName()); resultVO.setTime(f); String percentFormat = toPercent(Double.valueOf(0.0d)); resultVO.setValue(percentFormat); resultVO.setType(ProductivityTypeEnum.byCode(code.intValue()).getMessage()); result.add(resultVO); } }); }); String start = DateCycleUtil.getDateByDateCycle(startTime, anEnum, Boolean.TRUE); String end = DateCycleUtil.getDateByDateCycle(endTime, anEnum, Boolean.FALSE); WorkstationEfficiencyQueryVO workstationEfficiencyQueryVO = new WorkstationEfficiencyQueryVO(); workstationEfficiencyQueryVO.setStartTime(start); workstationEfficiencyQueryVO.setEndTime(end); workstationEfficiencyQueryVO.setWorkstationIds(ids); List superAggregateStates = getStatusDataByFactoryDate(ids, start, end); Map> workstationAggregateStateMap = superAggregateStates.parallelStream().collect(Collectors.groupingBy(s -> { return s.getWorkstationId() + "_" + s.getShiftIndex(); })); Map voMap = (Map) result.parallelStream().collect(LinkedHashMap::new, (map, c) -> { WorkstationEfficiencyVO workstationEfficiencyVO = (WorkstationEfficiencyVO) map.put(c.getWorkstationId() + "_" + c.getShiftIndex() + "_" + c.getTime() + "_" + c.getType(), c); }, (v0, v1) -> { v0.putAll(v1); }); resultCount(workstationAggregateStateMap, anEnum, voMap, productivityTypes); return ResultUtil.dataToJsonObject(voMap.values(), Long.valueOf(iPage.getTotal()), pageSize); } /* JADX WARN: Multi-variable type inference failed */ private List buildByTimeVO(List workstationVOS, List superAggregateStates, List productivityTypes, Integer dateCycle, Date startTime, Date endTime) { Map workstationVOMap = (Map) workstationVOS.stream().collect(Collectors.toMap((v0) -> { return v0.getWorkstationId(); }, item -> { return item; })); Map> yearListMap = groupYear(startTime, endTime, dateCycle); List vos = new ArrayList<>(); Map> workStateMap = (Map) superAggregateStates.stream().collect(Collectors.groupingBy((v0) -> { return v0.getWorkstationId(); })); Set yearSet = yearListMap.keySet(); Set workstationSet = workstationVOMap.keySet(); for (Long id : workstationSet) { WorkstationDataDTO workstationVO = workstationVOMap.get(id); Map> yearStateList = new HashMap<>(); if (workStateMap.containsKey(id)) { List workStateList = workStateMap.get(id); yearStateList = (Map) workStateList.stream().collect(Collectors.groupingBy((v0) -> { return v0.getFactoryYear(); })); } for (Integer year : yearSet) { Map> stateTime = new HashMap<>(); if (yearStateList.containsKey(year)) { List stateYear = yearStateList.get(year); if (Objects.equals(dateCycle, StatisticalMethodEnum.MONTH.getCode())) { stateTime = stateYear.stream().collect(Collectors.groupingBy((v0) -> { return v0.getFactoryMonth(); })); } else if (Objects.equals(dateCycle, StatisticalMethodEnum.WEEK.getCode())) { stateTime = stateYear.stream().collect(Collectors.groupingBy((v0) -> { return v0.getFactoryWeek(); })); } else if (Objects.equals(dateCycle, StatisticalMethodEnum.DAY.getCode())) { stateTime = stateYear.stream().collect(Collectors.groupingBy((v0) -> { return v0.getFactoryDate(); })); } } List times = yearListMap.get(year); for (Integer time : times) { List states = stateTime.get(time); for (Integer productivityType : productivityTypes) { WorkstationEfficiencyVO workstationEfficiencyVO = buildVO(workstationVO, time, dateCycle, year, states, productivityType); vos.add(workstationEfficiencyVO); } } } } return vos; } private void resultCount(Map> workstationAggregateStateMap, StatisticalMethodEnum anEnum, Map voMap, List productivityTypes) { workstationAggregateStateMap.forEach((key, list) -> { Map> aggregateState = list.parallelStream().collect(Collectors.groupingBy(groupKey(anEnum))); aggregateState.forEach((k, v) -> { ProductivityTypeEnum[] values = ProductivityTypeEnum.values(); for (ProductivityTypeEnum productivityTypeEnum : values) { WorkstationEfficiencyVO vo = (WorkstationEfficiencyVO) voMap.get(key + "_" + k + "_" + productivityTypeEnum.getMessage()); if (!Objects.isNull(vo)) { Double percent = EifficiencyUtils.openShiftTypeCalculationResults(v, productivityTypeEnum); String percentFormat = toPercent(percent); vo.setValue(percentFormat); } } }); }); } private WorkstationEfficiencyVO buildVO(WorkstationDataDTO workstationVO, Integer time, Integer dateCycle, Integer year, List itemValue, Integer productivityType) { String name = workstationVO.getWorkstationName(); String code = workstationVO.getWorkstationCode(); String groupName = workstationVO.getWorkstationGroup(); WorkstationEfficiencyVO workstationEfficiencyVO = new WorkstationEfficiencyVO(); workstationEfficiencyVO.setWorkstationCode(code); workstationEfficiencyVO.setWorkstationName(name); workstationEfficiencyVO.setWorkstationNameGroupName(groupName); ProductivityTypeEnum productivityTypeEnum = ProductivityTypeEnum.byCode(productivityType.intValue()); Double percent = Double.valueOf(0.0d); if (Func.isNotEmpty(productivityTypeEnum)) { percent = EifficiencyUtils.openShiftTypeCalculationResults(itemValue, productivityTypeEnum); } String percentFormat = toPercent(percent); workstationEfficiencyVO.setType(productivityTypeEnum.getMessage()); workstationEfficiencyVO.setValue(percentFormat); String timeText = ""; if (Objects.equals(dateCycle, StatisticalMethodEnum.MONTH.getCode())) { if (time.intValue() < 10) { timeText = year + "-0" + time; } else { timeText = year + "-" + time; } } else if (Objects.equals(dateCycle, StatisticalMethodEnum.WEEK.getCode())) { if (time.intValue() < 10) { timeText = year + "-第0" + time + "周"; } else { timeText = year + "-第" + time + "周"; } } else if (Objects.equals(dateCycle, StatisticalMethodEnum.DAY.getCode())) { timeText = DateUtil.format(DateUtil.parse(String.valueOf(time), "yyyyMMdd"), DateConstant.PATTERN_DATE); } workstationEfficiencyVO.setTime(timeText); return workstationEfficiencyVO; } private Map> groupYear(Date startTime, Date endTime, Integer dateCycle) { LocalDate startLocal = LocalDateTimeUtils.dateToLocalDate(startTime); LocalDate endLocal = LocalDateTimeUtils.dateToLocalDate(endTime); HashMap> data = new HashMap<>(); List dateDtos = LocalDateTimeUtils.getIntervalDate(startLocal, endLocal); for (IntervalDateDto intervalDateDto : dateDtos) { if (Objects.equals(dateCycle, StatisticalMethodEnum.MONTH.getCode())) { data.put(intervalDateDto.getYear(), intervalDateDto.getMonthList()); } else if (Objects.equals(dateCycle, StatisticalMethodEnum.WEEK.getCode())) { data.put(intervalDateDto.getYear(), intervalDateDto.getWeekList()); } else if (Objects.equals(dateCycle, StatisticalMethodEnum.DAY.getCode())) { data.put(intervalDateDto.getYear(), intervalDateDto.getDayList().stream().map(item -> { return Integer.valueOf(DateUtil.format(DateUtil.parse(item, DateConstant.PATTERN_DATE), "yyyyMMdd")); }).collect(Collectors.toList())); } } return data; } private Function groupKey(StatisticalMethodEnum statisticalMethodEnum) { switch (statisticalMethodEnum) { case DAY: return superAggregateState -> { return DateCycleUtil.getDateCycle(statisticalMethodEnum, String.valueOf(superAggregateState.getFactoryDate())); }; case WEEK: return superAggregateState2 -> { return DateCycleUtil.getDateCycle(statisticalMethodEnum, String.valueOf(superAggregateState2.getFactoryWeek())); }; case MONTH: return superAggregateState3 -> { return DateCycleUtil.getDateCycle(statisticalMethodEnum, String.valueOf(superAggregateState3.getFactoryMonth())); }; default: return superAggregateState4 -> { return DateCycleUtil.getDateCycle(statisticalMethodEnum, String.valueOf(superAggregateState4.getWorkstationId())); }; } } private String toPercent(Double value) { return NumberUtil.decimalFormat("#.##%", value.doubleValue()); } private List getStatusDataByFactoryDate(List workstationIds, String startDate, String endDate) { List itemTableNames = this.commonMapper.getItemBySuperTable(TABLE_NAME, workstationIds); List superAggregateStates = new ArrayList<>(); for (Long id : workstationIds) { if (itemTableNames.contains("aggregate_state_" + id)) { List statusDataByFactoryDateAndWorkstationId = this.superAggregateStateMapper.getStatusDataByFactoryDateAndWorkstationId(id, startDate, endDate); superAggregateStates.addAll(statusDataByFactoryDateAndWorkstationId); } } Date now = com.qianwen.core.tool.utils.DateUtil.now(); superAggregateStates.forEach(x -> { if (Func.isEmpty(x.getEndTime())) { x.setEndTime(new Timestamp(now.getTime())); } x.setDurationCollect(Long.valueOf(LocalDateTimeUtils.betweenTwoTime(x.getStartTime().toLocalDateTime(), LocalDateTimeUtils.DateToLocalDateTime(x.getEndTime()), ChronoUnit.MILLIS))); }); return superAggregateStates.stream().filter(x2 -> { return x2.getRps().intValue() > 0; }).collect(Collectors.toList()); } }