package jnpf.cultivate.v2.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import jnpf.cultivate.mapper.*;
import jnpf.cultivate.service.FtbCultivateExamUserService;
import jnpf.cultivate.service.FtbCultivateLearnTaskAssignmentService;
import jnpf.cultivate.service.FtbCultivateLearnTaskCourseService;
import jnpf.cultivate.v2.service.V2CultivateTaskCountService;
import jnpf.model.cultivate.po.learn.*;
import jnpf.model.cultivate.po.task.FtbCultivateTaskLog;
import jnpf.model.cultivate.v2.task.req.V2CultivateTaskCountReq;
import jnpf.model.cultivate.v2.task.vo.V2CultivateTaskCountVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * V2培训任务统计服务实现
 *
 * @author system
 * @create 2026-05-06
 */
@Slf4j
@Service
public class V2CultivateTaskCountServiceImpl implements V2CultivateTaskCountService {

    @Autowired
    private FtbCultivateLearnTaskMapper ftbCultivateLearnTaskMapper;

    @Autowired
    private FtbCultivateLearnTaskCourseMapper ftbCultivateLearnTaskCourseMapper;

    @Autowired
    private FtbCultivateLearnTaskExamMapper ftbCultivateLearnTaskExamMapper;

    @Autowired
    private FtbCultivateLearnTaskIdentificationMapper ftbCultivateLearnTaskIdentificationMapper;

    @Autowired
    private FtbCultivateLearnTaskCertificateMapper ftbCultivateLearnTaskCertificateMapper;

    @Autowired
    private FtbCultivateLearnTaskAssignmentMapper ftbCultivateLearnTaskAssignmentMapper;

    @Autowired
    private FtbCultivateTaskLogMapper ftbCultivateTaskLogMapper;

    @Autowired
    private FtbCultivateExamUserService ftbCultivateExamUserService;

    @Autowired
    private FtbCultivateLearnTaskCourseService ftbCultivateLearnTaskCourseService;

    @Autowired
    private FtbCultivateLearnTaskAssignmentService ftbCultivateLearnTaskAssignmentService;

    @Autowired
    private jnpf.cultivate.mapper.FtbCultivateCourseMapper ftbCultivateCourseMapper;

    @Autowired
    private jnpf.cultivate.mapper.FtbCultivateExamMapper ftbCultivateExamMapper;

    @Autowired
    private jnpf.cultivate.mapper.CultivateIdentifyTableMapper cultivateIdentifyTableMapper;

    @Autowired
    private jnpf.cultivate.mapper.FtbCultivateCertificateMapper ftbCultivateCertificateMapper;

    @Override
    public PageInfo<V2CultivateTaskCountVo> queryTaskCountList(V2CultivateTaskCountReq req) {
        // 1. 查询任务列表
        Page<V2CultivateTaskCountVo> page = ftbCultivateLearnTaskMapper.queryV2TaskCountList(
                Page.of(req.getCurrentPage(), req.getPageSize()),
                req.getTaskName(),
                req.getStatus(),
                req.getCreatorTimeStart(),
                req.getCreatorTimeEnd()
        );

        List<V2CultivateTaskCountVo> records = page.getRecords();
        if (CollUtil.isEmpty(records)) {
            PageInfo<V2CultivateTaskCountVo> pageInfo = new PageInfo<>();
            pageInfo.setList(new ArrayList<>());
            pageInfo.setPageSize((int) page.getSize());
            pageInfo.setPageNum((int) page.getCurrent());
            pageInfo.setTotal((int) page.getTotal());
            return pageInfo;
        }

        // 2. 提取任务ID列表
        List<String> taskIds = records.stream()
                .map(V2CultivateTaskCountVo::getId)
                .collect(Collectors.toList());

        // 3. 批量查询各项统计数据
        Map<String, Integer> courseNumMap = getCourseNumMap(taskIds);
        Map<String, List<String>> examNamesMap = getExamNamesMap(taskIds);
        Map<String, List<String>> identificationNamesMap = getIdentificationNamesMap(taskIds);
        Map<String, List<String>> certificateNamesMap = getCertificateNamesMap(taskIds);
        Map<String, Integer> assignedUserCountMap = getAssignedUserCountMap(taskIds);
        Map<String, Integer> completedUserCountMap = getCompletedUserCountMap(taskIds);
        Map<String, Integer> issuedCertificateCountMap = getIssuedCertificateCountMap(taskIds);

        // 4. 从任务日志中计算完课率、考试和鉴定相关指标
        Map<String, V2TaskLogStatistics> taskLogStatsMap = getTaskLogStatisticsMap(taskIds);

        // 5. 填充统计数据
        for (V2CultivateTaskCountVo vo : records) {
            String taskId = vo.getId();

            // 课程数量
            vo.setCourseNum(courseNumMap.getOrDefault(taskId, 0));

            // 考试名称列表
            vo.setExamNames(examNamesMap.getOrDefault(taskId, new ArrayList<>()));

            // 鉴定表名称列表
            vo.setIdentificationNames(identificationNamesMap.getOrDefault(taskId, new ArrayList<>()));

            // 证书名称列表
            vo.setCertificateNames(certificateNamesMap.getOrDefault(taskId, new ArrayList<>()));

            // 应完成人数
            vo.setAssignedUserCount(assignedUserCountMap.getOrDefault(taskId, 0));

            // 任务完成人数
            vo.setCompletedUserCount(completedUserCountMap.getOrDefault(taskId, 0));

            // 任务完成率
            Integer assignedCount = vo.getAssignedUserCount();
            Integer completedCount = vo.getCompletedUserCount();
            vo.setTaskCompleteRate(calculateRate(completedCount, assignedCount));

            // 已颁发证书数量
            vo.setIssuedCertificateCount(issuedCertificateCountMap.getOrDefault(taskId, 0));

            // 从任务日志中获取统计数据
            V2TaskLogStatistics stats = taskLogStatsMap.get(taskId);
            if (stats != null) {
                // 完课率
                vo.setCompleteCourseRate(calculateRate(stats.getCompleteCourseUserCount(), assignedCount));

                // 参与考试人数
                vo.setExamParticipantCount(stats.getExamParticipantCount());

                // 考试合格率
                vo.setExamPassRate(calculateRate(stats.getExamPassCount(), stats.getExamParticipantCount()));

                // 鉴定人数
                vo.setIdentifyParticipantCount(stats.getIdentifyParticipantCount());

                // 鉴定合格率
                vo.setIdentifyPassRate(calculateRate(stats.getIdentifyPassCount(), stats.getIdentifyParticipantCount()));
            } else {
                vo.setCompleteCourseRate("0%");
                vo.setExamParticipantCount(0);
                vo.setExamPassRate("0%");
                vo.setIdentifyParticipantCount(0);
                vo.setIdentifyPassRate("0%");
            }
        }

        // 6. 构建分页结果
        PageInfo<V2CultivateTaskCountVo> pageInfo = new PageInfo<>();
        pageInfo.setList(records);
        pageInfo.setPageSize((int) page.getSize());
        pageInfo.setPageNum((int) page.getCurrent());
        pageInfo.setTotal((int) page.getTotal());

        return pageInfo;
    }

    /**
     * 获取每个任务的课程数量（未删除、上架状态）
     */
    private Map<String, Integer> getCourseNumMap(List<String> taskIds) {
        Map<String, Integer> result = new HashMap<>();
        if (CollUtil.isEmpty(taskIds)) {
            return result;
        }

        // 1. 查询任务关联的课程（未删除）
        LambdaQueryWrapper<FtbCultivateLearnTaskCourse> wrapper = Wrappers.lambdaQuery();
        wrapper.in(FtbCultivateLearnTaskCourse::getTaskId, taskIds)
                .eq(FtbCultivateLearnTaskCourse::getEnableMark, 0);

        List<FtbCultivateLearnTaskCourse> taskCourses = ftbCultivateLearnTaskCourseMapper.selectList(wrapper);
        if (CollUtil.isEmpty(taskCourses)) {
            return result;
        }

        // 2. 提取所有课程ID
        List<String> courseIds = taskCourses.stream()
                .map(FtbCultivateLearnTaskCourse::getCourseId)
                .filter(StringUtils::isNotBlank)
                .distinct()
                .collect(Collectors.toList());

        if (CollUtil.isEmpty(courseIds)) {
            return result;
        }

        // 3. 查询课程表，过滤出上架且未删除的课程
        LambdaQueryWrapper<jnpf.model.cultivate.po.course.FtbCultivateCourse> courseWrapper = Wrappers.lambdaQuery();
        courseWrapper.in(jnpf.model.cultivate.po.course.FtbCultivateCourse::getId, courseIds)
                .eq(jnpf.model.cultivate.po.course.FtbCultivateCourse::getIsGroundIng, 1) // 1-上架
                .eq(jnpf.model.cultivate.po.course.FtbCultivateCourse::getEnableMark, 0); // 0-有效

        List<jnpf.model.cultivate.po.course.FtbCultivateCourse> validCourses = ftbCultivateCourseMapper.selectList(courseWrapper);
        Set<String> validCourseIds = validCourses.stream()
                .map(jnpf.model.cultivate.po.course.FtbCultivateCourse::getId)
                .collect(Collectors.toSet());

        // 4. 统计每个任务的有效课程数量
        result = taskCourses.stream()
                .filter(tc -> validCourseIds.contains(tc.getCourseId()))
                .collect(Collectors.groupingBy(
                        FtbCultivateLearnTaskCourse::getTaskId,
                        Collectors.collectingAndThen(Collectors.counting(), Long::intValue)
                ));

        return result;
    }

    /**
     * 获取每个任务的考试名称列表（去重）
     */
    private Map<String, List<String>> getExamNamesMap(List<String> taskIds) {
        Map<String, List<String>> result = new HashMap<>();
        if (CollUtil.isEmpty(taskIds)) {
            return result;
        }

        // 1. 查询任务关联的考试（未删除）
        LambdaQueryWrapper<FtbCultivateLearnTaskExam> wrapper = Wrappers.lambdaQuery();
        wrapper.in(FtbCultivateLearnTaskExam::getTaskId, taskIds)
                .eq(FtbCultivateLearnTaskExam::getEnableMark, 0);

        List<FtbCultivateLearnTaskExam> taskExams = ftbCultivateLearnTaskExamMapper.selectList(wrapper);
        if (CollUtil.isEmpty(taskExams)) {
            return result;
        }

        // 2. 提取所有考试ID（去重）
        List<String> examIds = taskExams.stream()
                .map(FtbCultivateLearnTaskExam::getExamId)
                .filter(StringUtils::isNotBlank)
                .distinct()
                .collect(Collectors.toList());

        if (CollUtil.isEmpty(examIds)) {
            return result;
        }

        // 3. 查询考试表，获取考试名称
        LambdaQueryWrapper<jnpf.model.cultivate.po.exam.FtbCultivateExam> examWrapper = Wrappers.lambdaQuery();
        examWrapper.in(jnpf.model.cultivate.po.exam.FtbCultivateExam::getId, examIds)
                .eq(jnpf.model.cultivate.po.exam.FtbCultivateExam::getEnabledMark, 1); // 1-有效

        List<jnpf.model.cultivate.po.exam.FtbCultivateExam> exams = ftbCultivateExamMapper.selectList(examWrapper);
        Map<String, String> examNameMap = exams.stream()
                .filter(e -> StringUtils.isNotBlank(e.getExamName()))
                .collect(Collectors.toMap(
                        jnpf.model.cultivate.po.exam.FtbCultivateExam::getId,
                        jnpf.model.cultivate.po.exam.FtbCultivateExam::getExamName,
                        (v1, v2) -> v1 // 如果有重复，保留第一个
                ));

        // 4. 按任务ID分组，收集考试名称
        Map<String, List<String>> taskExamMap = new HashMap<>();
        for (FtbCultivateLearnTaskExam taskExam : taskExams) {
            String examName = examNameMap.get(taskExam.getExamId());
            if (StringUtils.isNotBlank(examName)) {
                taskExamMap.computeIfAbsent(taskExam.getTaskId(), k -> new ArrayList<>())
                        .add(examName);
            }
        }

        // 5. 对每个任务的考试名称去重
        taskExamMap.forEach((taskId, names) -> {
            List<String> distinctNames = names.stream().distinct().collect(Collectors.toList());
            result.put(taskId, distinctNames);
        });

        return result;
    }

    /**
     * 获取每个任务的鉴定表名称列表（去重）
     */
    private Map<String, List<String>> getIdentificationNamesMap(List<String> taskIds) {
        Map<String, List<String>> result = new HashMap<>();
        if (CollUtil.isEmpty(taskIds)) {
            return result;
        }

        // 1. 查询任务关联的鉴定（未删除）
        LambdaQueryWrapper<FtbCultivateLearnTaskIdentification> wrapper = Wrappers.lambdaQuery();
        wrapper.in(FtbCultivateLearnTaskIdentification::getTaskId, taskIds)
                .eq(FtbCultivateLearnTaskIdentification::getEnableMark, 0);

        List<FtbCultivateLearnTaskIdentification> taskIdentifications = ftbCultivateLearnTaskIdentificationMapper.selectList(wrapper);
        if (CollUtil.isEmpty(taskIdentifications)) {
            return result;
        }

        // 2. 提取所有鉴定ID（去重）
        List<String> identifyIds = taskIdentifications.stream()
                .map(FtbCultivateLearnTaskIdentification::getIdentificationId)
                .filter(StringUtils::isNotBlank)
                .distinct()
                .collect(Collectors.toList());

        if (CollUtil.isEmpty(identifyIds)) {
            return result;
        }

        // 3. 查询鉴定表，获取鉴定名称
        LambdaQueryWrapper<jnpf.entity.cultivate.CultivateIdentifyTable> identifyWrapper = Wrappers.lambdaQuery();
        identifyWrapper.in(jnpf.entity.cultivate.CultivateIdentifyTable::getId, identifyIds)
                .eq(jnpf.entity.cultivate.CultivateIdentifyTable::getDeleteMark, 0); // 0-未删除

        List<jnpf.entity.cultivate.CultivateIdentifyTable> identifies = cultivateIdentifyTableMapper.selectList(identifyWrapper);
        Map<String, String> identifyNameMap = identifies.stream()
                .filter(i -> StringUtils.isNotBlank(i.getName()))
                .collect(Collectors.toMap(
                        jnpf.entity.cultivate.CultivateIdentifyTable::getId,
                        jnpf.entity.cultivate.CultivateIdentifyTable::getName,
                        (v1, v2) -> v1
                ));

        // 4. 按任务ID分组，收集鉴定表名称
        Map<String, List<String>> taskIdentifyMap = new HashMap<>();
        for (FtbCultivateLearnTaskIdentification taskIdentify : taskIdentifications) {
            String identifyName = identifyNameMap.get(taskIdentify.getIdentificationId());
            if (StringUtils.isNotBlank(identifyName)) {
                taskIdentifyMap.computeIfAbsent(taskIdentify.getTaskId(), k -> new ArrayList<>())
                        .add(identifyName);
            }
        }

        // 5. 对每个任务的鉴定表名称去重
        taskIdentifyMap.forEach((taskId, names) -> {
            List<String> distinctNames = names.stream().distinct().collect(Collectors.toList());
            result.put(taskId, distinctNames);
        });

        return result;
    }

    /**
     * 获取每个任务的证书名称列表（去重）
     */
    private Map<String, List<String>> getCertificateNamesMap(List<String> taskIds) {
        Map<String, List<String>> result = new HashMap<>();
        if (CollUtil.isEmpty(taskIds)) {
            return result;
        }

        // 1. 查询任务关联的证书（未删除）
        LambdaQueryWrapper<FtbCultivateLearnTaskCertificate> wrapper = Wrappers.lambdaQuery();
        wrapper.in(FtbCultivateLearnTaskCertificate::getTaskId, taskIds)
                .eq(FtbCultivateLearnTaskCertificate::getEnableMark, 0);

        List<FtbCultivateLearnTaskCertificate> taskCertificates = ftbCultivateLearnTaskCertificateMapper.selectList(wrapper);
        if (CollUtil.isEmpty(taskCertificates)) {
            return result;
        }

        // 2. 提取所有证书ID（去重）
        List<String> certificateIds = taskCertificates.stream()
                .map(FtbCultivateLearnTaskCertificate::getCertificateId)
                .filter(StringUtils::isNotBlank)
                .distinct()
                .collect(Collectors.toList());

        if (CollUtil.isEmpty(certificateIds)) {
            return result;
        }

        // 3. 查询证书表，获取证书名称
        LambdaQueryWrapper<jnpf.model.cultivate.po.certificate.FtbCertificateEntity> certWrapper = Wrappers.lambdaQuery();
        certWrapper.in(jnpf.model.cultivate.po.certificate.FtbCertificateEntity::getId, certificateIds)
                .eq(jnpf.model.cultivate.po.certificate.FtbCertificateEntity::getEnabledMark, 1); // 1-有效

        List<jnpf.model.cultivate.po.certificate.FtbCertificateEntity> certificates = ftbCultivateCertificateMapper.selectList(certWrapper);
        Map<String, String> certNameMap = certificates.stream()
                .filter(c -> StringUtils.isNotBlank(c.getName()))
                .collect(Collectors.toMap(
                        jnpf.model.cultivate.po.certificate.FtbCertificateEntity::getId,
                        jnpf.model.cultivate.po.certificate.FtbCertificateEntity::getName,
                        (v1, v2) -> v1
                ));

        // 4. 按任务ID分组，收集证书名称
        Map<String, List<String>> taskCertMap = new HashMap<>();
        for (FtbCultivateLearnTaskCertificate taskCert : taskCertificates) {
            String certName = certNameMap.get(taskCert.getCertificateId());
            if (StringUtils.isNotBlank(certName)) {
                taskCertMap.computeIfAbsent(taskCert.getTaskId(), k -> new ArrayList<>())
                        .add(certName);
            }
        }

        // 5. 对每个任务的证书名称去重
        taskCertMap.forEach((taskId, names) -> {
            List<String> distinctNames = names.stream().distinct().collect(Collectors.toList());
            result.put(taskId, distinctNames);
        });

        return result;
    }

    /**
     * 获取每个任务的分配用户数
     */
    private Map<String, Integer> getAssignedUserCountMap(List<String> taskIds) {
        Map<String, Integer> result = new HashMap<>();
        if (CollUtil.isEmpty(taskIds)) {
            return result;
        }

        LambdaQueryWrapper<FtbCultivateLearnTaskAssignment> wrapper = Wrappers.lambdaQuery();
        wrapper.in(FtbCultivateLearnTaskAssignment::getTaskId, taskIds)
                .eq(FtbCultivateLearnTaskAssignment::getEnableMark, 0);

        List<FtbCultivateLearnTaskAssignment> assignments = ftbCultivateLearnTaskAssignmentMapper.selectList(wrapper);

        if (CollUtil.isNotEmpty(assignments)) {
            result = assignments.stream()
                    .collect(Collectors.groupingBy(
                            FtbCultivateLearnTaskAssignment::getTaskId,
                            Collectors.collectingAndThen(Collectors.counting(), Long::intValue)
                    ));
        }

        return result;
    }

    /**
     * 获取每个任务的完成用户数
     */
    private Map<String, Integer> getCompletedUserCountMap(List<String> taskIds) {
        Map<String, Integer> result = new HashMap<>();
        if (CollUtil.isEmpty(taskIds)) {
            return result;
        }

        LambdaQueryWrapper<FtbCultivateLearnTaskAssignment> wrapper = Wrappers.lambdaQuery();
        wrapper.in(FtbCultivateLearnTaskAssignment::getTaskId, taskIds)
                .eq(FtbCultivateLearnTaskAssignment::getEnableMark, 0)
                .eq(FtbCultivateLearnTaskAssignment::getStudyStats, 2); // 2-已完成

        List<FtbCultivateLearnTaskAssignment> assignments = ftbCultivateLearnTaskAssignmentMapper.selectList(wrapper);

        if (CollUtil.isNotEmpty(assignments)) {
            result = assignments.stream()
                    .collect(Collectors.groupingBy(
                            FtbCultivateLearnTaskAssignment::getTaskId,
                            Collectors.collectingAndThen(Collectors.counting(), Long::intValue)
                    ));
        }

        return result;
    }

    /**
     * 获取每个任务的已颁发证书数量
     */
    private Map<String, Integer> getIssuedCertificateCountMap(List<String> taskIds) {
        Map<String, Integer> result = new HashMap<>();
        if (CollUtil.isEmpty(taskIds)) {
            return result;
        }

        LambdaQueryWrapper<FtbCultivateTaskLog> wrapper = Wrappers.lambdaQuery();
        wrapper.in(FtbCultivateTaskLog::getTaskId, taskIds)
                .eq(FtbCultivateTaskLog::getEnabledMark, 0)
                .eq(FtbCultivateTaskLog::getIssuedCertificate, 1); // 1-已颁发

        List<FtbCultivateTaskLog> logs = ftbCultivateTaskLogMapper.selectList(wrapper);

        if (CollUtil.isNotEmpty(logs)) {
            result = logs.stream()
                    .collect(Collectors.groupingBy(
                            FtbCultivateTaskLog::getTaskId,
                            Collectors.collectingAndThen(Collectors.counting(), Long::intValue)
                    ));
        }

        return result;
    }

    /**
     * 从任务日志中获取统计信息
     */
    private Map<String, V2TaskLogStatistics> getTaskLogStatisticsMap(List<String> taskIds) {
        Map<String, V2TaskLogStatistics> result = new HashMap<>();
        if (CollUtil.isEmpty(taskIds)) {
            return result;
        }

        LambdaQueryWrapper<FtbCultivateTaskLog> wrapper = Wrappers.lambdaQuery();
        wrapper.in(FtbCultivateTaskLog::getTaskId, taskIds)
                .eq(FtbCultivateTaskLog::getEnabledMark, 0);

        List<FtbCultivateTaskLog> logs = ftbCultivateTaskLogMapper.selectList(wrapper);

        if (CollUtil.isEmpty(logs)) {
            return result;
        }

        // 按任务ID分组统计
        Map<String, List<FtbCultivateTaskLog>> taskLogMap = logs.stream()
                .collect(Collectors.groupingBy(FtbCultivateTaskLog::getTaskId));

        for (Map.Entry<String, List<FtbCultivateTaskLog>> entry : taskLogMap.entrySet()) {
            String taskId = entry.getKey();
            List<FtbCultivateTaskLog> taskLogs = entry.getValue();

            V2TaskLogStatistics stats = new V2TaskLogStatistics();

            // 统计必修课程完成情况
            // 注意：这里需要从任务课程表中获取必修课程列表，然后判断哪些用户完成了所有必修课程
            // 简化处理：统计courseState=1的记录数
            int completeCourseUsers = (int) taskLogs.stream()
                    .filter(log -> log.getCourseState() != null && log.getCourseState() == 1)
                    .map(FtbCultivateTaskLog::getUserId)
                    .distinct()
                    .count();
            stats.setCompleteCourseUserCount(completeCourseUsers);

            // 统计参与考试人次（examState != -1 表示已触发考试）
            int examParticipants = (int) taskLogs.stream()
                    .filter(log -> log.getExamState() != null && log.getExamState() != -1)
                    .count();
            stats.setExamParticipantCount(examParticipants);

            // 统计考试合格人次（examResult = 3合格 或 5优秀）
            int examPassCount = (int) taskLogs.stream()
                    .filter(log -> log.getExamResult() != null &&
                            (log.getExamResult() == 3 || log.getExamResult() == 5))
                    .count();
            stats.setExamPassCount(examPassCount);

            // 统计参与鉴定人次（identifyState != -1 表示已触发鉴定）
            int identifyParticipants = (int) taskLogs.stream()
                    .filter(log -> log.getIdentifyState() != null && log.getIdentifyState() != -1)
                    .count();
            stats.setIdentifyParticipantCount(identifyParticipants);

            // 统计鉴定合格人次（identifyResult = 0合格 或 1优秀）
            int identifyPassCount = (int) taskLogs.stream()
                    .filter(log -> log.getIdentifyResult() != null &&
                            (log.getIdentifyResult() == 0 || log.getIdentifyResult() == 1))
                    .count();
            stats.setIdentifyPassCount(identifyPassCount);

            result.put(taskId, stats);
        }

        return result;
    }

    /**
     * 计算百分比
     */
    private String calculateRate(Integer numerator, Integer denominator) {
        if (numerator == null || numerator == 0 || denominator == null || denominator == 0) {
            return "0%";
        }
        int rate = Math.round((numerator / (float) denominator) * 100);
        return rate + "%";
    }

    /**
     * 任务日志统计数据内部类
     */
    private static class V2TaskLogStatistics {
        private int completeCourseUserCount = 0;
        private int examParticipantCount = 0;
        private int examPassCount = 0;
        private int identifyParticipantCount = 0;
        private int identifyPassCount = 0;

        public int getCompleteCourseUserCount() {
            return completeCourseUserCount;
        }

        public void setCompleteCourseUserCount(int completeCourseUserCount) {
            this.completeCourseUserCount = completeCourseUserCount;
        }

        public int getExamParticipantCount() {
            return examParticipantCount;
        }

        public void setExamParticipantCount(int examParticipantCount) {
            this.examParticipantCount = examParticipantCount;
        }

        public int getExamPassCount() {
            return examPassCount;
        }

        public void setExamPassCount(int examPassCount) {
            this.examPassCount = examPassCount;
        }

        public int getIdentifyParticipantCount() {
            return identifyParticipantCount;
        }

        public void setIdentifyParticipantCount(int identifyParticipantCount) {
            this.identifyParticipantCount = identifyParticipantCount;
        }

        public int getIdentifyPassCount() {
            return identifyPassCount;
        }

        public void setIdentifyPassCount(int identifyPassCount) {
            this.identifyPassCount = identifyPassCount;
        }
    }
}
