From 23855599412c4d61b38d78f0f3abd3430a48b5b1 Mon Sep 17 00:00:00 2001
From: zhangherong <571457620@qq.com>
Date: 星期三, 25 六月 2025 11:51:38 +0800
Subject: [PATCH] Merge branch 'mdc_hyjs_master'

---
 lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentStatisticalInfoServiceImpl.java |  312 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 312 insertions(+), 0 deletions(-)

diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentStatisticalInfoServiceImpl.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentStatisticalInfoServiceImpl.java
new file mode 100644
index 0000000..4e6a883
--- /dev/null
+++ b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEquipmentStatisticalInfoServiceImpl.java
@@ -0,0 +1,312 @@
+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);
+    }
+}

--
Gitblit v1.9.3