package cn.pluss.platform.phoneValidateCode.impl;

import cn.pluss.platform.entity.PhoneValidateCode;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.PhoneValidateCodeMapper;
import cn.pluss.platform.phoneValidateCode.PhoneValidateCodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Transactional
@Service("phoneValidateCodeService")
public class PhoneValidateCodeServiceImpl extends ServiceImpl<PhoneValidateCodeMapper, PhoneValidateCode> implements PhoneValidateCodeService {

    @Resource
    private PhoneValidateCodeMapper phoneValidateCodeMapper;

    @Override
    public PhoneValidateCode queryPhoneValidateCode(PhoneValidateCode phoneValidateCode) {
        return phoneValidateCodeMapper.queryPhoneValidateCode(phoneValidateCode);
    }

    @Override
    public List<PhoneValidateCode> queryPhoneValidateCodeList(PhoneValidateCode phoneValidateCode) {
        return phoneValidateCodeMapper.queryPhoneValidateCodeList(phoneValidateCode);
    }

    @Override
    public void savePhoneValidateCode(PhoneValidateCode phoneValidateCode) {
        phoneValidateCodeMapper.savePhoneValidateCode(phoneValidateCode);
    }

    @Override
    public void updatePhoneValidateCode(PhoneValidateCode phoneValidateCode) {
        phoneValidateCodeMapper.updatePhoneValidateCode(phoneValidateCode);
    }

    @Override
    public void deletePhoneValidateCode(PhoneValidateCode phoneValidateCode) {
        phoneValidateCodeMapper.deletePhoneValidateCode(phoneValidateCode);
    }

    @Override
    public List<PhoneValidateCode> queryPhoneValidateCodePage(Map map) {
        return phoneValidateCodeMapper.queryPhoneValidateCodePage(map);
    }

    @Override
    public Integer queryPhoneValidateCodePageCount(Map map) {
        return phoneValidateCodeMapper.queryPhoneValidateCodePageCount(map);
    }

    @Override
    public void saveOrReplaceIfExist(PhoneValidateCode phoneValidateCode) {
        //根据当前手机号查询当前手机号是否有验证码信息
        PhoneValidateCode queryPhoneValidateCode = new PhoneValidateCode();
        queryPhoneValidateCode.setPhone(phoneValidateCode.getPhone());
        List<PhoneValidateCode> queryPhoneValidateCodes = queryPhoneValidateCodeList(queryPhoneValidateCode);
        if (queryPhoneValidateCodes.size() > 0) {
            phoneValidateCode.setId(queryPhoneValidateCodes.get(0).getId());
            phoneValidateCode.setSendDt(new Date());
            updatePhoneValidateCode(phoneValidateCode);
        } else {
            phoneValidateCode.setSendDt(new Date());
            savePhoneValidateCode(phoneValidateCode);
        }
    }

    @Override
    public void checkSmsVerifyCode(String phone, String verifyCode) {
        MsgException.checkNull(phone,"手机号不能为空");
        MsgException.checkNull(verifyCode,"验证码不能为空");
        PhoneValidateCode validateCode = new PhoneValidateCode();
        validateCode.setPhone(phone);
        PhoneValidateCode querResult = queryPhoneValidateCode(validateCode);
        if(querResult == null){
            MsgException.throwException("手机号有误或验证码已过期！");
        }
        if(!querResult.getCode().equals(verifyCode)){
            MsgException.throwException("短信验证码有误！");
        }
        deletePhoneValidateCode(validateCode);
    }
}

