¶Ô±ÈÐÂÎļþ |
| | |
| | | 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.common.system.vo.DictModel; |
| | | import org.jeecg.modules.mdc.dto.MdcEquipmentStatisticalDto; |
| | | import org.jeecg.modules.mdc.entity.*; |
| | | import org.jeecg.modules.mdc.mapper.MdcEquipmentStatisticalInfoMapper; |
| | | import org.jeecg.modules.mdc.service.*; |
| | | import org.jeecg.modules.mdc.util.DateUtils; |
| | | import org.jeecg.modules.system.service.ISysDictService; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @Description: 设å¤åæ¥è¿è¡æ°æ®è¡¨ |
| | | * @Author: lius |
| | | * @Date: 2023-04-14 |
| | | * @Version: V1.0 |
| | | */ |
| | | @Service |
| | | public class MdcEquipmentStatisticalInfoServiceImpl extends ServiceImpl<MdcEquipmentStatisticalInfoMapper, MdcEquipmentStatisticalInfo> implements IMdcEquipmentStatisticalInfoService { |
| | | |
| | | @Resource |
| | | private IEquipmentService equipmentService; |
| | | |
| | | @Resource |
| | | private IMdcEquipmentRunningSectionService mdcEquipmentRunningSectionService; |
| | | |
| | | @Resource |
| | | private IMdcSystemParametersService mdcSystemParametersService; |
| | | |
| | | @Resource |
| | | private IMdcEquipmentFaultInfoService mdcEquipmentFaultInfoService; |
| | | |
| | | /** |
| | | * 计ç®è®¾å¤åæ¥è¿è¡æ°æ® |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = {Exception.class}) |
| | | public void runningAllEquipmentStatisticalProcess(String dateTime) { |
| | | if (StringUtils.isNotBlank(dateTime)) { |
| | | try { |
| | | Date initDate = DateUtils.toDate(dateTime, "yyyyMMdd"); |
| | | if (initDate != null) { |
| | | this.remove(new LambdaQueryWrapper<MdcEquipmentStatisticalInfo>(). |
| | | eq(MdcEquipmentStatisticalInfo::getTheDate, dateTime)); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("åæ°æ ¼å¼ä¸å¯¹", e); |
| | | } |
| | | } |
| | | List<Equipment> equipmentList = equipmentService.list(); |
| | | List<MdcEquipmentStatisticalInfo> result = new ArrayList<>(); |
| | | for (Equipment equipment : equipmentList) { |
| | | List<MdcEquipmentStatisticalInfo> equipmentStatisticalInfoList = equipmentStatisticalProcess(equipment, dateTime); |
| | | if (equipmentStatisticalInfoList != null && !equipmentStatisticalInfoList.isEmpty()) { |
| | | result.addAll(equipmentStatisticalInfoList); |
| | | } |
| | | } |
| | | if (!result.isEmpty()) { |
| | | this.saveBatch(result); |
| | | } |
| | | mdcEquipmentFaultInfoService.runningAllEquFaultStatistical(dateTime); |
| | | } |
| | | |
| | | private List<MdcEquipmentStatisticalInfo> equipmentStatisticalProcess(Equipment equipment, String dateTime) { |
| | | Date initDate = null; |
| | | //åæåçç»è®¡æ°æ® |
| | | if (StringUtils.isBlank(dateTime)) { |
| | | MdcEquipmentStatisticalInfo 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; |
| | | } |
| | | MdcSystemParameters mdcSystemParameters = mdcSystemParametersService.getOne(new LambdaQueryWrapper<MdcSystemParameters>().eq(MdcSystemParameters::getCode, "equip_log_statis_time")); |
| | | if (mdcSystemParameters == null) { |
| | | log.error("mdc_system_parameters è¡¨ä¸æ°æ®ç¼ºå¤±"); |
| | | return null; |
| | | } else { |
| | | String planTime = mdcSystemParameters.getValue(); |
| | | Date startDate = DateUtils.setTimeForDay(initDate, planTime); |
| | | //é®é¢ç¹ å¤å®å½åæ¶é´çå°æ¶å鿝å¦è¶
è¿ç³»ç»è§å®æ¶é´ |
| | | Date now = new Date(); |
| | | Date endDate; |
| | | if (StringUtils.isBlank(dateTime)) { |
| | | Date planDate = DateUtils.setTimeForDay(DateUtils.plusTime(now, 0), planTime); |
| | | long second = DateUtils.differentSecond(now, planDate); |
| | | if (second <= 0) { |
| | | endDate = planDate; |
| | | } else { |
| | | endDate = DateUtils.setTimeForDay(DateUtils.plusTime(now, -1), planTime); |
| | | } |
| | | List<MdcEquipmentRunningSection> equipmentRunningSectionList = mdcEquipmentRunningSectionService.listForEquipmentStatisticalInfo(equipment.getEquipmentid(), startDate, endDate); |
| | | return this.statisticsData(equipmentRunningSectionList, planTime, startDate, endDate, equipment); |
| | | } else { |
| | | Date end = DateUtils.plusTime(initDate, 1); |
| | | endDate = DateUtils.setTimeForDay(end, planTime); |
| | | List<MdcEquipmentRunningSection> equipmentRunningSectionList = mdcEquipmentRunningSectionService.listForEquipmentStatisticalInfo(equipment.getEquipmentid(), startDate, endDate); |
| | | return this.statisticsData(equipmentRunningSectionList, planTime, startDate, |
| | | DateUtils.setTimeForDay(DateUtils.plusTime(initDate, 0), planTime), equipment); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ¶é´åå² |
| | | */ |
| | | private List<MdcEquipmentStatisticalInfo> statisticsData(List<MdcEquipmentRunningSection> equipmentRunningSectionList, String planTime, Date startDate, Date endDate, Equipment equipment) { |
| | | Map<String, List<Map<String, Long>>> map = new HashMap<>(); |
| | | for (MdcEquipmentRunningSection temp : equipmentRunningSectionList) { |
| | | Date s1 = temp.getStartTime(); |
| | | //s2设置çèç¹æ¶é´ |
| | | Date s2 = DateUtils.setTimeForDay(s1, planTime); |
| | | Date e1 = temp.getEndTime(); |
| | | //e2 设置çèç¹æ¶é´ |
| | | Date e2 = DateUtils.getNextDay(s2); |
| | | if (s1.getTime() < s2.getTime()) { |
| | | if (e1.getTime() < s2.getTime()) { |
| | | String previousDay = DateUtils.format(DateUtils.getPreviousDay(s1), "yyyyMMdd"); |
| | | List list = map.get(previousDay); |
| | | if (list == null) { |
| | | list = new ArrayList(); |
| | | } |
| | | Map<String, Long> previous = new HashMap<>(); |
| | | previous.put("status", (long) temp.getStatus()); |
| | | previous.put("duration", (e1.getTime() - s1.getTime()) / 1000); |
| | | list.add(previous); |
| | | map.put(previousDay, list); |
| | | } else { |
| | | String previousDay = DateUtils.format(DateUtils.getPreviousDay(s1), "yyyyMMdd"); |
| | | List list = map.get(previousDay); |
| | | if (list == null) { |
| | | list = new ArrayList(); |
| | | } |
| | | Map<String, Long> previous = new HashMap<>(); |
| | | previous.put("status", (long) temp.getStatus()); |
| | | previous.put("duration", (s2.getTime() - s1.getTime()) / 1000); |
| | | list.add(previous); |
| | | map.put(previousDay, list); |
| | | String day = DateUtils.format(s1, "yyyyMMdd"); |
| | | List list1 = map.get(day); |
| | | if (list1 == null) { |
| | | list1 = new ArrayList(); |
| | | } |
| | | Map<String, Long> now = new HashMap<>(); |
| | | now.put("status", (long) temp.getStatus()); |
| | | now.put("duration", (e1.getTime() - s2.getTime()) / 1000); |
| | | list1.add(now); |
| | | map.put(day, list1); |
| | | } |
| | | } else { |
| | | if (e2.getTime() > e1.getTime()) { |
| | | //ä»å¤© |
| | | String nowDay = DateUtils.format(s1, "yyyyMMdd"); |
| | | List list1 = map.get(nowDay); |
| | | if (list1 == null) { |
| | | list1 = new ArrayList(); |
| | | } |
| | | Map<String, Long> now = new HashMap<>(); |
| | | now.put("status", (long) temp.getStatus()); |
| | | now.put("duration", (e1.getTime() - s1.getTime()) / 1000); |
| | | list1.add(now); |
| | | map.put(nowDay, list1); |
| | | } else { |
| | | // ä»å¤© |
| | | String nowDay = DateUtils.format(s1, "yyyyMMdd"); |
| | | List list1 = map.get(nowDay); |
| | | if (list1 == null) { |
| | | list1 = new ArrayList(); |
| | | } |
| | | Map<String, Long> now = new HashMap<>(); |
| | | now.put("status", (long) temp.getStatus()); |
| | | now.put("duration", (e2.getTime() - s1.getTime()) / 1000); |
| | | list1.add(now); |
| | | map.put(nowDay, list1); |
| | | //æå¤© |
| | | String nextDay = DateUtils.format(DateUtils.getNextDay(s1), "yyyyMMdd"); |
| | | List list2 = map.get(nextDay); |
| | | if (list2 == null) { |
| | | list2 = new ArrayList(); |
| | | } |
| | | Map<String, Long> next = new HashMap<>(); |
| | | next.put("status", (long) temp.getStatus()); |
| | | next.put("duration", (e1.getTime() - e2.getTime()) / 1000); |
| | | list2.add(next); |
| | | map.put(nextDay, list2); |
| | | } |
| | | } |
| | | } |
| | | int maxday = 0; |
| | | Date now = new Date(); |
| | | int dateInt1 = Integer.parseInt(DateUtils.format(now, "yyyyMMdd")); |
| | | int lastDay; |
| | | Date planDate = DateUtils.setTimeForDay(DateUtils.plusTime(now, 0), planTime); |
| | | Long second = DateUtils.differentSecond(planDate, now); |
| | | if (second < 0) { |
| | | lastDay = Integer.parseInt(DateUtils.format(DateUtils.plusTime(now, -1), "yyyyMMdd")); |
| | | } else { |
| | | lastDay = dateInt1; |
| | | } |
| | | Set<String> keys = map.keySet(); |
| | | for (String key : keys) { |
| | | int tmp = Integer.parseInt(key); |
| | | if (tmp == dateInt1 || tmp == lastDay) { |
| | | continue; |
| | | } |
| | | if (tmp > maxday) { |
| | | maxday = tmp; |
| | | } |
| | | } |
| | | boolean flag = true; |
| | | Date date = startDate; |
| | | List<MdcEquipmentStatisticalInfo> equipmentStatisticalInfos = new ArrayList<>(); |
| | | while (flag) { |
| | | int dateInt = Integer.parseInt(DateUtils.format(date, "yyyyMMdd")); |
| | | if (date.getTime() <= endDate.getTime() && dateInt <= maxday) { |
| | | List<Map<String, Long>> dayList = map.get(String.valueOf(dateInt)); |
| | | MdcEquipmentStatisticalInfo equipmentStatisticalInfo = new MdcEquipmentStatisticalInfo(); |
| | | equipmentStatisticalInfo.setEquipmentId(equipment.getEquipmentid()); |
| | | equipmentStatisticalInfo.setTheDate(String.valueOf(dateInt)); |
| | | long waitingLong = 0L; |
| | | long processingLong = 0L; |
| | | long closedLong = 0L; |
| | | long errorLong = 0L; |
| | | for (Map<String, Long> n : dayList) { |
| | | long du = n.get("duration"); |
| | | int status = new Long(n.get("status")).intValue(); |
| | | if (status == 2) { |
| | | waitingLong += du; |
| | | } |
| | | if (status == 3) { |
| | | processingLong += du; |
| | | } |
| | | if (status == 0) { |
| | | closedLong += du; |
| | | } |
| | | if (status == 22) { |
| | | errorLong += du; |
| | | } |
| | | } |
| | | equipmentStatisticalInfo.setWaitLong(new BigDecimal(waitingLong)); |
| | | equipmentStatisticalInfo.setProcessLong(new BigDecimal(processingLong)); |
| | | equipmentStatisticalInfo.setCloseLong(new BigDecimal(closedLong)); |
| | | equipmentStatisticalInfo.setOpenLong(equipmentStatisticalInfo.getWaitLong().add(equipmentStatisticalInfo.getProcessLong())); |
| | | equipmentStatisticalInfo.setErrorLong(new BigDecimal(errorLong)); |
| | | equipmentStatisticalInfo.setCreateTime(new Date()); |
| | | equipmentStatisticalInfos.add(equipmentStatisticalInfo); |
| | | } else { |
| | | flag = false; |
| | | } |
| | | date = DateUtils.getNextDay(date); |
| | | } |
| | | return equipmentStatisticalInfos; |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢è®¾å¤è¿è¡æ¶é´ |
| | | * |
| | | * @param equipmentId |
| | | * @param validDate |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Integer selectProcessLong(String equipmentId, String validDate) { |
| | | Integer processLong = this.baseMapper.selectProcessLong(equipmentId, validDate); |
| | | if (processLong == null) { |
| | | return 0; |
| | | } else { |
| | | return Integer.parseInt(new BigDecimal(processLong).divide(new BigDecimal("60"), 0, RoundingMode.HALF_UP).toString()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public MdcEquipmentStatisticalDto findByEquipmentAndMonth(String equipmentId, String date) { |
| | | return this.baseMapper.findByEquipmentAndMonth(equipmentId, date); |
| | | } |
| | | |
| | | @Override |
| | | public MdcEquipmentStatisticalInfo findByEquIdsAndMonth(List<String> equipmentIdList, String month) { |
| | | return this.baseMapper.findByEquIdsAndMonth(equipmentIdList, month); |
| | | } |
| | | |
| | | @Override |
| | | public List<MdcEquipmentStatisticalInfo> findByEquipmentAndDate(List<String> equipmentIdList, String date) { |
| | | return this.baseMapper.findByEquipmentAndDate(equipmentIdList, date); |
| | | } |
| | | } |