package com.xinelu.mobile.controller;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.xinelu.common.config.AliYunSmsConfig;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.utils.uuid.IdUtils;
import com.xinelu.manage.dto.smssend.SmsInfoDTO;
import com.xinelu.mobile.utils.SmsSendUtils;
import com.xinelu.mobile.utils.WeChatAppletUtils;
import com.xinelu.mobile.utils.WeChatOfficialAccountUtils;
import com.xinelu.mobile.vo.wechatofficialaccountcallback.PatientVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description 测试controller
 * @Author 纪寒
 * @Date 2024-02-18 16:39:49
 * @Version 1.0
 */
@RestController
@RequestMapping("/testMobile")
public class MobileTestController {
    @Resource
    private WeChatAppletUtils weChatAppletUtils;
    @Resource
    private WeChatOfficialAccountUtils weChatOfficialAccountUtils;
    @Resource
    private SmsSendUtils smsSendUtils;
    @Resource
    private AliYunSmsConfig aliYunSmsConfig;

    /**
     * 测试获取微信小程序accessToken
     */
    @GetMapping("/getAppletAcccessToken")
    public String getAppletAccessToken() {
        return weChatAppletUtils.getWeChatAppletAccessToken();
    }

    /**
     * 测试获取微信公众号accessToken
     */
    @GetMapping("/getOfficialAccountAccessToken")
    public String getOfficialAccountAccessToken() {
        return weChatOfficialAccountUtils.getWeChatOfficialAccountAccessToken();
    }

    /**
     * 测试微信公众号模板消息发送
     */
    @GetMapping("/sendOfficialAccountTemplate")
    public void sendOfficialAccountTemplateMessage() {
        weChatOfficialAccountUtils.sendOfficialAccountTemplateMessage();
    }

    /**
     * 测试微信小程序模板消息发送
     */
    @GetMapping("/sendAppletTemplate")
    public void sendAppletTemplate(PatientVO patientVO) {
        weChatOfficialAccountUtils.sendAppletTemplateMessage(patientVO);
    }

    /**
     *发送短信验证
     */
    @GetMapping("/sendSms")
    public AjaxResult sendAppletTemplate(String phoneNum) throws ClientException {
        SmsInfoDTO smsInfoDTO = new SmsInfoDTO();
        smsInfoDTO.setPhoneNumbers(phoneNum);
        smsInfoDTO.setSignName(aliYunSmsConfig.getSignName());
        smsInfoDTO.setTemplateCode(aliYunSmsConfig.getTemplateCode());
        // 生成六位数验证码
        String code = String.valueOf(IdUtils.generateSixDigitNumber());
        JSONObject obj = new JSONObject();
        obj.put("code", code);
        smsInfoDTO.setTemplateParam(obj);
        Boolean b = smsSendUtils.sendSms(smsInfoDTO);
        if (!b) {
            return AjaxResult.error("短信发送失败");
        }
        return AjaxResult.success("短信发送成功");
    }
}
