package com.srs.stureg.service.impl;

import java.time.Duration;
import java.util.List;
import java.util.Map;

import com.srs.common.exception.ServiceException;
import com.srs.common.utils.DateUtils;
import com.srs.common.utils.StringUtils;
import com.srs.framework.autoconfig.template.SmsTemplate;
import com.srs.stureg.domain.SrsMsgQueue;
import com.srs.stureg.domain.SrsStuReg;
import com.srs.stureg.mapper.SrsMsgQueueMapper;
import com.srs.stureg.mapper.SrsStuRegMapper;
import com.srs.stureg.service.ISrsMsgQueueService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import static com.srs.common.utils.SecurityUtils.getUsername;

/**
 * 短信队列Service业务层处理
 *
 * @author srs
 * @date 2023-07-24
 */
@Service
public class SrsMsgQueueServiceImpl implements ISrsMsgQueueService
{
    @Autowired
    private SrsMsgQueueMapper srsMsgQueueMapper;
    //redis
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private SmsTemplate template;//阿里云接口
    @Autowired
    private SrsStuRegMapper srsStuRegMapper;

    /**
     * 查询短信队列
     *
     * @param id 短信队列主键
     * @return 短信队列
     */
    @Override
    public SrsMsgQueue selectSrsMsgQueueById(Long id)
    {
        return srsMsgQueueMapper.selectSrsMsgQueueById(id);
    }

    /**
     * 查询短信队列列表
     *
     * @param srsMsgQueue 短信队列
     * @return 短信队列
     */
    @Override
    public List<SrsMsgQueue> selectSrsMsgQueueList(SrsMsgQueue srsMsgQueue)
    {
        return srsMsgQueueMapper.selectSrsMsgQueueList(srsMsgQueue);
    }



    //发送短信
    @Override
    public boolean sendSms(Map<String,Object> msgMap) {
        //随机生成6位数字
        String s = RandomStringUtils.randomNumeric(6);
        String phone=msgMap.get("SJH").toString();
        SrsStuReg srsStuReg = new SrsStuReg();
        srsStuReg.setKSH(msgMap.get("KSH").toString());
        srsStuReg.setSFZH(msgMap.get("SFZH").toString());
        srsStuReg.setSJH(msgMap.get("SJH").toString());
        if(StringUtils.isEmpty(msgMap.get("KSH")+"")||StringUtils.isEmpty(msgMap.get("SFZH")+"")){
            throw new ServiceException("身份证号错误！");
        }
        //查询学生
        List<SrsStuReg> srsStuRegs = srsStuRegMapper.selectSrsStuRegList(srsStuReg);
        if (srsStuRegs.size()==0){
            throw new ServiceException("填写信息错误！");
        }
        //调用template对象，发送手机短信
        try {
            //调用阿里云短信接口
            boolean b = template.sendSms(phone, s,1);
            if (!b){
                return false;
            }
        }catch (Exception e){
            //srsMsgQueue.setStatus("0");
            //srsMsgQueueMapper.insertSrsMsgQueue(srsMsgQueue);
            return false;
        }
        //将验证码存入到redis
        redisTemplate.opsForValue().set("CHECK_CODE_"+phone,s,
                Duration.ofMinutes(5));//ofminutes：设置验证码过期时间
        return true;
       // }
    }

    @Override
    public boolean sendSmsUpdatePhone(Map<String, Object> msgMap) {
        String s = RandomStringUtils.randomNumeric(6);
        String phone=msgMap.get("SJH").toString();
        SrsStuReg srsStuReg = new SrsStuReg();
        srsStuReg.setKSH(msgMap.get("KSH").toString());
        srsStuReg.setSFZH(msgMap.get("SFZH").toString());
        if(StringUtils.isEmpty(msgMap.get("KSH")+"")||StringUtils.isEmpty(msgMap.get("SFZH")+"")){
            return false;
        }
        //查询学生
        List<SrsStuReg> srsStuRegs = srsStuRegMapper.selectSrsStuRegList(srsStuReg);
        if (srsStuRegs.size()==0){
            throw new ServiceException("填写信息错误！");
        }
        //调用template对象，发送手机短信
        try {
            //调用阿里云短信接口
            boolean b = template.sendSms(phone, s,1);
            if (!b){
                return false;
            }
        }catch (Exception e){
            return false;
        }
        //将验证码存入到redis
        redisTemplate.opsForValue().set("CHECK_CODE_"+phone,s,
                Duration.ofMinutes(5));//ofminutes：设置验证码过期时间
        return true;
    }


    @Override
    public boolean sendSmsUpdatePwd(Map<String, Object> msgMap) {
        String s = RandomStringUtils.randomNumeric(6);
        String phone=msgMap.get("SJH").toString();
        SrsStuReg srsStuReg = new SrsStuReg();
        srsStuReg.setXH(msgMap.get("XH").toString());
        srsStuReg.setSFZH(msgMap.get("SFZH").toString());
        if(StringUtils.isEmpty(msgMap.get("XH")+"")||StringUtils.isEmpty(msgMap.get("SFZH")+"")){
            return false;
        }
        //查询学生
        List<SrsStuReg> srsStuRegs = srsStuRegMapper.selectSrsStuRegList(srsStuReg);
        if (srsStuRegs.size()==0){
            throw new ServiceException("填写信息错误！");
        }
        //调用template对象，发送手机短信
        try {
            //调用阿里云短信接口
            boolean b = template.sendSms(phone, s,1);
            if (!b){
                return false;
            }
        }catch (Exception e){
            return false;
        }
        //将验证码存入到redis
        redisTemplate.opsForValue().set("CHECK_CODE_"+phone,s,
                Duration.ofMinutes(5));//ofminutes：设置验证码过期时间
        return true;
    }
    /**
     * 发送所有通知短信
     * @param notice 通知内容
     * @throws Exception
     */
    @Override
    public void sendSmsAll(String notice) throws Exception {
        SrsMsgQueue srsMsgQueue1 = new SrsMsgQueue();
        srsMsgQueue1.setStatus("0");
        List<SrsMsgQueue> srsMsgQueue = srsMsgQueueMapper.selectSrsMsgQueueList(srsMsgQueue1);
        for (SrsMsgQueue s: srsMsgQueue){
            boolean b = template.sendSms(s.getPhone(), notice,2);
            if (b) {
                s.setStatus("1");
                s.setUpdateBy(getUsername());
                s.setUpdateTime(DateUtils.getNowDate());
                srsMsgQueueMapper.updateSrsMsgQueue(s);
            }

        }
    }

    /**
     * 新增短信队列
     *
     * @param srsMsgQueue 短信队列
     * @return 结果
     */
    @Override
    public int insertSrsMsgQueue(SrsMsgQueue srsMsgQueue)
    {
        srsMsgQueue.setCreateTime(DateUtils.getNowDate());
        return srsMsgQueueMapper.insertSrsMsgQueue(srsMsgQueue);
    }

    /**
     * 修改短信队列
     *
     * @param srsMsgQueue 短信队列
     * @return 结果
     */
    @Override
    public int updateSrsMsgQueue(SrsMsgQueue srsMsgQueue)
    {
        srsMsgQueue.setUpdateTime(DateUtils.getNowDate());
        return srsMsgQueueMapper.updateSrsMsgQueue(srsMsgQueue);
    }

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

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