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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.exam.CoreExaminationPublishGradeConvertType;
import com.yida.data.common.core.entity.exam.CoreExaminationStudent;
import com.yida.data.common.core.entity.exam.CoreStudentReturnData;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.school.exam.mapper.CoreExaminationPublishGradeConvertTypeMapper;
import com.yida.data.school.exam.service.CoreExaminationStudentService;
import com.yida.data.school.exam.service.CorePerformanceService;
import com.yida.data.school.vo.exam.StudentExamListVO;
import com.yida.data.school.vo.exam.StudentExamScoreVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Api(tags = "成绩-前台")
@RestController
@RequestMapping(value = "/exam/performance")
public class CorePerformanceController {

    @Resource
    private CorePerformanceService corePerformanceService;
    @Resource
    private CoreExaminationStudentService coreExaminationStudentService;
    @Resource
    private CoreExaminationPublishGradeConvertTypeMapper coreExaminationPublishGradeConvertTypeMapper;

    @ApiOperation("根据登录用户id查询最新一次的成绩信息 默认进去为最新一次的成绩")
    @GetMapping("/findLastStudentGrade")
    public ResultBean<CoreStudentReturnData> findLastStudentGrade(@ApiParam("用户id，学生或教师") Long userId,
                                                                  @ApiParam("用户类型 4=学生、家长 2=老师") String userType,
                                                                  @ApiParam("用户类型 4=学生、家长 2=老师") Long examId) {
        //判断appId是否为空
        if (userId == null) {
            return ResultBean.buildError("用户没有登录！请登录");
        }
        //当前用户类型；4=学生、家长 2=老师
        //暂时只有学生或者家长能查看成绩信息
        if (!"4".equals(userType)) {
            return ResultBean.buildSuccess();
        }
        //根据学生id查询H5最新一次学生成绩信息
        CoreStudentReturnData coreStudentReturnData = corePerformanceService.findStudentExamineGrade(userId, examId);
        return ResultBean.buildSuccess(coreStudentReturnData);

    }

    @ApiOperation("查询学生历次考试列表")
    @GetMapping("/findStudentExamineList")
    public ResultBean<List<StudentExamListVO>> findStudentExamineList(@ApiParam("用户id，学生、教师") Long userId,
                                                                      @ApiParam("用户类型 4=学生、家长 2=老师") String userType,
                                                                      @ApiParam("考试名称") String examinationName) {
        //判断appId是否为空
        if (userId == null) {
            return ResultBean.buildError("用户没有登录！请登录");
        }
        //暂时只有学生或者家长能查看成绩信息
        if (!"4".equals(userType)) {
            return ResultBean.buildSuccess();
        }
        //根据学生id查询考试列表数据
        List<StudentExamListVO> list = corePerformanceService.findStudentExamineList(userId, examinationName);
        return ResultBean.buildSuccess(list);

    }

    @ApiOperation("根据学生id和考试id查询成绩详情")
    @GetMapping("/findStudentExamineGrade")
    public ResultBean<CoreStudentReturnData> findStudentExamineGrade(
            @ApiParam("学生id") Long studentId,
            @ApiParam("考试id") Long examinationId) {

        if (studentId == null || examinationId == null) {
            return ResultBean.buildError("学生与考试为空");
        }


        //根据学生id和考试id查询H5学生成绩信息
        CoreStudentReturnData coreStudentReturnData = corePerformanceService.findStudentExamineGrade(studentId, examinationId);

        return ResultBean.buildSuccess(coreStudentReturnData);

    }

    @ApiOperation("家长签字")
    @GetMapping("/signGrade")
    public ResultBean signGrade(@ApiParam("考生信息id") Long examinationStudentId) {

        //考生信息id
        Asserts.isFalse(examinationStudentId == null, "考生id不能为空");

        //判断是否重复签字
        CoreExaminationStudent student = coreExaminationStudentService.getOne(Wrappers.lambdaQuery(new CoreExaminationStudent())
                .eq(CoreExaminationStudent::getId, examinationStudentId)
                .eq(CoreExaminationStudent::getReceiptStatus, "1"));
        if (student != null) {
            return ResultBean.buildError("请勿重复签字！");
        }
        //家长签字
        CoreExaminationStudent coreExaminationStudent = new CoreExaminationStudent();
        coreExaminationStudent.setId(examinationStudentId);
        coreExaminationStudent.setReceiptStatus("1");
        coreExaminationStudentService.saveOrUpdate(coreExaminationStudent);

        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询考生该堂考试成绩(仅发布成绩后可调用！)")
    @GetMapping("/getScore")
    public ResultBean<StudentExamScoreVO> getScore(@ApiParam("考试id") Long examId,
                                                   @ApiParam("学生id") Long studentId,
                                                   @ApiParam("科目id，不传为总成绩") Long courseId) {
        return ResultBean.buildSuccess(corePerformanceService.getScore(examId, studentId, courseId));
    }

    @ApiOperation("查询考试的等级转换区间")
    @GetMapping("/listGradeConvertType")
    public ResultBean<List<CoreExaminationPublishGradeConvertType>> listGradeConvertType(@ApiParam("考试id") Long examId) {
        return ResultBean.buildSuccess(coreExaminationPublishGradeConvertTypeMapper.listType(examId));
    }
}
