package cn.iocoder.yudao.module.pms.service.iotfivedailyreport;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotfivedailyreport.vo.IotFiveDailyReportPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotfivedailyreport.vo.IotFiveDailyReportSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotfivedailyreport.vo.IotFiveDailyReportStatisticsRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotfivedailyreport.IotFiveDailyReportDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotfivedailyreport.IotFiveDailyReportMapper;
import cn.iocoder.yudao.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_FIVE_DAILY_REPORT_NOT_EXISTS;

/**
 * 5#国日报 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotFiveDailyReportServiceImpl implements IotFiveDailyReportService {

    @Resource
    private IotFiveDailyReportMapper iotFiveDailyReportMapper;
    @Resource
    private DeptService deptService;

    @Override
    public Long createIotFiveDailyReport(IotFiveDailyReportSaveReqVO createReqVO) {
        // 插入
        IotFiveDailyReportDO iotFiveDailyReport = BeanUtils.toBean(createReqVO, IotFiveDailyReportDO.class);
        iotFiveDailyReportMapper.insert(iotFiveDailyReport);
        // 返回
        return iotFiveDailyReport.getId();
    }

    @Override
    public void updateIotFiveDailyReport(IotFiveDailyReportSaveReqVO updateReqVO) {
        // 校验存在
        validateIotFiveDailyReportExists(updateReqVO.getId());
        // 更新
        IotFiveDailyReportDO updateObj = BeanUtils.toBean(updateReqVO, IotFiveDailyReportDO.class);
        // 填报 保存后 修改日报 状态
        updateObj.setStatus(1);
        iotFiveDailyReportMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotFiveDailyReport(Long id) {
        // 校验存在
        validateIotFiveDailyReportExists(id);
        // 删除
        iotFiveDailyReportMapper.deleteById(id);
    }

    private void validateIotFiveDailyReportExists(Long id) {
        if (iotFiveDailyReportMapper.selectById(id) == null) {
            throw exception(IOT_FIVE_DAILY_REPORT_NOT_EXISTS);
        }
    }

    @Override
    public IotFiveDailyReportDO getIotFiveDailyReport(Long id) {
        return iotFiveDailyReportMapper.selectById(id);
    }

    @Override
    public PageResult<IotFiveDailyReportDO> getIotFiveDailyReportPage(IotFiveDailyReportPageReqVO pageReqVO) {
        // 查询选择部门下面所有子部门
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
            pageReqVO.setDeptIds(ids);
        }
        return iotFiveDailyReportMapper.selectPage(pageReqVO);
    }

    @Override
    public void batchAddDailyReports(List<IotFiveDailyReportDO> reports) {
        iotFiveDailyReportMapper.insertBatch(reports);
    }

    @Override
    public IotFiveDailyReportStatisticsRespVO totalWorkload(IotFiveDailyReportPageReqVO pageReqVO) {
        IotFiveDailyReportStatisticsRespVO result = new IotFiveDailyReportStatisticsRespVO();
        // 不分页统计所有数据
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            // 找到所有子部门对象集合
            ids.add(pageReqVO.getDeptId());
            pageReqVO.setDeptIds(ids);
        }
        // 检查contractName不为空但projectIds为空的情况
        if (StrUtil.isNotBlank(pageReqVO.getProjectName()) && (CollUtil.isEmpty(pageReqVO.getProjectIds()))) {
            return result;
        }
        // 检查taskName不为空但taskIds为空的情况
        if (StrUtil.isNotBlank(pageReqVO.getWellName()) && (CollUtil.isEmpty(pageReqVO.getTaskIds()))) {
            return result;
        }

        // key工作量标识   value累计 油耗
        Map<String, BigDecimal> cumulativeFuelsPair = new HashMap<>();
        // key工作量标识   value累计 段数 累计施工-层
        Map<String, BigDecimal> cumulativeWorkingLayersPair = new HashMap<>();
        // key工作量标识   value累计 井数
        Map<String, BigDecimal> cumulativeWorkingWellPair = new HashMap<>();

        // 查询时间区间内日报的 主设备泵车数量 累加
        BigDecimal totalMainDeviceNum = BigDecimal.ZERO;
        // 查询时间区间内日报的 作业泵车数量 累加
        BigDecimal totalWokDeviceNum = BigDecimal.ZERO;
        // 查询时间区间内日报的 压裂层 累加
        BigDecimal totalLayers = BigDecimal.ZERO;
        // 查询时间区间内日报的 注砂 累加
        BigDecimal totalSand = BigDecimal.ZERO;
        // 查询时间区间内日报的 注液 累加
        BigDecimal totalLiquid = BigDecimal.ZERO;
        // 查询时间区间内日报的 油耗 累加
        BigDecimal totalFuel = BigDecimal.ZERO;
        // 查询时间区间内日报的 非生产时间 累加
        BigDecimal totalNptTime = BigDecimal.ZERO;

        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<IotFiveDailyReportDO> pageReports = iotFiveDailyReportMapper.selectPage(pageReqVO);
        List<IotFiveDailyReportDO> reports = pageReports.getList();
        if (CollUtil.isNotEmpty(reports)) {
            for (IotFiveDailyReportDO report : reports) {
                // 当日主压层数(层)
                BigDecimal dailyLayers = report.getDailyWorkingLayers();
                // 当日加砂
                BigDecimal dailySand = report.getDailySandVolume();
                // 当日注液
                BigDecimal dailyLiquid = report.getDailyLiquidVolume();
                // 当日油耗
                BigDecimal dailyFuel = report.getDailyFuel();
                // 非生产时间
                BigDecimal nonProductionTime = report.getNonProductionTime();
                // 设备利用率  作业泵车数量/主设备泵车数量
                BigDecimal wokDeviceNum = report.getWokDeviceNum();
                BigDecimal mainDeviceNum = report.getMainDeviceNum();
                totalMainDeviceNum = totalMainDeviceNum.add(mainDeviceNum);
                totalWokDeviceNum = totalWokDeviceNum.add(wokDeviceNum);
                totalLayers = totalLayers.add(dailyLayers);
                totalSand = totalSand.add(dailySand);
                totalLiquid = totalLiquid.add(dailyLiquid);
                totalFuel = totalFuel.add(dailyFuel);
                totalNptTime = totalNptTime.add(nonProductionTime);
            }
            // 计算总的 设备利用率
            if (totalMainDeviceNum.compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal totalUtilizationRate = totalWokDeviceNum.divide(totalMainDeviceNum, 4, RoundingMode.HALF_UP );
                result.setUtilizationRate(totalUtilizationRate);
            }
            result.setTotalDailyFuel(totalFuel);
            result.setCumulativeWorkingLayers(totalLayers);
            result.setCumulativeMixSand(totalSand);
            result.setCumulativeLiquidVolume(totalLiquid);
            result.setCumulativeNptTime(totalNptTime);

        }

        // 查询全年的 工作量数据 默认当前时间所在年份

        // 查询当月完成的工作量


        return result;
    }

    @Override
    public List<IotFiveDailyReportStatisticsRespVO> summaryStatistics(IotFiveDailyReportPageReqVO pageReqVO) {
        List<IotFiveDailyReportStatisticsRespVO> result = new ArrayList<>();
        // 不分页统计所有数据
        Set<Long> ids = new HashSet<>();
        if (ObjUtil.isEmpty(pageReqVO.getDeptId())) {
            pageReqVO.setDeptId(388l);
        }
        ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
        // 找到所有子部门对象集合
        ids.add(pageReqVO.getDeptId());
        pageReqVO.setDeptIds(ids);
        // 检查contractName不为空但projectIds为空的情况
        if (StrUtil.isNotBlank(pageReqVO.getProjectName()) && (CollUtil.isEmpty(pageReqVO.getProjectIds()))) {
            return new ArrayList<>();
        }
        // 检查taskName不为空但taskIds为空的情况
        if (StrUtil.isNotBlank(pageReqVO.getWellName()) && (CollUtil.isEmpty(pageReqVO.getTaskIds()))) {
            return new ArrayList<>();
        }
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<IotFiveDailyReportDO> pageReports = iotFiveDailyReportMapper.selectPage(pageReqVO);
        List<IotFiveDailyReportDO> dailyReports = pageReports.getList();

        // 默认显示所有项目部的汇总数据(新疆分公司也展示 下属各项目部的数据)
        // 点击项目部 显示 下属队伍的数据
        // 首先判断点击的部门是属于 公司 还是 队伍 如果没有点击任何部门 默认查询所有项目部数据
        if (ObjUtil.isEmpty(pageReqVO.getDeptId())) {
            result = statisticsByProjectDept(dailyReports, 388L, pageReqVO);
        } else {
            // 判断点击的组织树中的部门类型 类型(公司级1 项目部2 队伍3)
            DeptDO selectedDept = deptService.getDept(pageReqVO.getDeptId());
            if ("1".equals(selectedDept.getType())) {
                // 以项目部为维度汇总数据
                result = statisticsByProjectDept(dailyReports, pageReqVO.getDeptId(), pageReqVO);
            } else if ("2".equals(selectedDept.getType())) {
                // 以队伍为维度汇总数据
                result = statisticsByProjectDepartment(dailyReports, pageReqVO);
            } else if ("3".equals(selectedDept.getType())) {
                // 显示单个队伍的汇总数据
                result = statisticsByProjectDepartment(dailyReports, pageReqVO);
            } else {
                // 点击的部门没有类型 判断部门下的是否包含 项目部类型部门 新疆分公司
                // 以项目部为维度汇总数据
                result = statisticsByProjectDept(dailyReports, pageReqVO.getDeptId(), pageReqVO);
            }
        }
        // 根据result集合内对象的 sort 属性正序排列 sort 类型为 integer 类型
        if (CollUtil.isNotEmpty(result)) {
            result.sort(Comparator.comparing(
                    IotFiveDailyReportStatisticsRespVO::getSort,
                    Comparator.nullsLast(Comparator.naturalOrder())
            ));
        }
        return result;
    }


    /**
     * 按项目部维度 汇总 统计数据
     * @param dailyReports 日报数据列表
     * @param rootDeptId 根部门ID（如 388L为 5#国 根部门，或其他公司级部门ID）
     * @return 项目部维度统计结果列表
     */
    private List<IotFiveDailyReportStatisticsRespVO> statisticsByProjectDept(List<IotFiveDailyReportDO> dailyReports,
                                                                           Long rootDeptId, IotFiveDailyReportPageReqVO pageReqVO) {
        List<IotFiveDailyReportStatisticsRespVO> result = new ArrayList<>();

        Set<Long> projectDeptIds = new HashSet<>();
        // key项目部id    value项目部名称
        Map<Long, DeptDO> projectDeptPair = new HashMap<>();
        // key部门id   value部门parentId
        Map<Long, Long> teamProjectIdPair = new HashMap<>();
        // key队伍id    value队伍部门
        Map<Long, DeptDO> teamDeptPair = new HashMap<>();

        // key队伍id/项目部id   value累计 桥塞(个数)
        Map<Long, BigDecimal> cumulativeBridgePlugPair = new HashMap<>();
        // key队伍id/项目部id   value累计 注液
        Map<Long, BigDecimal> cumulativeLiquidPair = new HashMap<>();
        // key队伍id/项目部id   value累计 作业泵车数量
        Map<Long, BigDecimal> cumulativeWorkDevicePair = new HashMap<>();
        // key队伍id/项目部id   value累计 油耗
        Map<Long, BigDecimal> cumulativeFuelsPair = new HashMap<>();
        // key队伍id/项目部id   value累计 累计主设备泵车数量
        Map<Long, BigDecimal> cumulativeMainDevicePair = new HashMap<>();
        // key队伍id/项目部id   value累计 段数 累计施工-层
        Map<Long, BigDecimal> cumulativeWorkingLayersPair = new HashMap<>();
        // key队伍id/项目部id   value累计 台次 当日仪表/混砂
        Map<Long, BigDecimal> cumulativeMixSandPair = new HashMap<>();

        // 队伍id 集合
        Set<Long> teamIds = new HashSet<>();

        // 以项目部为维度统计数据
        // 找到所有项目部与队伍的对应关系
        // 查询指定根部门下的所有子部门
        Set<Long> allRhChildDeptIds = deptService.getChildDeptIdListFromCache(rootDeptId);
        DeptListReqVO reqVO = new DeptListReqVO();
        reqVO.setDeptIds(allRhChildDeptIds);
        List<DeptDO> depts = deptService.getDeptList(reqVO);

        depts.forEach(dept -> {
            if ("2".equals(dept.getType())) {
                // 项目部
                projectDeptIds.add(dept.getId());
                projectDeptPair.put(dept.getId(), dept);
            }
            if ("3".equals(dept.getType())) {
                // 队伍
                teamIds.add(dept.getId());
                teamDeptPair.put(dept.getId(), dept);
            }
            // 可能是项目部 也可能是队伍
            teamProjectIdPair.put(dept.getId(), dept.getParentId());
        });

        // 计算指定时间区间内包含的天数
        long daysCount;
        if (ObjUtil.isNotEmpty(pageReqVO.getCreateTime()) && pageReqVO.getCreateTime().length >= 2) {
            LocalDateTime start = pageReqVO.getCreateTime()[0];
            LocalDateTime end = pageReqVO.getCreateTime()[1];

            if (ObjUtil.isNotEmpty(start) && ObjUtil.isNotEmpty(end) && !end.isBefore(start)) {
                // 使用ChronoUnit.DAYS.between计算天数差，并+1包含首尾两天
                daysCount = ChronoUnit.DAYS.between(
                        start.toLocalDate(),
                        end.toLocalDate()
                ) + 1;
            } else {
                daysCount = 0L;
            }
        } else {
            daysCount = 0L;
        }

        // 累计计算各项 工作量
        if (CollUtil.isNotEmpty(dailyReports)) {
            dailyReports.forEach(report -> {
                // 油耗
                BigDecimal dailyFuel = report.getDailyFuel();
                // 当日主压层数(层)
                BigDecimal dailyLayers = report.getDailyWorkingLayers();
                // 当日加砂
                BigDecimal dailySand = report.getDailySandVolume();
                // 当日注液
                BigDecimal dailyLiquid = report.getDailyLiquidVolume();
                // 设备利用率  作业泵车数量/主设备泵车数量
                BigDecimal wokDeviceNum = report.getWokDeviceNum();
                BigDecimal mainDeviceNum = report.getMainDeviceNum();

                if (ObjUtil.isNotEmpty(report.getDeptId()) && teamProjectIdPair.containsKey(report.getDeptId())) {
                    // projectDeptId可能是项目部 也可能是项目的上级
                    Long projectDeptId = 0l;
                    if (teamDeptPair.containsKey(report.getDeptId())) {
                        // 日报deptId如果是队伍 获取队伍的上级项目部
                        projectDeptId = teamProjectIdPair.get(report.getDeptId());
                    }
                    if (projectDeptPair.containsKey(report.getDeptId())) {
                        // 日报deptId如果是项目部 使用当前项目部
                        projectDeptId = report.getDeptId();
                    }

                    if (ObjUtil.isNotEmpty(projectDeptId) && (projectDeptId > 0)) {
                        // 累计油耗
                        if (cumulativeFuelsPair.containsKey(projectDeptId)) {
                            BigDecimal existTotalFuel = cumulativeFuelsPair.get(projectDeptId);
                            BigDecimal tempTotalFuel = existTotalFuel.add(dailyFuel);
                            cumulativeFuelsPair.put(projectDeptId, tempTotalFuel);
                        } else {
                            cumulativeFuelsPair.put(projectDeptId, dailyFuel);
                        }
                        // 累计主压层数(层)
                        if (cumulativeWorkingLayersPair.containsKey(projectDeptId)) {
                            BigDecimal existTotalLayers = cumulativeWorkingLayersPair.get(projectDeptId);
                            BigDecimal tempTotalLayers = existTotalLayers.add(dailyLayers);
                            cumulativeWorkingLayersPair.put(projectDeptId, tempTotalLayers);
                        } else {
                            cumulativeWorkingLayersPair.put(projectDeptId, dailyLayers);
                        }
                        // 累计加砂
                        if (cumulativeMixSandPair.containsKey(projectDeptId)) {
                            BigDecimal existTotalSand = cumulativeMixSandPair.get(projectDeptId);
                            BigDecimal tempTotalSand = existTotalSand.add(dailySand);
                            cumulativeMixSandPair.put(projectDeptId, tempTotalSand);
                        } else {
                            cumulativeMixSandPair.put(projectDeptId, dailySand);
                        }
                        // 累计注液
                        if (cumulativeLiquidPair.containsKey(projectDeptId)) {
                            BigDecimal existTotalLiquid = cumulativeLiquidPair.get(projectDeptId);
                            BigDecimal tempTotalLiquid = existTotalLiquid.add(dailyLiquid);
                            cumulativeLiquidPair.put(projectDeptId, tempTotalLiquid);
                        } else {
                            cumulativeLiquidPair.put(projectDeptId, dailyLiquid);
                        }
                        // 累计作业泵车数量
                        if (cumulativeWorkDevicePair.containsKey(projectDeptId)) {
                            BigDecimal existTotalWorkNum = cumulativeWorkDevicePair.get(projectDeptId);
                            BigDecimal tempWorkDeviceNum = existTotalWorkNum.add(wokDeviceNum);
                            cumulativeWorkDevicePair.put(projectDeptId, tempWorkDeviceNum);
                        } else {
                            cumulativeWorkDevicePair.put(projectDeptId, wokDeviceNum);
                        }
                        // 累计主设备数量
                        if (cumulativeMainDevicePair.containsKey(projectDeptId)) {
                            BigDecimal existMainDeviceNum = cumulativeMainDevicePair.get(projectDeptId);
                            BigDecimal tempMainDeviceNum = existMainDeviceNum.add(mainDeviceNum);
                            cumulativeMainDevicePair.put(projectDeptId, tempMainDeviceNum);
                        } else {
                            cumulativeMainDevicePair.put(projectDeptId, mainDeviceNum);
                        }
                    }
                }
            });
        }

        // 生成返回的数据列表集合
        projectDeptPair.forEach((deptId, dept) -> {
            IotFiveDailyReportStatisticsRespVO statistics = new IotFiveDailyReportStatisticsRespVO();
            statistics.setProjectDeptId(deptId);
            statistics.setProjectDeptName(dept.getName());
            statistics.setSort(dept.getSort());
            statistics.setType("2");
            // 计算项目部下设备利用率
            if (CollUtil.isNotEmpty(cumulativeWorkDevicePair) && CollUtil.isNotEmpty(cumulativeMainDevicePair)) {
                BigDecimal workDeviceNum = cumulativeWorkDevicePair.get(deptId);
                BigDecimal mainDeviceNum = cumulativeMainDevicePair.get(deptId);
                if (ObjUtil.isNotEmpty(mainDeviceNum) && mainDeviceNum.compareTo(BigDecimal.ZERO) > 0) {
                    BigDecimal utilizationRate = workDeviceNum.divide(mainDeviceNum, 4, RoundingMode.HALF_UP);
                    statistics.setUtilizationRate(utilizationRate);
                }
            }
            statistics.setCumulativeWorkingLayers(cumulativeWorkingLayersPair.get(deptId));
            statistics.setCumulativeMixSand(cumulativeMixSandPair.get(deptId));
            statistics.setCumulativeLiquidVolume(cumulativeLiquidPair.get(deptId));
            BigDecimal cumulativeFuel = cumulativeFuelsPair.get(deptId);
            if (ObjUtil.isNotEmpty(cumulativeFuel)) {
                // 油耗单位转换成 万升
                BigDecimal FuelWan = cumulativeFuel
                        .divide(BigDecimal.valueOf(10000), 2, RoundingMode.HALF_UP);
                statistics.setTotalDailyFuel(FuelWan);
            }

            result.add(statistics);
        });
        return result;
    }

    /**
     * 按 队伍 维度统计 汇总 数据
     * @param dailyReports 日报数据列表
     * @param pageReqVO (deptId)项目部ID 或 队伍id
     * @return 队伍 维度统计结果列表
     */
    private List<IotFiveDailyReportStatisticsRespVO> statisticsByProjectDepartment(List<IotFiveDailyReportDO> dailyReports, IotFiveDailyReportPageReqVO pageReqVO) {
        List<IotFiveDailyReportStatisticsRespVO> result = new ArrayList<>();

        Set<Long> teamDeptIds = new HashSet<>();
        // key项目部id    value项目部名称
        Map<Long, DeptDO> projectDeptPair = new HashMap<>();
        // key队伍id    value队伍名称
        Map<Long, DeptDO> teamDeptPair = new HashMap<>();
        // key部门id   value部门parentId
        Map<Long, Long> teamProjectIdPair = new HashMap<>();

        // key队伍id/项目部id   value累计 作业泵车数量
        Map<Long, BigDecimal> cumulativeWorkDevicePair = new HashMap<>();
        // key队伍id/项目部id   value累计 趟数
        Map<Long, BigDecimal> cumulativeLiquidPair = new HashMap<>();
        // key队伍id/项目部id   value累计 油耗
        Map<Long, BigDecimal> cumulativeFuelsPair = new HashMap<>();
        // key队伍id/项目部id   value累计 段数 累计施工-层
        Map<Long, BigDecimal> cumulativeWorkingLayersPair = new HashMap<>();
        // key队伍id/项目部id   value累计 台次 当日仪表/混砂
        Map<Long, BigDecimal> cumulativeMixSandPair = new HashMap<>();
        // key队伍id/项目部id   value累计 主设备泵车数量
        Map<Long, BigDecimal> cumulativeMainDevicePair = new HashMap<>();

        // 以 队伍 为维度统计数据
        // 找到所有项目部与队伍的对应关系
        // 查询指定根部门下的所有子部门
        Set<Long> allRhChildDeptIds = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
        DeptListReqVO reqVO = new DeptListReqVO();
        // 查询某支队伍 或 所属项目部的信息
        allRhChildDeptIds.add(pageReqVO.getDeptId());
        reqVO.setDeptIds(allRhChildDeptIds);
        List<DeptDO> depts = deptService.getDeptList(reqVO);

        // 构建项目部映射和父子部门关系
        depts.forEach(dept -> {
            if ("3".equals(dept.getType())) {
                // 队伍
                teamDeptIds.add(dept.getId());
                teamDeptPair.put(dept.getId(), dept);
            }
            if ("2".equals(dept.getType())) {
                // 项目部
                projectDeptPair.put(dept.getId(), dept);
            }
            teamProjectIdPair.put(dept.getId(), dept.getParentId());
        });

        LocalDateTime[] createTime = pageReqVO.getCreateTime();
        List<Long> daysCounts = new ArrayList<>();
        // 如果 createTime 包含的时间是 1天则查询 这1天时间内的 队伍总数 施工队伍数量 施工准备队伍数量 驻地待命队伍数量
        if (ObjUtil.isNotEmpty(createTime)) {
            LocalDateTime startTime = createTime[0];
            LocalDateTime endTime = createTime[1];
            if (ObjUtil.isNotEmpty(startTime) && ObjUtil.isNotEmpty(endTime) && !endTime.isBefore(startTime)) {
                // 查询时间区间包含的天数
                // 使用ChronoUnit.DAYS.between计算天数差，并+1包含首尾两天
                long daysCount = ChronoUnit.DAYS.between(
                        startTime.toLocalDate(),
                        endTime.toLocalDate()
                ) + 1;
                daysCounts.add(daysCount);
            }
        }

        // 累计计算 工作量
        if (CollUtil.isNotEmpty(dailyReports)) {
            dailyReports.forEach(report -> {
                // 油耗
                BigDecimal dailyFuel = report.getDailyFuel();
                // 当日主压层数(层)
                BigDecimal dailyLayers = report.getDailyWorkingLayers();
                // 当日加砂
                BigDecimal dailySand = report.getDailySandVolume();
                // 当日注液
                BigDecimal dailyLiquid = report.getDailyLiquidVolume();
                // 设备利用率  作业泵车数量/主设备泵车数量
                BigDecimal wokDeviceNum = report.getWokDeviceNum();
                BigDecimal mainDeviceNum = report.getMainDeviceNum();

                if (ObjUtil.isNotEmpty(report.getDeptId()) && teamProjectIdPair.containsKey(report.getDeptId())) {
                    // projectDeptId 可能是项目部 也可能是项目的上级
                    Long detailDeptId = 0l;
                    detailDeptId = report.getDeptId();
                    if (ObjUtil.isNotEmpty(detailDeptId) && (detailDeptId > 0)) {
                        // 累计油耗
                        if (cumulativeFuelsPair.containsKey(detailDeptId)) {
                            BigDecimal existTotalFuel = cumulativeFuelsPair.get(detailDeptId);
                            BigDecimal tempTotalFuel = existTotalFuel.add(dailyFuel);
                            cumulativeFuelsPair.put(detailDeptId, tempTotalFuel);
                        } else {
                            cumulativeFuelsPair.put(detailDeptId, dailyFuel);
                        }
                        // 累计主压层数(层)
                        if (cumulativeWorkingLayersPair.containsKey(detailDeptId)) {
                            BigDecimal existTotalLayers = cumulativeWorkingLayersPair.get(detailDeptId);
                            BigDecimal tempTotalLayers = existTotalLayers.add(dailyLayers);
                            cumulativeWorkingLayersPair.put(detailDeptId, tempTotalLayers);
                        } else {
                            cumulativeWorkingLayersPair.put(detailDeptId, dailyLayers);
                        }
                        // 累计加砂
                        if (cumulativeMixSandPair.containsKey(detailDeptId)) {
                            BigDecimal existTotalSand = cumulativeMixSandPair.get(detailDeptId);
                            BigDecimal tempTotalSand = existTotalSand.add(dailySand);
                            cumulativeMixSandPair.put(detailDeptId, tempTotalSand);
                        } else {
                            cumulativeMixSandPair.put(detailDeptId, dailySand);
                        }
                        // 累计注液
                        if (cumulativeLiquidPair.containsKey(detailDeptId)) {
                            BigDecimal existTotalLiquid = cumulativeLiquidPair.get(detailDeptId);
                            BigDecimal tempTotalLiquid = existTotalLiquid.add(dailyLiquid);
                            cumulativeLiquidPair.put(detailDeptId, tempTotalLiquid);
                        } else {
                            cumulativeLiquidPair.put(detailDeptId, dailyLiquid);
                        }
                        // 累计作业泵车数量
                        if (cumulativeWorkDevicePair.containsKey(detailDeptId)) {
                            BigDecimal existTotalWorkNum = cumulativeWorkDevicePair.get(detailDeptId);
                            BigDecimal tempWorkDeviceNum = existTotalWorkNum.add(wokDeviceNum);
                            cumulativeWorkDevicePair.put(detailDeptId, tempWorkDeviceNum);
                        } else {
                            cumulativeWorkDevicePair.put(detailDeptId, wokDeviceNum);
                        }
                        // 累计主设备数量
                        if (cumulativeMainDevicePair.containsKey(detailDeptId)) {
                            BigDecimal existMainDeviceNum = cumulativeMainDevicePair.get(detailDeptId);
                            BigDecimal tempMainDeviceNum = existMainDeviceNum.add(mainDeviceNum);
                            cumulativeMainDevicePair.put(detailDeptId, tempMainDeviceNum);
                        } else {
                            cumulativeMainDevicePair.put(detailDeptId, mainDeviceNum);
                        }
                    }
                }
            });
            // 计算每个队伍的设备利用率 (作业泵车数量) / (主设备泵车数量)
        }

        // 队伍数据  生成返回的数据列表集合
        teamDeptPair.forEach((teamDeptId, dept) -> {
            IotFiveDailyReportStatisticsRespVO statistics = new IotFiveDailyReportStatisticsRespVO();
            statistics.setTeamId(teamDeptId);
            statistics.setTeamName(dept.getName());
            statistics.setSort(dept.getSort());
            if (teamDeptPair.containsKey(teamDeptId)) {
                statistics.setType("3");
            }
            // 计算队伍设备利用率
            if (CollUtil.isNotEmpty(cumulativeWorkDevicePair) && CollUtil.isNotEmpty(cumulativeMainDevicePair)) {
                BigDecimal workDeviceNum = cumulativeWorkDevicePair.get(teamDeptId);
                BigDecimal mainDeviceNum = cumulativeMainDevicePair.get(teamDeptId);
                if (ObjUtil.isNotEmpty(mainDeviceNum) && mainDeviceNum.compareTo(BigDecimal.ZERO) > 0) {
                    BigDecimal utilizationRate = workDeviceNum.divide(mainDeviceNum, 4, RoundingMode.HALF_UP);
                    statistics.setUtilizationRate(utilizationRate);
                }
            }
            statistics.setCumulativeWorkingLayers(cumulativeWorkingLayersPair.get(teamDeptId));
            statistics.setCumulativeMixSand(cumulativeMixSandPair.get(teamDeptId));
            statistics.setCumulativeLiquidVolume(cumulativeLiquidPair.get(teamDeptId));
            BigDecimal cumulativeFuel = cumulativeFuelsPair.get(teamDeptId);
            if (ObjUtil.isNotEmpty(cumulativeFuel)) {
                // 油耗单位转换成 万升
                BigDecimal FuelWan = cumulativeFuel
                        .divide(BigDecimal.valueOf(10000), 2, RoundingMode.HALF_UP);
                statistics.setTotalDailyFuel(FuelWan);
            }

            result.add(statistics);
        });

        return result;
    }

}