package com.template.controller;


import com.template.api.SmartTeachingControllerAPI;
import com.template.model.pojo.SmartClass;
import com.template.model.pojo.SmartGrade;
import com.template.model.pojo.SmartSubject;
import com.template.model.result.CommonResult;
import com.template.model.vo.SubjectClassVo;
import com.template.model.vo.SubjectGradeVo;
import com.template.model.vo.SubjectVo;
import com.template.services.SmartClassService;
import com.template.services.SmartGradeService;
import com.template.services.SmartSubjectService;
import com.template.services.SmartTeachingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

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

/**
 * <p>
 * 应用管理 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2024-05-24
 */
@RestController
public class SmartTeachingController implements SmartTeachingControllerAPI {

    @Autowired
    private SmartTeachingService smartTeachingService;

    @Autowired
    private SmartGradeService smartGradeService;

    @Autowired
    private SmartClassService smartClassService;

    @Autowired
    private SmartSubjectService smartSubjectService;


    @Override
    public CommonResult queryTeachings() {
        List<SubjectVo> result = new ArrayList<>();
        List<SmartGrade> grades = smartGradeService.list(null);
        List<SmartSubject> subjects = smartSubjectService.list(null);
        List<SmartClass> classes = smartClassService.list(null);
        for (SmartSubject subject : subjects) {
            SubjectVo subjectData = new SubjectVo();
            subjectData.setId(subject.getId());
            subjectData.setName(subject.getName());
            List<SubjectGradeVo> subjectVos = new ArrayList<>();
            List<SmartGrade> ownerGrades = grades.stream().filter(e -> ("," + e.getSubjectId() + ",").contains("," + subject.getId() + ",")).collect(Collectors.toList());
            if (ownerGrades != null && ownerGrades.size() > 0) {
                for (SmartGrade ownerGrade : ownerGrades) {
                    SubjectGradeVo gradeDetail = new SubjectGradeVo();
                    gradeDetail.setId(ownerGrade.getId());
                    gradeDetail.setName(ownerGrade.getName());
                    List<SubjectClassVo> classVos = new ArrayList<>();
                    List<SmartClass> ownerClasses = classes.stream().filter(e -> e.getGradeId().equals(ownerGrade.getId())).collect(Collectors.toList());
                    if(ownerClasses != null && ownerClasses.size() > 0){
                        for (SmartClass ownerClass :ownerClasses){
                            SubjectClassVo classVo = new SubjectClassVo();
                            classVo.setId(ownerClass.getId());
                            classVo.setName(ownerClass.getName());
                            classVos.add(classVo);
                        }
                    }

                    gradeDetail.setChildren(classVos);
                    subjectVos.add(gradeDetail);
                }

            }
            subjectData.setChildren(subjectVos);
            result.add(subjectData);
        }

        return CommonResult.ok(result);
    }
}

