package com.keao.edu.user.controller;

import com.keao.edu.auth.api.client.SysUserFeignService;
import com.keao.edu.auth.api.entity.SysUser;
import com.keao.edu.common.controller.BaseController;
import com.keao.edu.common.entity.HttpResponseResult;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.user.dao.EmployeeDao;
import com.keao.edu.user.dto.StudentExamResultStatisticsDto;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.entity.ExamReview;
import com.keao.edu.user.entity.StudentExamResult;
import com.keao.edu.user.page.StudentExamResultQueryInfo;
import com.keao.edu.user.service.EmployeeService;
import com.keao.edu.user.service.StudentExamResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2020.06.30
 */
@RestController
@RequestMapping("studentExamResult")
@Api(tags = "考试结果服务")
public class StudentExamResultController extends BaseController {

    @Autowired
    private StudentExamResultService studentExamResultService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation("查询考试结果")
    @GetMapping(value = "/queryStudentExamResult")
    public HttpResponseResult<PageInfo<StudentExamResult>> queryStudentExamResult(StudentExamResultQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(!sysUser.getIsSuperAdmin()&&Objects.isNull(queryInfo.getOrganId())){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.nonNull(employee)){
                queryInfo.setOrganId(employee.getOrganId());
            }
        }
        return succeed(studentExamResultService.queryStudentExamResult(queryInfo));
    }

    @ApiOperation("修改考试结果")
    @PostMapping(value = "/update")
    public HttpResponseResult update(StudentExamResult examResult){
        studentExamResultService.updateStudentExamResult(examResult);
        return succeed();
    }

    @ApiOperation("考试结果统计信息")
    @PostMapping(value = "/getStudentExamResultStatisticsInfo")
    public HttpResponseResult<StudentExamResultStatisticsDto> getStudentExamResultStatisticsInfo(Integer examId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        return succeed(studentExamResultService.getStudentExamResultStatisticsInfo(sysUser.getId(), examId));
    }

}
