package com.ruoyi.web.controller.test.teacher;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.test.domain.*;
import com.ruoyi.test.domain.DO.ClassExamPaperDO;
import com.ruoyi.test.domain.DO.ExamPaperDO;
import com.ruoyi.test.seriver.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "教师端成绩分析")
@RestController
@RequestMapping("teacher/analysis")
public class TeacherAnalysisController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private ITeacherManageClassService iTeacherManageClassService;

    @Autowired
    private IClassExamPaperService iClassExamPaperService;

    @Autowired
    private IClass1Service iClass1Service;

    @Autowired
    private IExamPaperService iExamPaperService;

    @Autowired
    private IStudentClassService iStudentClassService;

    @Autowired
    private IMarkedtestService iMarkedtestService;

    @ApiOperation("试卷列表")
    @GetMapping("examPaperList")
    public R<IPage<ExamPaperDO>> examPaperList(int pagenum){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        Long teacherid = iUserService.selectByzhid(zhid);
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        List<ExamPaperDO> list1 = new ArrayList<>();
        Map<Long,List<String>> map = new HashMap<>();
        for(TeacherManageClass teacherManageClass : list){
            String grade = teacherManageClass.getGrade();
            String Class1 = teacherManageClass.getClass1();
            List<ClassExamPaper> classExamPaper = iClassExamPaperService.selectByGradeAndClass(grade,Class1);
            for(ClassExamPaper classExamPaper1 : classExamPaper){
                Long id = classExamPaper1.getId();
                if(!map.containsKey(id)){
                    com.ruoyi.test.domain.Class1 class1 = iClass1Service.classList(grade,Class1);
                    List<String> list2 = new ArrayList<>();
                    list2.add(class1.getName());
                    map.put(id,list2);
                }else{
                    List<String> list2 = map.get(id);
                    Class1 class1 = iClass1Service.classList(grade,Class1);
                    list2.add(class1.getName());
                    map.put(id,list2);
                }
            }
        }
        for(Map.Entry<Long,List<String>> entry : map.entrySet()){
            ExamPaperDO examPaperDO = new ExamPaperDO();
            Long id = entry.getKey();
            examPaperDO.setId(id);
            ExamPaper examPaper = iExamPaperService.selectById(id);
            examPaperDO.setSubject(examPaper.getSubject());
            examPaperDO.setGrade(examPaper.getGrade());
            examPaperDO.setName(examPaper.getName());
            examPaperDO.setCreatetime(examPaper.getCreatetime());
            list1.add(examPaperDO);
        }
        Page<ExamPaperDO> page = new Page<>(pagenum,10);
        IPage<ExamPaperDO> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list1);
        return R.ok(iPage);
    }

    @ApiOperation("班级平均分")
    @GetMapping("classAverage")
    public R<String> classAverage(Long id,Long testid){
        Class1 class1 = iClass1Service.selectClass1(id);
        String grade = class1.getGrade();
        String Class1 = class1.getClass1();
        List<StudentClass> list = iStudentClassService.selectByGradeAndClass(grade,Class1);
        System.out.println(list);
        Double sum = 0.0;
        for(StudentClass studentClass : list){
            Long studentid = studentClass.getStudentid();
            String tname = iUserService.user(studentid).getTname();
            System.out.println(tname);
            Double score = 0.0;
            if(iMarkedtestService.selectmarkedtestBySenderAndTestid(tname,testid)!=null){
                score =iMarkedtestService.selectmarkedtestBySenderAndTestid(tname,testid).getScore();
            }
            sum+=score;
        }
        String message = class1.getName()+"的平均分是"+sum/list.size();
        return R.ok(message);
    }

}
