package com.qnfhq.modules.user.service.impl;

import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.qnfhq.aliyun.AlConfig;
import com.qnfhq.common.exception.RenException;
import com.qnfhq.common.service.impl.BaseServiceImpl;
import com.qnfhq.common.utils.MessageUtils;
import com.qnfhq.common.utils.Result;
import com.qnfhq.modules.user.dao.SmsSendDao;
import com.qnfhq.modules.user.dto.SendPhoneDto;
import com.qnfhq.modules.user.dto.setting.SmsSetting;
import com.qnfhq.modules.user.entity.SettingEntity;
import com.qnfhq.modules.user.entity.SmsSendEntity;
import com.qnfhq.modules.user.entity.SmsTplEntity;
import com.qnfhq.modules.user.enums.SettingEnum;
import com.qnfhq.modules.user.service.SettingService;
import com.qnfhq.modules.user.service.SmsSendService;
import com.qnfhq.modules.user.service.SmsTplService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 短信发送情况
 *
 * @author yelz 30262728@qq.com
 * @since 1.0.0 2025-11-13
 */
@Service
public class SmsSendServiceImpl extends BaseServiceImpl<SmsSendDao, SmsSendEntity> implements SmsSendService {

    @Resource
    private SettingService settingService;

    @Resource
    private SmsTplService smsTplService;


    /**
     * 新版本发送验证码
     * @param mobileNumber
     * @return
     */
    @Override
    public Result sendMobileCodeV2(SendPhoneDto mobileNumber) {
        //短信发送状态
        Result result = new Result();
        try {
            SettingEntity setting = settingService.getSetting(SettingEnum.SMS_SETTING.name());
            SmsSetting smsSetting = JSONUtil.toBean(setting.getSettingValue(), SmsSetting.class);
            Client client = AlConfig.createClient(smsSetting.getMobileAccount(), smsSetting.getMobilePassword());

            SmsTplEntity smsTpl = smsTplService.selectSmsTplByCode(mobileNumber.getCode());
            if(smsTpl == null) {
                throw new RenException(MessageUtils.message("sms.template.not.exist"));//短信模板不存在
            }
            //构建短信数据
            SmsSendEntity smsSend = new SmsSendEntity();
            smsSend.setCode(mobileNumber.getCode());
            smsSend.setMobile(mobileNumber.getMobile());
            smsSend.setCreateTime(new Date());
            smsSend.setContent(AlConfig.getTplParams(smsTpl.getContent(),mobileNumber.getParams()));
            //发送短信信息
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setPhoneNumbers(mobileNumber.getMobile())
                    .setSignName(smsSetting.getName())
                    .setTemplateCode(smsTpl.getSendCode())
                    .setTemplateParam(JSONUtil.toJsonStr(mobileNumber.getParams()));
            // 获取响应对象
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            if(sendSmsResponse.getBody().getCode().equals("OK")) {
//                result.setCode(200);
                result.setMsg(MessageUtils.message("common.send.success"));//发送成功
                smsSend.setIsSuccess(1);
            } else {
                smsSend.setIsSuccess(-1);
                smsSend.setError(sendSmsResponse.getBody().getMessage());
                result.error(sendSmsResponse.getBody().getMessage());
                log.error("in err----------"+sendSmsResponse.getBody().getMessage());
            }
            insert(smsSend);
            return result;


        } catch (Exception e) {

            log.error(e.getMessage(), e);
            return result.error(e.getMessage());
        }

    }
}