package com.xunhong.erp.turbo.admin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.alibaba.cola.dto.SingleResponse;
import com.xunhong.erp.turbo.api.facade.api.WxMaServiceI;
import com.xunhong.erp.turbo.api.facade.dto.vo.WxMaOcrBankCardVO;
import com.xunhong.erp.turbo.api.facade.dto.vo.WxMaOcrIdCardVO;
import com.xunhong.erp.turbo.base.utils.FileUploadUtil;
import com.xunhong.erp.turbo.file.FileService;
import com.xunhong.erp.turbo.file.config.OssProperties;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
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.IOException;

/**
 * @author shenyifei
 */
@Tag(name = "Ocr", description = "OCR识别")
@RestController("operationOcrController")
@RequestMapping(value = "/operation")
@RequiredArgsConstructor
public class OcrController {
	private final OssProperties properties;

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

	private final FileService fileService;

	@SaCheckLogin
	@PostMapping(value = "ocrIdCard", consumes = "multipart/form-data")
	@Operation(summary = "OCR识别身份证", method = "POST", hidden = true)
	public SingleResponse<WxMaOcrIdCardVO> ocrIdCard(@RequestParam @Schema(title = "图片文件", requiredMode = Schema.RequiredMode.REQUIRED, type = "file") MultipartFile file) throws IOException {
		// 使用FileUploadUtil安全处理文件名
		String objectName = FileUploadUtil.generateObjectName(file);
		fileService.upload(objectName, file.getInputStream());
		String ocrUrl = properties.getDomain() + objectName;

		return SingleResponse.of(wxMaService.ocrIdCard(ocrUrl));
	}

	@SaCheckLogin
	@PostMapping(value = "ocrBankCard", consumes = "multipart/form-data")
	@Operation(summary = "OCR识别银行卡", method = "POST", hidden = true)
	public SingleResponse<WxMaOcrBankCardVO> ocrBankCard(@RequestParam @Schema(title = "图片文件", requiredMode = Schema.RequiredMode.REQUIRED, type = "file") MultipartFile file) throws IOException {
		// 使用FileUploadUtil安全处理文件名
		String objectName = FileUploadUtil.generateObjectName(file);
		fileService.upload(objectName, file.getInputStream());
		String ocrUrl = properties.getDomain() + objectName;

		return SingleResponse.of(wxMaService.ocrBankCard(ocrUrl));
	}

}
