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.Class1;
import com.ruoyi.test.domain.ClassExamPaper;
import com.ruoyi.test.domain.DO.*;
import com.ruoyi.test.domain.ExamPaper;
import com.ruoyi.test.domain.TeacherManageClass;
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.*;

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

@Api(tags = "教师端卷库管理")
@RestController
@RequestMapping("/teacher/examPaper")
public class TeacherExamPaperController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IExamPaperService iExamPaperService;

    @Autowired
    private IClassExamPaperService iClassExamPaperService;

    @Autowired
    private ITeacherManageClassService iTeacherManageClassService;

    @Autowired
    private IClass1Service iClass1Service;

    @Autowired
    private IExamCreateService iExamCreateService;

    @Autowired
    private IQuestionbankService iQuestionbankService;

    @ApiOperation("试卷列表")
    @GetMapping("examPaperList")
    public R<IPage<ClassExamPaperDO>> examPaperList(int pagenum){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        Long teacherid = iUserService.selectByzhid(zhid);
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        List<ClassExamPaperDO> 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)){
                   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()){
            ClassExamPaperDO classExamPaperDO = new ClassExamPaperDO();
            Long id = entry.getKey();
            classExamPaperDO.setId(id);
            ExamPaper examPaper = iExamPaperService.selectById(id);
            classExamPaperDO.setSubject(examPaper.getSubject());
            classExamPaperDO.setGrade(examPaper.getGrade());
            classExamPaperDO.setName(examPaper.getName());
            classExamPaperDO.setList(entry.getValue());
            classExamPaperDO.setCreatetime(examPaper.getCreatetime());
            list1.add(classExamPaperDO);
        }
        Page<ClassExamPaperDO> page = new Page<>(pagenum,10);
        IPage<ClassExamPaperDO> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list1);
        return R.ok(iPage);
    }

    @ApiOperation("试卷列表(根据试卷id)")
    @GetMapping("examPaperListById")
    public R<ClassExamPaperDO> examPaperListById(int pagenum,Long id){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        Long teacherid = iUserService.selectByzhid(zhid);
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        ClassExamPaperDO classExamPaperDO = new ClassExamPaperDO();
        classExamPaperDO.setId(id);
        ExamPaper examPaper = iExamPaperService.selectById(id);
        classExamPaperDO.setSubject(examPaper.getSubject());
        classExamPaperDO.setGrade(examPaper.getGrade());
        classExamPaperDO.setName(examPaper.getName());
        classExamPaperDO.setCreatetime(examPaper.getCreatetime());
        List<String> list1 = new ArrayList<>();
        for(TeacherManageClass teacherManageClass : list){
            String grade = teacherManageClass.getGrade();
            String Class1 = teacherManageClass.getClass1();
            List<ClassExamPaper> classExamPaper = iClassExamPaperService.selectByGradeAndClass(grade,Class1);
            for(ClassExamPaper classExamPaper1 : classExamPaper){
                Long id1 = classExamPaper1.getId();
                if(id1==id){
                    Class1 class1 = iClass1Service.classList(grade,Class1);
                    list1.add(class1.getName());
                }
            }
        }
        classExamPaperDO.setList(list1);
        return R.ok(classExamPaperDO);
    }

    @ApiOperation("试卷列表(根据学科)")
    @GetMapping("examPaperListByStuject")
    public R<IPage<ClassExamPaperDO>> examPaperListById(int pagenum,String subject){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        Long teacherid = iUserService.selectByzhid(zhid);
        List<ExamPaper> list3 = iExamPaperService.selectBySubject(subject);
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        List<ClassExamPaperDO> list1 = new ArrayList<>();
        Map<Long,List<String>> map = new HashMap<>();
        for(ExamPaper examPaper : list3){
            map.put(examPaper.getId(),new ArrayList<>());
        }
        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)){
                    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()){
            List<String> list2 = entry.getValue();
            if(list2.isEmpty()){
                continue;
            }
            ClassExamPaperDO classExamPaperDO = new ClassExamPaperDO();
            Long id = entry.getKey();
            classExamPaperDO.setId(id);
            ExamPaper examPaper = iExamPaperService.selectById(id);
            classExamPaperDO.setSubject(examPaper.getSubject());
            classExamPaperDO.setGrade(examPaper.getGrade());
            classExamPaperDO.setName(examPaper.getName());
            classExamPaperDO.setList(entry.getValue());
            classExamPaperDO.setCreatetime(examPaper.getCreatetime());
            list1.add(classExamPaperDO);
        }
        Page<ClassExamPaperDO> page = new Page<>(pagenum,10);
        IPage<ClassExamPaperDO> iPage = new Page<>(page.getCurrent(),page.getSize(),page.getTotal());
        iPage.setRecords(list1);
        return R.ok(iPage);
    }

    @ApiOperation("管理的班级")
    @PostMapping("Class")
    public R<List<Class1>> Class(){
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long zhid = sysUser.getUserId();
        Long teacherid = iUserService.selectByzhid(zhid);
        List<TeacherManageClass> list = iTeacherManageClassService.totalClass(teacherid);
        List<Class1> list1 = new ArrayList<>();
        for(TeacherManageClass teacherManageClass : list){
            list1.add(iClass1Service.classList(teacherManageClass.getGrade(),teacherManageClass.getClass1()));
        }
        return R.ok(list1);
    }


    @ApiOperation("试卷创编（添加试卷）")
    @PostMapping("addPaperExam")
    public R<String> addPaperExam(@RequestBody PaperCreateDO1 paperCreateDO1){
        int b = iExamPaperService.addPaperExam(paperCreateDO1.getPaperCreateDO());
        if(b!=0){
            Long pid = iExamPaperService.selectId(paperCreateDO1.getPaperCreateDO());
            for(Long a : paperCreateDO1.getId()){
                Class1 class1 = iClass1Service.selectClass1(a);
                iClassExamPaperService.addClassExamPaper(class1.getGrade(), class1.getClass1(), pid);
            }
            return R.ok("添加成功！");
        }else if(b==-1){
            return R.ok("该试卷已存在！");
        }
        return R.fail("试卷添加失败！");
    }

    @GetMapping("selectId")
    public Long selectId(@RequestBody PaperCreateDO paperCreateDO){
        return iExamPaperService.selectId(paperCreateDO);
    }

    @ApiOperation("试卷创编（添加题目）")
    @GetMapping("addPaperCreate")
    public R<String> addPaperCreate(Long id,Long pid){
        if(pid!=-1){
            if(iExamCreateService.addPaperCreate(id,pid)!=0){
                return R.ok("添加成功！");
            }
            return R.fail("添加失败！");
        }
        return R.fail("找不到该试卷！");
    }

    @ApiOperation("试卷创编（删除题目）")
    @DeleteMapping("deletePaperCreate")
    public R<String> deletePaperCreate(Long id,Long pid){
        if(iExamCreateService.deletePaperCreate(id,pid)!=0){
            return R.ok("删除成功！");
        }
        return R.fail("删除失败！");
    }

    @ApiOperation("试卷创编（试卷显示）")
    @GetMapping("selectPaperCreate")
    public R<List<QuestionbankDO>> selectPaperCreate(Long pid){
        return R.ok(iExamCreateService.selectPaperCreate(pid));
    }

    @ApiOperation("试卷创编（通过id查找题库）")
    @GetMapping("selectQuestionBankById")
    public R<QuestionbankDO1> selectQuestionBankById(Long id){
        return R.ok(iQuestionbankService.selectQuestionBankById(id));
    }

    @ApiOperation("试卷创编（通过题目类型查找题库）")
    @GetMapping("selectQuestionBankByQuestionType")
    public R<List<QuestionbankDO1>> selectQuestionBankByQuestionType(String type){
        return R.ok(iQuestionbankService.selectQuestionBankByQuestionType(type));
    }
}
