package com.cool.store.controller.webc;

import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.IDCardSideEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.AliyunService;
import com.cool.store.vo.BusinessLicenseInfoVO;
import com.cool.store.vo.FoodLicenseVO;
import com.cool.store.vo.IdentityCardInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * https://ocr.console.aliyun.com/overview?spm=5176.6660585.ocr_enterprisecard_public_cn-top.i1.3d657992vcDnCC
 * 目前接入了身份证识别、营业执照识别、食品经营许可证
 */
@RestController
@RequestMapping({"/mini/orc"})
@Slf4j
@Api(tags = "文字识别OCR")
public class OcrController {

    @Resource
    AliyunService aliyunService;

    @GetMapping(path = "/getIdentityCardInfo")
    @ApiOperation("根据身份证正面解析获取数据")
    public ResponseResult<IdentityCardInfoVO> getIdentityCardInfo(@RequestParam(value = "faceImageUrl")String faceImageUrl,
                                                                  @RequestParam("side") IDCardSideEnum sideEnum){
        try {
            IdentityCardInfoVO identityCardInfo = aliyunService.getIdentityCardInfo(faceImageUrl, sideEnum);
            return ResponseResult.success(identityCardInfo);
        } catch (Exception e) {
            throw new ServiceException(ErrorCodeEnum.IDENTITY_CARD_PARSE_FAIL);
        }
    }

    @GetMapping(path = "/getBusinessLicenseInfo")
    @ApiOperation("根据营业证照解析获取数据")
    public ResponseResult<BusinessLicenseInfoVO> getBusinessLicenseInfo(@RequestParam(value = "imageUrl")String imageUrl){
        try {
            BusinessLicenseInfoVO businessLicenseInfo = aliyunService.getBusinessLicenseInfo(imageUrl);
            return ResponseResult.success(businessLicenseInfo);
        } catch (Exception e) {
            throw new ServiceException(ErrorCodeEnum.BUSINESS_LICENSE_PARSE_FAIL);
        }
    }

    @GetMapping(path = "/getFoodBusinessLicense")
    @ApiOperation("根据食营证照解析获取数据")
    public ResponseResult<FoodLicenseVO> getFoodLicense(@RequestParam(value = "imageUrl")String imageUrl){
        try {
            FoodLicenseVO businessLicenseInfo = aliyunService.getFoodLicense(imageUrl);
            return ResponseResult.success(businessLicenseInfo);
        } catch (Exception e) {
            throw new ServiceException(ErrorCodeEnum.FOOD_BUSINESS_LICENSE_PARSE_FAIL);
        }
    }
}
