package com.keao.edu.user.controller;

import com.alibaba.fastjson.JSONObject;
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.api.enums.StudentExamResultApiDto;
import com.keao.edu.user.dto.RecordNotify;
import com.keao.edu.user.dto.StudentExamResultStatisticsDto;
import com.keao.edu.user.entity.Employee;
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.*;

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;

    @RequestMapping(value = "/recordSync")
    public void recordSync(@RequestBody String body) throws Exception {
        RecordNotify recordNotify = JSONObject.parseObject(body, RecordNotify.class);
        studentExamResultService.recordSync(recordNotify);
    }

    @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.isNull(employee)){
                return failed("用户信息异常");
            }
            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 = "/api/updateFinishedExam")
    public void updateFinishedExam(Long examRegistrationId,Integer finishedExam){
        studentExamResultService.updateFinishedExam(examRegistrationId,finishedExam);
    }

    @ApiOperation("获取考试结果")
    @PostMapping(value = "/api/get")
    public StudentExamResultApiDto get(Long id){
        return studentExamResultService.getStudentExamResultApiDto(id);
    }

    @ApiOperation("考试结果统计信息")
    @GetMapping(value = "/getStudentExamResultStatisticsInfo")
    public HttpResponseResult<StudentExamResultStatisticsDto> getStudentExamResultStatisticsInfo(Integer examId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        Integer organId=null;
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.isNull(employee)){
                return failed("用户信息异常");
            }
            organId=employee.getOrganId();
        }
        return succeed(studentExamResultService.getStudentExamResultStatisticsInfo(organId, examId));
    }

    @ApiOperation("确认考生")
    @PostMapping(value = "/confirmStudent")
    public HttpResponseResult confirmStudent(Long examRegistrationId) {
        studentExamResultService.confirmStudent(examRegistrationId);
        return succeed();
    }

    /**
     * 屏蔽指定用户
     * @param roomId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/shieldUserId", method = RequestMethod.POST)
    public Object shieldUserId(Long roomId,Integer shieldFlag)throws Exception {
        studentExamResultService.shieldUserId(roomId,shieldFlag);
        return succeed();
    }
}
