package com.ruoyi.hezhi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.hezhi.domain.TbTeacherAuthentication;
import com.ruoyi.hezhi.domain.dto.TeacherAuthenticationDTO;
import com.ruoyi.hezhi.mapper.TbTeacherAuthenticationMapper;
import com.ruoyi.hezhi.service.ITbTeacherAuthenticationService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 名师认证Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-13
 */
@Service
public class TbTeacherAuthenticationServiceImpl implements ITbTeacherAuthenticationService
{
    @Autowired
    private TbTeacherAuthenticationMapper tbTeacherAuthenticationMapper;

    /**
     * 查询名师认证
     *
     * @param teacherAuthenticationId 名师认证主键
     * @return 名师认证
     */
    @Override
    public TbTeacherAuthentication selectTbTeacherAuthenticationByTeacherAuthenticationId(Long teacherAuthenticationId)
    {
        return tbTeacherAuthenticationMapper.selectTbTeacherAuthenticationByTeacherAuthenticationId(teacherAuthenticationId);
    }
    /**
     * 查询名师认证
     *
     * @param phone 名师认证主键
     * @return 名师认证
     */
    @Override
    public TbTeacherAuthentication selectTbTeacherAuthenticationByPhone(String phone)
    {
        return tbTeacherAuthenticationMapper.selectTbTeacherAuthenticationByPhone(phone);
    }

    /**
     * 查询名师认证列表
     *
     * @param tbTeacherAuthentication 名师认证
     * @return 名师认证
     */
    @Override
    public List<TbTeacherAuthentication> selectTbTeacherAuthenticationList(TbTeacherAuthentication tbTeacherAuthentication)
    {
        return tbTeacherAuthenticationMapper.selectTbTeacherAuthenticationList(tbTeacherAuthentication);
    }

    /**
     * 新增名师认证
     *
     * @param tbTeacherAuthentication 名师认证
     * @return 结果
     */
    @Override
    public int insertTbTeacherAuthentication(TbTeacherAuthentication tbTeacherAuthentication)
    {
        tbTeacherAuthentication.setCreateTime(DateUtils.getNowDate());
        return tbTeacherAuthenticationMapper.insertTbTeacherAuthentication(tbTeacherAuthentication);
    }

    /**
     * 修改名师认证
     *
     * @param tbTeacherAuthentication 名师认证
     * @return 结果
     */
    @Override
    public int updateTbTeacherAuthentication(TbTeacherAuthentication tbTeacherAuthentication)
    {
        tbTeacherAuthentication.setUpdateTime(DateUtils.getNowDate());
        tbTeacherAuthentication.setUpdateBy(SecurityUtils.getUsername());
        tbTeacherAuthentication.setDelFlag(0);
        return tbTeacherAuthenticationMapper.updateTbTeacherAuthentication(tbTeacherAuthentication);
    }

    /**
     * 批量删除名师认证
     *
     * @param teacherAuthenticationIds 需要删除的名师认证主键
     * @return 结果
     */
    @Override
    public int deleteTbTeacherAuthenticationByTeacherAuthenticationIds(Long[] teacherAuthenticationIds)
    {
        return tbTeacherAuthenticationMapper.deleteTbTeacherAuthenticationByTeacherAuthenticationIds(teacherAuthenticationIds);
    }

    /**
     * 删除名师认证信息
     *
     * @param teacherAuthenticationId 名师认证主键
     * @return 结果
     */
    @Override
    public int deleteTbTeacherAuthenticationByTeacherAuthenticationId(Long teacherAuthenticationId)
    {
        return tbTeacherAuthenticationMapper.deleteTbTeacherAuthenticationByTeacherAuthenticationId(teacherAuthenticationId);
    }

    /**
     * 提交名师认证
     * @param teacherAuthenticationDTO 参数
     * @return 结果
     */
    @Override
    public int submitTeacherAuthentication(TeacherAuthenticationDTO teacherAuthenticationDTO){
        Long memberUserId = teacherAuthenticationDTO.getMemberUserId();

        // 查询是否提交过
        TbTeacherAuthentication tbTeacherAuthentication = tbTeacherAuthenticationMapper.getByMemberUserId(memberUserId);
        if (ObjectUtils.isEmpty(tbTeacherAuthentication)){
            tbTeacherAuthentication = BeanUtil.copyProperties(teacherAuthenticationDTO, TbTeacherAuthentication.class);
            tbTeacherAuthentication.setStatus(0);
            tbTeacherAuthentication.setCreateTime(DateUtils.getNowDate());
            tbTeacherAuthentication.setDelFlag(0);
            return tbTeacherAuthenticationMapper.insertTbTeacherAuthentication(tbTeacherAuthentication);
        }else {
            throw new ServiceException("已提交实名认证，请勿重复提交");
        }

    }
}
