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.api.entity.ExamRoomStudentRelation;
import com.keao.edu.user.dto.ExamRoomStudentRelationDto;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.page.ExamRoomStudentRelationQueryInfo;
import com.keao.edu.user.service.EmployeeService;
import com.keao.edu.user.service.ExamRoomStudentRelationService;
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;

@RestController
@RequestMapping("examRoomStudentRelation")
@Api(tags = "考场与学生关联服务")
public class ExamRoomStudentRelationController extends BaseController {

    @Autowired
    private ExamRoomStudentRelationService examRoomStudentRelationService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation("开启/关闭教室")
    @GetMapping(value = "/switchClassRoom")
    public HttpResponseResult switchClassRoom(Integer openFlag,Integer examinationBasicId,Integer studentId) {
        examRoomStudentRelationService.switchClassRoom(openFlag,examinationBasicId,studentId);
        return succeed();
    }

    @ApiOperation("获取教室学员关联")
    @GetMapping(value = "/getExamRoomStudentRelation")
    public ExamRoomStudentRelation getExamRoomStudentRelation(Integer basicId, String roomId, Integer studentId) {
        return examRoomStudentRelationService.getExamRoomStudentRelation(basicId,roomId,studentId);
    }

    @ApiOperation("给教室分配学员")
    @PostMapping(value = "/addStudentForRoom")
    public HttpResponseResult addStudentForRoom(Long examRoomId, String studentIds){
        examRoomStudentRelationService.addStudentForRoom(examRoomId, studentIds);
        return succeed();
    }

    @ApiOperation("获取教室学员")
    @GetMapping(value = "/findExamRoomStudents")
    public HttpResponseResult<PageInfo<ExamRoomStudentRelationDto>> findExamRoomStudents(ExamRoomStudentRelationQueryInfo queryInfo){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.nonNull(employee)){
                queryInfo.setOrganId(employee.getOrganId());
            }
        }
        return succeed(examRoomStudentRelationService.findExamRoomStudents(queryInfo));
    }

    @ApiOperation("删除指定教室学员")
    @PostMapping(value = "/deleteStudentFromRoom")
    public HttpResponseResult deleteStudentFromRoom(Long examRoomId, String studentIds){
        examRoomStudentRelationService.deleteStudentFromRoom(examRoomId, studentIds);
        return succeed();
    }
}
