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.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.MusicianAuthEntryRecordVo;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yonge.cooleshow.biz.dal.entity.TeacherAuthMusicianRecord;
import com.yonge.cooleshow.biz.dal.dao.TeacherAuthMusicianRecordDao;
import com.yonge.cooleshow.biz.dal.service.TeacherAuthMusicianRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service
public class TeacherAuthMusicianRecordServiceImpl extends ServiceImpl<TeacherAuthMusicianRecordDao, TeacherAuthMusicianRecord> implements TeacherAuthMusicianRecordService {
    @Autowired
    private TeacherDao teacherDao;

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

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

    @Override
    public AuthStatusEnum getMusicianAuthStatus(Long id) {
        TeacherAuthMusicianRecord lastRecordByUserId = getLastRecordByUserId(id);
        return null == lastRecordByUserId ? null : lastRecordByUserId.getTeacherAuthStatus();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HttpResponseResult<Boolean> doApply(Long userId) {
        //判断用户是否已经提交申请
        if (hasApply(userId)) {
            return HttpResponseResult.failed("已经提交过申请");
        }
        //判断用户是否已经通过申请
        Teacher teacher = teacherDao.selectById(userId);
        if(YesOrNoEnum.YES.equals(teacher.getMusicianFlag())){
            return HttpResponseResult.failed("已经通过审批");
        }

        TeacherAuthMusicianRecord record = new TeacherAuthMusicianRecord();
        record.setUserId(userId);
        record.setTeacherAuthStatus(AuthStatusEnum.DOING);
        return HttpResponseResult.status(baseMapper.insert(record) > 0);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HttpResponseResult<Boolean> doAuth(AuthOperaReq authOperaReq, SysUser sysUser) {
        TeacherAuthMusicianRecord 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 = new Teacher();
        teacher.setUserId(build.getUserId());
        teacher.setMusicianFlag(authOperaReq.getPass() ? YesOrNoEnum.YES : YesOrNoEnum.NO);
        teacher.setMusicianDate(new Date());
        teacher.setUpdateTime(new Date());
        teacherDao.updateById(teacher);

        return HttpResponseResult.succeed(baseMapper.updateById(build) > 0);
    }

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

}