package com.qs.mp.sms.service.impl;

import cn.jsms.api.SendSMSResult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qs.mp.common.enums.ErrorCodeEnum;
import com.qs.mp.common.enums.SmsSendSceneEnum;
import com.qs.mp.common.exception.ServiceException;
import com.qs.mp.common.jsms.JSMSUtils;
import com.qs.mp.sms.domain.SmsLog;
import com.qs.mp.sms.mapper.SmsLogMapper;
import com.qs.mp.sms.service.ISmsLogService;
import com.qs.mp.system.domain.SysUser;
import com.qs.mp.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @auther ygp
 * @create 2021-09-03 11:30:44
 * @describe 短信发送日志表服务实现类
 */
@Service
public class SmsLogServiceImpl extends ServiceImpl<SmsLogMapper, SmsLog> implements ISmsLogService {

  @Autowired
  private ISmsLogService smsLogService;

  @Autowired
  private ISysUserService sysUserService;

  @Override
  public String sendSmsVerifyCode(String mobile) {
    /*SysUser sysUser = sysUserService.selectUserByUserName(mobile);
    if (null == sysUser) {
      throw new ServiceException(ErrorCodeEnum.ERROR_CODE_1013);
    }*/
    SendSMSResult res = JSMSUtils.sendSMSCode(mobile);
    if (res != null) {
      saveSmsLog(mobile, res, SmsSendSceneEnum.VERIFY);
    }
    return res.getMessageId();
  }


  private void saveSmsLog(String mobile, SendSMSResult res, SmsSendSceneEnum scene) {
    SmsLog smsLog = new SmsLog();
    smsLog.setScene(scene);
    smsLog.setMessageId(res.getMessageId());
    smsLog.setMobile(mobile);
    smsLog.setMsg(res.getOriginalContent());
    smsLogService.save(smsLog);
  }
}
