package org.springblade.gateway.common_gateway.ocr.builder;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.gateway.common_gateway.ocr.entity.OCRBankInfo;
import org.springblade.gateway.common_gateway.ocr.entity.OCRBizilicenseInfo;
import org.springblade.gateway.common_gateway.ocr.entity.OCRIdInfo;
import org.springblade.wx.constant.MiniProgramType;
import org.springblade.wx.service.WeChatService;

/**
 * @author: lianghanqiang
 * @description: 微信ocr识别
 * @since: 9/17/21 -- 2:54 PM
 */
public class WechatOcrTemplate implements OcrTemplate {

	private MiniProgramType miniProgramType;
	private WeChatService weChatService;

	public WechatOcrTemplate(MiniProgramType type){

		this.miniProgramType = type;

		this.weChatService = SpringUtil.getBean(WeChatService.class);
	}

	@Override
	public OCRBankInfo analysisBank(String url) {
		HttpResponse httpResponse = weChatService.ocrBank(url, miniProgramType);
		return transformEntity(httpResponse,OCRBankInfo.class);
	}

	@Override
	public OCRIdInfo analysisIdcard(String url) {
		HttpResponse httpResponse = weChatService.ocrIdcard(url, miniProgramType);
		return transformEntity(httpResponse,OCRIdInfo.class);
	}

	@Override
	public OCRBizilicenseInfo analysisBlizilicense(String url) {
		HttpResponse httpResponse = weChatService.ocrBizilicense(url, miniProgramType);
		return transformEntity(httpResponse,OCRBizilicenseInfo.class);
	}

	private <T>T transformEntity(HttpResponse response,Class<T> clazz){
		return JSON.parseObject(response.body(),clazz);
	}
}
