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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.utils.TreeUtil;
import com.xjrsoft.module.student.dto.ClassHonorsPageDto;
import com.xjrsoft.module.student.entity.ClassHonors;
import com.xjrsoft.module.student.mapper.ClassHonorsMapper;
import com.xjrsoft.module.student.service.IClassHonorsService;
import com.xjrsoft.module.student.vo.ClassHonorsPageVo;
import com.xjrsoft.module.student.vo.ClassHonorsSemesterClassTreeVo;
import com.xjrsoft.module.student.vo.ClassHonorsVo;
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 szs
* @Date: 2023-12-06
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class ClassHonorsServiceImpl extends MPJBaseServiceImpl<ClassHonorsMapper, ClassHonors> implements IClassHonorsService {


    private final ClassHonorsMapper classHonorsMapper;
    private final IFileService fileService;
    /**
     * 班级荣誉列表(分页)PC端
     * @param dto
     * @return
     */
    @Override
    public IPage<ClassHonorsPageVo> getPagePC(ClassHonorsPageDto dto) {
        return classHonorsMapper.getPagePC(ConventPage.getPage(dto),dto);
    }

    /**
     * 根据id查询班级荣誉信息
     * @param id
     * @return
     */
    @Override
    public ClassHonorsVo getInfoById(Long id) {
        ClassHonorsVo classHonorsVo = classHonorsMapper.getInfoById(id);
        if(ObjectUtil.isNull(classHonorsVo)){
            throw new MyException("该数据不存在");
        }

        // 添加文件列表
        classHonorsVo.setFileList(fileService.list(Wrappers.<File>query().lambda().eq(File::getFolderId, classHonorsVo.getFileId())));
        return classHonorsVo;
    }

    @Override
    public List<ClassHonorsSemesterClassTreeVo> getSemesterClassTree() {
        List<ClassHonorsSemesterClassTreeVo> treeVoList = new ArrayList<ClassHonorsSemesterClassTreeVo>();

        //获取班级荣誉中出现的学期作为树的父级
        classHonorsMapper.getSemester().forEach((node) -> {
            treeVoList.add(new ClassHonorsSemesterClassTreeVo(){{
                setId(node.getBaseSemesterId());
                setName(node.getBaseSemesterIdCN());
            }});
        });

        //获取班级荣誉中出现的学期下的所有班级作为树的子级
        classHonorsMapper.getSemesterClass().forEach((node) -> {
            treeVoList.add(new ClassHonorsSemesterClassTreeVo(){{
                setId(node.getClassId());
                setName(node.getClassIdCN());
                setParentId(node.getBaseSemesterId());
            }});
        });

        List<ClassHonorsSemesterClassTreeVo> resultTreeVoList = TreeUtil.build(treeVoList);
        return resultTreeVoList;
    }
}
