package com.ruoyi.hywz.controller;

import cn.com.esgcc.ai.matrix.sdk.constant.OcrMethod;
import cn.com.esgcc.ai.matrix.sdk.esgccenum.ExceptionEnum;
import cn.com.esgcc.ai.matrix.sdk.request.QueryData;
import cn.com.esgcc.ai.matrix.sdk.request.RequestClient;
import cn.com.esgcc.ai.matrix.sdk.response.SdkResponse;
import cn.com.esgcc.ai.matrix.sdk.response.SdkTokenResponse;
import cn.com.esgcc.ai.matrix.sdk.util.encrypt.base64.Base64Util;
import cn.com.esgcc.ai.matrix.sdk.util.encrypt.sm2.Sm2KeyVO;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ai/ocr")
@Api(value = "/ai/ocr", tags = "AI ocr识别")
public class OCRController {

	@Value("${ai.ocr.clientId}")
	private String clientId;
	@Value("${ai.ocr.clientSecret}")
	private String clientSecret;
	@Value("${ai.ocr.username}")
	private String username;
	@Value("${ai.ocr.password}")
	private String password;
	@Value("${ai.ocr.ocrServer}")
	private String ocrServer;
	@Value("${ai.ocr.deviceId}")
	private String deviceId;
	@Value("${ai.ocr.sm2PublicKey}")
	private String sm2PublicKey;
	@Value("${ai.ocr.sm2PrivateKey}")
	private String sm2PrivateKey;
	@Value("${ai.ocr.tempPath}")
	private String tempPath;
	private final static Logger logger = LoggerFactory.getLogger(OCRController. class);

	@PostMapping("/fp")
	public R fp(@RequestParam("file") MultipartFile file) {
		Map<String, String> map = new HashMap<>();
		try {
			QueryData queryData = new QueryData();
			File tempFolder = new File(tempPath);
			if(!tempFolder.exists()){
				tempFolder.mkdirs();
			}
			File tempFile = new File(tempPath+"\\"+file.getOriginalFilename());
			FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile);
			String base64String = Base64Util.file2Base64(tempFile);
			queryData.setImg(base64String);
			SdkResponse sdkResponse = getClient().esgccOcr(OcrMethod.BIZ_ZP, queryData);
			if (ExceptionEnum.COMMON_SUCCESS_1B.getResultCode().equals(sdkResponse.getCode())) {
				JSONObject jsonObject1 = (JSONObject) sdkResponse.getData();
				if (jsonObject1 != null) {
					JSONObject resultStr = jsonObject1.getJSONObject("result");
					map.put("vDate",resultStr.getStr("开票日期"));
					map.put("vNo",resultStr.getStr("发票号码"));
					return R.ok(map);
				}
			}
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
		return R.ok(map);
	}

	private RequestClient getClient() {
		Sm2KeyVO sm2KeyVO = new Sm2KeyVO();
		sm2KeyVO.setPrivateKey(sm2PrivateKey);
		sm2KeyVO.setPublicKey(sm2PublicKey);
		RequestClient client = new RequestClient(ocrServer, deviceId, clientId, clientSecret, username, password, sm2KeyVO);
		SdkTokenResponse sdkResponse = client.oauthUserToken();
		if (ExceptionEnum.COMMON_SUCCESS_1B.getResultCode().equals(sdkResponse.getCode())) {
			logger.debug(sdkResponse.getAccessToken());
		}
		return client;
	}

}