package com.css.txw.common.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.txw.common.configuration.SMSConfig;
import com.css.txw.common.configuration.SMSConstant;
import com.css.txw.common.net.IHttpService;
import com.css.txw.common.pojo.dto.sms.SMSReqDTO;
import com.css.txw.common.pojo.dto.sms.SMSResDTO;
import com.css.txw.common.service.ISMService;
import com.css.txw.common.util.TxwHttpUtils;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: 短信服务
 * @Autor wujy
 * @Date 2025/11/21
 */
@Slf4j
@Service
public class SMServiceImpl implements ISMService {

    @Resource
    private SMSConfig config;
    @Resource
    private IHttpService httpService;

    //发送短信验证码
    @Override
    public SMSResDTO sendCaptcha(String phone, String code) {
        //时间戳，如：1611472667681
        long timestamp = new Date().getTime();
        String app = config.getApp();
        //请求唯一标识，使用UUID
        String token = GyUtils.getUuid();
        //校验码，使用timestamp+app+token+key进行MD5加密后的字符再进行性一次加密最后形成校验码
        String vcode = DigestUtil.md5Hex(DigestUtil.md5Hex(timestamp + app + token + config.getKey()));
        String url = config.getHost() + SMSConstant.SEND_SM_URI + "?timestamp=" + timestamp + "&token=" + token +
                "&app=" + app + "&vcode=" + vcode;
        //构建报文
        String content = String.format(config.getTemplate(), code);
        SMSReqDTO smReqDTO = new SMSReqDTO();
        smReqDTO.setReceiveMobile(phone);
        smReqDTO.setContent(content);
        //发送
        SMSResDTO res = httpService.postJson(url, new Gson().toJson(smReqDTO), SMSResDTO.class);
        log.info("短信发送 Response {}, code:{} message:{}", res.getType(),res.getCode(), res.getMessage());
        return res;
    }

    // 发送其他短信
    public String sendMsg(SMSReqDTO reqDTO) {
        // to do something
        return null;
    }

}
