package com.hotent.uc.manager.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hotent.base.cache.annotation.*;
import com.hotent.base.constants.CacheKeyConst;
import com.hotent.base.exception.BaseException;
import com.hotent.base.manager.impl.BaseManagerImpl;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.base.util.UniqueIdUtil;
import com.hotent.uc.dao.OrgExternalSettingDao;
import com.hotent.uc.dao.OrgUserDao;
import com.hotent.uc.dao.ShortMsgLogDao;
import com.hotent.uc.dao.UserDao;
import com.hotent.uc.manager.OrgExternalSettingManager;
import com.hotent.uc.manager.OrgUserManager;
import com.hotent.uc.manager.ShortMsgLogManager;
import com.hotent.uc.manager.UserManager;
import com.hotent.uc.model.OrgExternalSetting;
import com.hotent.uc.model.OrgUser;
import com.hotent.uc.model.ShortMsgLog;
import com.hotent.uc.model.User;
import com.hotent.uc.util.AliSmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 用户与外部通讯录关系表 服务实现类
 *
 * @company 广州宏天软件股份有限公司
 * @author pangquan
 * @since 2020-10-30
 */
@Service
public class ShortMsgLogManagerImpl extends BaseManagerImpl<ShortMsgLogDao, ShortMsgLog> implements ShortMsgLogManager {
    @Autowired
    private ShortMsgLogManager shortMsgLogManager;
    @Autowired
    private UserDao userDao;
    @Override
    public JSONObject sendMsgYzmCode(String mobile,String signName,String use) {
        User user=userDao.getByMobile(mobile);
        if(user==null){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code","500");
            jsonObject.put("tasked","");
            jsonObject.put("msg","没有该手机号匹配的用户");
            jsonObject.put("data","");
            return jsonObject;
        }
        String msgContent=generatecCaptcha();
        String codeRel= shortMsgLogManager.getMsgVerCode(mobile,use);
        if(StringUtil.isNotEmpty(codeRel)){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code","500");
            jsonObject.put("tasked","");
            jsonObject.put("msg","验证码还在有效期内，请稍后再试");
            jsonObject.put("data","");
            return jsonObject;
        }
        shortMsgLogManager.sendMsgVerCode(mobile,msgContent,use);
        System.out.println("验证码："+msgContent);
        return   AliSmsUtil.sendMsg(mobile,"验证码："+msgContent,signName);
    }

    @Override
    @Cacheable(value ="code",key = "#mobile+'_'+#use",firstCache = @FirstCache(timeUnit = TimeUnit.SECONDS,expireTime=300),
            secondaryCache = @SecondaryCache( timeUnit = TimeUnit.SECONDS,expireTime = 300))
    public String sendMsgVerCode(String mobile, String msg,String use) {
        ShortMsgLog shortMsgLog=new ShortMsgLog();
        shortMsgLog.setContent("验证码："+msg);
        shortMsgLog.setSendNumber(mobile);
        shortMsgLog.setUse(use);
        shortMsgLog.setSendNumber("1");
        shortMsgLog.setCreateTime(LocalDateTime.now());
        this.save(shortMsgLog);
        return msg;
    }

    @Override
    @Cacheable(value ="code", key="#mobile+'_'+#use",firstCache = @FirstCache(timeUnit = TimeUnit.SECONDS,expireTime=300),
            secondaryCache = @SecondaryCache( timeUnit = TimeUnit.SECONDS,expireTime = 300))
    public String getMsgVerCode(String mobile,String use) {
        return null;
    }

    @Override
    public User verifyPhoneCode(String mobile,String use, String code) {
       String codeRel= shortMsgLogManager.getMsgVerCode(mobile,use);
       if(codeRel!=null&&codeRel.equals(code)){
           User user= userDao.getByMobile(mobile);
           return  user;
       }
        return  null;
    }

    @Override
    @CacheEvict(value = "code", key="#mobile+'_'+#use",allEntries=true)
    public void removeMsgYzmCode(String mobile,String use) {

    }

    @Override
    public JSONObject sendMsg(String mobile, String signName, String content) {
        return  AliSmsUtil.sendMsg(mobile,"业务中台告警内容："+content,signName);
    }

    /**
     * @description  生成验证码
     * @author  yangzibin
     * @date    2024/2/22 15:17
     * @param
     * @return  java.lang.String
     */
    public static String generatecCaptcha() {
        Random random = new Random();
        String str = "";
        for (int i = 0; i < 4; i++) {
            int i1 = random.nextInt(10);
            str += i1;
        }
        return str;
    }
}
