package com.cwhelp.admin.business.controller.api;

import com.cwhelp.common.api.baidu.BaiduAipOCR;
import com.cwhelp.common.api.baidu.entity.vatinvoice.*;
import com.cwhelp.common.constant.ApiConst;
import com.cwhelp.common.enums.InvoiceTypeEnum;
import com.cwhelp.common.utils.ResultVoUtil;
import com.cwhelp.common.vo.ResultVo;
import com.cwhelp.component.fileUpload.FileUpload;
import com.cwhelp.component.fileUpload.enums.UploadResultEnum;
import com.cwhelp.devtools.generate.utils.jAngel.utils.StringUtil;
import com.cwhelp.modules.business.domain.BssVatInvoice;
import com.cwhelp.modules.system.domain.Upload;
import com.cwhelp.modules.system.service.ApiBizService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;

/**
 * Created by huangjc on 2019/12/1 0001.
 */
@RestController
@RequestMapping("/apiBiz")
public class BssApiBizController {

    @Autowired
    private ApiBizService apiBizService;

    @PostMapping("/invoiceOCR")
    @ResponseBody
    public ResultVo invoiceOCR(@RequestParam("image") MultipartFile multipartFile,
                               @RequestParam("invoiceType") int invoiceType,
                               @RequestParam("operator") String operator){

        if (multipartFile.getSize() == 0){
            return new ResultVo(ApiConst.API_PARAM_BLANK_CODE, UploadResultEnum.NO_FILE_NULL.getMessage());
        }

        if(invoiceType == 0 || StringUtil.isBlank(operator)){
            return new ResultVo(ApiConst.API_PARAM_BLANK_CODE, ApiConst.API_PARAM_BLANK_MSG);
        }

        // 创建Upload实体对象
        Upload upload = FileUpload.getFile(multipartFile, "/images");
        try {
            FileUpload.transferTo(multipartFile, upload);
            File destFile = FileUpload.getDestFile(upload);

            BssVatInvoice vatInvoice = new BssVatInvoice();
            if(invoiceType == InvoiceTypeEnum.TRAIN_INVOICE.getCode()){
                TrainResult trainResult = (TrainResult)BaiduAipOCR.queryBaiduVatInvoiceApi(destFile, invoiceType);
                TrainWordsResult trainWordsResult = trainResult.getWords_result();
                vatInvoice = apiBizService.getResultByTrain(trainWordsResult, destFile.getPath());
            }else if(invoiceType == InvoiceTypeEnum.ADD_VALUE_TAX_INVOICE.getCode()){
                VatInvoiceResult vatInvoiceResult = (VatInvoiceResult)BaiduAipOCR.queryBaiduVatInvoiceApi(destFile, invoiceType);
                WordsResult wordsResult = vatInvoiceResult.getWords_result();
                vatInvoice = apiBizService.getResultByAddValue(wordsResult, destFile.getPath());
            }


            return ResultVoUtil.success(ApiConst.API_OPT_SUCCESS_MSG, vatInvoice);

        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVo(ApiConst.API_UPLOAD_PICTURE_FAILED_CODE, ApiConst.API_UPLOAD_PICTURE_FAILED_MSG);
        }



    }


}
