package com.yonge.cooleshow.biz.dal.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.TeacherDao;
import com.yonge.cooleshow.biz.dal.dto.req.AuthOperaReq;
import com.yonge.cooleshow.biz.dal.dto.req.TeacherApplyDetailReq;
import com.yonge.cooleshow.biz.dal.dto.search.AuthEntryRecordSearch;
import com.yonge.cooleshow.biz.dal.entity.Teacher;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.enums.YesOrNoEnum;
import com.yonge.cooleshow.biz.dal.vo.TeacherAuthEntryRecordVo;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.utils.string.StringUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yonge.cooleshow.biz.dal.entity.TeacherAuthEntryRecord;
import com.yonge.cooleshow.biz.dal.dao.TeacherAuthEntryRecordDao;
import com.yonge.cooleshow.biz.dal.service.TeacherAuthEntryRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class TeacherAuthEntryRecordServiceImpl extends ServiceImpl<TeacherAuthEntryRecordDao, TeacherAuthEntryRecord> implements TeacherAuthEntryRecordService {

    @Autowired
    private TeacherDao teacherDao;

    @Override
    public TeacherAuthEntryRecordVo detail(Long id) {
        return baseMapper.detail(id);
    }

    @Override
    public IPage<TeacherAuthEntryRecordVo> selectPage(IPage<TeacherAuthEntryRecordVo> page, AuthEntryRecordSearch search) {
        return page.setRecords(baseMapper.selectPage(page, search));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HttpResponseResult<Boolean> doAuth(AuthOperaReq authOperaReq, SysUser sysUser) throws Exception {
        TeacherAuthEntryRecord build = baseMapper.selectById(authOperaReq.getId());
        build.setTeacherAuthStatus(authOperaReq.getPass() ? AuthStatusEnum.PASS : AuthStatusEnum.UNPASS);
        build.setReason(authOperaReq.getReason());
        build.setVerifyUserId(sysUser.getId());
        build.setUpdateTime(new Date());

        //修改teacher表
        Teacher teacher = teacherDao.selectById(build.getUserId());
        if (null == teacher) {
            return HttpResponseResult.failed("未找到用户信息");
        }
        if (authOperaReq.getPass()) {
            teacher.setSubjectId(StringUtil.isEmpty(teacher.getSubjectId()) ? build.getSubjectId() : teacher.getSubjectId());
            teacher.setIntroduction(StringUtil.isEmpty(teacher.getIntroduction()) ? build.getIntroduction() : teacher.getIntroduction());
            teacher.setGraduateSchool(StringUtil.isEmpty(teacher.getGraduateSchool()) ? build.getGraduateSchool() : teacher.getGraduateSchool());
            teacher.setSubject(StringUtil.isEmpty(teacher.getSubject()) ? build.getSubject() : teacher.getSubject());
            teacher.setGradCertificate(StringUtil.isEmpty(teacher.getGradCertificate()) ? build.getGradCertificate() : teacher.getGradCertificate());
            teacher.setDegreeCertificate(StringUtil.isEmpty(teacher.getDegreeCertificate()) ? build.getDegreeCertificate() : teacher.getDegreeCertificate());
            teacher.setTeacherCertificate(StringUtil.isEmpty(teacher.getTeacherCertificate()) ? build.getTeacherCertificate() : teacher.getTeacherCertificate());
        }
        teacher.setEntryFlag(authOperaReq.getPass() ? YesOrNoEnum.YES : YesOrNoEnum.NO);
        teacher.setEntryAuthDate(new Date());
        teacher.setUpdateTime(new Date());
        teacherDao.updateById(teacher);
        return HttpResponseResult.succeed(baseMapper.updateById(build) > 0);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HttpResponseResult<Boolean> doApply(TeacherApplyDetailReq teacherApplyDetailDto, SysUser sysUser) throws Exception {
        teacherApplyDetailDto.setUserId(sysUser.getId());
        //判断用户是否已经提交申请
        if (hasApply(sysUser.getId())) {
            return HttpResponseResult.failed("已经提交过申请");
        }
        //判断用户是否已经通过申请
        Teacher teacher = teacherDao.selectById(sysUser.getId());
        if (YesOrNoEnum.YES.equals(teacher.getEntryFlag())) {
            return HttpResponseResult.failed("已经通过审批");
        }
        //新增申请记录
        TeacherAuthEntryRecord teacherAuthEntryRecord = new TeacherAuthEntryRecord();
        BeanUtils.copyProperties(teacherAuthEntryRecord, teacherApplyDetailDto);
        teacherAuthEntryRecord.setTeacherAuthStatus(AuthStatusEnum.DOING);

        return HttpResponseResult.status(baseMapper.insert(teacherAuthEntryRecord) > 0);
    }

    @Override
    public TeacherAuthEntryRecordVo getLastRecordByUserId(Long userId) {
        return baseMapper.getLastRecordByUserId(userId);
    }

    /***
     * 判断用户是否已经提交申请
     * @author liweifan
     * @param: userId
     * @updateTime 2022/3/21 11:37
     * @return: java.lang.Boolean
     */
    private Boolean hasApply(Long userId) {
        TeacherAuthEntryRecord teacherAuthEntryRecord = baseMapper.selectOne(Wrappers.<TeacherAuthEntryRecord>query().lambda()
                .eq(TeacherAuthEntryRecord::getUserId, userId)
                .eq(TeacherAuthEntryRecord::getTeacherAuthStatus, 1));
        return teacherAuthEntryRecord != null;
    }

}