package com.xunwang.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;

import com.xunwang.servicebase.utils.JwtUtils;
import com.xunwang.servicebase.utils.R;
import com.xunwang.education.entity.TeacherCertifications;
import com.xunwang.education.entity.dto.Schools;
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;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private TeacherCertificationsService certificationsService;

    /**
     * TODO
     *
     * @Description 增加 Recommend_uid 字段
     * @Author Paul  Email:8203193@qq.com
     * @Version 1.0
     * @Date 2023/6/13 12:59
     *
     */
    @ApiOperation("老师提交认证信息")
    @PostMapping("saveCertificationInfo")
    public R saveCertificationInfo(@RequestBody TeacherCertifications teacherCertifications, HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        String recouid = JwtUtils.getRecoUIdByJwtToken(request);

        String tempTeacherPhoto = teacherCertifications.getTeacherPhoto();
        teacherCertifications.setTeacherPhotoTemp(tempTeacherPhoto);
        teacherCertifications.setTeacherPhoto("");

        teacherCertifications.setUid(uid);
        teacherCertifications.setRecommendUid(recouid);

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

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

        String tempTeacherPhoto = teacherCertifications.getTeacherPhoto();
        teacherCertifications.setTeacherPhotoTemp(tempTeacherPhoto);
        teacherCertifications.setTeacherPhoto("");

        teacherCertifications.setVerifyStatus("审核中");
        teacherCertifications.setVerifyRefuseReason("无");
        boolean uid1 = certificationsService.update(teacherCertifications, new QueryWrapper<TeacherCertifications>().eq("uid", uid));
        if (uid1) {
            return R.ok().message("更新成功");
        } else {
            return R.ok().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 ("未通过".equals(verifyStatus)) {
            return R.ok().message("未通过");
        }
        if ("审核中".equals(verifyStatus)) {
            return R.ok().message("审核中");
        }
        if ("已通过".equals(verifyStatus)) {
            return R.ok().message("已通过");
        }
        return R.error().message("查询认证未知错误！");
    }

    @ApiOperation("根据用户id查询姓名，学历，头像")
    @GetMapping("getInfos")
    public R getInfos(HttpServletRequest request) {
        Map map = certificationsService.getInfos(request);
        return R.ok().data(map);
    }

    @ApiOperation("根据id查询认证信息")
    @GetMapping("findInfosByUid")
    public R findInfosByUid(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        LambdaQueryWrapper<TeacherCertifications> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeacherCertifications::getUid, uid);
        TeacherCertifications one = certificationsService.getOne(wrapper);
        return R.ok().data("one", one);
    }

    @ApiOperation("获取所有老师注册过的所有学校")
    @GetMapping("/getAllSchools")
    public R getAllSchools() {
        List<Schools> allSchools = certificationsService.getAllSchools();
        return R.ok().data("schools", allSchools);
    }

/**
 * TODO
 *
 * @Description
 * @Author Paul  Email:8203193@qq.com
 * @Version 1.0
 * @Date 2023/6/14 12:17
 *
 */
    @ApiOperation("查询 全部 审核中 的 教员认证 信息")
    @GetMapping("getAllCheckingTeacherInfo")
    public R getAllCheckingTeacherInfo(HttpServletRequest request){
        String uid = JwtUtils.getUIdByJwtToken(request);
        QueryWrapper<TeacherCertifications> queryWrapper = new QueryWrapper<TeacherCertifications>().eq("verify_status", "审核中");
        List<TeacherCertifications> list = certificationsService.list(queryWrapper);
        return R.ok().data("list", list);
    }


/**
 * TODO
 *
 * @Description
 * @Author Paul  Email:8203193@qq.com
 * @Version 1.0
 * @Date 2023/6/14 16:31
 *
 */
    @ApiOperation("审核教员认证信息")
    @GetMapping("updateTeacherQualificationInfo")
    public R updateTeacherQualificationInfo(HttpServletRequest request) {

        String uid = request.getParameter("uid");
        String checkstatus = request.getParameter("checkstatus");
        String checktext = request.getParameter("checktext");

        LambdaQueryWrapper<TeacherCertifications> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TeacherCertifications::getUid, uid);
        TeacherCertifications one = certificationsService.getOne(wrapper);

        if(checkstatus.equals("未通过")){
            one.setVerifyStatus(checkstatus);
            one.setVerifyRefuseReason(checktext);
        }else{
            one.setVerifyStatus(checkstatus);
            one.setVerifyRefuseReason(checktext);
            one.setTeacherPhoto(one.getTeacherPhotoTemp());
        }

        boolean uid1 = certificationsService.update(one, new QueryWrapper<TeacherCertifications>().eq("uid", uid));
        if (uid1) {
            return R.ok().message("教员认证审核成功");
        } else {
            return R.ok().message("教员认证审核失败");
        }
    }
}

