Lius
2025-06-09 a79d1641ab162cf1e96d3af666d1e77abde68c1b
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcEfficiencyReportServiceImpl.java
@@ -2,12 +2,18 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.mdc.dto.*;
import org.jeecg.modules.mdc.entity.*;
import org.jeecg.modules.mdc.mapper.MdcEfficiencyReportMapper;
import org.jeecg.modules.mdc.service.*;
import org.jeecg.modules.mdc.util.CustomExcelView;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.vo.MdcUtilizationRateDto;
import org.jeecg.modules.mdc.vo.*;
@@ -17,8 +23,11 @@
import org.jeecg.modules.system.service.ISysDepartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
@@ -69,238 +78,272 @@
    public MdcEfficiencyVo efficiencyList(String userId, MdcEfficiencyReportQueryVo vo) {
        MdcEfficiencyVo result = new MdcEfficiencyVo();
        List<MdcEfficiencyListDto> listDtos = new ArrayList<>();
        List<String> equipmentIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(vo.getParentId()) && StringUtils.isEmpty(vo.getEquipmentId())) {
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, vo.getParentId());
            } else {
                // 产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, vo.getParentId());
            }
        } else if (StringUtils.isNotEmpty(vo.getEquipmentId())) {
            // 单台设备信息
            vo.setEquipmentIdList(Collections.singletonList(vo.getEquipmentId()));
        } else {
            // 查询用户拥有的所有设备信息
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
            } else {
                // 产线层级
                equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
            }
        }
        // 获取设备 ID 列表
        List<String> equipmentIds = getEquipmentIds(userId, vo);
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            vo.setEquipmentIdList(equipmentIds);
        }
        // 获取日期列表
        List<String> dates = DateUtils.getDatesStringList2(DateUtils.getShortDate2(vo.getStartTime()), DateUtils.getShortDate2(vo.getEndTime()));
        result.setDates(dates);
        // 利用率等级
        // 获取设备利用率列表
        List<MdcUtilizationRate> mdcUtilizationRateList = mdcUtilizationRateService.listByType("lyl");
        // 如果设备 ID 列表为空,直接返回
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            result.setMdcEfficiencyList(listDtos);
        } else {
            List<String> equipmentIdList = mdcEquipmentService.listEquipmentId(vo);
            if (equipmentIdList != null && !equipmentIdList.isEmpty()) {
                vo.setEquipmentIdList(equipmentIdList);
            } else {
                result.setMdcEfficiencyList(listDtos);
                return result;
            }
            // 查询利用率数据
            List<MdcEfficiencyDto> efficiencyList = mdcEfficiencyReportMapper.efficiencyList(vo);
            // 封装结果
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                List<MdcEquDepDto> equipmentList = mdcEquipmentService.findEquDepList(vo.getEquipmentIdList());
                // 查询所有部门信息
                List<SysDepart> departList = sysDepartService.list(new LambdaQueryWrapper<SysDepart>().ne(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_1.toString()).orderByAsc(SysDepart::getDepartOrder));
                for (MdcEquDepDto mdcEquDepDto : equipmentList) {
                    MdcEfficiencyListDto mdcEfficiencyListDto = new MdcEfficiencyListDto();
                    mdcEfficiencyListDto.setEquipmentId(mdcEquDepDto.getEquipmentId());
                    mdcEfficiencyListDto.setEquipmentName(mdcEquDepDto.getEquipmentName());
                    mdcEfficiencyListDto.setEquipmentType(mdcEquDepDto.getEquipmentType());
                    mdcEfficiencyListDto.setEquipmentModel(mdcEquDepDto.getEquipmentModel());
                    switch (mdcEquDepDto.getOrgType()) {
                        case "1":
                            mdcEfficiencyListDto.setLevel1(mdcEquDepDto.getDepartName());
                            break;
                        case "2":
                            mdcEfficiencyListDto.setLevel2(mdcEquDepDto.getDepartName());
                            break;
                        case "3":
                            mdcEfficiencyListDto.setLevel3(mdcEquDepDto.getDepartName());
                            break;
                        default:
                    }
                    SysDepart sysDepart = departList.stream().filter(depart -> depart.getId().equals(mdcEquDepDto.getParentId())).findAny().orElse(null);
                    if (sysDepart != null) {
                        switch (sysDepart.getOrgType()) {
                            case "1":
                                mdcEfficiencyListDto.setLevel1(sysDepart.getDepartName());
                                break;
                            case "2":
                                mdcEfficiencyListDto.setLevel2(sysDepart.getDepartName());
                                break;
                            case "3":
                                mdcEfficiencyListDto.setLevel3(sysDepart.getDepartName());
                                break;
                            default:
                        }
                        if (StringUtils.isNotEmpty(sysDepart.getParentId())) {
                            departList.stream().filter(depart -> depart.getId().equals(sysDepart.getParentId())).findAny().ifPresent(depart1 -> {
                                switch (depart1.getOrgType()) {
                                    case "1":
                                        mdcEfficiencyListDto.setLevel1(depart1.getDepartName());
                                        break;
                                    case "2":
                                        mdcEfficiencyListDto.setLevel2(depart1.getDepartName());
                                        break;
                                    case "3":
                                        mdcEfficiencyListDto.setLevel3(depart1.getDepartName());
                                        break;
                                    default:
                                }
                            });
                        }
                    }
                    List<MdcEfficiencyResultDto> list = new ArrayList<>();
                    MdcEfficiencyResultDto mdcEfficiencyResultDto = new MdcEfficiencyResultDto();
                    MdcEfficiencyResultDto mdcEfficiencyResultDto1 = new MdcEfficiencyResultDto();
                    mdcEfficiencyResultDto1.setTheDate("合计");
                    mdcEfficiencyResultDto.setTheDate("平均值");
                    for (String date : dates) {
                        list.add(this.efficiencyRate(efficiencyList, date, mdcEquDepDto.getEquipmentId(), mdcUtilizationRateList, mdcEfficiencyResultDto));
                    }
                    mdcEfficiencyResultDto1.setProcessLong(mdcEfficiencyResultDto.getProcessLong());
                    mdcEfficiencyResultDto.setProcessLong(mdcEfficiencyResultDto.getProcessLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto.setUtilizationRate(mdcEfficiencyResultDto.getUtilizationRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto1.setUtilizationRate(mdcEfficiencyResultDto.getUtilizationRate());
                    mdcEfficiencyResultDto.setStartRate(mdcEfficiencyResultDto.getStartRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto1.setStartRate(mdcEfficiencyResultDto.getStartRate());
                    mdcEfficiencyResultDto.setOpenRate(mdcEfficiencyResultDto.getOpenRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto1.setOpenRate(mdcEfficiencyResultDto.getOpenRate());
                    mdcEfficiencyResultDto1.setOpenLong(mdcEfficiencyResultDto.getOpenLong());
                    mdcEfficiencyResultDto.setOpenLong(mdcEfficiencyResultDto.getOpenLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto1.setWaitLong(mdcEfficiencyResultDto.getWaitLong());
                    mdcEfficiencyResultDto.setWaitLong(mdcEfficiencyResultDto.getWaitLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto1.setCloseLong(mdcEfficiencyResultDto.getCloseLong());
                    mdcEfficiencyResultDto.setCloseLong(mdcEfficiencyResultDto.getCloseLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
                    long rate = mdcEfficiencyResultDto.getUtilizationRate().multiply(new BigDecimal("100")).longValue();
                    for (MdcUtilizationRate mdcUtilizationRate : mdcUtilizationRateList) {
                        if (rate >= mdcUtilizationRate.getMinimumRange() && rate < mdcUtilizationRate.getMaximumRange()) {
                            mdcEfficiencyResultDto.setColor(mdcUtilizationRate.getRateParameterColor());
                            mdcEfficiencyResultDto1.setColor(mdcUtilizationRate.getRateParameterColor());
                        }
                    }
                    list.add(mdcEfficiencyResultDto1);
                    list.add(mdcEfficiencyResultDto);
                    mdcEfficiencyListDto.setDataList(list);
                    listDtos.add(mdcEfficiencyListDto);
                }
            } else {
                // 产线层级
                List<MdcEquProDto> equipmentList = mdcEquipmentService.findEquProList(vo.getEquipmentIdList());
                // 查询所有产线信息
                List<MdcProduction> productionList = mdcProductionService.list(new LambdaQueryWrapper<MdcProduction>().ne(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_1.toString()).orderByAsc(MdcProduction::getProductionOrder));
                for (MdcEquProDto mdcEquProDto : equipmentList) {
                    MdcEfficiencyListDto mdcEfficiencyListDto = new MdcEfficiencyListDto();
                    mdcEfficiencyListDto.setEquipmentId(mdcEquProDto.getEquipmentId());
                    mdcEfficiencyListDto.setEquipmentName(mdcEquProDto.getEquipmentName());
                    mdcEfficiencyListDto.setEquipmentType(mdcEquProDto.getEquipmentType());
                    mdcEfficiencyListDto.setEquipmentModel(mdcEquProDto.getEquipmentModel());
                    switch (mdcEquProDto.getOrgType()) {
                        case "1":
                            mdcEfficiencyListDto.setLevel1(mdcEquProDto.getProductionName());
                            break;
                        case "2":
                            mdcEfficiencyListDto.setLevel2(mdcEquProDto.getProductionName());
                            break;
                        case "3":
                            mdcEfficiencyListDto.setLevel3(mdcEquProDto.getProductionName());
                            break;
                        default:
                    }
                    MdcProduction mdcProduction = productionList.stream().filter(production -> production.getId().equals(mdcEquProDto.getParentId())).findAny().orElse(null);
                    if (mdcProduction != null) {
                        switch (mdcProduction.getOrgType()) {
                            case "1":
                                mdcEfficiencyListDto.setLevel1(mdcProduction.getProductionName());
                                break;
                            case "2":
                                mdcEfficiencyListDto.setLevel2(mdcProduction.getProductionName());
                                break;
                            case "3":
                                mdcEfficiencyListDto.setLevel3(mdcProduction.getProductionName());
                                break;
                            default:
                        }
                        if (StringUtils.isNotEmpty(mdcProduction.getParentId())) {
                            productionList.stream().filter(production -> production.getId().equals(mdcProduction.getParentId())).findAny().ifPresent(production1 -> {
                                switch (production1.getOrgType()) {
                                    case "1":
                                        mdcEfficiencyListDto.setLevel1(production1.getProductionName());
                                        break;
                                    case "2":
                                        mdcEfficiencyListDto.setLevel2(production1.getProductionName());
                                        break;
                                    case "3":
                                        mdcEfficiencyListDto.setLevel3(production1.getProductionName());
                                        break;
                                    default:
                                }
                            });
                        }
                    }
                    List<MdcEfficiencyResultDto> list = new ArrayList<>();
                    MdcEfficiencyResultDto mdcEfficiencyResultDto = new MdcEfficiencyResultDto();
                    MdcEfficiencyResultDto mdcEfficiencyResultDto1 = new MdcEfficiencyResultDto();
                    mdcEfficiencyResultDto1.setTheDate("合计");
                    mdcEfficiencyResultDto.setTheDate("平均值");
                    for (String date : dates) {
                        list.add(this.efficiencyRate(efficiencyList, date, mdcEquProDto.getEquipmentId(), mdcUtilizationRateList, mdcEfficiencyResultDto));
                    }
                    mdcEfficiencyResultDto1.setProcessLong(mdcEfficiencyResultDto.getProcessLong());
                    mdcEfficiencyResultDto.setProcessLong(mdcEfficiencyResultDto.getProcessLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto.setUtilizationRate(mdcEfficiencyResultDto.getUtilizationRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto1.setUtilizationRate(mdcEfficiencyResultDto.getUtilizationRate());
                    mdcEfficiencyResultDto.setStartRate(mdcEfficiencyResultDto.getStartRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto1.setStartRate(mdcEfficiencyResultDto.getStartRate());
                    mdcEfficiencyResultDto.setOpenRate(mdcEfficiencyResultDto.getOpenRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto1.setOpenRate(mdcEfficiencyResultDto.getOpenRate());
                    mdcEfficiencyResultDto1.setOpenLong(mdcEfficiencyResultDto.getOpenLong());
                    mdcEfficiencyResultDto.setOpenLong(mdcEfficiencyResultDto.getOpenLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto1.setWaitLong(mdcEfficiencyResultDto.getWaitLong());
                    mdcEfficiencyResultDto.setWaitLong(mdcEfficiencyResultDto.getWaitLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
                    mdcEfficiencyResultDto1.setCloseLong(mdcEfficiencyResultDto.getCloseLong());
                    mdcEfficiencyResultDto.setCloseLong(mdcEfficiencyResultDto.getCloseLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
                    long rate = mdcEfficiencyResultDto.getUtilizationRate().multiply(new BigDecimal("100")).longValue();
                    for (MdcUtilizationRate mdcUtilizationRate : mdcUtilizationRateList) {
                        if (rate >= mdcUtilizationRate.getMinimumRange() && rate < mdcUtilizationRate.getMaximumRange()) {
                            mdcEfficiencyResultDto.setColor(mdcUtilizationRate.getRateParameterColor());
                            mdcEfficiencyResultDto1.setColor(mdcUtilizationRate.getRateParameterColor());
                        }
                    }
                    list.add(mdcEfficiencyResultDto1);
                    list.add(mdcEfficiencyResultDto);
                    mdcEfficiencyListDto.setDataList(list);
                    listDtos.add(mdcEfficiencyListDto);
                }
            }
            result.setMdcEfficiencyList(listDtos);
            return result;
        }
        // 获取设备 ID 列表
        List<String> equipmentIdList = mdcEquipmentService.listEquipmentId(vo);
        if (equipmentIdList == null || equipmentIdList.isEmpty()) {
            result.setMdcEfficiencyList(listDtos);
            return result;
        }
        vo.setEquipmentIdList(equipmentIdList);
        // 查询设备效率数据
        List<MdcEfficiencyDto> efficiencyList = mdcEfficiencyReportMapper.efficiencyList(vo);
        // 根据类型树处理不同的层级
        if ("2".equals(vo.getTypeTree())) {
            // 部门层级
            listDtos = processDepartmentLevel(vo, efficiencyList, dates, mdcUtilizationRateList);
        } else {
            // 产线层级
            listDtos = processProductionLevel(vo, efficiencyList, dates, mdcUtilizationRateList);
        }
        result.setMdcEfficiencyList(listDtos);
        // 添加合计值和平均值
        addSummaryAndAverage(result, listDtos, mdcUtilizationRateList);
        return result;
    }
    /**
     * 获取设备 ID 列表
     */
    private List<String> getEquipmentIds(String userId, MdcEfficiencyReportQueryVo vo) {
        if (StringUtils.isNotEmpty(vo.getParentId()) && StringUtils.isEmpty(vo.getEquipmentId())) {
            return "2".equals(vo.getTypeTree())
                    ? mdcEquipmentService.getEquipmentIdsByDepart(userId, vo.getParentId())
                    : mdcEquipmentService.getEquipmentIdsProduction(userId, vo.getParentId());
        } else if (StringUtils.isNotEmpty(vo.getEquipmentId())) {
            vo.setEquipmentIdList(Collections.singletonList(vo.getEquipmentId()));
            return Collections.emptyList();
        } else {
            return "2".equals(vo.getTypeTree())
                    ? mdcEquipmentService.getEquipmentIdsByDepart(userId, null)
                    : mdcEquipmentService.getEquipmentIdsProduction(userId, null);
        }
    }
    /**
     * 处理部门层级
     */
    private List<MdcEfficiencyListDto> processDepartmentLevel(MdcEfficiencyReportQueryVo vo, List<MdcEfficiencyDto> efficiencyList, List<String> dates, List<MdcUtilizationRate> mdcUtilizationRateList) {
        List<MdcEquDepDto> equipmentList = mdcEquipmentService.findEquDepList(vo.getEquipmentIdList());
        List<SysDepart> departList = sysDepartService.list(new LambdaQueryWrapper<SysDepart>()
                .ne(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_1.toString())
                .orderByAsc(SysDepart::getDepartOrder));
        return equipmentList.stream().map(mdcEquDepDto -> {
            MdcEfficiencyListDto mdcEfficiencyListDto = createEfficiencyListDto(mdcEquDepDto);
            setDepartmentLevels(mdcEfficiencyListDto, mdcEquDepDto, departList);
            mdcEfficiencyListDto.setDataList(processEfficiencyData(efficiencyList, dates, mdcEquDepDto.getEquipmentId(), mdcUtilizationRateList));
            return mdcEfficiencyListDto;
        }).collect(Collectors.toList());
    }
    /**
     * 处理产线层级
     */
    private List<MdcEfficiencyListDto> processProductionLevel(MdcEfficiencyReportQueryVo vo, List<MdcEfficiencyDto> efficiencyList, List<String> dates, List<MdcUtilizationRate> mdcUtilizationRateList) {
        List<MdcEquProDto> equipmentList = mdcEquipmentService.findEquProList(vo.getEquipmentIdList());
        List<MdcProduction> productionList = mdcProductionService.list(new LambdaQueryWrapper<MdcProduction>()
                .ne(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_1.toString())
                .orderByAsc(MdcProduction::getProductionOrder));
        return equipmentList.stream().map(mdcEquProDto -> {
            MdcEfficiencyListDto mdcEfficiencyListDto = createEfficiencyListDto(mdcEquProDto);
            setProductionLevels(mdcEfficiencyListDto, mdcEquProDto, productionList);
            mdcEfficiencyListDto.setDataList(processEfficiencyData(efficiencyList, dates, mdcEquProDto.getEquipmentId(), mdcUtilizationRateList));
            return mdcEfficiencyListDto;
        }).collect(Collectors.toList());
    }
    /**
     * 创建 MdcEfficiencyListDto 对象
     */
    private MdcEfficiencyListDto createEfficiencyListDto(MdcEquDepDto mdcEquDepDto) {
        MdcEfficiencyListDto dto = new MdcEfficiencyListDto();
        dto.setEquipmentId(mdcEquDepDto.getEquipmentId());
        dto.setEquipmentName(mdcEquDepDto.getEquipmentName());
        dto.setEquipmentType(mdcEquDepDto.getEquipmentType());
        dto.setEquipmentModel(mdcEquDepDto.getEquipmentModel());
        return dto;
    }
    private MdcEfficiencyListDto createEfficiencyListDto(MdcEquProDto mdcEquProDto) {
        MdcEfficiencyListDto dto = new MdcEfficiencyListDto();
        dto.setEquipmentId(mdcEquProDto.getEquipmentId());
        dto.setEquipmentName(mdcEquProDto.getEquipmentName());
        dto.setEquipmentType(mdcEquProDto.getEquipmentType());
        dto.setEquipmentModel(mdcEquProDto.getEquipmentModel());
        return dto;
    }
    /**
     * 设置部门层级信息
     */
    private void setDepartmentLevels(MdcEfficiencyListDto dto, MdcEquDepDto mdcEquDepDto, List<SysDepart> departList) {
        switch (mdcEquDepDto.getOrgType()) {
            case "1":
                dto.setLevel1(mdcEquDepDto.getDepartName());
                break;
            case "2":
                dto.setLevel2(mdcEquDepDto.getDepartName());
                break;
            case "3":
                dto.setLevel3(mdcEquDepDto.getDepartName());
                break;
        }
        Optional<SysDepart> sysDepart = departList.stream().filter(depart -> depart.getId().equals(mdcEquDepDto.getParentId())).findAny();
        sysDepart.ifPresent(depart -> {
            switch (depart.getOrgType()) {
                case "1":
                    dto.setLevel1(depart.getDepartName());
                    break;
                case "2":
                    dto.setLevel2(depart.getDepartName());
                    break;
                case "3":
                    dto.setLevel3(depart.getDepartName());
                    break;
            }
            if (StringUtils.isNotEmpty(depart.getParentId())) {
                departList.stream().filter(d -> d.getId().equals(depart.getParentId())).findAny().ifPresent(parent -> {
                    switch (parent.getOrgType()) {
                        case "1":
                            dto.setLevel1(parent.getDepartName());
                            break;
                        case "2":
                            dto.setLevel2(parent.getDepartName());
                            break;
                        case "3":
                            dto.setLevel3(parent.getDepartName());
                            break;
                    }
                });
            }
        });
    }
    /**
     * 设置部门层级信息
     */
    private void setProductionLevels(MdcEfficiencyListDto dto, MdcEquProDto mdcEquProDto, List<MdcProduction> productionList) {
        switch (mdcEquProDto.getOrgType()) {
            case "1":
                dto.setLevel1(mdcEquProDto.getProductionName());
                break;
            case "2":
                dto.setLevel2(mdcEquProDto.getProductionName());
                break;
            case "3":
                dto.setLevel3(mdcEquProDto.getProductionName());
                break;
        }
        Optional<MdcProduction> mdcProduction = productionList.stream().filter(production -> production.getId().equals(mdcEquProDto.getParentId())).findAny();
        mdcProduction.ifPresent(production -> {
            switch (production.getOrgType()) {
                case "1":
                    dto.setLevel1(production.getProductionName());
                    break;
                case "2":
                    dto.setLevel2(production.getProductionName());
                    break;
                case "3":
                    dto.setLevel3(production.getProductionName());
                    break;
            }
            if (StringUtils.isNotEmpty(production.getParentId())) {
                productionList.stream().filter(p -> p.getId().equals(production.getParentId())).findAny().ifPresent(parent -> {
                    switch (parent.getOrgType()) {
                        case "1":
                            dto.setLevel1(parent.getProductionName());
                            break;
                        case "2":
                            dto.setLevel2(parent.getProductionName());
                            break;
                        case "3":
                            dto.setLevel3(parent.getProductionName());
                            break;
                    }
                });
            }
        });
    }
    /**
     * 处理设备效率数据
     */
    private List<MdcEfficiencyResultDto> processEfficiencyData(List<MdcEfficiencyDto> efficiencyList, List<String> dates, String equipmentId, List<MdcUtilizationRate> mdcUtilizationRateList) {
        List<MdcEfficiencyResultDto> list = new ArrayList<>();
        MdcEfficiencyResultDto avgDto = new MdcEfficiencyResultDto();
        avgDto.setTheDate("平均值");
        MdcEfficiencyResultDto sumDto = new MdcEfficiencyResultDto();
        sumDto.setTheDate("合计");
        for (String date : dates) {
            MdcEfficiencyResultDto dto = efficiencyRate(efficiencyList, date, equipmentId, mdcUtilizationRateList, avgDto);
            list.add(dto);
        }
        sumDto.setProcessLong(avgDto.getProcessLong());
        avgDto.setProcessLong(avgDto.getProcessLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
        avgDto.setUtilizationRate(avgDto.getUtilizationRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
        sumDto.setUtilizationRate(avgDto.getUtilizationRate());
        avgDto.setStartRate(avgDto.getStartRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
        sumDto.setStartRate(avgDto.getStartRate());
        avgDto.setOpenRate(avgDto.getOpenRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
        sumDto.setOpenRate(avgDto.getOpenRate());
        sumDto.setOpenLong(avgDto.getOpenLong());
        avgDto.setOpenLong(avgDto.getOpenLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
        sumDto.setWaitLong(avgDto.getWaitLong());
        avgDto.setWaitLong(avgDto.getWaitLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
        sumDto.setCloseLong(avgDto.getCloseLong());
        avgDto.setCloseLong(avgDto.getCloseLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
        long rate = avgDto.getUtilizationRate().multiply(new BigDecimal("100")).longValue();
        for (MdcUtilizationRate mdcUtilizationRate : mdcUtilizationRateList) {
            if (rate >= mdcUtilizationRate.getMinimumRange() && rate < mdcUtilizationRate.getMaximumRange()) {
                avgDto.setColor(mdcUtilizationRate.getRateParameterColor());
                sumDto.setColor(mdcUtilizationRate.getRateParameterColor());
            }
        }
        list.add(sumDto);
        list.add(avgDto);
        return list;
    }
    /**
     * 添加合计值和平均值
     */
    private void addSummaryAndAverage(MdcEfficiencyVo result, List<MdcEfficiencyListDto> listDtos, List<MdcUtilizationRate> mdcUtilizationRateList) {
        result.getDates().add("合计");
        result.getDates().add("平均值");
        MdcEfficiencyListDto sum = new MdcEfficiencyListDto();
        sum.setLevel1("合计");
        sum.setLevel2("合计");
@@ -308,8 +351,7 @@
        sum.setEquipmentId("合计");
        sum.setEquipmentName("合计");
        sum.setEquipmentType("合计");
        //计算合计值
        sum.setDataList(this.calculateTotal(result.getMdcEfficiencyList(), mdcUtilizationRateList));
        sum.setDataList(calculateTotal(listDtos, mdcUtilizationRateList));
        MdcEfficiencyListDto avg = new MdcEfficiencyListDto();
        avg.setLevel1("平均值");
@@ -318,12 +360,12 @@
        avg.setEquipmentId("平均值");
        avg.setEquipmentName("平均值");
        avg.setEquipmentType("平均值");
        //计算平均值
        avg.setDataList(this.calculateAverage(result.getMdcEfficiencyList(), mdcUtilizationRateList));
        avg.setDataList(calculateAverage(listDtos, mdcUtilizationRateList));
        result.getMdcEfficiencyList().add(sum);
        result.getMdcEfficiencyList().add(avg);
        return result;
    }
    /**
     * 计算平均值
@@ -454,7 +496,7 @@
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            result.setMdcEfficiencyList(listDtos);
        } else {
            List<String> equipmentIdList = mdcEquipmentService.listEquipmentId(vo);
            if (equipmentIdList != null && !equipmentIdList.isEmpty()) {
                vo.setEquipmentIdList(equipmentIdList);
@@ -1344,6 +1386,10 @@
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            return result;
        }
        vo.setDeviceCategoryList(Arrays.asList(vo.getDeviceCategory().split(",")));
        vo.setDeviceLevelList(Arrays.asList(vo.getDeviceLevel().split(",")));
        vo.setDriveTypeList(Arrays.asList(vo.getDriveType().split(",")));
        vo.setEquipmentTypeList(Arrays.asList(vo.getEquipmentType().split(",")));
        // 查询原始数据
        List<MdcComAnaDto> resultDtos = mdcEfficiencyReportMapper.comparativeAnalysis(vo);
@@ -1727,10 +1773,6 @@
                    List<MdcUtilizationResultDto> list = new ArrayList<>();
                    for (String date : dateList) {
//                        Date startTime = DateUtils.toDate(date + " 00:00:00", DateUtils.STR_DATE_TIME_SMALL);
//                        Date endTime = DateUtils.toDate(date + " 00:00:00", DateUtils.STR_DATE_TIME_SMALL);
//                        endTime = DateUtils.addDays(endTime, 1);
//                        list.add(this.utilizationRate(mdcEquDepDto.getEquipmentId(), mdcEquDepDto.getEquipmentName(), mdcEquDepDto.getEquipmentType(), startTime, endTime, date, mdcUtilizationRateList));
                        list.add(this.utilizationRateTrend(mdcEquDepDto.getEquipmentId(), mdcEquDepDto.getEquipmentName(), mdcEquDepDto.getEquipmentType(), date, mdcUtilizationRateList));
                    }
                    mdcEfficiencyListDto.setDataList(list);
@@ -1789,6 +1831,413 @@
        return mdcEfficiencyReportMapper.getEfficiencyRate(equipmentId, date);
    }
    /**
     * 利用率列表导出
     *
     * @param vo
     * @return
     */
    @Override
    public ModelAndView exportEfficiencyXls(MdcEfficiencyReportQueryVo vo){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // 获取设备 ID 列表
        List<String> equipmentIds = getEquipmentIds(user.getId(), vo);
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            vo.setEquipmentIdList(equipmentIds);
        }
        // 如果设备 ID 列表为空,直接返回
        if (vo.getEquipmentIdList() == null || vo.getEquipmentIdList().isEmpty()) {
            throw new JeecgBootException("无数据");
        }
        // 获取设备 ID 列表
        List<String> equipmentIdList = mdcEquipmentService.listEquipmentId(vo);
        if (equipmentIdList == null || equipmentIdList.isEmpty()) {
            throw new JeecgBootException("无数据");
        }
        vo.setEquipmentIdList(equipmentIdList);
        // 3. 准备设备数据
        List<MdcEfficiencyListDto> exportData = new ArrayList<>();
        // 获取日期列表并按月分组
        List<String> dates = DateUtils.getDatesStringList2(DateUtils.getShortDate2(vo.getStartTime()), DateUtils.getShortDate2(vo.getEndTime()));
        Map<String, List<String>> groupedByMonth = dates.stream()
                .collect(Collectors.groupingBy(
                        date -> date.substring(0, 6) // 提取年月(如 "202405")
                ));
        // 2. 提取每月的首尾日期
        List<Map<String, Object>> monthRanges = groupedByMonth.entrySet().stream()
                .map(entry -> {
                    List<String> monthDates = entry.getValue();
                    Map<String, Object> map = new HashMap<>();
                    map.put("startTime", monthDates.get(0));
                    map.put("endTime", monthDates.get(monthDates.size() - 1));
                    map.put("dateList", monthDates);
                    return map;
                })
                .collect(Collectors.toList());
        for (Map<String, Object> monthRange : monthRanges) {
            List<MdcEfficiencyListDto> listDtos = new ArrayList<>();
            vo.setStartTime((String) monthRange.get("startTime"));
            vo.setEndTime((String) monthRange.get("endTime"));
            List<String> dateList = (List<String>) monthRange.get("dateList");
            // 查询设备效率数据
            List<MdcEfficiencyDto> efficiencyList = mdcEfficiencyReportMapper.efficiencyList(vo);
            // 处理数据
            // 根据类型树处理不同的层级
            if ("2".equals(vo.getTypeTree())) {
                // 部门层级
                listDtos = processDeLevel(vo, efficiencyList, dateList);
            } else {
                // 产线层级
                listDtos = processProLevel(vo, efficiencyList, dateList);
            }
            if (exportData.isEmpty()) {
                exportData.addAll(listDtos);
            } else {
                Map<String, MdcEfficiencyListDto> map = listDtos.stream().collect(Collectors.toMap(MdcEfficiencyListDto::getEquipmentId, a -> a, (k1, k2) -> k1));
                exportData.forEach(mdcEfficiencyListDto -> {
                    List<MdcEfficiencyResultDto> dataList = mdcEfficiencyListDto.getDataList();
                    if (map.containsKey(mdcEfficiencyListDto.getEquipmentId())) {
                        MdcEfficiencyListDto mdcEfficiencyListDto1 = map.get(mdcEfficiencyListDto.getEquipmentId());
                        List<MdcEfficiencyResultDto> dataList1 = mdcEfficiencyListDto1.getDataList();
                        dataList.addAll(dataList1);
                        mdcEfficiencyListDto.setDataList(dataList);
                    }
                });
            }
        }
        // 数据排序
        exportData = exportData.stream().sorted(Comparator.comparing(MdcEfficiencyListDto::getLevel2)).sorted(Comparator.comparing(MdcEfficiencyListDto::getLevel3)).collect(Collectors.toList());
        // 1. 创建工作簿和工作表
        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("利用率数据");
        // 3. 准备设备数据
        // 创建样式
        CellStyle centerStyle = createCenterStyle(workbook);
        CellStyle fixedHeaderStyle = createFixedHeaderStyle(workbook);
        CellStyle dateHeaderStyle = createDateHeaderStyle(workbook);
        CellStyle metricHeaderStyle = createMetricHeaderStyle(workbook);
//        dates.add("合计");
//        dates.add("平均");
        // 写入标题行
        writeHeaderRows(sheet, fixedHeaderStyle, dateHeaderStyle, metricHeaderStyle, dates);
        // 写入数据行
        writeDataRows(sheet, centerStyle, exportData, dates);
        // 添加自适应列宽(所有列,从0开始到最后一列)
//        autoSizeAllColumns(sheet, 0, sheet.getRow(0).getLastCellNum() - 1);
        // 7. 将工作簿转换为字节数组输出流
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            workbook.write(bos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 具体实现可能需要根据你的项目结构进行调整
        ModelAndView mv = new ModelAndView(new CustomExcelView(bos.toByteArray()));
        return mv;
    }
    /**
     * 自适应所有列宽(处理合并单元格)
     * @param sheet 工作表
     * @param startCol 起始列索引
     * @param endCol 结束列索引
     */
    private void autoSizeAllColumns(Sheet sheet, int startCol, int endCol) {
        // 先处理合并单元格对列宽的影响
        adjustMergedCells(sheet);
        // 自动调整列宽
        for (int i = startCol; i <= endCol; i++) {
            sheet.autoSizeColumn(i);
            // 解决POI自动列宽过窄问题,增加额外宽度(单位:1/256字符宽度,这里增加200相当于约3个字符)
            sheet.setColumnWidth(i, sheet.getColumnWidth(i) + 1800);
        }
    }
    /**
     * 处理合并单元格,确保列宽计算正确
     * (POI的autoSizeColumn对合并单元格支持不佳,需要手动展开计算)
     */
    private void adjustMergedCells(Sheet sheet) {
        List<CellRangeAddress> mergedRegions = new ArrayList<>();
        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            mergedRegions.add(sheet.getMergedRegion(i));
        }
        for (CellRangeAddress region : mergedRegions) {
            int firstCol = region.getFirstColumn();
            int lastCol = region.getLastColumn();
            if (firstCol == lastCol) continue; // 单列合并无需处理
            // 计算合并区域的最大宽度
            int maxWidth = 0;
            for (int row = region.getFirstRow(); row <= region.getLastRow(); row++) {
                Row rowData = sheet.getRow(row);
                if (rowData == null) continue;
                Cell cell = rowData.getCell(firstCol);
                if (cell == null) continue;
                int width = getCellWidth(cell);
                if (width > maxWidth) {
                    maxWidth = width;
                }
            }
            // 应用到所有合并列(只设置首列宽度,POI会自动同步到合并列)
            sheet.setColumnWidth(firstCol, maxWidth);
        }
    }
    /**
     * 获取单元格内容宽度(考虑字体和内容)
     */
    private int getCellWidth(Cell cell) {
        // 通过Sheet获取Workbook,兼容性更好
        Workbook workbook = cell.getSheet().getWorkbook();
        CellStyle style = cell.getCellStyle();
        Font font = workbook.getFontAt(style.getFontIndex());
        String text = cell.getStringCellValue();
        // 计算文本宽度(粗略估计,可根据实际字体调整)
        int width = text.length() * 256;
        if (font.getBold()) width = (int) (width * 1.2); // 加粗字体增加20%宽度
        return width + 512; // 增加额外宽度
    }
    /**
     * 处理部门层级
     */
    private List<MdcEfficiencyListDto> processDeLevel(MdcEfficiencyReportQueryVo vo, List<MdcEfficiencyDto> efficiencyList, List<String> dates) {
        List<MdcEquDepDto> equipmentList = mdcEquipmentService.findEquDepList(vo.getEquipmentIdList());
        List<SysDepart> departList = sysDepartService.list(new LambdaQueryWrapper<SysDepart>()
                .ne(SysDepart::getDelFlag, CommonConstant.DEL_FLAG_1.toString())
                .orderByAsc(SysDepart::getDepartOrder));
        return equipmentList.stream().map(mdcEquDepDto -> {
            MdcEfficiencyListDto mdcEfficiencyListDto = createEfficiencyListDto(mdcEquDepDto);
            setDepartmentLevels(mdcEfficiencyListDto, mdcEquDepDto, departList);
            mdcEfficiencyListDto.setDataList(processEffData(efficiencyList, dates, mdcEquDepDto.getEquipmentId()));
            return mdcEfficiencyListDto;
        }).collect(Collectors.toList());
    }
    private List<MdcEfficiencyListDto> processProLevel(MdcEfficiencyReportQueryVo vo, List<MdcEfficiencyDto> efficiencyList, List<String> dates) {
        List<MdcEquProDto> equipmentList = mdcEquipmentService.findEquProList(vo.getEquipmentIdList());
        List<MdcProduction> productionList = mdcProductionService.list(new LambdaQueryWrapper<MdcProduction>()
                .ne(MdcProduction::getDelFlag, CommonConstant.DEL_FLAG_1.toString())
                .orderByAsc(MdcProduction::getProductionOrder));
        return equipmentList.stream().map(mdcEquProDto -> {
            MdcEfficiencyListDto mdcEfficiencyListDto = createEfficiencyListDto(mdcEquProDto);
            setProductionLevels(mdcEfficiencyListDto, mdcEquProDto, productionList);
            mdcEfficiencyListDto.setDataList(processEffData(efficiencyList, dates, mdcEquProDto.getEquipmentId()));
            return mdcEfficiencyListDto;
        }).collect(Collectors.toList());
    }
    private List<MdcEfficiencyResultDto> processEffData(List<MdcEfficiencyDto> efficiencyList, List<String> dates, String equipmentId) {
        List<MdcEfficiencyResultDto> list = new ArrayList<>();
//        MdcEfficiencyResultDto avgDto = new MdcEfficiencyResultDto();
//        avgDto.setTheDate("平均值");
//        MdcEfficiencyResultDto sumDto = new MdcEfficiencyResultDto();
//        sumDto.setTheDate("合计");
        for (String date : dates) {
            MdcEfficiencyResultDto dto = effRate(efficiencyList, date, equipmentId);
            list.add(dto);
        }
//        sumDto.setProcessLong(avgDto.getProcessLong());
//        avgDto.setProcessLong(avgDto.getProcessLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
//        avgDto.setUtilizationRate(avgDto.getUtilizationRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
//        sumDto.setUtilizationRate(avgDto.getUtilizationRate());
//        avgDto.setStartRate(avgDto.getStartRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
//        sumDto.setStartRate(avgDto.getStartRate());
//        avgDto.setOpenRate(avgDto.getOpenRate().divide(new BigDecimal(dates.size()), 4, RoundingMode.HALF_UP));
//        sumDto.setOpenRate(avgDto.getOpenRate());
//        sumDto.setOpenLong(avgDto.getOpenLong());
//        avgDto.setOpenLong(avgDto.getOpenLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
//        sumDto.setWaitLong(avgDto.getWaitLong());
//        avgDto.setWaitLong(avgDto.getWaitLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
//        sumDto.setCloseLong(avgDto.getCloseLong());
//        avgDto.setCloseLong(avgDto.getCloseLong().divide(new BigDecimal(dates.size()), 0, RoundingMode.HALF_UP));
//
//        list.add(sumDto);
//        list.add(avgDto);
        return list;
    }
    private MdcEfficiencyResultDto effRate(List<MdcEfficiencyDto> efficiencyList, String date, String equipmentId) {
        MdcEfficiencyResultDto mdcEfficiencyResultDto = new MdcEfficiencyResultDto();
        if (efficiencyList != null && !efficiencyList.isEmpty()) {
            Map<String, MdcEfficiencyDto> collect = new HashMap<>();
            efficiencyList.forEach(mdcEfficiencyDto -> {
                if (!collect.containsKey(mdcEfficiencyDto.getEquipmentId() + "_" + mdcEfficiencyDto.getTheDate())) {
                    collect.put(mdcEfficiencyDto.getEquipmentId() + "_" + mdcEfficiencyDto.getTheDate(), mdcEfficiencyDto);
                }
            });
            if (collect.containsKey(equipmentId + "_" + date)) {
                MdcEfficiencyDto efficiencyDto = collect.get(equipmentId + "_" + date);
                mdcEfficiencyResultDto.setTheDate(efficiencyDto.getTheDate());
                mdcEfficiencyResultDto.setProcessLong(efficiencyDto.getProcessLong().divide(new BigDecimal("3600"), 2, RoundingMode.HALF_UP));
                mdcEfficiencyResultDto.setUtilizationRate(efficiencyDto.getUtilizationRate().multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP));
                mdcEfficiencyResultDto.setStartRate(efficiencyDto.getStartRate().multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP));
                mdcEfficiencyResultDto.setOpenRate(efficiencyDto.getOpenRate().multiply(new BigDecimal("100")).setScale(2, RoundingMode.HALF_UP));
                mdcEfficiencyResultDto.setOpenLong(efficiencyDto.getOpenLong().divide(new BigDecimal("3600"), 2, RoundingMode.HALF_UP));
                mdcEfficiencyResultDto.setWaitLong(efficiencyDto.getWaitLong().divide(new BigDecimal("3600"), 2, RoundingMode.HALF_UP));
                mdcEfficiencyResultDto.setCloseLong(efficiencyDto.getCloseLong().divide(new BigDecimal("3600"), 2, RoundingMode.HALF_UP));
            } else {
                mdcEfficiencyResultDto.setTheDate(date);
            }
        } else {
            mdcEfficiencyResultDto.setTheDate(date);
            mdcEfficiencyResultDto.setProcessLong(new BigDecimal("0"));
            mdcEfficiencyResultDto.setUtilizationRate(new BigDecimal("0"));
            mdcEfficiencyResultDto.setStartRate(new BigDecimal("0"));
            mdcEfficiencyResultDto.setOpenRate(new BigDecimal("0"));
            mdcEfficiencyResultDto.setOpenLong(new BigDecimal("0"));
            mdcEfficiencyResultDto.setWaitLong(new BigDecimal("0"));
            mdcEfficiencyResultDto.setCloseLong(new BigDecimal("0"));
        }
        return mdcEfficiencyResultDto;
    }
    private void writeHeaderRows(Sheet sheet, CellStyle fixedHeaderStyle, CellStyle dateHeaderStyle, CellStyle metricHeaderStyle, List<String> dateList) {
        Row titleRow1 = sheet.createRow(0);
        Row titleRow2 = sheet.createRow(1);
        int colIndex = 0;
        // 写入固定列标题(公司、车间等)
        String[] fixedHeaders = {"公司", "车间", "工段", "设备ID", "设备名称", "设备类型"};
        for (int i = 0; i < fixedHeaders.length; i++) {
            createCellWithStyle(titleRow1, colIndex, fixedHeaders[i], fixedHeaderStyle);
            createCellWithStyle(titleRow2, colIndex, "", fixedHeaderStyle);
            // 合并单元格(跨行不跨列)
            CellRangeAddress region = new CellRangeAddress(0, 1, colIndex, colIndex);
            sheet.addMergedRegion(region);
            colIndex++;
        }
        // 写入日期,跨6列合并
        for (String date : dateList) {
            CellRangeAddress dateRegion = new CellRangeAddress(0, 0, colIndex, colIndex + 5);
            sheet.addMergedRegion(dateRegion);
            createCellWithStyle(titleRow1, colIndex, date, dateHeaderStyle);
            colIndex += 6;
        }
        // 写入日期指标标题(每个日期对应6列)
        colIndex = 6;
        for (String date : dateList) {
            createCellWithStyle(titleRow2, colIndex++, "利用率(%)", metricHeaderStyle);
            createCellWithStyle(titleRow2, colIndex++, "开机率(%)", metricHeaderStyle);
            createCellWithStyle(titleRow2, colIndex++, "开机时长(小时)", metricHeaderStyle);
            createCellWithStyle(titleRow2, colIndex++, "加工时长(小时)", metricHeaderStyle);
            createCellWithStyle(titleRow2, colIndex++, "待机时长(小时)", metricHeaderStyle);
            createCellWithStyle(titleRow2, colIndex++, "关机时长(小时)", metricHeaderStyle);
        }
    }
    private CellStyle createCenterStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        // 添加完整边框
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        return style;
    }
    private CellStyle createFixedHeaderStyle(Workbook workbook) {
        CellStyle style = createCenterStyle(workbook);
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        return style;
    }
    private CellStyle createDateHeaderStyle(Workbook workbook) {
        CellStyle style = createCenterStyle(workbook);
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        return style;
    }
    private CellStyle createMetricHeaderStyle(Workbook workbook) {
        CellStyle style = createCenterStyle(workbook);
        style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        return style;
    }
    private void writeDataRows(Sheet sheet, CellStyle style, List<MdcEfficiencyListDto> exportData, List<String> dateList) {
        int rowIndex = 2;
        for (MdcEfficiencyListDto device : exportData) {
            Row dataRow = sheet.createRow(rowIndex++);
            int colIndex = 0;
            // 写入固定列数据(公司、车间等)
            createCellWithStyle(dataRow, colIndex++, device.getLevel1(), style);
            createCellWithStyle(dataRow, colIndex++, device.getLevel2(), style);
            createCellWithStyle(dataRow, colIndex++, device.getLevel3(), style);
            createCellWithStyle(dataRow, colIndex++, device.getEquipmentId(), style);
            createCellWithStyle(dataRow, colIndex++, device.getEquipmentName(), style);
            createCellWithStyle(dataRow, colIndex++, device.getEquipmentType(), style);
            // 写入日期指标数据(每个日期对应6列)
            for (String date : dateList) {
                // 确保dateStr对应的Map已初始化
                MdcEfficiencyResultDto dateData = device.getDataList().stream().filter(mdcEfficiencyResultDto -> date.equals(mdcEfficiencyResultDto.getTheDate())).collect(Collectors.toList()).get(0);
                createCellWithStyle(dataRow, colIndex++, dateData.getUtilizationRate().doubleValue(), style);
                createCellWithStyle(dataRow, colIndex++, dateData.getOpenRate().doubleValue(), style);
                createCellWithStyle(dataRow, colIndex++, dateData.getOpenLong().doubleValue(), style);
                createCellWithStyle(dataRow, colIndex++, dateData.getProcessLong().doubleValue(), style);
                createCellWithStyle(dataRow, colIndex++, dateData.getWaitLong().doubleValue(), style);
                createCellWithStyle(dataRow, colIndex++, dateData.getCloseLong().doubleValue(), style);
            }
        }
    }
    private void createCellWithStyle(Row row, int colIndex, Object value, CellStyle style) {
        Cell cell = row.createCell(colIndex);
        if (value instanceof String) {
            cell.setCellValue((String) value);
        } else if (value instanceof Double) {
            cell.setCellValue((Double) value);
        }
        cell.setCellStyle(style);
    }
    private MdcUtilizationResultDto utilizationRate(String equipmentId, String equipmentName, String equipmentType, Date startTime, Date endTime, String date, List<MdcUtilizationRate> mdcUtilizationRateList) {
        MdcUtilizationResultDto dto = new MdcUtilizationResultDto();
        dto.setEquipmentId(equipmentId);