package com.hzs.third.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzs.common.core.constant.MagicNumberConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.domain.third.sms.TSmsRecord;
import com.hzs.common.domain.third.sms.TSmsTemplate;
import com.hzs.third.sms.dto.SmsServiceDTO;
import com.hzs.third.sms.service.ISmsService;
import com.hzs.third.sms.service.ITSmsRecordService;
import com.hzs.third.sms.service.ITSmsTemplateService;
import com.hzs.third.sms.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 短信服务
 * @Author: jiang chao
 * @Time: 2023/3/8 14:18
 * @Classname: SmsServiceImpl
 * @PackageName: com.hzs.third.sms.service.impl
 */
@Service
public class SmsServiceImpl implements ISmsService {

    @Autowired
    private ITSmsTemplateService itSmsTemplateService;
    @Autowired
    private ITSmsRecordService itSmsRecordService;

    @Override
    public String sendSms(SmsServiceDTO param) {
        LambdaQueryWrapper<TSmsTemplate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TSmsTemplate::getTemplateCode, param.getTemplateCode());
        queryWrapper.eq(TSmsTemplate::getPkCountry, param.getPkCountry());
        TSmsTemplate smsTemplate = itSmsTemplateService.getOne(queryWrapper);
        if (null == smsTemplate) {
            return "短信模板不存在";
        }
        if (EYesNo.NO.getIntValue() == smsTemplate.getEnable()) {
            return "短信模板禁止使用";
        }

        // 手机号
        String phone = param.getPhone();
        // 发送内容
        String content = smsTemplate.getTemplateContent();
        // 参数列表
        List<String> paramList = param.getParamList();

        if (smsTemplate.getParamNum() != 0) {
            if (smsTemplate.getParamNum() != paramList.size()) {
                return "缺少短信参数";
            }
            content = String.format(content, paramList.toArray());
        }

        Date nowDate = new Date();

        // 短信发送记录
        TSmsRecord smsRecord = new TSmsRecord();

        // 发送短信
        String smsStr = SmsUtil.sendSms(phone, content);
        if (null == smsStr) {
            // 发送成功
            smsRecord.setSendResult(ESmsSendResult.SUCCESS.getValue());
        } else {
            // 发送失败
            smsRecord.setSendResult(ESmsSendResult.FAIL.getValue());
        }

        if (null == param.getSaveFlag() || param.getSaveFlag()) {
            // 保存短信发送记录
            smsRecord.setPkSmsTemplate(smsTemplate.getPkId());
            smsRecord.setPkMember(param.getPkMember());
            smsRecord.setPhone(phone);
            smsRecord.setContent(content);
            smsRecord.setSendScope(ESmsSendScope.MEMBER.getValue());
            smsRecord.setSendType(ESmsSendType.IMMEDIATELY.getValue());
            smsRecord.setSendStatus(ESmsSendStatus.SEND.getValue());
            smsRecord.setSendTime(nowDate);
            if (null != param.getUserId()) {
                smsRecord.setPkCreator(param.getUserId());
            } else {
                smsRecord.setPkCreator(MagicNumberConstants.PK_ADMIN);
            }
            smsRecord.setCreationTime(nowDate);
            smsRecord.setModifiedTime(nowDate);
            smsRecord.setPkCountry(param.getPkCountry());
            itSmsRecordService.save(smsRecord);
        }
        return smsStr;
    }

}
