package com.qmth.paper.library.api;


import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.paper.library.business.bean.result.EditResult;
import com.qmth.paper.library.business.service.ExamStudentService;
import com.qmth.paper.library.common.contant.ApiPrefixConstant;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.entity.ExamStudent;
import com.qmth.paper.library.common.util.Result;
import com.qmth.paper.library.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * <p>
 * 考生管理 前端控制器
 * </p>
 */
@Api(tags = "考生管理controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + ApiPrefixConstant.PREFIX_EXAM + "/student")
@Validated
public class ExamStudentController {
    @Resource
    private ExamStudentService examStudentService;

    @ApiOperation(value = "查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findBasicStudentList(@ApiParam(value = "考试ID", required = true) @RequestParam() Long examId,
                                       @ApiParam(value = "课程代码") @RequestParam(required = false) String courseCode,
                                       @ApiParam(value = "学院名称") @RequestParam(required = false) String collegeName,
                                       @ApiParam(value = "专业名称") @RequestParam(required = false) String majorName,
                                       @ApiParam(value = "班级名称") @RequestParam(required = false) String className,
                                       @ApiParam(value = "查询参数（学生学号或姓名）") @RequestParam(required = false) String queryParams,
                                       @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                       @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(examStudentService.findStudentPage(examId, courseCode, collegeName, majorName, className, queryParams, pageNumber, pageSize));
    }

    @ApiOperation(value = "新增/编辑")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = EditResult.class)})
    public Result saveBasicStudent(@Valid @RequestBody ExamStudent basicStudent, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        examStudentService.saveBasicStudent(basicStudent);
        return ResultUtil.ok(true, "");
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    public Result deleteBasicStudent(@RequestParam List<Long> ids) {
        return ResultUtil.ok(examStudentService.removeBasicStudentBatch(ids));
    }

    @ApiOperation(value = "导入模板下载")
    @RequestMapping(value = "/download_template", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    public void downloadTemplate(HttpServletResponse response){
        examStudentService.downloadTemplate(response);
    }

    @ApiOperation(value = "导入")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    public Result basicStudentImportAsync(@ApiParam(value = "学期ID", required = true) @RequestParam Long semesterId,
                                          @ApiParam(value = "考试ID", required = true) @RequestParam Long examId,
                                          @ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file) throws Exception {
        examStudentService.dataImport(semesterId, examId, file);
        return ResultUtil.ok(true, "");
    }

}
