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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.constant.CoreAverageConstant;
import com.yida.data.common.core.entity.exam.CoreExamination;
import com.yida.data.common.core.entity.exam.CoreExaminationCoreAverage;
import com.yida.data.common.core.entity.exam.CoreExaminationCourse;
import com.yida.data.school.dto.exam.FindClassExaminationDTO;
import com.yida.data.school.dto.exam.SelectClassScoreDTO;
import com.yida.data.school.exam.mapper.CoreExaminationCoreAverageMapper;
import com.yida.data.school.exam.service.CoreExaminationCoreAverageService;
import com.yida.data.school.exam.service.CoreExaminationTeacherService;
import com.yida.data.school.vo.exam.EduStudentExamVO;
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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "成绩-后台-教师")
@RestController
@RequestMapping("/exam/teacher")
public class CoreExaminationTeacherController {

    @Resource
    private CoreExaminationCoreAverageMapper coreExaminationCoreAverageMapper;
    @Resource
    private CoreExaminationTeacherService coreExaminationTeacherService;

    @Resource
    private CoreExaminationCoreAverageService coreExaminationCoreAverageService;


    @ApiOperation("查询考试列表")
    @GetMapping("/findExaminationList")
    public ResultBean<IPage<CoreExamination>> findExaminationList(@ApiParam("考试年级id") @RequestParam(required = false) Long gradeId,
                                                                  @ApiParam("考试名字") @RequestParam String name,
                                                                  @ApiParam("教师id") @RequestParam Long teacherId,
                                                                  @RequestParam(defaultValue = "1") Integer current,
                                                                  @RequestParam(defaultValue = "10") Integer size){

        Page<CoreExamination> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        IPage<CoreExamination> examinationList = coreExaminationTeacherService.findExaminationList(page,gradeId,name,teacherId);

        return ResultBean.buildSuccess(examinationList);
    }

    @ApiOperation("查询教师负责年级和当前学期")
    @GetMapping("/findTeacherGrade")
    public ResultBean<Map<String, Object>> findTeacherGrade(@ApiParam("教师id") @RequestParam Long teacherId) {

        String date = "1";
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime ldt = LocalDateTime.parse(now.getYear()+"-08-31 00:00:00", df);
        LocalDateTime llt = LocalDateTime.parse(now.getYear()+"-03-01 00:00:00", df);
        LocalDateTime a = LocalDateTime.parse(now.getYear()+"-01-01 00:00:00", df);
        LocalDateTime b = LocalDateTime.parse(now.getYear()+"-12-31 23:59:59", df);
        //现在的日期在9月1号之前
        if (now.isBefore(ldt) && now.isAfter(llt)) {
            date = (now.getYear() - 1) + "-" + now.getYear() + "学年第二学期";
        }else if (now.isBefore(llt) && now.isAfter(a)){
            date = (now.getYear() - 1) + "-" + now.getYear() + "学年第一学期";
        }else if (now.isBefore(b)&&now.isAfter(ldt)){
        date = now.getYear() + "-" + (now.getYear() + 1) + "学年第一学期";}

        HashMap<String, Object> map = new HashMap<>();
        map.put("date", date);
        return ResultBean.buildSuccess(map);
    }


    @ApiOperation("查询年级考试详情")
    @PostMapping("/findClassExamination")
    public ResultBean<EduStudentExamVO> findClassExamination(@ApiParam("考试id,教师id,课程id,班级id") @RequestBody FindClassExaminationDTO findClassExaminationDTO) {

        return ResultBean.buildSuccess(coreExaminationTeacherService.findClassExamination(findClassExaminationDTO));
    }

    @ApiOperation("查询考试科目接口")
    @GetMapping("/findExaminationCourseList")
    public ResultBean<List<CoreExaminationCourse>> findExaminationCourseList(@ApiParam("考试id") @RequestParam Long examId,
                                                                             @ApiParam("教师id") @RequestParam Long teacherId,
                                                                             @ApiParam("学校id") @RequestParam Long schoolId) {

        return ResultBean.buildSuccess(coreExaminationTeacherService.findExaminationCourseList(examId,teacherId,schoolId));
    }

    @ApiOperation("查询老师对应班级成绩单")
    @PostMapping("/selectClassScore")
    public ResultBean selectClassScore(@RequestBody SelectClassScoreDTO selectClassScoreDTO) {

        return ResultBean.buildSuccess(coreExaminationTeacherService.selectClassScore(selectClassScoreDTO));
    }

    @ApiOperation("查询学生个人成绩详情")
    @GetMapping("/selectStudentScore")
    public ResultBean selectStudentScore(@ApiParam("学生id") @RequestParam Long studentId,
                                         @ApiParam("考试id") @RequestParam Long examId,
                                         @ApiParam("课程id") @RequestParam(required = false) Long course) {

        return ResultBean.buildSuccess(coreExaminationTeacherService.selectStudentScore(studentId, examId,course));
    }

    @ApiOperation("设置及格率")
    @PostMapping("/updateScoreAvenge")
    public ResultBean updateScoreAvenge(@RequestBody CoreExaminationCoreAverage coreExaminationCoreAverage) {
        coreExaminationCoreAverageService.updateScoreAvenge(coreExaminationCoreAverage);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询平均分")
    @PostMapping("/findScoreAvenge")
    public ResultBean<CoreExaminationCoreAverage> findScoreAvenge(@ApiParam("考试id") @RequestParam Long examId,
                                                                  @ApiParam("教师id") @RequestParam Long teacherId) {
        CoreExaminationCoreAverage coreExaminationCoreAverage = coreExaminationCoreAverageMapper.selectOne(Wrappers.lambdaQuery(new CoreExaminationCoreAverage())
                .eq(CoreExaminationCoreAverage::getCoreExaminationId, examId)
                .eq(CoreExaminationCoreAverage::getTeacherId, teacherId));
        if (ObjectUtils.isEmpty(coreExaminationCoreAverage)){
            CoreExaminationCoreAverage coreExaminationCoreAverage1 = new CoreExaminationCoreAverage();
            coreExaminationCoreAverage1.setTeacherId(teacherId);
            coreExaminationCoreAverage1.setCoreExaminationId(examId);
            coreExaminationCoreAverage1.setGreat(CoreAverageConstant.AVERAGE_GREAT);
            coreExaminationCoreAverage1.setGood(CoreAverageConstant.AVERAGE_GOOD);
            coreExaminationCoreAverage1.setPass(CoreAverageConstant.AVERAGE_PASS);
            coreExaminationCoreAverage1.setPoor(CoreAverageConstant.AVERAGE_POOR);
            return ResultBean.buildSuccess(coreExaminationCoreAverage1);
        }

        return ResultBean.buildSuccess();
    }
}
