package com.xinelu.applet.controller.wechatappletcallback;

import com.xinelu.applet.dto.appletcallback.MessageSignDTO;
import com.xinelu.applet.service.wechatappletcallback.WeChatAppletCallBackService;
import com.xinelu.applet.utils.XmlUtil;
import com.xinelu.applet.vo.messagepush.WeChatMessagePushVO;
import com.xinelu.common.config.AppletChatConfig;
import com.xinelu.common.utils.aes.AesException;
import com.xinelu.common.utils.aes.WXBizMsgCrypt;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description 微信小程序事件回调控制器
 * @Author 纪寒
 * @Date 2023-03-14 15:09:50
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/nurseApplet/weChatAppletCallBack")
public class WeChatAppletCallBackController {

    @Resource
    private AppletChatConfig appletChatConfig;
    @Resource
    private WeChatAppletCallBackService weChatAppletCallBackService;

    /**
     * 微信小程序回调验证方法
     *
     * @param messageSignDTO 微信输入参数
     * @return 解密后的字符串
     * @throws AesException 异常信息
     */
    @GetMapping
    public String getWeChatAppletCallBack(MessageSignDTO messageSignDTO) throws AesException {
        WXBizMsgCrypt wxCpt = new WXBizMsgCrypt(appletChatConfig.getToken(), appletChatConfig.getEncodingAesKey(), appletChatConfig.getAppletId());
        String verifyMessage = wxCpt.verifyUrl(messageSignDTO.getSignature(), messageSignDTO.getTimestamp(), messageSignDTO.getNonce(), messageSignDTO.getEchostr());
        log.info("微信小程序回调设置验证URL成功，验证信息：verifyMessage = [{}]", verifyMessage);
        return verifyMessage;
    }

    /**
     * 微信小程序消息推送事件回调POST处理
     *
     * @param signature 签名
     * @param timestamp 时间戳
     * @param nonce     随机字符串
     * @param postData  消息体，xml格式
     */
    @PostMapping
    public void handleWeChatAppletCallBack(@RequestParam("signature") String signature,
                                           @RequestParam("timestamp") String timestamp,
                                           @RequestParam("nonce") String nonce,
                                           @RequestBody String postData) throws AesException {
        WXBizMsgCrypt wxCpt = new WXBizMsgCrypt(appletChatConfig.getToken(), appletChatConfig.getEncodingAesKey(), appletChatConfig.getAppletId());
        String decryptMsg = wxCpt.decryptMsg(signature, timestamp, nonce, postData);
        WeChatMessagePushVO weChatMessagePushVO = (WeChatMessagePushVO) XmlUtil.fromXml(decryptMsg, WeChatMessagePushVO.class);
        if (Objects.isNull(weChatMessagePushVO)) {
            log.error("xml数据转换失败，请求信息为： [{}]", decryptMsg);
            return;
        }
        if (StringUtils.isBlank(weChatMessagePushVO.getEvent()) || StringUtils.isBlank(weChatMessagePushVO.getMsgType())) {
            return;
        }
        log.info("微信小程序消息推送回调执行，消息数据为： [{}]", weChatMessagePushVO);
        weChatAppletCallBackService.handleWeChatAppletCallBack(weChatMessagePushVO);
    }
}
