//
// Created by 34753 on 2025/6/20.
//
#include "studentService.h"
#include "core/student.h"

StudentService::StudentService(DataStorage *storage, QObject *parent)
    : QObject(parent), m_storage(storage)
{
    loadStudents();
    emit studentsChanged(); // 添加这一行
}

void StudentService::addStudent(Student *student)
{
}

void StudentService::updateStudent(const QString &id, Student *newInfo)
{
}

void StudentService::deleteStudent(const QString &id)
{
}

Student *StudentService::getStudentById(const QString &id)
{
    return nullptr;
}
QVariantList StudentService::getCollegeStats() const
{
    QVariantList result;

    // 示例数据 - 替换为您的实际统计逻辑
    QMap<QString, int> stats;
    stats["计算机学院"] = 120;
    stats["经济管理学院"] = 85;
    stats["外语学院"] = 65;
    stats["艺术学院"] = 45;

    for (auto it = stats.begin(); it != stats.end(); ++it) {
        QVariantMap item;
        item["college"] = it.key();
        item["count"] = it.value();
        result.append(item);
    }

    return result ;
}
QVariantList StudentService::getHometownStats() const
{
    QVariantList result;

    // 示例数据 - 替换为您的实际统计逻辑
    QMap<QString, int> stats;
    stats["北京"] = 50;
    stats["上海"] = 45;
    stats["广州"] = 38;
    stats["深圳"] = 35;
    stats["杭州"] = 28;

    for (auto it = stats.begin(); it != stats.end(); ++it) {
        QVariantMap item;
        item["hometown"] = it.key();
        item["count"] = it.value();
        result.append(item);
    }

    return result ;
}


void StudentService::sortStudents(const QString &field)
{
}

QList<Student *> StudentService::searchStudents(const QString &field, const QString &value)
{
    return {};
}

QVariantMap StudentService::countStatistics()
{
    QVariantMap result;
    return result;
}

QVariantList StudentService::students() const
{
    QVariantList list;
    for (const auto* student : m_students) {
        QVariantMap studentMap;
        studentMap["id"] = student->id();
        studentMap["name"] = student->name();
        studentMap["gender"] = student->gender();
        studentMap["college"] = student->college();
        studentMap["department"] = student->department();
        studentMap["className"] = student->classname();
        studentMap["dormitory"] = student->dorm();
        studentMap["hometown"] = student->hometown();
        studentMap["phone"] = student->phone();
        list.append(studentMap);
    }
    return list;
}

QList<Student*> StudentService::loadStudents()
{
    if (m_students.isEmpty()) {
        // 添加示例数据
        m_students.append(new Student("2023001", "张三", "男", "计算机学院", "软件工程系", "软工1班", "A101", "北京", "13800138000", this));
        m_students.append(new Student("2023002", "李四", "女", "经济管理学院", "金融系", "金融2班", "B203", "上海", "13900139000", this));
        m_students.append(new Student("2023003", "王五", "男", "外国语学院", "英语系", "英语3班", "C305", "广州", "13700137000", this));
    }
    return m_students;
}

void StudentService::calculateStatistics()
{
    m_totalStudents = m_students.size();
    m_maleCount = 0;
    m_femaleCount = 0;

    QSet<QString> colleges;
    QSet<QString> classes;

    for (const auto* student : m_students) {
        if (student->gender() == "男") {
            m_maleCount++;
        } else if (student->gender() == "女") {
            m_femaleCount++;
        }

        colleges.insert(student->college());
        classes.insert(student->classname());
    }

    m_collegeCount = colleges.size();
    m_classCount = classes.size();

    emit statsUpdated();
}