package com.xunhong.erp.turbo.facade.app.executor.cmd;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.xunhong.erp.turbo.api.facade.dto.cmd.WxMaCallbackCmd;
import com.xunhong.erp.turbo.api.infra.api.SettingServiceI;
import com.xunhong.erp.turbo.facade.app.executor.common.WxCmdExe;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class WxMaCallbackCmdExe extends WxCmdExe {

	@DubboReference(version = "1.0.0")
	private final SettingServiceI settingService;

	public String execute(WxMaCallbackCmd wxMaCallbackCmd) {
		WxMaService wxMaService = getWxMaService(settingService);

		boolean checkSignature = wxMaService.checkSignature(wxMaCallbackCmd.getTimestamp(), wxMaCallbackCmd.getNonce(), wxMaCallbackCmd.getSignature());
		if (!checkSignature) {
			return "非法请求";
		}

		if (wxMaCallbackCmd.getEchostr() != null) {
			return wxMaCallbackCmd.getEchostr();
		}

		return "";
	}
}
