package com.xjrsoft.module.base.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.module.attendance.dto.AttendanceStatisticDto;
import com.xjrsoft.module.attendance.vo.ClassStatisticsVo;
import com.xjrsoft.module.base.dto.BaseClassPageDto;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.mapper.BaseClassMapper;
import com.xjrsoft.module.base.service.IBaseClassService;
import com.xjrsoft.module.base.vo.BaseClassPageVo;
import com.xjrsoft.module.base.vo.StudentClassVo;
import com.xjrsoft.module.organization.entity.UserStudent;
import com.xjrsoft.module.organization.service.IUserStudentService;
import com.xjrsoft.module.organization.vo.UserStudentVo;
import com.xjrsoft.module.student.entity.BaseStudent;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.student.entity.BaseStudentUser;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class BaseClassServiceImpl extends MPJBaseServiceImpl<BaseClassMapper, BaseClass> implements IBaseClassService {

    private final IUserStudentService userStudentService;

    private final BaseClassMapper baseClassMapper;

    /**
     * 获取家长关联的学生 id 是家长的ID
     * @param id
     * @return
     */
    public List<UserStudentVo> getStudents(long id) {
        List<Long> studentIds = userStudentService.list(Wrappers.lambdaQuery(UserStudent.class)
                        .eq(UserStudent::getUserId, id))
                .stream().map(UserStudent::getStudentId).collect(Collectors.toList());
        List<UserStudentVo> list = null;
        if (studentIds.size() > 0) {
            MPJLambdaWrapper<BaseClass> queryWrapper = new MPJLambdaWrapper<>();
            queryWrapper
                    .disableSubLogicDel()
                    .rightJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getClassId, BaseStudent::getId)
                    .innerJoin(BaseStudentUser.class, BaseStudentUser::getId, BaseStudentSchoolRoll::getUserId)
                    .in(BaseStudentSchoolRoll::getUserId, studentIds)
                    .selectAs(BaseClass::getName, UserStudentVo::getClassName)
                    .selectAs(BaseStudentUser::getName, UserStudentVo::getStudentName)
                    .selectAs(BaseStudentUser::getId, UserStudentVo::getStudentId)
                    .selectAs(BaseStudentSchoolRoll::getClassId, UserStudentVo::getClassId)
                    .selectAs(BaseStudentUser::getAvatar, UserStudentVo::getAvatar);

            list = selectJoinList(UserStudentVo.class, queryWrapper);
        }
        return list;
    }

    /**
     * 获取学生的班级信息
     * @param id
     * @return
     */
    public UserStudentVo getClassInfo(long id) {
        MPJLambdaWrapper<BaseClass> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .disableSubLogicDel()
                .rightJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getClassId, BaseStudent::getId)
                .innerJoin(BaseStudentUser.class, BaseStudentUser::getId, BaseStudentSchoolRoll::getUserId)
                .eq(BaseStudentSchoolRoll::getUserId, id)
                .selectAs(BaseClass::getName, UserStudentVo::getClassName)
                .selectAs(BaseStudentUser::getName, UserStudentVo::getStudentName)
                .selectAs(BaseStudentUser::getId, UserStudentVo::getStudentId)
                .selectAs(BaseStudentSchoolRoll::getClassId, UserStudentVo::getClassId)
                .selectAs(BaseStudentUser::getAvatar, UserStudentVo::getAvatar);
        return selectJoinOne(UserStudentVo.class, queryWrapper);
    }

    @Override
    public Page<BaseClassPageVo> getPage(Page<BaseClassPageVo> page, BaseClassPageDto dto) {
        return baseClassMapper.getPage(page, dto);
    }

    @Override
    public List<StudentClassVo> getStudentClass() {
        return baseClassMapper.getStudentClass();
    }

    @Override
    public Page<ClassStatisticsVo> getAttendancePage(Page<ClassStatisticsVo> page, AttendanceStatisticDto dto){
        return baseClassMapper.getAttendanceClass(page, dto);
    };
}
