package com.ruoyi.hezhi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbSmsCode;
import com.ruoyi.hezhi.mapper.TbSmsCodeMapper;
import com.ruoyi.hezhi.service.ITbSmsCodeService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * 短信验证码Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-06
 */
@Service
public class TbSmsCodeServiceImpl implements ITbSmsCodeService
{
    @Autowired
    private TbSmsCodeMapper tbSmsCodeMapper;

    /**
     * 查询短信验证码
     *
     * @param smsCodeId 短信验证码主键
     * @return 短信验证码
     */
    @Override
    public TbSmsCode selectTbSmsCodeBySmsCodeId(Long smsCodeId)
    {
        return tbSmsCodeMapper.selectTbSmsCodeBySmsCodeId(smsCodeId);
    }

    /**
     * 查询短信验证码列表
     *
     * @param tbSmsCode 短信验证码
     * @return 短信验证码
     */
    @Override
    public List<TbSmsCode> selectTbSmsCodeList(TbSmsCode tbSmsCode)
    {
        return tbSmsCodeMapper.selectTbSmsCodeList(tbSmsCode);
    }

    /**
     * 新增短信验证码
     *
     * @param tbSmsCode 短信验证码
     * @return 结果
     */
    @Override
    public int insertTbSmsCode(TbSmsCode tbSmsCode)
    {
        tbSmsCode.setCreateTime(DateUtils.getNowDate());
        return tbSmsCodeMapper.insertTbSmsCode(tbSmsCode);
    }

    /**
     * 修改短信验证码
     *
     * @param tbSmsCode 短信验证码
     * @return 结果
     */
    @Override
    public int updateTbSmsCode(TbSmsCode tbSmsCode)
    {
        tbSmsCode.setUpdateTime(DateUtils.getNowDate());
        return tbSmsCodeMapper.updateTbSmsCode(tbSmsCode);
    }

    /**
     * 批量删除短信验证码
     *
     * @param smsCodeIds 需要删除的短信验证码主键
     * @return 结果
     */
    @Override
    public int deleteTbSmsCodeBySmsCodeIds(Long[] smsCodeIds)
    {
        return tbSmsCodeMapper.deleteTbSmsCodeBySmsCodeIds(smsCodeIds);
    }

    /**
     * 删除短信验证码信息
     *
     * @param smsCodeId 短信验证码主键
     * @return 结果
     */
    @Override
    public int deleteTbSmsCodeBySmsCodeId(Long smsCodeId)
    {
        return tbSmsCodeMapper.deleteTbSmsCodeBySmsCodeId(smsCodeId);
    }

    /**
     * 生成验证码记录
     * @param phone 手机号
     * @param smsCode 验证码
     * @param type 类型（0-默认，1-手机号登录，2-修改个人资料，3-考试报名）
     * @return 结果
     */
    @Override
    public AjaxResult sendSmsCodeInsert(String phone, String smsCode, Integer type) {
        TbSmsCode tbSmsCode = new TbSmsCode();
        tbSmsCode.setPhone(phone);
        tbSmsCode.setSmsCode(smsCode);
        tbSmsCode.setType(type);
        tbSmsCode.setStatus(0);
        tbSmsCode.setCreateTime(DateUtils.getNowDate());
        tbSmsCode.setDelFlag(0);
        tbSmsCodeMapper.insertTbSmsCode(tbSmsCode);
        return AjaxResult.success("发送成功").put("data", smsCode);
    }

    /**
     * 校验验证码
     * @param phone 手机号
     * @param smsCode 验证码
     * @param type 类型（0-默认，1-手机号登录，2-修改个人资料，3-考试报名）
     * @return 结果
     */
    @Override
    public String checkSmsCode(String phone, String smsCode, Integer type) {
        TbSmsCode tbSmsCode = tbSmsCodeMapper.checkSmsCode(phone, smsCode, type);
        if (ObjectUtils.isEmpty(tbSmsCode)) {
            return "验证码错误";
        }
        if (tbSmsCode.getStatus() == 1) {
            return "验证码已使用，请重新获取";
        }
        if (DateUtils.compare(tbSmsCode.getCreateTime(), Calendar.MINUTE, 5)) {
            return "验证码已超时";
        }
        tbSmsCode.setStatus(1);
        tbSmsCode.setUpdateTime(DateUtils.getNowDate());
        if (tbSmsCodeMapper.updateTbSmsCode(tbSmsCode) > 0) {
            return "SUCCESS";
        } else {
            return "FAIL";
        }
    }

    /**
     * 生成验证码记录
     * @param phone 手机号
     * @param smsCode 验证码
     * @param type 类型（0-默认，1-手机号登录，2-修改个人资料，3-考试报名）
     * @return 结果
     */
    @Override
    public AjaxResult checkPhoneSmsCode(String phone, String smsCode, Integer type) {
        TbSmsCode tbSmsCode = tbSmsCodeMapper.checkSmsCode(phone, smsCode, type);
        if (ObjectUtils.isEmpty(tbSmsCode)) {
            return AjaxResult.error("验证码错误");
        }
        if (tbSmsCode.getStatus() == 1) {
            return AjaxResult.error("验证码已使用，请重新获取");
        }
        if (DateUtils.compare(tbSmsCode.getCreateTime(), Calendar.MINUTE, 5)) {
            return AjaxResult.error("验证码已超时");
        }
        tbSmsCode.setStatus(1);
        tbSmsCode.setUpdateTime(DateUtils.getNowDate());
        // tbSmsCodeMapper.updateTbSmsCode(tbSmsCode);
        return AjaxResult.success();
    }
}
