package org.jeecg.modules.mdc.subcontrol.service.impl; import org.apache.commons.lang.StringUtils; import org.checkerframework.checker.units.qual.A; import org.jeecg.modules.mdc.entity.EquipmentLog; import org.jeecg.modules.mdc.service.*; import org.jeecg.modules.mdc.subcontrol.service.MdcBigScreenService; import org.jeecg.modules.mdc.subcontrol.vo.LastWeekDataRankingVo; import org.jeecg.modules.mdc.subcontrol.vo.LastWeekDataVo; import org.jeecg.modules.mdc.subcontrol.vo.MdcEquipmentOpVo; import org.jeecg.modules.mdc.util.DateUtils; import org.jeecg.modules.mdc.vo.MdcEquipmentVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.xml.crypto.Data; import java.math.BigDecimal; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; import java.util.*; import java.util.stream.Collectors; /** * @author: LiuS * @create: 2023-01-05 11:42 */ @Service public class MdcBigScreenServiceImpl implements MdcBigScreenService { @Resource private IMdcEquipmentService mdcEquipmentService; @Resource private IEquipmentStatisticalInfoService equipmentStatisticalInfoService; @Autowired private IMdcEquipmentStatisticalShiftInfoService mdcEquipmentStatisticalShiftInfoService; @Autowired private IMdcEquipmentStatisticalInfoService mdcEquipmentStatisticalInfoService; @Resource private IEquipmentLogService equipmentLogService; /** * 根据厂区id获取上周数据 */ @Override public Map findDataByLastWeek() { Map result = new HashMap<>(); List openingRateList = new ArrayList<>(); List processingRateList = new ArrayList<>(); List shiftRateList = new ArrayList<>(); //获取上周周一日期和周六日期 LocalDate today = LocalDate.now().minusDays(7L); LocalDate monday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); LocalDate saturday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY)); List dates = getMiddleDate(monday, saturday); List equipmentIdList = new ArrayList<>(); //根据厂区id获取厂区设备信息 equipmentIdList = mdcEquipmentService.listEquipmentId(null); //上周利用率和开机率数据 List lastWeekDataVos = equipmentStatisticalInfoService.findDataForBigScreen(equipmentIdList, monday.format(DateTimeFormatter.BASIC_ISO_DATE), saturday.format(DateTimeFormatter.BASIC_ISO_DATE)); if (lastWeekDataVos.isEmpty()) { for (int i = 0; i < dates.size(); i++) { openingRateList.add(new BigDecimal("0")); processingRateList.add(new BigDecimal("0")); } } else { for (int i = 0; i < dates.size(); i++) { Integer size = equipmentStatisticalInfoService.findEquipmentId(equipmentIdList, dates.get(i)); if (dates.get(i).equals(lastWeekDataVos.get(i).getTheDate())) { openingRateList.add(new BigDecimal(String.format("%.2f", (Float.parseFloat(lastWeekDataVos.get(i).getOpeningLong()) / (86400 * size) * 100)))); processingRateList.add(new BigDecimal(String.format("%.2f", (Float.parseFloat(lastWeekDataVos.get(i).getProcessingLong()) / (86400 * size) * 100)))); } else { openingRateList.add(new BigDecimal("0")); processingRateList.add(new BigDecimal("0")); } } } //上周班次利用率数据 List lastWeekDataList = mdcEquipmentStatisticalShiftInfoService. findDataForBigScreen(equipmentIdList, monday.toString(), saturday.toString()); List middleDates = getMiddleDates(monday, saturday); if (lastWeekDataList.isEmpty()) { for (int i = 0; i < middleDates.size(); i++) { shiftRateList.add(new BigDecimal("0")); } } else { Map map = new HashMap<>(); for (LastWeekDataVo s : lastWeekDataList) { map.put(s.getTheDate(),s); } for (int i = 0; i < middleDates.size(); i++) { if (i < lastWeekDataList.size()) { if ( map.containsKey(middleDates.get(i))) { LastWeekDataVo vo = map.get(middleDates.get(i)); if (!"0".equals(vo.getTotalLong())) { shiftRateList.add(new BigDecimal(vo.getProcessingLong()). divide(new BigDecimal(vo.getTotalLong()), 4, BigDecimal.ROUND_HALF_UP) .setScale(4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))); } else { shiftRateList.add(new BigDecimal("0")); } } } else { shiftRateList.add(new BigDecimal("0")); } } } result.put("openingRateList", openingRateList); result.put("processingRateList", processingRateList); result.put("shiftRateList", shiftRateList); return result; } /** * 根据厂区id获取上周运行效率排名 */ @Override public List findDataRankingByLastWeek() { List voList = new ArrayList<>(); //获取上周周一日期和周六日期 LocalDate today = LocalDate.now(); String monday = today.minusDays(7).format(DateTimeFormatter.BASIC_ISO_DATE); String saturday = today.minusDays(1).format(DateTimeFormatter.BASIC_ISO_DATE); //根据id获取设备id集合 List equipmentIdList = mdcEquipmentService.listEquipmentId(null); if (!equipmentIdList.isEmpty()) { List lastWeekDataVos = equipmentStatisticalInfoService.findDataRankingForBigScreen(equipmentIdList, monday, saturday); if (!lastWeekDataVos.isEmpty()) { for (LastWeekDataVo lastWeekDataVo : lastWeekDataVos) { Long size = mdcEquipmentStatisticalInfoService.selectSize(lastWeekDataVo.getEquipment(), monday, saturday); if (StringUtils.isNotEmpty(lastWeekDataVo.getProcessingLong()) && Float.parseFloat(lastWeekDataVo.getProcessingLong()) >0) { lastWeekDataVo.setUtilization(new BigDecimal(String.format("%.2f", (Float.parseFloat(lastWeekDataVo.getProcessingLong()) / (86400 * size) * 100)))); } else { lastWeekDataVo.setUtilization(new BigDecimal(0)); } } List collect = lastWeekDataVos.stream().sorted(Comparator.comparing(LastWeekDataVo::getUtilization).reversed()).collect(Collectors.toList()); if (collect.size() > 6) { List topList = collect.subList(0, 6); for (LastWeekDataVo vo : topList) { LastWeekDataRankingVo rankingVo = new LastWeekDataRankingVo(); rankingVo.setTopName(vo.getEquipment()); rankingVo.setTopRate(vo.getUtilization().toString()); voList.add(rankingVo); } } else { for (LastWeekDataVo vo : collect) { LastWeekDataRankingVo rankingVo = new LastWeekDataRankingVo(); rankingVo.setTopName(vo.getEquipment()); rankingVo.setTopRate(vo.getUtilization().toString()); voList.add(rankingVo); } } } } return voList; } /** * 获取设备当前状态 */ @Override public List findEquipmentStatus() { List result = new ArrayList<>(); //根据id获取设备id集合 List equipmentIdList = mdcEquipmentService.listEquipmentId(null); if (!equipmentIdList.isEmpty()) { List logs = equipmentLogService.getEquipmentStatusList(equipmentIdList); //根据设备id集合查询设备实时状态 for (EquipmentLog equipmentId : logs) { if (equipmentId != null) { MdcEquipmentOpVo vo = new MdcEquipmentOpVo(); vo.setEquipmentId(equipmentId.getEquipmentId()); if (equipmentId.getOporation() != null) { switch (equipmentId.getOporation()) { case 1: vo.setOporation(1); vo.setOporationDict("待机"); break; case 2: vo.setOporationDict("待机"); vo.setOporation(2); break; case 3: vo.setOporationDict("运行"); vo.setOporation(3); break; case 22: vo.setOporationDict("报警"); vo.setOporation(22); break; default: vo.setOporationDict("关机"); vo.setOporation(0); break; } } else { vo.setOporation(0); vo.setOporationDict("关机"); } result.add(vo); } else { MdcEquipmentOpVo vo = new MdcEquipmentOpVo(); vo.setOporation(0); vo.setOporationDict("关机"); result.add(vo); } } } return result; } /** * 获取全厂当前设备状态信息 */ @Override public Map findAllEquipmentStatus() { int runCount = 0; int standbyCount = 0; int alarmCount = 0; int offCount = 0; List equipmentIdList = mdcEquipmentService.listEquipmentId(null); List equipmentLogs = new ArrayList<>(); for (String equipmentId : equipmentIdList) { EquipmentLog equipmentLog = equipmentLogService.selectEquipmentOporation(equipmentId); if (equipmentLog != null) { equipmentLogs.add(equipmentLog); } else { offCount += 1; } } if (!equipmentLogs.isEmpty()) { for (EquipmentLog equipmentLog : equipmentLogs) { if (equipmentLog.getOporation() != null) { switch (equipmentLog.getOporation()) { case 1: case 2: standbyCount += 1; break; case 3: runCount += 1; break; case 22: alarmCount += 1; break; default: offCount += 1; break; } } else { offCount += 1; } } } Map result = new HashMap<>(); result.put("runCount", runCount); result.put("standbyCount", standbyCount); result.put("alarmCount", alarmCount); result.put("offCount", offCount); return result; } /** * 20230101 * * @param begin 开始日期 * @param end 结束日期 * @return 开始与结束之间的所有日期,包括起止 */ public static List getMiddleDate(LocalDate begin, LocalDate end) { List localDateList = new ArrayList<>(); long length = end.toEpochDay() - begin.toEpochDay(); for (long i = length; i >= 0; i--) { localDateList.add(end.minusDays(i).format(DateTimeFormatter.BASIC_ISO_DATE)); } return localDateList; } /** * 2023-01-01 * * @param begin 开始日期 * @param end 结束日期 * @return 开始与结束之间的所有日期,包括起止 */ public static List getMiddleDates(LocalDate begin, LocalDate end) { List localDateList = new ArrayList<>(); long length = end.toEpochDay() - begin.toEpochDay(); for (long i = length; i >= 0; i--) { String date = end.minusDays(i).toString().replace("-",""); localDateList.add(date); } return localDateList; } }