package com.kym.miniapp.controller;


import com.kym.common.annotation.SysLog;
import com.kym.common.utils.wx.WxPbUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.PrintWriter;


@RestController
@RequestMapping("wx")
public class WeixinController {
    /**
     * 消息通知（订阅、用户消息）
     *
     * @param request
     *//*

    @PostMapping(value = "check", produces = MediaType.TEXT_XML_VALUE)
    @SysLog("微信服务器推送消息")
    public void handleMessage(HttpServletRequest request, HttpServletResponse response) {
        String result = "";
        try {
            String xml = IoUtil.getContent(request.getInputStream());
            logger.info("wx pb receive message:\n{}", xml);
            Map<String, Object> ret = wxPbService.handleMessage(xml);
            if (!CommUtil.isEmptyOrNull(ret)) {
                response.setContentType("text/html");
                result = XmlUtil.map2xml(ret);
            }
        } catch (Exception e) {
            logger.error("WxPbController.handleMessage ERR", e);
        } finally {
            try {
                logger.info("WxPbController.handleMessage response: \n{}",result);
                PrintWriter writer = response.getWriter();
                writer.print(result);
                writer.close();
            } catch (IOException e) {
                logger.error(e.getMessage(),e);
            }
        }
    }
*/

//    @Resource
//    WxMpService wxMpService;
//    private Logger logger = LoggerFactory.getLogger(WeixinController.class);


    /*


     */

    /**
     * 微信服务器token验证
     */
//    @GetMapping(value = "check")
//    @SysLog("微信服务器token验证")
//    public void checkSign(HttpServletResponse response, String signature, String timestamp, String nonce, String echostr) {
//        String responseVal = echostr;
//        try {
//
//            boolean resp = WxPbUtil.checkSign(signature, timestamp, nonce);
//            if (!resp) {
//                responseVal = "failure";
//                logger.error("wxpb check sign ERR!!!!");
//            }
//        } catch (Exception e) {
//            responseVal = "failure";
//        } finally {
//            try {
//                PrintWriter writer = response.getWriter();
//                writer.print(responseVal);
//                writer.flush();
//                writer.close();
//            } catch (Exception e) {
//                logger.error(e.getMessage(), e);
//            }
//        }
//    }

//    @GetMapping(value = "/test")
//    public void test() throws WxErrorException {
//        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
//                .toUser("oK-oR5gAn9hrYNim4x64l4kzmMXs")
//                .templateId("SjszYYiHcN-GaEeZTKJsCcB6ACxYJWfTddkSywf1q1g")
//                .url("")
//                .build();
//        templateMessage.addData(new WxMpTemplateData("character_string2.DATA", "000001"));
//        templateMessage.addData(new WxMpTemplateData("thing9.DATA}", "测试站点"));
//        templateMessage.addData(new WxMpTemplateData("character_string3.DATA", "编号001"));
//        templateMessage.addData(new WxMpTemplateData("time4.DATA", "2022-12-01 19:30:00"));
//        wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
//
//    }

}
