zhangherong
2025-05-09 f9a9fb9090c2e9e1a4e00374d48181efe2463a56
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcBoardServiceImpl.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,283 @@
package org.jeecg.modules.mdc.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.mdc.constant.MdcConstant;
import org.jeecg.modules.mdc.entity.Equipment;
import org.jeecg.modules.mdc.entity.MdcEquipment;
import org.jeecg.modules.mdc.entity.MdcEquipmentStatisticalInfo;
import org.jeecg.modules.mdc.entity.MdcOeeInfo;
import org.jeecg.modules.mdc.service.*;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.vo.MdcBoardRateVo;
import org.jeecg.modules.mdc.vo.MdcEquipmentStatusVo;
import org.jeecg.modules.mdcJc.service.IMdcJcRcJobreportService;
import org.jeecg.modules.system.entity.MdcProduction;
import org.jeecg.modules.system.service.IMdcProductionService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
/**
 * @Author: Lius
 * @CreateTime: 2025-05-06
 * @Description: çœ‹æ¿æŽ¥å£
 */
@Service
public class MdcBoardServiceImpl implements IMdcBoardService {
    @Resource
    private IMdcProductionService mdcProductionService;
    @Resource
    private IEquipmentService equipmentService;
    @Resource
    private IMdcEquipmentService mdcEquipmentService;
    @Resource
    private IMdcEquipmentStatisticalInfoService mdcEquipmentStatisticalInfoService;
    @Resource
    private IMdcOeeInfoService mdcOeeInfoService;
    @Resource
    private IMdcJcRcJobreportService mdcJcRcJobreportService;
    /**
     * è®¾å¤‡çŠ¶æ€
     */
    @Override
    public Map<String, Object> equipmentStatusStatistics(String productionId) {
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isBlank(productionId)) {
            MdcProduction mdcProduction = mdcProductionService.getOne(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getParentId, ""));
            productionId = mdcProduction.getId();
        }
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return result;
        }
        List<Equipment> equipmentList = equipmentService.listByProds(proIds);
        if (equipmentList == null || equipmentList.isEmpty()) {
            return result;
        }
        MdcEquipmentStatusVo mdcEquipmentStatusVo = new MdcEquipmentStatusVo();
        for (Equipment equipment : equipmentList) {
            if (equipment.getOporation() != null) {
                switch (equipment.getOporation()) {
                    case 1:
                    case 2:
                        mdcEquipmentStatusVo.setWaitCount(mdcEquipmentStatusVo.getWaitCount() + 1);
                        break;
                    case 3:
                        mdcEquipmentStatusVo.setRunCount(mdcEquipmentStatusVo.getRunCount() + 1);
                        break;
                    case 22:
                        mdcEquipmentStatusVo.setAlarmCount(mdcEquipmentStatusVo.getAlarmCount() + 1);
                        break;
                    default:
                        mdcEquipmentStatusVo.setCloseCount(mdcEquipmentStatusVo.getCloseCount() + 1);
                        break;
                }
            } else {
                mdcEquipmentStatusVo.setCloseCount(mdcEquipmentStatusVo.getCloseCount() + 1);
            }
        }
        result.put("equipmentStatus", mdcEquipmentStatusVo);
        return result;
    }
    /**
     * èŽ·å–äº§çº¿åˆ—è¡¨
     */
    @Override
    public List<MdcProduction> proList(String productionId) {
        if (StringUtils.isBlank(productionId)) {
            MdcProduction mdcProduction = mdcProductionService.getOne(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getParentId, ""));
            productionId = mdcProduction.getId();
        }
        return mdcProductionService.list(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getParentId, productionId).eq(MdcProduction::getMdcFlag, "1").eq(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_0).orderByAsc(MdcProduction::getProductionOrder));
    }
    /**
     * çŽ‡åˆ†æžèµ°åŠ¿
     */
    @Override
    public Map<String, Object> rateAnalysisTrend(String productionId) {
        Map<String, Object> result = new HashMap<>();
        if (StringUtils.isBlank(productionId)) {
            MdcProduction mdcProduction = mdcProductionService.getOne(new LambdaQueryWrapper<MdcProduction>().eq(MdcProduction::getParentId, ""));
            productionId = mdcProduction.getId();
        }
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return result;
        }
        List<String> equipmentIdList = mdcEquipmentService.getEquIdsByProIds(proIds);
        if (equipmentIdList == null || equipmentIdList.isEmpty()) {
            return result;
        }
        LocalDate now = LocalDate.now();
        Date startDate = DateUtils.toDate(now.plusDays(-15).toString(), DateUtils.STR_DATE);
        Date endDate = DateUtils.toDate(now.plusDays(-1).toString(), DateUtils.STR_DATE);
        String start = DateUtils.format(startDate, DateUtils.STR_DATE);
        String end = DateUtils.format(endDate, DateUtils.STR_DATE);
        List<String> dateList = DateUtils.getDatesStringList(startDate, endDate);
        List<String> dates = dateList.stream().map(date -> date.substring(5, 10)).collect(Collectors.toList());
        result.put("dateList", dates);
        Map<String, MdcBoardRateVo> statisticsMap = new LinkedHashMap<>();
        dateList.forEach(date -> {
            statisticsMap.put(date, new MdcBoardRateVo(date.substring(5, 10)));
        });
        // TEEP å¼€æœºçއ å¼€åŠ¨çŽ‡
        List<MdcEquipmentStatisticalInfo> mdcEquipmentStatisticalInfo = mdcEquipmentStatisticalInfoService.findByEquipmentAndDate(equipmentIdList, start.replaceAll("-", ""), end.replaceAll("-", ""));
        if (mdcEquipmentStatisticalInfo != null && !mdcEquipmentStatisticalInfo.isEmpty()) {
            mdcEquipmentStatisticalInfo.forEach(equipmentStatisticalInfo -> {
                String date = DateUtils.format(DateUtils.toDate(equipmentStatisticalInfo.getTheDate(), DateUtils.STRDATE), DateUtils.STR_DATE);
                if (statisticsMap.containsKey(date)) {
                    MdcBoardRateVo mdcBoardRateVo = statisticsMap.get(date);
                    if (equipmentStatisticalInfo.getProcessLong().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setUtilizationRate(equipmentStatisticalInfo.getProcessLong().divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP));
                    }
                    if (equipmentStatisticalInfo.getOpenLong().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setOpenRate(equipmentStatisticalInfo.getOpenLong().divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP));
                    }
                    if (equipmentStatisticalInfo.getOpenLong().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setStartRate(equipmentStatisticalInfo.getProcessLong().multiply(new BigDecimal("100").divide(equipmentStatisticalInfo.getOpenLong(), 2, RoundingMode.HALF_UP)));
                    }
                    statisticsMap.put(date, mdcBoardRateVo);
                }
            });
        }
        // OEE
        List<MdcOeeInfo> oeeInfo = mdcOeeInfoService.findByEquIdsAndDate(equipmentIdList, start, end);
        if (oeeInfo != null && !oeeInfo.isEmpty()) {
            oeeInfo.forEach(mdcOeeInfo -> {
                if (statisticsMap.containsKey(mdcOeeInfo.getTheDate())) {
                    MdcBoardRateVo mdcBoardRateVo = statisticsMap.get(mdcOeeInfo.getTheDate());
                    if (mdcOeeInfo.getOee().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setOee(mdcOeeInfo.getOee().setScale(2, RoundingMode.HALF_UP));
                    }
                    statisticsMap.put(mdcOeeInfo.getTheDate(), mdcBoardRateVo);
                }
            });
        }
        List<MdcBoardRateVo> dataList = new ArrayList<>(statisticsMap.values());
        result.put("dataList", dataList);
        return result;
    }
    /**
     * è®¾å¤‡åˆ—表
     */
    @Override
    public List<MdcEquipment> equipmentList(String productionId) {
        List<String> proIds = mdcProductionService.findChildByProId(productionId);
        if (proIds == null || proIds.isEmpty()) {
            return null;
        }
        return mdcEquipmentService.findByProIds(proIds);
    }
    /**
     * è®¾å¤‡æ—¥åˆ©ç”¨çŽ‡åˆ†æž
     */
    @Override
    public Map<String, Object> rateAnalysisTrendDay(String equipmentId) {
        Map<String, Object> result = new HashMap<>();
        LocalDate now = LocalDate.now();
        Date startDate = DateUtils.toDate(now.plusDays(-7).toString(), DateUtils.STR_DATE);
        Date endDate = DateUtils.toDate(now.plusDays(-1).toString(), DateUtils.STR_DATE);
        String start = DateUtils.format(startDate, DateUtils.STR_DATE);
        String end = DateUtils.format(endDate, DateUtils.STR_DATE);
        List<String> dayBetween = DateUtils.getDatesStringList(startDate, endDate);
        List<String> dateList = dayBetween.stream().map(date -> date.substring(5, 10)).collect(Collectors.toList());
        result.put("dateList", dateList);
        Map<String, MdcBoardRateVo> statisticsMap = new LinkedHashMap<>();
        dayBetween.forEach(date -> {
            statisticsMap.put(date, new MdcBoardRateVo(date.substring(5, 10)));
        });
        // TEEP
        List<MdcEquipmentStatisticalInfo> mdcEquipmentStatisticalInfo = mdcEquipmentStatisticalInfoService.findByEquIdAndDate(equipmentId, start.replaceAll("-", ""), end.replaceAll("-", ""));
        if (mdcEquipmentStatisticalInfo != null && !mdcEquipmentStatisticalInfo.isEmpty()) {
            mdcEquipmentStatisticalInfo.forEach(equipmentStatisticalInfo -> {
                String date = DateUtils.format(DateUtils.toDate(equipmentStatisticalInfo.getTheDate(), DateUtils.STRDATE), DateUtils.STR_DATE);
                if (statisticsMap.containsKey(date)) {
                    MdcBoardRateVo mdcBoardRateVo = statisticsMap.get(date);
                    if (equipmentStatisticalInfo.getProcessLong().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setUtilizationRate(equipmentStatisticalInfo.getProcessLong().divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP));
                    }
                    statisticsMap.put(date, mdcBoardRateVo);
                }
            });
        }
        // OEE
        List<MdcOeeInfo> oeeInfo = mdcOeeInfoService.findByEquIdAndDate(equipmentId, start, end);
        if (oeeInfo != null && !oeeInfo.isEmpty()) {
            oeeInfo.forEach(mdcOeeInfo -> {
                if (statisticsMap.containsKey(mdcOeeInfo.getTheDate())) {
                    MdcBoardRateVo mdcBoardRateVo = statisticsMap.get(mdcOeeInfo.getTheDate());
                    if (mdcOeeInfo.getOee().compareTo(BigDecimal.ZERO) > 0) {
                        mdcBoardRateVo.setOee(mdcOeeInfo.getOee().setScale(2, RoundingMode.HALF_UP));
                    }
                    statisticsMap.put(mdcOeeInfo.getTheDate(), mdcBoardRateVo);
                }
            });
        }
        List<MdcBoardRateVo> dataList = new ArrayList<>(statisticsMap.values());
        result.put("dataList", dataList);
        return result;
    }
    /**
     * è®¾å¤‡æœˆçŽ‡åˆ†æž
     */
    @Override
    public Map<String, Object> rateAnalysisTrendMonth(String equipmentId) {
        Map<String, Object> result = new HashMap<>();
        LocalDate now = LocalDate.now();
        Date start = DateUtils.toDate(now.plusMonths(-6).toString(), DateUtils.STR_DATE);
        Date end = DateUtils.toDate(now.plusMonths(-1).toString(), DateUtils.STR_DATE);
        List<String> monthBetween = DateUtils.getMonthBetween(start, end);
        List<String> dateList = new ArrayList<>();
        List<MdcBoardRateVo> dataList = new ArrayList<>();
        for (String month : monthBetween) {
            String name = month.substring(month.lastIndexOf("-") + 1).replaceFirst("^0*", "") + "月";
            dateList.add(name);
            MdcBoardRateVo mdcBoardRateVo = new MdcBoardRateVo();
            mdcBoardRateVo.setDate(name);
            // TEEP
            MdcEquipmentStatisticalInfo mdcEquipmentStatisticalInfo = mdcEquipmentStatisticalInfoService.findByEquIdAndMonth(equipmentId, month.replaceAll("-", ""));
            if (mdcEquipmentStatisticalInfo != null) {
                mdcBoardRateVo.setUtilizationRate(mdcEquipmentStatisticalInfo.getProcessLong().divide(new BigDecimal("864"), 2, RoundingMode.HALF_UP));
            }
            // OEE
            BigDecimal oee = mdcOeeInfoService.findByEquIdAndMonth(equipmentId, month);
            if (oee != null) {
                mdcBoardRateVo.setOee(oee.setScale(2, RoundingMode.HALF_UP));
            }
            // åˆæ ¼çއ
            BigDecimal passRate = mdcJcRcJobreportService.findRateByMonth(equipmentId, month);
            if (passRate != null) {
                mdcBoardRateVo.setPassRate(passRate);
            }
            dataList.add(mdcBoardRateVo);
        }
        result.put("dateList", dateList);
        result.put("dataList", dataList);
        return result;
    }
}