package com.xunwang.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.xunwang.commonutils.JwtUtils;
import com.xunwang.commonutils.R;
import com.xunwang.education.entity.TeacherCertifications;
import com.xunwang.education.service.TeacherCertificationsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 老师认证 前端控制器
 * </p>
 *
 * @author Long
 * @since 2022-11-24
 */
@ApiSupport(author = "Long")
@Api("老师认证")
@RestController
@RequestMapping("/education/teacher-certifications")
public class TeacherCertificationsController {

    @Autowired
    private TeacherCertificationsService certificationsService;

    @ApiOperation("老师提交认证信息")
    @PostMapping("saveCertificationInfo")
    public R saveCertificationInfo(@RequestBody TeacherCertifications teacherCertifications, HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        teacherCertifications.setUid(uid);

        boolean save = certificationsService.save(teacherCertifications);
        if (save) {
            return R.ok().message("提交审核成功，请耐心等待…");
        }
        return R.error().message("提交审核失败");
    }

    @ApiOperation("查询老师认证是否审核成功")
    @GetMapping("findVerifyStatus")
    public R findVerifyStatus(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        TeacherCertifications teacher = certificationsService.getOne(new QueryWrapper<TeacherCertifications>().eq("uid", uid));
        // 未申请认证
        if (teacher == null) {
            return R.ok().message("未申请认证");
        }
        String verifyStatus = teacher.getVerifyStatus();
        // 拒绝通过
        if ("2".equals(verifyStatus)) {
            return R.ok().message(teacher.getVerifyRefuseReason());
        }
        if ("0".equals(verifyStatus)) {
            return R.ok().message("正在审核中…");
        }
        if ("1".equals(verifyStatus)) {
            return R.ok().message("认证成功,点击前往课程发布");
        }
        return R.error().message("查询认证未知错误！");
    }

}

