package com.keao.edu.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.common.page.QueryInfo;
import com.keao.edu.thirdparty.ocr.Ocr;
import com.keao.edu.user.dto.ExamRegistrationDto;
import com.keao.edu.user.dto.ExamRegistrationStatisticsDto;
import com.keao.edu.user.dto.StudentBaseExamsDto;
import com.keao.edu.user.dto.StudentExamListDto;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.entity.ExamRegistration;
import com.keao.edu.user.page.ApplyListQueryInfo;
import com.keao.edu.user.page.ExamRecordQueryInfo;
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 = "applyList")
    public HttpResponseResult<PageInfo<ExamRegistrationDto>> applyList(ApplyListQueryInfo queryInfo) {
        return succeed(examRegistrationService.applyList(queryInfo));
    }

    @ApiOperation(value = "学生端考级曲库考试列表")
    @GetMapping(value = "studentBaseExams")
    public HttpResponseResult<PageInfo<StudentBaseExamsDto>> studentBaseExams(QueryInfo queryInfo) {
        return succeed(examRegistrationService.studentBaseExams(queryInfo));
    }

    @ApiOperation(value = "学生端获取学员考试记录")
    @GetMapping(value = "examList")
    public HttpResponseResult<PageInfo<StudentExamListDto>> examList(ExamRecordQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        queryInfo.setStudentId(sysUser.getId());
        return succeed(examRegistrationService.examList(queryInfo));
    }

    @ApiOperation(value = "修改学员报名信息")
    @PostMapping(value = "update")
    public HttpResponseResult update(@RequestBody ExamRegistration examRegistration) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(Objects.isNull(sysUser)){
            return failed("用户信息异常");
        }
        examRegistration.setStudentId(sysUser.getId());
        examRegistrationService.updateExamRegistration(examRegistration);
        return succeed();
    }


    @ApiOperation(value = "报名")
    @PostMapping(value = "add")
    public HttpResponseResult add(@RequestBody 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();
        Integer organId=null;
        if(!sysUser.getIsSuperAdmin()){
            Employee employee = employeeService.get(sysUser.getId());
            if(Objects.isNull(employee)){
                return failed("用户信息异常");
            }
            organId=employee.getOrganId();
        }
        return succeed(examRegistrationService.getExamRegistrationStaticsInfo(organId, 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));
    }

    @ApiOperation(value = "获取报名信息(报名)")
    @GetMapping(value = "getExamRegistration")
    @ApiImplicitParams({@ApiImplicitParam(name = "examRegistrationId", value = "报名id", required = true, dataType = "int")})
    public HttpResponseResult<ExamRegistrationDto> getExamRegistration(Long examRegistrationId) {
        return succeed(examRegistrationService.getExamRegistration(examRegistrationId));
    }

    @ApiOperation(value = "重新支付(报名用)")
    @PostMapping(value = "repay")
    public HttpResponseResult repay(@RequestParam String orderNo) throws Exception {
        SysUser student = sysUserFeignService.queryUserInfo();
        return succeed(examRegistrationService.repay(student.getId(), orderNo));
    }

}
