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

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.mapper.BaseClassMapper;
import com.xjrsoft.module.student.dto.StudentHonorsPageDto;
import com.xjrsoft.module.student.entity.StudentHonors;
import com.xjrsoft.module.student.mapper.StudentHonorsMapper;
import com.xjrsoft.module.student.service.IStudentHonorsService;
import com.xjrsoft.module.student.vo.StudentHonorsListVo;
import com.xjrsoft.module.student.vo.StudentHonorsPageVo;
import com.xjrsoft.module.student.vo.StudentHonorsVo;
import com.xjrsoft.module.system.entity.File;
import com.xjrsoft.module.system.service.IFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @title: 学生荣誉
* @Author dzx
* @Date: 2023-12-05
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class StudentHonorsServiceImpl extends MPJBaseServiceImpl<StudentHonorsMapper, StudentHonors> implements IStudentHonorsService {
    private final StudentHonorsMapper studentHonorsMapper;
    private final IFileService fileService;

    private final BaseClassMapper baseClassMapper;
    @Override
    public IPage<StudentHonorsPageVo> getPage(StudentHonorsPageDto dto) {
        //获取当前用户所管理的班级
        LambdaQueryWrapper<BaseClass> queryWrapperClass = new LambdaQueryWrapper<>();
        queryWrapperClass
                .eq(BaseClass::getTeacherId, StpUtil.getLoginIdAsLong());
        List<BaseClass> baseClassList = baseClassMapper.selectList(queryWrapperClass);
        List<Long> classIdList = new ArrayList<>();
        if(baseClassList != null && baseClassList.size() > 0){
            for (BaseClass baseClass : baseClassList) {
                classIdList.add(baseClass.getId());
            }
            dto.setClassIdList(classIdList);
        }
        return studentHonorsMapper.getPage(ConventPage.getPage(dto), dto);
    }

    @Override
    public List<StudentHonorsListVo> getClassInfo() {
        return studentHonorsMapper.getClassInfo();
    }

    @Override
    public List<StudentHonorsListVo> getSemesterInfo() {
        return studentHonorsMapper.getSemesterInfo();
    }

    @Override
    public StudentHonorsVo getInfo(Long id) {
        StudentHonorsVo result = studentHonorsMapper.getInfo(id);
        if(ObjectUtil.isNotNull(result) && ObjectUtil.isNotNull(result.getFileId())){
            result.setFileInfos(fileService.list(Wrappers.<File>query().lambda().eq(File::getFolderId, result.getFileId())));
        }

        return result;
    }

    @Override
    public Integer getSortCode() {
        return studentHonorsMapper.getSortCode();
    }
}
