package com.yida.data.school.exam.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.exam.*;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.exam.service.*;
import com.yida.data.school.vo.exam.ExamCourseVO;
import com.yida.data.school.vo.exam.ExamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "成绩-后台-考试")
@RestController
@RequestMapping(value = "/exam/exam")
public class CoreExaminationController{

    @Resource
    private CoreExaminationService coreExaminationService;
    @Resource
    private CoreExaminationStudentCourseService coreExaminationStudentCourseService;
    @Resource
    private CoreExaminationStudentService coreExaminationStudentService;
    @Resource
    private CoreExaminationTypeService coreExaminationTypeService;
    @Resource
    private CoreExaminationCourseService coreExaminationCourseService;

    @ApiOperation("查询考试列表数据")
    @ResponseBody
    @PostMapping("/findExaminationPageList")
    public ResultBean<IPage<CoreExamination>> findExaminationPageList(@ApiParam("考试查询信息") @RequestBody CoreExamination coreExamination) {
        //查询分页数据

        return ResultBean.buildSuccess(coreExaminationService.findExaminationPageList(coreExamination, FebsUtil.getCurrentUser()));
    }

    @ApiOperation("查询所有考试类型")
    @ResponseBody
    @GetMapping("/findAllExaminationType")
    public ResultBean findAllExaminationType() {


        List<CoreExaminationType> list = coreExaminationTypeService.list();

        return ResultBean.buildSuccess(list);

    }

    @ApiOperation("根据考试主键id查询所有考试科目")
    @ResponseBody
    @GetMapping("/findAllExaminationCourse")
    public ResultBean findAllExaminationCourse(@ApiParam("考试id") Integer examinationId) {


        List<CoreExaminationCourse> list =
                coreExaminationCourseService.list(new LambdaQueryWrapper<CoreExaminationCourse>().eq(CoreExaminationCourse::getCoreExaminationId, examinationId));

        return ResultBean.buildSuccess(list);

    }

    @ApiOperation("保存考试信息 新增或编辑")
    @ResponseBody
    @PostMapping("/saveExamination")
    public ResultBean saveExamination(@RequestBody CoreExamination coreExamination) {


        CurrentUser currentUser = FebsUtil.getCurrentUser();

        coreExaminationService.saveExamination(coreExamination, currentUser);

        return ResultBean.buildSuccess();
    }

    @ApiOperation("根据考试id查询考试详细信息")
    @ResponseBody
    @GetMapping("/findExamination")
    public ResultBean findExamination(@ApiParam("考试id") Integer examinationId) {

        CoreExamination examination = coreExaminationService.findExamination(examinationId);
        return ResultBean.buildSuccess(examination);

    }

    /**
     * 家长端使用
     *
     * @param schoolId
     * @param studentId
     * @return
     */
    @ApiOperation("查询学生历史班级")
    @ResponseBody
    @GetMapping("/findHistoryExamination")
    public ResultBean findHistoryExamination(@ApiParam("考试id") Long schoolId, Long studentId) {

        return ResultBean.buildSuccess(coreExaminationService.findHistoryExamination(schoolId, studentId));

    }

    /**
     * 家长端使用
     *
     * @return
     */
    @ApiOperation("查询往期考试列表")
    @ResponseBody
    @GetMapping("/findHistoryExaminationList")
    public ResultBean<List<ExamVO>> findHistoryExaminationList(@ApiParam("考试名字") String examName,
                                                               @ApiParam("学生id") Long studentId) {
        return ResultBean.buildSuccess(coreExaminationService.findHistoryExaminationList(examName, studentId));
    }

    @ApiOperation("查询学生参与的考试科目")
    @GetMapping("/listStudentExamCourse")
    public ResultBean<List<ExamCourseVO>> listStudentExamCourse(@ApiParam("考试id") Long examinationId,
                                                                @ApiParam("学生id") Long studentId) {
        CoreExaminationStudent student = coreExaminationStudentService.getOne(Wrappers.<CoreExaminationStudent>lambdaQuery()
                .eq(CoreExaminationStudent::getCoreExaminationId, examinationId)
                .eq(CoreExaminationStudent::getCoreStudentId, studentId));
        Asserts.isNotNull(student, "考生不存在");
        List<CoreExaminationStudentCourse> list = coreExaminationStudentCourseService.list(Wrappers.<CoreExaminationStudentCourse>lambdaQuery()
                .eq(CoreExaminationStudentCourse::getCoreExaminationId, examinationId)
                .eq(CoreExaminationStudentCourse::getCoreExaminationStudentId, student.getId()));
        List<ExamCourseVO> res = new ArrayList<>();
        if (CollUtil.isNotEmpty(list)) {
            for (CoreExaminationStudentCourse course : list) {
                ExamCourseVO vo = new ExamCourseVO();
                vo.setCoreCourseId(course.getCoreExaminationCourseId());
                vo.setCoreCourseName(course.getCoreExaminationCourseName());
                res.add(vo);
            }
        }
        return ResultBean.buildSuccess(res);
    }

    @ApiOperation("删除考试")
    @PostMapping("/delExamBatch")
    public ResultBean delExamBatch(@RequestBody Long[] ids) {
        coreExaminationService.delExamBatch(ids);
        return ResultBean.buildSuccess();
    }
}
