package com.shulead.datasync.manager;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shulead.datasync.bo.StudentBO;
import com.shulead.datasync.entity.MOrganization;
import com.shulead.datasync.entity.MParent;
import com.shulead.datasync.entity.MStudent;
import com.shulead.datasync.mapper.MOrganizationMapper;
import com.shulead.datasync.mapper.MParentMapper;
import com.shulead.datasync.mapper.MStudentMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

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

/**
 * @Classname StudentManager
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/4/30 11:05
 * @Created by liuxiangshun
 */
@Component
public class StudentManager {
    @Resource
    private MStudentMapper mStudentMapper;

    @Resource
    private MParentMapper mParentMapper;

    @Resource
    private MOrganizationMapper mOrganizationMapper;

    public List<StudentBO> pageStudent(Long startId, Integer limit) {
        LambdaQueryWrapper<MStudent> wrapper = Wrappers.lambdaQuery(MStudent.class)
                .ge(MStudent::getId, startId)
                .orderByAsc(MStudent::getId)
                .last("limit " + limit);

        List<MStudent> students = mStudentMapper.selectList( wrapper);

        if (CollectionUtils.isEmpty(students)) {
            return Collections.emptyList();
        }

        List<Long> familyIds = students.stream().map(MStudent::getFamilyId).filter(Objects::nonNull).toList();

        List<Long> orgIds = students.stream().map(MStudent::getOrgId).filter(Objects::nonNull).toList();

        LambdaQueryWrapper<MParent> parentWrapper = Wrappers.lambdaQuery(MParent.class)
                .in(MParent::getFamilyId, familyIds);
        List<MParent> parents = mParentMapper.selectList(parentWrapper);
        Map<Long, MParent> parentMap = parents.stream().collect(Collectors.toMap(MParent::getFamilyId, Function.identity(), (oldValue, newValue) -> oldValue));

        LambdaQueryWrapper<MOrganization> organizationWrapper = Wrappers.lambdaQuery(MOrganization.class)
                .in(MOrganization::getId, orgIds);
        List<MOrganization> organizations = mOrganizationMapper.selectList(organizationWrapper);
        Map<Long, MOrganization> organizationMap = organizations.stream().collect(Collectors.toMap(MOrganization::getId, Function.identity(), (oldValue, newValue) -> oldValue));

        return students.stream().map(student -> buildStudentBO(student, parentMap, organizationMap)).toList();
    }

    private StudentBO buildStudentBO(MStudent student, Map<Long, MParent> parentMap, Map<Long, MOrganization> organizationMap) {
        StudentBO studentBO = new StudentBO();
        studentBO.setId(student.getId());
        studentBO.setStudentName(student.getStudentName());
        MParent parent = parentMap.get(student.getFamilyId());
        if (null != parent) {
            studentBO.setParentName(parent.getParentName());
            studentBO.setMobile(parent.getMobile());
        }
        MOrganization organization = organizationMap.get(student.getOrgId());
        if (null != organization) {
            studentBO.setOrgName(organization.getOrgName());
            studentBO.setSchoolArea(organization.getOrgName());
        }
        return studentBO;
    }

    public StudentBO getStudentByMobile(String mobile) {
        LambdaQueryWrapper<MParent> wrapper = Wrappers.lambdaQuery(MParent.class)
                .eq(MParent::getMobile, mobile)
                .last("limit 1");
        MParent parent = mParentMapper.selectOne(wrapper);

        if (null == parent) {
            return null;
        }

        LambdaQueryWrapper<MStudent> wrapper1 = Wrappers.lambdaQuery(MStudent.class)
                .eq(MStudent::getFamilyId, parent.getFamilyId())
                .last("limit 1");
        MStudent mStudent = mStudentMapper.selectOne(wrapper1);

        if (null == mStudent) {
            return null;
        }

        LambdaQueryWrapper<MOrganization> wrapper2 = Wrappers.lambdaQuery(MOrganization.class)
                .eq(MOrganization::getId, mStudent.getOrgId())
                .last("limit 1");
        MOrganization organization = mOrganizationMapper.selectOne(wrapper2);

        if (null == organization) {
            return null;
        }

        StudentBO studentBO = new StudentBO();
        studentBO.setId(mStudent.getId());
        studentBO.setStudentName(mStudent.getStudentName());
        studentBO.setParentName(parent.getParentName());
        studentBO.setMobile(parent.getMobile());
        studentBO.setOrgName(organization.getOrgName());
        studentBO.setSchoolArea(organization.getOrgName());
        studentBO.setSchoolAreaCode(organization.getOrgCode());
        return studentBO;
    }
}
