package com.ruoyi.mall.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.ApiResult;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallMemberCmsMapper;
import com.ruoyi.mall.domain.MallMemberCms;
import com.ruoyi.mall.service.IMallMemberCmsService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 手机验证码Service业务层处理
 *
 * @author ruoyi
 * @date 2022-06-28
 */
@Service
public class MallMemberCmsServiceImpl implements IMallMemberCmsService
{
    @Resource
    private MallMemberCmsMapper mallMemberCmsMapper;

    /**
     * 比对验证码，有效期10分钟
     * @param phone 手机号
     * @param cmsCode 验证码
     * @return 0-成功，1-超时，2-错误
     */
    @Override
    public int checkCmsCode(String phone,String cmsCode){
        try {
            //验证码验证
            MallMemberCms cmsByPhone = mallMemberCmsMapper.getCmsByPhone(phone, cmsCode);
            if (ObjectUtils.isNotEmpty(cmsByPhone)){
                String createAt = cmsByPhone.getCreateAt();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date parse = simpleDateFormat.parse(createAt);
                //有效期10分钟
                if (parse.getTime() - new Date().getTime() > 10*60*1000){
                    return 1;
                }
                //验证重置
                MallMemberCms mallMemberCms = new MallMemberCms();
                mallMemberCms.setPhone(phone);
                mallMemberCmsMapper.resetCmsCode(mallMemberCms);
                return 0;
            }
            return 2;
        }catch (Exception e){
            e.printStackTrace();
            return 2;
        }
    }

    /**
     * 查询手机验证码
     *
     * @param id 手机验证码主键
     * @return 手机验证码
     */
    @Override
    public MallMemberCms selectMallMemberCmsById(Long id)
    {
        return mallMemberCmsMapper.selectMallMemberCmsById(id);
    }

    /**
     * 查询手机验证码列表
     *
     * @param mallMemberCms 手机验证码
     * @return 手机验证码
     */
    @Override
    public List<MallMemberCms> selectMallMemberCmsList(MallMemberCms mallMemberCms)
    {
        return mallMemberCmsMapper.selectMallMemberCmsList(mallMemberCms);
    }

    /**
     * 新增手机验证码
     *
     * @param mallMemberCms 手机验证码
     * @return 结果
     */
    @Override
    public int insertMallMemberCms(MallMemberCms mallMemberCms)
    {
        return mallMemberCmsMapper.insertMallMemberCms(mallMemberCms);
    }

    /**
     * 发送手机验证码
     *
     * @param mallMemberCms 手机验证码
     * @return 结果
     */
    @Override
    @Transactional
    public ApiResult sendCmsCode(MallMemberCms mallMemberCms)
    {
        //重置之前的验证码
        mallMemberCmsMapper.resetCmsCode(mallMemberCms);

        //新增
        if (mallMemberCmsMapper.insertMallMemberCms(mallMemberCms) > 0){
            return ApiResult.ok("发送成功");
        }else {
            return ApiResult.ok("网络异常");
        }
    }

    /**
     * 修改手机验证码
     *
     * @param mallMemberCms 手机验证码
     * @return 结果
     */
    @Override
    public int updateMallMemberCms(MallMemberCms mallMemberCms)
    {
        return mallMemberCmsMapper.updateMallMemberCms(mallMemberCms);
    }

    /**
     * 批量删除手机验证码
     *
     * @param ids 需要删除的手机验证码主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberCmsByIds(Long[] ids)
    {
        return mallMemberCmsMapper.deleteMallMemberCmsByIds(ids);
    }

    /**
     * 删除手机验证码信息
     *
     * @param id 手机验证码主键
     * @return 结果
     */
    @Override
    public int deleteMallMemberCmsById(Long id)
    {
        return mallMemberCmsMapper.deleteMallMemberCmsById(id);
    }
}
