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.thirdparty.ocr.Ocr;
import com.keao.edu.user.dto.ExamRegistrationStatisticsDto;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.entity.ExamRegistration;
import com.keao.edu.user.page.ExamRegistrationQueryInfo;
import com.keao.edu.user.service.EmployeeService;
import com.keao.edu.user.service.ExamRegistrationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2020.06.23
 */
@RestController
@Api(tags = "学员报名服务")
@RequestMapping(value = "examRegistration")
public class ExamRegistrationController extends BaseController {

    @Autowired
    private ExamRegistrationService examRegistrationService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeService employeeService;

    @ApiOperation(value = "学员报名记录查询")
    @GetMapping(value = "list")
    @PreAuthorize("@pcs.hasPermissions('examRegistration/list')")
    public HttpResponseResult<PageInfo<ExamRegistration>> list(ExamRegistrationQueryInfo 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(examRegistrationService.queryExamRegistrationStudents(queryInfo));
    }

    @ApiOperation(value = "修改学员报名信息")
    @PostMapping(value = "update")
    @PreAuthorize("@pcs.hasPermissions('examRegistration/update')")
    public HttpResponseResult update(@RequestBody ExamRegistration examRegistration) {
        examRegistrationService.update(examRegistration);
        return succeed();
    }


    @ApiOperation(value = "报名")
    @PostMapping(value = "add")
    @PreAuthorize("@pcs.hasPermissions('examRegistration/add')")
    public HttpResponseResult add(ExamRegistration examRegistration) throws Exception {
        SysUser student = sysUserFeignService.queryUserInfo();
        examRegistration.setStudentId(student.getId());
        return succeed(examRegistrationService.addRegistration(examRegistration));
    }

    @ApiOperation(value = "获取招生统计信息")
    @GetMapping(value = "getExamRegistrationStaticsInfo")
    @PreAuthorize("@pcs.hasPermissions('examRegistration/getExamRegistrationStaticsInfo')")
    public HttpResponseResult<ExamRegistrationStatisticsDto> getExamRegistrationStaticsInfo(Integer examId){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        return succeed(examRegistrationService.getExamRegistrationStaticsInfo(sysUser.getId(), examId));
    }


    @ApiOperation(value = "ocr")
    @PostMapping(value = "ocr")
    @ApiImplicitParams({ @ApiImplicitParam(name = "file", value = "身份证图片", required = true, dataType = "file"),
            @ApiImplicitParam(name = "idCardSide", value = "身份证（front-人像面 back-国徽面）", required = true, dataType = "String") })
    public HttpResponseResult ocr(@RequestParam("file") MultipartFile file,String idCardSide) throws IOException {
        return succeed(Ocr.idcard(file,idCardSide));
    }


}
