package org.jeecg.modules.mdc.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.commons.lang.StringUtils; import org.jeecg.modules.mdc.entity.Equipment; import org.jeecg.modules.mdc.entity.MdcEquipmentRunningSection; import org.jeecg.modules.mdc.entity.MdcEquipmentStatisticalShiftInfo; import org.jeecg.modules.mdc.mapper.MdcEquipmentStatisticalShiftInfoMapper; import org.jeecg.modules.mdc.service.IEquipmentService; import org.jeecg.modules.mdc.service.IMdcDeviceCalendarService; import org.jeecg.modules.mdc.service.IMdcEquipmentRunningSectionService; import org.jeecg.modules.mdc.service.IMdcEquipmentStatisticalShiftInfoService; import org.jeecg.modules.mdc.util.DateUtils; import org.jeecg.modules.mdc.vo.MdcDeviceCalendarVo; import org.jeecg.modules.mdc.vo.MdcShiftDateVo; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * @Description: 设备单日班次运行数据表 * @author: LiuS * @create: 2023-07-24 11:19 */ @Service public class MdcEquipmentStatisticalShiftInfoServiceImpl extends ServiceImpl implements IMdcEquipmentStatisticalShiftInfoService { @Resource private IEquipmentService equipmentService; @Resource private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService; @Resource private IMdcDeviceCalendarService mdcDeviceCalendarService; /** * 计算设备单日班次运行数据 * * @param dateTime */ @Override @Transactional(rollbackFor = {Exception.class}) public void runningAllEquipmentShiftStatisticalProcess(String dateTime) { if (StringUtils.isNotBlank(dateTime)) { try { Date initDate = DateUtils.toDate(dateTime, "yyyyMMdd"); if (initDate != null) { this.remove(new LambdaQueryWrapper().eq(MdcEquipmentStatisticalShiftInfo::getTheDate, dateTime)); } } catch (Exception e) { log.error("参数格式不对", e); } } List equipmentList = equipmentService.list(); List result = new ArrayList<>(); for (Equipment equipment : equipmentList) { List equipmentStatisticalShiftInfoList = equipmentShiftStatisticalProcess(equipment, dateTime); if (equipmentStatisticalShiftInfoList != null && !equipmentStatisticalShiftInfoList.isEmpty()) { result.addAll(equipmentStatisticalShiftInfoList); } } if (!result.isEmpty()) { this.saveBatch(result); } } @Transactional(readOnly = true) List equipmentShiftStatisticalProcess(Equipment equipment, String dateTime) { Date initDate = null; //取最后的统计数据 if (StringUtils.isBlank(dateTime)) { MdcEquipmentStatisticalShiftInfo nearestDate = this.baseMapper.getMaxStaticsData(equipment.getEquipmentid()); if (nearestDate != null) { initDate = DateUtils.toDate(nearestDate.getTheDate(), "yyyyMMdd"); initDate = DateUtils.plusTime(initDate, 1); } else { //初次取值 取最早时间 MdcEquipmentRunningSection equipmentRunningSection = mdcEquipmentRunningSectionService.getFirstData(equipment.getEquipmentid()); if (equipmentRunningSection != null) { initDate = equipmentRunningSection.getStartTime(); } } } else { try { initDate = DateUtils.toDate(dateTime, "yyyyMMdd"); initDate = DateUtils.plusTime(initDate, 0); } catch (Exception e) { log.error("参数格式不对", null); return null; } } if (initDate == null) { return null; } Date endDate = DateUtils.plusTime(DateUtils.getNow(), 0); if (!DateUtils.less(initDate, endDate)) { return Collections.emptyList(); } //获取两个时间段的差 List stringDates = DateUtils.getDatesStringList2(initDate, DateUtils.plusTime(endDate, -1)); if (stringDates.isEmpty()) { return Collections.emptyList(); } //查询班制班次信息 Map> listMap = this.mdcDeviceCalendarMap(equipment.getEquipmentid(), stringDates); if (listMap.isEmpty()) { listMap = new HashMap<>(); } List resultList = new ArrayList<>(); for (String stringDate : stringDates) { if (listMap.containsKey(stringDate)) { List mdcDeviceCalendarVos = listMap.get(stringDate); for (MdcDeviceCalendarVo mdcDeviceCalendarVo : mdcDeviceCalendarVos) { //班制班次时间集合 List datesListByMdcDeviceCalendarVo = this.getDatesListByMdcDeviceCalendarVo(mdcDeviceCalendarVo); if (datesListByMdcDeviceCalendarVo == null || datesListByMdcDeviceCalendarVo.isEmpty()) { break; } //合并 MdcEquipmentStatisticalShiftInfo equipmentStatisticalShiftInfo = new MdcEquipmentStatisticalShiftInfo(); equipmentStatisticalShiftInfo.setEquipmentId(equipment.getEquipmentid()); //处理时间 Date date = DateUtils.strToDate(stringDate, DateUtils.STRDATE); equipmentStatisticalShiftInfo.setTheDate(DateUtils.format(date, DateUtils.STRDATE)); equipmentStatisticalShiftInfo.setShiftId(mdcDeviceCalendarVo.getShiftId()); equipmentStatisticalShiftInfo.setShiftSubId(mdcDeviceCalendarVo.getShiftSubId()); equipmentStatisticalShiftInfo.setCreateTime(new Date()); for (MdcShiftDateVo dates : datesListByMdcDeviceCalendarVo) { //处理数据 MdcEquipmentStatisticalShiftInfo shiftInfo = this.selectRunningEquipment(dates, equipment.getEquipmentid()); equipmentStatisticalShiftInfo.setWaitLong(equipmentStatisticalShiftInfo.getWaitLong().add(shiftInfo.getWaitLong())); equipmentStatisticalShiftInfo.setProcessLong(equipmentStatisticalShiftInfo.getProcessLong().add(shiftInfo.getProcessLong())); equipmentStatisticalShiftInfo.setCloseLong(equipmentStatisticalShiftInfo.getCloseLong().add(shiftInfo.getCloseLong())); equipmentStatisticalShiftInfo.setOpenLong(equipmentStatisticalShiftInfo.getOpenLong().add(shiftInfo.getOpenLong())); equipmentStatisticalShiftInfo.setErrorLong(equipmentStatisticalShiftInfo.getErrorLong().add(shiftInfo.getErrorLong())); } } } } return resultList; } /** * 查询某一天某个班次的数据 * * @param dates * @param equipmentid * @return */ private MdcEquipmentStatisticalShiftInfo selectRunningEquipment(MdcShiftDateVo dates, String equipmentid) { if (dates == null || StringUtils.isBlank(equipmentid)) { return null; } //不过滤休班和维修 //获取班次时间段内数据 List sectionList = mdcEquipmentRunningSectionService.listEquipmentRunningSection(equipmentid, dates.getStart(), dates.getEnd()); //查询无数据需要EquipmentLog解析 if (sectionList.isEmpty()) { sectionList = mdcEquipmentRunningSectionService.listRunningSectionFromLog(equipmentid, dates.getStart(), dates.getEnd()); } if (sectionList == null || sectionList.isEmpty()) { sectionList = new ArrayList<>(); } //处理数据 long datesStart = dates.getStart(); long datesEnd = dates.getEnd(); for (int i = 0; i < sectionList.size(); i++) { MdcEquipmentRunningSection equipmentRunningSection = sectionList.get(i); long start = equipmentRunningSection.getStartTime().getTime(); long end = equipmentRunningSection.getEndTime().getTime(); if (datesStart <= start) { equipmentRunningSection.setStartTime(equipmentRunningSection.getStartTime()); } else { equipmentRunningSection.setStartTime(dates.getStartDate()); } if (datesEnd >= end) { equipmentRunningSection.setEndTime(equipmentRunningSection.getEndTime()); } else { equipmentRunningSection.setEndTime(dates.getEndDate()); } Long sen = DateUtils.differentSecond(equipmentRunningSection.getStartTime(), equipmentRunningSection.getEndTime()); if (sen <= 0) { sectionList.remove(i); i--; } else { equipmentRunningSection.setDuration(sen); equipmentRunningSection.setStartLong(equipmentRunningSection.getStartTime().getTime()); equipmentRunningSection.setEndLong(equipmentRunningSection.getEndTime().getTime()); } } MdcEquipmentStatisticalShiftInfo shiftInfo = new MdcEquipmentStatisticalShiftInfo(); shiftInfo.setEquipmentId(equipmentid); shiftInfo.setShiftId(dates.getShiftId()); shiftInfo.setShiftSubId(dates.getShiftSubId()); shiftInfo.setTheDate(dates.getDay()); long waitLong = 0L; long processLong = 0L; long closeLong = 0L; long errorLong = 0L; for (MdcEquipmentRunningSection equipmentRunningSection : sectionList) { Long duration = equipmentRunningSection.getDuration(); int status = equipmentRunningSection.getStatus(); if (status == 2) { waitLong += duration; } if (status == 3) { processLong += duration; } if (status == 0) { closeLong += duration; } if (status == 22) { errorLong += duration; } } shiftInfo.setWaitLong(new BigDecimal(waitLong)); shiftInfo.setProcessLong(new BigDecimal(processLong)); shiftInfo.setCloseLong(new BigDecimal(closeLong)); //开机时间计算为 有效时长 - 关机时长 shiftInfo.setOpenLong(new BigDecimal((datesEnd - datesStart) / 1000).subtract(shiftInfo.getCloseLong())); shiftInfo.setErrorLong(new BigDecimal(errorLong)); return shiftInfo; } /** * 时间问题处理 如果当天最末尾时间大于当前时间 返回0 * * @param temp * @return */ private List getDatesListByMdcDeviceCalendarVo(MdcDeviceCalendarVo temp) { List result = new ArrayList<>(); String startDateStr = temp.getStartDate(); String endDateStr = temp.getEndDate(); String sleepStartDateStr = temp.getSleepStartDate(); String sleepEndDateStr = temp.getSleepEndDate(); String effectiveDateStr = temp.getEffectiveDate(); String eff = DateUtils.format(DateUtils.toDate(effectiveDateStr, DateUtils.STRDATE), DateUtils.STR_DATE); try { Date effectiveDate = new SimpleDateFormat("yyyyMMdd").parse(effectiveDateStr); long startDate = getLongDate(effectiveDate, startDateStr, "false"); long endDate = getLongDate(effectiveDate, endDateStr, temp.getIsDaySpan()); //处理最后的时间问题 是否超过当前时间 如果超过不列入计算 BUG Date endTime = null; if ("true".equals(temp.getIsDaySpan())) { Date day = DateUtils.toDate(effectiveDateStr, DateUtils.STRDATE); day = DateUtils.plusTime(day, 1); String dayTime = DateUtils.format(day, DateUtils.STR_DATE); endTime = DateUtils.toDate(dayTime + " " + temp.getEndDate(), DateUtils.STR_DATE_TIME_SMALL); } else { Date day = DateUtils.toDate(effectiveDateStr, DateUtils.STRDATE); String dayTime = DateUtils.format(day, DateUtils.STR_DATE); endTime = DateUtils.toDate(dayTime + " " + temp.getEndDate(), DateUtils.STR_DATE_TIME_SMALL); } if (endTime.getTime() > DateUtils.getNow().getTime()) { return null; } if (StringUtils.isNotEmpty(sleepStartDateStr)) { long sleepStartDate = getLongDate(effectiveDate, sleepStartDateStr, "false"); long sleepEndDate = getLongDate(effectiveDate, sleepEndDateStr, "false"); Date start1 = DateUtils.getFormatDate(eff + " " + startDateStr, DateUtils.STR_DATE_TIME_SMALL); Date end1 = DateUtils.getFormatDate(eff + " " + sleepStartDateStr, DateUtils.STR_DATE_TIME_SMALL); Date start2 = DateUtils.getFormatDate(eff + " " + sleepEndDateStr, DateUtils.STR_DATE_TIME_SMALL); Date end2 = DateUtils.getFormatDate(eff + " " + endDateStr, DateUtils.STR_DATE_TIME_SMALL); if ("true".equals(temp.getIsDaySpan())) { //跨天 判断班次开始时间和结束时间是否跨天 if (startDateStr.compareTo(endDateStr) < 0) { //班次开始时间和结束时间都跨天 startDate = getLongDate(effectiveDate, startDateStr, temp.getIsDaySpan()); start1 = DateUtils.addDays(start1, 1); end2 = DateUtils.addDays(end2, 1); //班次开始时间和结束时间都跨天 休息开始时间和结束时间也一定跨天 sleepStartDate = getLongDate(effectiveDate, sleepStartDateStr, temp.getIsDaySpan()); end1 = DateUtils.addDays(end1, 1); sleepEndDate = getLongDate(effectiveDate, sleepEndDateStr, temp.getIsDaySpan()); start2 = DateUtils.addDays(start2, 1); } else { //班次开始时间不跨天, 结束时间跨天 end2 = DateUtils.addDays(end2, 1); //判断休息开始时间是否跨天 if (startDateStr.compareTo(sleepStartDateStr) > 0) { //开始休息时间跨天, 结束休息时间也一定跨天 sleepStartDate = getLongDate(effectiveDate, sleepStartDateStr, temp.getIsDaySpan()); end1 = DateUtils.addDays(end1, 1); sleepEndDate = getLongDate(effectiveDate, sleepEndDateStr, temp.getIsDaySpan()); start2 = DateUtils.addDays(start2, 1); } else { //休息开始时间不跨天, 判断休息结束时间是否跨天 if (sleepStartDateStr.compareTo(sleepEndDateStr) > 0) { //休息结束时间跨天 sleepEndDate = getLongDate(effectiveDate, sleepEndDateStr, temp.getIsDaySpan()); start2 = DateUtils.addDays(start2, 1); } } } } MdcShiftDateVo dates1 = new MdcShiftDateVo(temp.getShiftId(), temp.getShiftSubId(), startDate, sleepStartDate, effectiveDateStr, endTime, start1, end1); result.add(dates1); MdcShiftDateVo dates2 = new MdcShiftDateVo(temp.getShiftId(), temp.getShiftSubId(), sleepEndDate, endDate, effectiveDateStr, endTime, start2, end2); result.add(dates2); } else { /*获取班次的开始时间结束时间*/ Date start = DateUtils.getFormatDate(eff + " " + startDateStr, DateUtils.STR_DATE_TIME_SMALL); Date end = DateUtils.getFormatDate(eff + " " + endDateStr, DateUtils.STR_DATE_TIME_SMALL); if ("true".equals(temp.getIsDaySpan())) { if (startDateStr.compareTo(endDateStr) < 0) { startDate = getLongDate(effectiveDate, startDateStr, temp.getIsDaySpan()); start = DateUtils.addDays(start, 1); } end = DateUtils.addDays(end, 1); } MdcShiftDateVo dates = new MdcShiftDateVo(temp.getShiftId(), temp.getShiftSubId(), startDate, endDate, effectiveDateStr, endTime, start, end); result.add(dates); } } catch (ParseException e) { e.printStackTrace(); } return result; } /** * 设备班次转换 * * @param equipmentid * @param stringDates * @return */ private Map> mdcDeviceCalendarMap(String equipmentid, List stringDates) { List mdcDeviceCalendarVos = mdcDeviceCalendarService.listByEquipmentAndDate(equipmentid, stringDates); if (mdcDeviceCalendarVos.isEmpty()) { return null; } Map> map = new HashMap<>(); for (MdcDeviceCalendarVo mdcDeviceCalendarVo : mdcDeviceCalendarVos) { List mdcDeviceCalendarVos1; if (map.containsKey(mdcDeviceCalendarVo.getEffectiveDate())) { mdcDeviceCalendarVos1 = map.get(mdcDeviceCalendarVo.getEffectiveDate()); } else { mdcDeviceCalendarVos1 = new ArrayList<>(); } mdcDeviceCalendarVos1.add(mdcDeviceCalendarVo); map.put(mdcDeviceCalendarVo.getEffectiveDate(), mdcDeviceCalendarVos1); } return map; } private long getLongDate(Date effectiveDate, String startDateStr, String isDaySpan) { String[] startDateArray = startDateStr.split(":"); Calendar cal = Calendar.getInstance(); cal.setTime(effectiveDate); cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(startDateArray[0])); cal.set(Calendar.MINUTE, Integer.parseInt(startDateArray[1])); cal.set(Calendar.SECOND, Integer.parseInt(startDateArray[2])); if ("true".equals(isDaySpan)) { cal.add(Calendar.DAY_OF_YEAR, 1); } return cal.getTime().getTime(); } }