package com.keao.edu.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
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.ExamRoom;
import com.keao.edu.user.dto.ExamRoomDto;
import com.keao.edu.user.dto.ExamRoomStatisticsDto;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.page.ExamRoomQueryInfo;
import com.keao.edu.user.service.EmployeeService;
import com.keao.edu.user.service.ExamRoomService;
import com.netflix.discovery.converters.Auto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("examRoom")
@Api(tags = "考场服务")
public class ExamRoomController extends BaseController {

    @Autowired
    private ExamRoomService examRoomService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation("分页查询监考列表")
    @GetMapping(value = "/list")
    public HttpResponseResult<PageInfo<ExamRoomDto>> getList(ExamRoomQueryInfo 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(examRoomService.queryExamRoomPage(queryInfo));
    }

    @ApiOperation("更新考场")
    @PostMapping(value = "/update")
    public HttpResponseResult update(@RequestBody ExamRoom examRoom){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        examRoom.setOrganId(sysUser.getId());
        examRoomService.update(examRoom);
        return succeed();
    }

    @ApiOperation("创建教室")
    @PostMapping(value = "/createExamRoom")
    public HttpResponseResult createExamRoom(@RequestBody ExamRoom examRoom){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        examRoom.setOrganId(sysUser.getId());
        examRoomService.createExamRoom(examRoom);
        return succeed();
    }

    @ApiOperation("更新教室信息")
    @PostMapping(value = "/updateExamRoom")
    public HttpResponseResult<ExamRoom> updateExamRoom(@RequestBody ExamRoom examRoom){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        examRoom.setOrganId(sysUser.getId());
        return succeed(examRoomService.updateExamRoom(examRoom));
    }

    @ApiOperation("删除教室")
    @PostMapping(value = "/deleteExamRooms")
    public HttpResponseResult deleteExamRooms(String examRoomIds){
        examRoomService.deleteExamRooms(examRoomIds);
        return succeed();
    }

    @ApiOperation("获取教室")
    @GetMapping(value = "/get")
    public ExamRoom getExamRoom(Long id){
        return examRoomService.get(id);
    }

    @ApiOperation("获取考场统计信息")
    @GetMapping(value = "/getExamRoomStatisticsInfo")
    public HttpResponseResult<ExamRoomStatisticsDto> getExamRoomStatisticsInfo(Integer examId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        return succeed(examRoomService.getExamRoomStatisticsInfo(sysUser.getId(), examId));
    }
}
