package com.yiqi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.SysBoolType;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.system.mapper.SysSmsMapper;
import com.yiqi.system.domain.SysSms;
import com.yiqi.system.service.ISysSmsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 短信Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-03
 */
@Service
public class SysSmsServiceImpl extends ServiceImpl<SysSmsMapper,SysSms> implements ISysSmsService {

    /**
     * 查询短信
     *
     * @param id 短信主键
     * @return 短信
     */
    @Override
    public SysSms selectSysSmsById(Long id) {
        return baseMapper.selectSysSmsById(id);
    }

    /**
     * 查询短信列表
     *
     * @param sysSms 短信
     * @return 短信
     */
    @Override
    public List<SysSms> selectSysSmsList(SysSms sysSms) {
        return baseMapper.selectSysSmsList(sysSms);
    }

    /**
     * 新增短信
     *
     * @param sysSms 短信
     * @return 结果
     */
    @Override
    public int insertSysSms(SysSms sysSms) {
        sysSms.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertSysSms(sysSms);
    }

    /**
     * 修改短信
     *
     * @param sysSms 短信
     * @return 结果
     */
    @Override
    public int updateSysSms(SysSms sysSms) {
        return baseMapper.updateSysSms(sysSms);
    }

    /**
     * 批量删除短信
     *
     * @param ids 需要删除的短信主键
     * @return 结果
     */
    @Override
    public int deleteSysSmsByIds(Long[] ids) {
        return baseMapper.deleteSysSmsByIds(ids);
    }

    /**
     * 删除短信信息
     *
     * @param id 短信主键
     * @return 结果
     */
    @Override
    public int deleteSysSmsById(Long id) {
        return baseMapper.deleteSysSmsById(id);
    }

    @Override
    public R checkSms(String phone, String code, String smsType) {
        SysSms smsRecord = getOne(new QueryWrapper<SysSms>().lambda()
                .eq(SysSms::getPhone,phone)
                .eq(SysSms::getSmsType, smsType)
                .gt(SysSms::getCreateTime,new Date(System.currentTimeMillis() - 300000L))
                .orderByDesc(SysSms::getCreateTime),false);
        if(smsRecord == null){
            return R.fail("验证码不正确，请重新输入");
        }
        if(SysBoolType.YES.getCode().equals(smsRecord.getIsUsed()) || smsRecord.getCheckErrorNum() >= 5){
            smsRecord.setCheckErrorNum(smsRecord.getCheckErrorNum() + 1);
            updateById(smsRecord);
            return R.fail("验证码已失效，请重新发送验证码");
        }
        if(!smsRecord.getCode().equals(code)){
            smsRecord.setCheckErrorNum(smsRecord.getCheckErrorNum() + 1);
            updateById(smsRecord);
            return R.fail("验证码不正确，请重新输入");
        }

        //更新验证码为已使用状态
        smsRecord.setIsUsed(SysBoolType.YES.getCode());
        updateById(smsRecord);
        return R.ok();
    }


}
