package com.kym.miniapp.controller;

import com.kym.common.R;
import com.kym.entity.miniapp.UserCoupon;
import com.kym.service.miniapp.MpRelationService;
import com.kym.service.miniapp.UserService;
import com.kym.service.mq.producer.UserCouponSender;
import com.kym.service.wechat.WeixinMPService;
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.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/test")
public class TestController {


    private final UserCouponSender userCouponSender;

    private final WxMpService wxMpService;

    private final UserService userService;
    private final MpRelationService mpRelationService;

    private final WeixinMPService weixinMPService;

    public TestController(UserCouponSender userCouponSender, WxMpService wxMpService, UserService userService, MpRelationService mpRelationService, WeixinMPService weixinMPService) {

        this.userCouponSender = userCouponSender;
        this.wxMpService = wxMpService;
        this.userService = userService;
        this.mpRelationService = mpRelationService;
        this.weixinMPService = weixinMPService;
    }

    /**
     * 测试rabbitmq
     *
     * @return
     */
    @GetMapping(value = "/test")
    R<?> test() {
        for (int i = 0; i < 10; i++) {
            var userCoupon = new UserCoupon().setUserId(i + 1L);
            userCouponSender.sendMessage(userCoupon);
        }
        return R.success();
    }

    @GetMapping(value = "/test1")
    R<?> test1() throws WxErrorException {
        mpRelationService.batchBindMpUser();
        return R.success();
    }

    /**
     * 测试公众号消息
     *
     * @return
     */
    @GetMapping(value = "/test2")
    R<?> test2() throws WxErrorException {
        // 通过unionid获取公众号openid
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("ox6fm6KbWb8Y8q0-xFs3kPREaPm8")
                .templateId("SjszYYiHcN-GaEeZTKJsCcB6ACxYJWfTddkSywf1q1g")
                .url("")
                .build();

        templateMessage.addData(new WxMpTemplateData("character_string2", "3820221130166989945749995"));
        templateMessage.addData(new WxMpTemplateData("thing9", "测试站点"));
        templateMessage.addData(new WxMpTemplateData("character_string3", "SN0001"));
        templateMessage.addData(new WxMpTemplateData("time4", "2022-12-01 19:30:00"));

        wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);

        return R.success();
    }

    @GetMapping(value = "/test3")
    R<?> test3() throws WxErrorException {
        var dateList = List.of("3820221130166989945749995", "测试站点", "SN0001", "2022-12-01 19:30:00");
        weixinMPService.sendMPTemplateMessage("ox6fm6KbWb8Y8q0-xFs3kPREaPm8", "充电开始通知", dateList);
        return R.success();
    }

}
