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

import com.baomidou.mybatisplus.core.metadata.IPage;
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.health.EduGrade;
import com.yida.data.common.core.entity.health.EduProject;
import com.yida.data.common.core.entity.health.EduStudentPhysicalExaminationGradeResults;
import com.yida.data.common.core.entity.health.EduStudentProjectTestResult;
import com.yida.data.school.health.service.EduGradeService;
import com.yida.data.school.health.service.EduStudentPhysicalExaminationGradeResultsService;
import com.yida.data.school.health.service.EduStudentProjectTestResultService;
import com.yida.data.school.vo.health.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 学生单项成绩记录表 Controller
 *
 * @author wjm
 * @date 2021-09-14 17:16:24
 */
@Api(tags = "体质健康-学生单项成绩记录")
@Slf4j
@Validated
@RestController
@RequestMapping("/health/testResult")
@RequiredArgsConstructor
public class EduStudentProjectTestResultController {

    private final EduGradeService eduGradeService;
    private final EduStudentProjectTestResultService resultService;
    private final EduStudentPhysicalExaminationGradeResultsService gradeResultsService;

    @PostMapping("/saveStudentSelfTest")
    @ApiOperation("保存学生自测成绩")
    public ResultBean saveStudentSelfTest(EduStuProjectVO stuProjectVO) {
        resultService.saveStudentSelfTest(stuProjectVO);
        return ResultBean.buildSuccess();
    }

    @GetMapping("/findStuSelfTestProjectList")
    @ApiOperation("查询学生自测成绩列表")
    public ResultBean<List<EduProjectVO>> findStuSelfTestProjectList(
            @ApiParam(required = true, value = "学生性别") @RequestParam String stuSex,
            @ApiParam(required = true, value = "学生性别") @RequestParam Long id,
            @ApiParam(required = true, value = "填报年级主键id") @RequestParam Long recordGradeId) {

        return ResultBean.buildSuccess(resultService.findStuSelfTestProjectList(stuSex, recordGradeId, id));
    }

    /**
     * 查询学年国际自测成绩
     *
     * @param id 成绩主键id
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.school.vo.health.EduNationalProjectVO>
     * @author ZYJ
     * @date 2021/11/10 15:57
     */
    @GetMapping("/findStuSelfTest")
    @ApiOperation("查询学年国际自测成绩")
    public ResultBean<EduNationalProjectVO> findStuSelfTest(@RequestParam Long id) {
        return ResultBean.buildSuccess(resultService.findStuSelfTest(id));
    }

    @GetMapping("/findStuNationalTest")
    @ApiOperation("查询学年国际体测成绩")
    public ResultBean<EduNationalProjectVO> findStuNationalTest(@RequestParam Long id) {
        return ResultBean.buildSuccess(resultService.findStuNationalTest(id));
    }

    @GetMapping("/findStuHistoryResults")
    @ApiOperation("查询学年国际体测历史列表")
    public ResultBean<List<EduProjectVO>> findStuHistoryResultsList(
            @ApiParam(required = true, value = "学生主键id") @RequestParam Long stuId,
            @ApiParam(required = true, value = "填报年级id") @RequestParam Long recordGradeId,
            @ApiParam(required = true, value = "学生性别") @RequestParam String stuSex) {
        return ResultBean.buildSuccess(resultService.findStuHistoryResultsList(stuId, recordGradeId, stuSex));
    }

    @GetMapping("/findStuNationalAndSelfTest")
    @ApiOperation("app查询自测和国标体测成绩")
    public ResultBean findStuNationalAndSelfTest(@ApiParam("学生id") @RequestParam Long studentId) {

        return ResultBean.buildSuccess(resultService.findStuNationalAndSelfTest(studentId));
    }

    @GetMapping("/findPageStuSelfTestProjectDetail")
    @ApiOperation("后台查询学生自测成绩详情")
    public ResultBean<List<EduNationalProjectVO>> findPageStuSelfTestProjectDetail(@ApiParam("学生id") @RequestParam Long studentId) {

        return ResultBean.buildSuccess(resultService.findPageStuSelfTestProjectList(studentId));
    }

    @GetMapping("/findOneStuSelfTestProject")
    @ApiOperation("后台总成绩成绩详情")
    public ResultBean<EduNationalProjectVO> findOneStuSelfTestProject(@ApiParam("年级总成绩id") @RequestParam Long gradeResultId) {

        return ResultBean.buildSuccess(resultService.findOneStuSelfTestProject(gradeResultId));
    }


    @GetMapping("/findPageStuSelfTestProjectList")
    @ApiOperation("后台查询学生自测成绩列表")
    public ResultBean<IPage<StudentHealthListVO>> findPageStuSelfTestProjectList(
            @ApiParam(required = true, value = "学校id") @RequestParam Long schoolId,
            @ApiParam(value = "组id") @RequestParam(required = false) Long groupId,
            @ApiParam("年级id") @RequestParam(required = false) Long gradeId,
            @ApiParam("班级id") @RequestParam(required = false) Long classId,
            @ApiParam("学生姓名") @RequestParam(required = false) String studentName,
            @ApiParam("类型，0为国际体测，1为自测") @RequestParam(required = false) Integer type,
            @RequestParam(defaultValue = "1") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize) {
        Page<EduNationalProjectVO> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return ResultBean.buildSuccess(resultService.findPageStudent(schoolId,groupId, gradeId, classId, studentName, page,type));
    }

    @PostMapping("/saveOrUpTestProject")
    @ApiOperation("后台新增或修改成绩")
    public ResultBean saveOrUpTestProject(@RequestBody EduProject eduProject) throws Exception {
        resultService.saveOrUpTestProject(eduProject);
        return ResultBean.buildSuccess();
    }

    @GetMapping("/removeTestProject")
    @ApiOperation("删除学生成绩")
    public ResultBean removeTestProject(@ApiParam("总分统计列表Id") @RequestParam Long gradeResultId) {
        resultService.removeTestProject(gradeResultId);
        return ResultBean.buildSuccess();
    }

    @GetMapping("/findGradeProject")
    @ApiOperation("查询年级对应项目")
    public ResultBean<List<ProjectVO>> findGradeProject(@ApiParam("年级Id") @RequestParam Long gradeId,
                                                        @ApiParam("性别,0为男1为女") @RequestParam Integer sextype) {

        return ResultBean.buildSuccess(eduGradeService.findGradeProject(gradeId, sextype));
    }

    @GetMapping("/findAllGrade")
    @ApiOperation("查询所有年级")
    public ResultBean<List<EduGrade>> findAllGrade() {
        return ResultBean.buildSuccess(eduGradeService.list());
    }

    @GetMapping("/findLastHistoryResults")
    @ApiOperation("app查询最新自测")
    public ResultBean<List<SelfHealthVO>> findLastHistoryResults(@ApiParam("学生id") @RequestParam Long studentId,
                                                                 @ApiParam("最近自测数量") @RequestParam Integer num) {

        return ResultBean.buildSuccess(resultService.findStuHistoryResults(studentId, num));
    }

    @GetMapping("/findLastResults")
    @ApiOperation("app查询最新国际体测")
    public ResultBean<NationalHealthVO> findLastResults(@ApiParam("学生id") @RequestParam Long studentId) {

        return ResultBean.buildSuccess(resultService.findLastResults(studentId));
    }

    @GetMapping("/findStudentHistoryResults")
    @ApiOperation("app查询最新国际体测历史数据")
    public ResultBean<List<AppHistoryHealthVO>> findStudentHistoryResults(@ApiParam("学生id") @RequestParam Long studentId) {

        return ResultBean.buildSuccess(resultService.findStudentHistoryResults(studentId));
    }

    @PostMapping("/saveHealthSelfTest")
    @ApiOperation("app保存学生自测成绩")
    public ResultBean saveHealthSelfTest(@RequestBody EduProject eduProject) throws Exception {
        resultService.saveOrUpTestProject(eduProject);

        gradeResultsService.update(Wrappers.lambdaUpdate(new EduStudentPhysicalExaminationGradeResults())
                .eq(EduStudentPhysicalExaminationGradeResults::getId, eduProject.getCoreStudentPhysicalExaminationGradeResults().getId())
                .set(EduStudentPhysicalExaminationGradeResults::getSourceType, 1));

        resultService.update(Wrappers.lambdaUpdate(new EduStudentProjectTestResult())
                .eq(EduStudentProjectTestResult::getGradeResultId, eduProject.getCoreStudentPhysicalExaminationGradeResults().getId())
                .set(EduStudentProjectTestResult::getSourceType, 1));
        return ResultBean.buildSuccess();
    }

    @GetMapping("/findLastEyesTest")
    @ApiOperation("app查询左右眼视力筛查")
    public ResultBean<EyesTestVO> findLastEyesTest(@ApiParam("学生id") @RequestParam Long studentId,
                                                   @ApiParam("列表数量") @RequestParam Integer num) {

        return ResultBean.buildSuccess(resultService.findLastEyesTest(studentId, num));
    }

}
