package com.xunhong.erp.turbo.facade.adapter.web.controller;

import com.xunhong.erp.turbo.api.facade.api.WxMaServiceI;
import com.xunhong.erp.turbo.api.facade.dto.cmd.WxMaCallbackCmd;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * 微信小程序回调
 */
@Slf4j
@Tag(name = "WxMa", description = "微信小程序")
@RestController("facadeWxMaController")
@RequiredArgsConstructor
@RequestMapping(value = "/facade/wx/ma")
public class WxMaController {

	@DubboReference(version = "1.0.0")
	private final WxMaServiceI wxMaService;

	/**
	 * 验证消息的确来自微信服务器
	 */
	@GetMapping("{appId}/event")
	@Operation(summary = "验证消息的确来自微信服务器", method = "GET", hidden = true)
	public Object callback(
		@PathVariable("appId") String appId,
		@RequestParam("signature") String signature,
		@RequestParam("timestamp") String timestamp,
		@RequestParam("nonce") String nonce,
		@RequestParam("echostr") String echostr
	) {
		log.info(
			"\n接收微信请求：[appId=[{}], signature=[{}], timestamp=[{}], nonce=[{}], echostr=[{}] ",
			appId, signature, timestamp, nonce, echostr);

		if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
			throw new IllegalArgumentException("请求参数非法，请核实!");
		}

		WxMaCallbackCmd wxMpCallbackCmd = new WxMaCallbackCmd();
		wxMpCallbackCmd.setAppId(appId);
		wxMpCallbackCmd.setTimestamp(timestamp);
		wxMpCallbackCmd.setNonce(nonce);
		wxMpCallbackCmd.setSignature(signature);
		wxMpCallbackCmd.setEchostr(echostr);
		return wxMaService.callback(wxMpCallbackCmd);
	}

	@PostMapping("{appId}/event")
	@Operation(summary = "微信消息回调", method = "POST", hidden = true)
	public Object callback(
		@RequestBody(required = false) String requestBody,
		@PathVariable("appId") String appId,
		@RequestParam("signature") String signature,
		@RequestParam("timestamp") String timestamp,
		@RequestParam("nonce") String nonce,
		@RequestParam("openid") String openid,
		@RequestParam("encrypt_type") String encType,
		@RequestParam("msg_signature") String msgSignature
	) {
		log.info(
			"\n接收微信请求：[appId=[{}], openid=[{}], signature=[{}], encType=[{}], msgSignature=[{}],"
				+ " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
			appId, openid, signature, encType, msgSignature, timestamp, nonce, requestBody);


		if (StringUtils.isAnyBlank(signature, timestamp, nonce)) {
			throw new IllegalArgumentException("请求参数非法，请核实!");
		}

		WxMaCallbackCmd wxMpCallbackCmd = new WxMaCallbackCmd();
		wxMpCallbackCmd.setAppId(appId);
		wxMpCallbackCmd.setOpenid(openid);
		wxMpCallbackCmd.setRequestBody(requestBody);
		wxMpCallbackCmd.setTimestamp(timestamp);
		wxMpCallbackCmd.setNonce(nonce);
		wxMpCallbackCmd.setSignature(signature);
		wxMpCallbackCmd.setEncType(encType);
		wxMpCallbackCmd.setMsgSignature(msgSignature);
		return wxMaService.callback(wxMpCallbackCmd);
	}
}
