package com.dxhy.core.service.openservice.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.dxhy.common.util.FileUtil;
import com.dxhy.core.job.service.impl.MakeAppSecService;
import com.dxhy.core.model.openservice.*;
import com.dxhy.core.service.openservice.IOpenServicePlatformService;
import com.dxhy.core.service.openservice.model.ocr.*;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author user
 */

@Slf4j
@Service
@SuppressWarnings("AlibabaUndefineMagicConstant")

public class OpenServicePlatformServiceImpl implements IOpenServicePlatformService {

    @Value("${openService.token.appKey:}")
    private String openServiceAppKey;

    @Value("${openService.token.appSecret:}")
    private String openServiceAppSecret;

    @Value("${openService.check.url:}")
    private String openServiceCheckUrl;

    @Value("${openService.ocr.url:}")
    private String openServiceOcrUrl;

    @Value("${openService.token.url:}")
    private String openServiceUrl;

    @Value("${openService.username:}")
    private String ocrUserName;
    @Value("${openService.password:}")
    private String ocrPassword;
    @Value("${openService.taxno:}")
    private String ocrTaxno;
    @Value("${openService.ocrUrl:}")
    private String ocrUrl;
    @Value("${openService.ocrEnterprisePermissionFlag}")
    private boolean ocrEnterprisePermissionFlag;
    @Value("${sn.ocrUrl:}")
    private String snOcrUrl;
    @Resource
    private MakeAppSecService makeAppSecService;

    /**
     * 开放平台通用请求接口
     *
     * @param requestUrl
     *            请求连接
     * @param content
     *            参数密文
     * @param uuid
     *            请求流水号（32位随机数）
     * @param identify
     *            请求标识（什么请求，干啥的）
     * @param resultClass
     *            返回结果类
     * @return <T> 返回结果
     */
    private <T> T openServiceRequest(String requestUrl, String content, String uuid, String identify,
        Class<T> resultClass) throws OpenServiceInvokeException {
        try {
            // 生成token请求参数
            OpenServiceTokenRequest tokenRequest = new OpenServiceTokenRequest(openServiceAppKey, openServiceAppSecret);
            log.info("开始请求开放平台token，请求参数：{}", JSON.toJSONString(tokenRequest));
            // token请求结果
            String tokenResult = HttpUtil.post(openServiceUrl, JSON.toJSONString(tokenRequest), 30000);
            log.info("开放平台token请求完成，结果：{}", tokenResult);
            // 获取token值
            String accessToken = JSON.parseObject(tokenResult).getString("access_token");
            if (StringUtils.isEmpty(accessToken)) {
                log.info("调用开放平台token失败");
                throw new OpenServiceInvokeException("开放平台系统异常");
            }
            // 请求参数
            OpenServiceRequest request = new OpenServiceRequest(uuid, identify, content);
            log.info("开始请求开放平台接口，参数：" + JSON.toJSONString(request));
            // 请求结果
            String rs = HttpUtil.post(requestUrl + accessToken, JSON.toJSONString(request), 30000);
            log.info("请求开放平台接口返回：{}", rs);
            OpenServiceResponse openServiceResponse = JSON.parseObject(rs, new TypeReference<OpenServiceResponse>() {});
            String result = Optional.ofNullable(openServiceResponse).map(OpenServiceResponse::getReturnStateInfo)
                .map(OpenServiceReturnStateInfo::getReturnCode).orElse(null);
            if ("0000".equals(result)) {
                String checkResult = Base64.decodeStr(openServiceResponse.getContent());
                return JSON.parseObject(checkResult).toJavaObject(resultClass);
            } else {
                log.info("开放平台接口调用返回失败：{}", JSON.toJSONString(openServiceResponse));
                throw new OpenServiceInvokeException("调用开放平台接口失败");
            }
        } catch (Exception e) {
            log.error("开放平台接口调用代码异常", e);
            throw new OpenServiceInvokeException("开放平台接口调用失败");
        }
    }

    /**
     * 开放平台单一发票查验接口
     *
     * @param request
     *            请求参数
     * @return 返回参数
     * @throws OpenServiceInvokeException
     *             异常
     */
    @Override
    public OpenServiceSingleInvoiceCheck checkSingleInvoice(OpenServiceSingleInvoiceCheckRequest request)
        throws OpenServiceInvokeException {
        // 查验参数
        String content = Base64.encode(JSON.toJSONString(request));
        OpenServiceSingleInvoiceCheckResponse response =
            openServiceRequest(openServiceCheckUrl, content, UUID.randomUUID().toString().replace("-", ""),
                "dxhy-invoice-check", OpenServiceSingleInvoiceCheckResponse.class);
        if (response == null || response.getData() == null) {
            log.info("查验接口调用返回成功，但解析返回值失败，content：{}", JSON.toJSONString(response));
            throw new OpenServiceInvokeException("调用查验接口失败");
        }

        return response.getData();
    }

    @Override
    public List<OpenServiceOcr> getCustomsOcrResult(String uuid, String picture) throws OpenServiceInvokeException {
        OpenServiceOcrRequest ocrRequest = new OpenServiceOcrRequest(uuid, picture);
        String content = Base64.encode(JSON.toJSONString(ocrRequest));
        OpenServiceOcrResponse response =
            openServiceRequest(openServiceOcrUrl, content, uuid, "dxhy-custom-ocr", OpenServiceOcrResponse.class);
        String result = Optional.ofNullable(response).map(OpenServiceOcrResponse::getReturnStateInfo)
            .map(OpenServiceReturnStateInfo::getReturnCode).orElse(null);
        if (!"0000".equals(result)) {
            log.info("OCR识别接口调用返回成功，但解析返回值失败，content：{}", JSON.toJSONString(response));
            throw new OpenServiceInvokeException("调用OCR识别接口失败");
        }
        return response.getInvoice();
    }

    @Override
    public List<OpenServiceOcr> ocrInvoice(String picture, String userName, String password, String taxno) {
        return OcrToSn(picture);
//        String sendDate = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
//        String signStr = "username=%s&sendDate=%s&version=%s";
//        String version = "1.0";
//        if (StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(taxno)) {
//            userName = ocrUserName;
//            password = ocrPassword;
//            taxno = ocrTaxno;
//        }
//        signStr = String.format(signStr, userName, sendDate, version);
//        JSONObject dataJson = new JSONObject();
//        dataJson.put("taxno", taxno);
//        dataJson.put("picture", picture);
//        JSONObject json = new JSONObject();
//        json.put("username", userName);
//        json.put("password", password);
//        json.put("sendDate", sendDate);
//        json.put("signature", makeAppSecService.getSign(signStr, password));
//        json.put("version", version);
//        log.info("请求报文，去除picture：{} ， picture是否有值：{}", json.toJSONString(), StringUtils.isNotBlank(picture));
//        json.put("data", Base64.encode(dataJson.toString()));
//
//        String post = HttpUtil.post(ocrUrl, json.toString(), 60000);
//        log.info("ocr识别结果：{}", post);
//        JSONObject jsonObject = JSONObject.parseObject(post, JSONObject.class);
//        String returnCode = jsonObject.getString("returnCode");
//
//        if (!"0000".equals(returnCode)) {
//            return null;
//        }
//
//        String decodeStr = Base64.decodeStr(jsonObject.getString("data"));
//        return JSONObject.parseArray(decodeStr, OpenServiceOcr.class);
        }


    @Override
    public List<OpenServiceOcr> ocrInvoice(String picture) {
        return OcrToSn(picture);
    }
    /**
     * Ocr识别使用山能
     * @param picture   文件流
     * @return
     */
    public List<OpenServiceOcr> OcrToSn(String picture){
        //判断是否为图片流文件, 兼容公司平台进行base64解密
        String requestUrl = snOcrUrl;
        log.info("调用山能OCR识别请求路径："+snOcrUrl);
        String result = FileUtil.uploadFile(requestUrl, Base64Decoder.decode(picture));
        if(StringUtils.isBlank(result)){
            log.info("调用山能ocr识别失败");
        }else {
            OcrResponseVo ocrResponseVo = JSONObject.parseObject(result, OcrResponseVo.class);
            if(ocrResponseVo != null && ocrResponseVo.getCode() == 200){
                List<OcrDataListEntity> ocrDataListEntities = ocrResponseVo.getResult().getObjectList();
                 return dxOcrConvertSnOcr(ocrDataListEntities);
            }
        }
            return null;
        }

    /**
     * Ocr识别使用山能
     * @param picture   文件流
     * @return
     */
    public static List<OpenServiceOcr> OcrToSnTest(String picture){
        //判断是否为图片流文件, 兼容公司平台进行base64解密
        String requestUrl = "http://192.168.33.102:30006/cci_ai/service/v1/receipt_crop_and_recog?crop_complete_image=1";
        String result = FileUtil.uploadFile(requestUrl, Base64Decoder.decode(picture));
        if(StringUtils.isBlank(result)){
            log.info("调用山能ocr识别失败");
        }else {
            OcrResponseVo ocrResponseVo = JSONObject.parseObject(result, OcrResponseVo.class);
            if(ocrResponseVo != null && ocrResponseVo.getCode() == 200){
                List<OcrDataListEntity> ocrDataListEntities = ocrResponseVo.getResult().getObjectList();
                return dxOcrConvertSnOcr(ocrDataListEntities);
            }
        }
        return null;
    }

    //实现实体类转换
    public static List<OpenServiceOcr> dxOcrConvertSnOcr(List<OcrDataListEntity> ocrDataListEntities){
        List<OpenServiceOcr> openServices = Lists.newArrayList();
        for (OcrDataListEntity ocrDataListEntity : ocrDataListEntities) {
            List<List<OcrProductListEntity>> productList = ocrDataListEntity.getProductList();

            List<OcrItemListEntity> itemList = ocrDataListEntity.getItemList();

            String type = ocrDataListEntity.getType();
            if("other".equals(type)){
                break;
            }

            Map<String, String> itemData = itemList.stream().collect(Collectors.toMap(OcrItemListEntity::getKey, OcrItemListEntity::getValue));
            OpenServiceOcr openServiceOcr = new OpenServiceOcr();
            openServiceOcr.setIMAGE(ocrDataListEntity.getImage());
            //type 为 vat_special_invoice（增值税专用发票）、vat_electronic_invoice（增
            //值税电子普通发票）、vat_common_invoice（增值税普通发票）、
            //vat_electronic_toll_invoice（增值税电子普通发票（通行费））
            //type 为 motor_vehicle_sale_invoice（机动车销售统一发票）
            //type 为 used_car_purchase_invoice（二手车销售统一发票）
            //type 为 vat_roll_invoice（增值税普通发票（卷票））
            //type 为 vehicle_toll（过路过桥费发票、汽车通行费）
            //type 为 quota_invoice（通用定额发票）
            //type 为 taxi_ticket(出租车发票)
            //type 为 air_transport（行程单）
            //type 为 train_ticket（火车票）
            //type 为 general_machine_invoice（通用机打发票）、 highway_passenger_invoice（公路客运发票）、shipping_invoice（船运客票）、passenger_transport_invoice（旅客运输普票）
            //type 为 parking_invoice（停车费发票）

            openServiceOcr.setFPLXDM(OcrDataTypeEnum.getVal(type));
//            openServiceOcr.setXZJD(itemData.get(OcrDataTypeEnum.VAT_INVOICE_TYPE.getKey()));
            OpenServiceOcr.InvoiceOcrData invoiceOcrData = new OpenServiceOcr.InvoiceOcrData();
            invoiceOcrData.setSFCY("0"); //是否查验
            //判断发票类型
            if("vat_special_invoice".equals(type) || "vat_electronic_invoice".equals(type) || "vat_common_invoice".equals(type) || "vat_electronic_toll_invoice".equals(type)){
                invoiceOcrData.setJYM(itemData.get(OcrDataTypeEnum.VAT_INVOICE_CORRECT_CODE.getKey()));
                invoiceOcrData.setFPDM(itemData.get(OcrDataTypeEnum.VAT_INVOICE_DAIMA.getKey()));
                invoiceOcrData.setFPHM(itemData.get(OcrDataTypeEnum.VAT_INVOICE_HAOMA.getKey()));

                invoiceOcrData.setKPRQ(itemData.get(OcrDataTypeEnum.VAT_INVOICE_ISSUE_DATE.getKey()));
                invoiceOcrData.setGMFSBH(itemData.get(OcrDataTypeEnum.VAT_INVOICE_RATE_PAYER_ID.getKey()));


                invoiceOcrData.setJDHM(itemData.get(OcrDataTypeEnum.VAT_INVOICE_JIDA_HAOMA.getKey()));
                invoiceOcrData.setXHFMC(itemData.get(OcrDataTypeEnum.VAT_INVOICE_SELLER_NAME.getKey()));
                invoiceOcrData.setXHFSBH(itemData.get(OcrDataTypeEnum.VAT_INVOICE_SELLER_ID.getKey()));
                invoiceOcrData.setXHFDZDH(itemData.get(OcrDataTypeEnum.VAT_INVOICE_SELLER_ADDR_TELL.getKey()));
                invoiceOcrData.setXHFYHZH(itemData.get(OcrDataTypeEnum.VAT_INVOICE_SELLER_BANK_ACCOUNT.getKey()));
                invoiceOcrData.setGMFMC(itemData.get(OcrDataTypeEnum.VAT_INVOICE_PAYER_NAME.getKey()));
                invoiceOcrData.setGMFDZDH(itemData.get(OcrDataTypeEnum.VAT_INVOICE_PAYER_ADDR_TELL.getKey()));
                invoiceOcrData.setGMFYHZH(itemData.get(OcrDataTypeEnum.VAT_INVOICE_PAYER_BANK_ACCOUNT.getKey()));

                invoiceOcrData.setHJJE(itemData.get(OcrDataTypeEnum.VAT_INVOICE_PRICE.getKey()));


                //是否代开 vat_invoice_zhuan_yong_flag  TODO
                invoiceOcrData.setSFDK(itemData.get(OcrDataTypeEnum.VAT_INVOICE_DAI_KAI_FLAG.getKey()));
                invoiceOcrData.setHJSE(itemData.get(OcrDataTypeEnum.VAT_INVOICE_TAX_TOTAL.getKey()));

                invoiceOcrData.setJSHJ(itemData.get(OcrDataTypeEnum.VAT_INVOICE_TOTAL_COVER_TAX_DIGITS.getKey()));
                invoiceOcrData.setFPLC(itemData.get(OcrDataTypeEnum.VAT_INVOICE_PAGE_NUMBER.getKey()));
                openServiceOcr.setFPLXDM(itemData.get(OcrDataTypeEnum.VAT_INVOICE_TYPE.getKey()));
                invoiceOcrData.setBZ(itemData.get(OcrDataTypeEnum.VAT_INVOICE_TOTAL_NOTE.getKey()));
                invoiceOcrData.setFWM(itemData.get(OcrDataTypeEnum.VAT_INVOICE_CIPHER_FIELD.getKey()));
                invoiceOcrData.setSKR(itemData.get(OcrDataTypeEnum.VAT_INVOICE_PAYEE.getKey()));
                invoiceOcrData.setFHR(itemData.get(OcrDataTypeEnum.VAT_INVOICE_REVIEW.getKey()));
                invoiceOcrData.setKPR(itemData.get(OcrDataTypeEnum.VAT_INVOICE_DRAWER.getKey()));
                invoiceOcrData.setSFYGSYZ("False".equals(itemData.get(OcrDataTypeEnum.EXIST_STAMPLE.getKey()))?"0":"1");

                List<OpenServiceOcr.InvoiceOcrDetail> invoiceOcrDetailList = Lists.newArrayList();
                for (List<OcrProductListEntity> productListEntities: productList) {
                    Map<String, String> productMap = productListEntities.stream().collect(Collectors.toMap(OcrProductListEntity::getKey, OcrProductListEntity::getValue));
                    OpenServiceOcr.InvoiceOcrDetail invoiceOcrDetail = new OpenServiceOcr.InvoiceOcrDetail();
//                    invoiceOcrDetail.setMXXH(itemData.get(OcrDataTypeEnum.VAT_INVOICE_TYPE.getKey()));
                    invoiceOcrDetail.setHWMC(productMap.get(OcrDataTypeEnum.VAT_INVOICE_GOODS.getKey()));
                    invoiceOcrDetail.setGGXH(productMap.get(OcrDataTypeEnum.VAT_INVOICE_PLATE_SPECIFIC.getKey()));
                    invoiceOcrDetail.setDW(productMap.get(OcrDataTypeEnum.VAT_INVOICE_ELECTRANS_UNIT.getKey()));
                    invoiceOcrDetail.setSPSL(productMap.get(OcrDataTypeEnum.VAT_INVOICE_ELECTRANS_QUANTITY.getKey()));
                    invoiceOcrDetail.setDJ(productMap.get(OcrDataTypeEnum.VAT_INVOICE_ELECTRANS_UNIT_PRICE.getKey()));
                    invoiceOcrDetail.setJE(productMap.get(OcrDataTypeEnum.VAT_INVOICE_PRICE.getKey()));
                    invoiceOcrDetail.setSL(productMap.get(OcrDataTypeEnum.VAT_INVOICE_TAX_RATE.getKey()));
                    invoiceOcrDetail.setSE(productMap.get(OcrDataTypeEnum.VAT_INVOICE_TAX.getKey()));
                    invoiceOcrDetailList.add(invoiceOcrDetail);
                }
                invoiceOcrData.setFPMX(invoiceOcrDetailList);
            } else if ("vat_roll_invoice".equals(type)) {
                invoiceOcrData.setKPRQ(itemData.get(OcrDataTypeEnum.DATE_OF_INVOICE.getKey()));
                invoiceOcrData.setFPBT(itemData.get(OcrDataTypeEnum.INVOICE_HEAD.getKey()));
                invoiceOcrData.setJYM(itemData.get(OcrDataTypeEnum.CHECK_NUMBER.getKey()));
                invoiceOcrData.setFPDM(itemData.get(OcrDataTypeEnum.INVOICE_NUMBER.getKey()));
                invoiceOcrData.setFPHM(itemData.get(OcrDataTypeEnum.INVOICE_NUMBER.getKey()));
                invoiceOcrData.setJQBM(itemData.get(OcrDataTypeEnum.MACHINE_CODE.getKey()));
                invoiceOcrData.setGMFSBH(itemData.get(OcrDataTypeEnum.BUY_TAX_NUMBER.getKey()));
                invoiceOcrData.setJSHJ(itemData.get(OcrDataTypeEnum.TOTAL_MONEY.getKey()));
                invoiceOcrData.setXHFMC(itemData.get(OcrDataTypeEnum.SOLD_NAME.getKey()));
                invoiceOcrData.setXHFSBH(itemData.get(OcrDataTypeEnum.SOLD_TAX_NUMBER.getKey()));
                invoiceOcrData.setGMFMC(itemData.get(OcrDataTypeEnum.BUY_NAME.getKey()));
                invoiceOcrData.setSKR(itemData.get(OcrDataTypeEnum.CASHIER_NAME.getKey()));
                //是否代开 vat_invoice_zhuan_yong_flag  TODO
                invoiceOcrData.setSFYGSYZ("False".equals(itemData.get(OcrDataTypeEnum.EXIST_STAMPLE.getKey()))?"0":"1");

                List<OpenServiceOcr.InvoiceOcrDetail> invoiceOcrDetailList = Lists.newArrayList();
                for (List<OcrProductListEntity> productListEntities : productList) {
                    Map<String, String> productMap = productListEntities.stream().collect(Collectors.toMap(OcrProductListEntity::getKey, OcrProductListEntity::getValue));
                    OpenServiceOcr.InvoiceOcrDetail invoiceOcrDetail = new OpenServiceOcr.InvoiceOcrDetail();
//                    invoiceOcrDetail.setMXXH(itemData.get(OcrDataTypeEnum.VAT_INVOICE_TYPE.getKey()));
                    invoiceOcrDetail.setHWMC(productMap.get(OcrDataTypeEnum.VAT_INVOICE_GOODS.getKey()));
                    invoiceOcrDetail.setGGXH(productMap.get(OcrDataTypeEnum.VAT_INVOICE_PLATE_SPECIFIC.getKey()));
                    invoiceOcrDetail.setDW(productMap.get(OcrDataTypeEnum.VAT_INVOICE_ELECTRANS_UNIT.getKey()));
                    invoiceOcrDetail.setSPSL(productMap.get(OcrDataTypeEnum.VAT_INVOICE_ELECTRANS_QUANTITY.getKey()));
                    invoiceOcrDetail.setDJ(productMap.get(OcrDataTypeEnum.VAT_INVOICE_ELECTRANS_UNIT_PRICE.getKey()));
                    invoiceOcrDetail.setJE(productMap.get(OcrDataTypeEnum.VAT_INVOICE_PRICE.getKey()));
                    invoiceOcrDetail.setSL(productMap.get(OcrDataTypeEnum.VAT_INVOICE_TAX_RATE.getKey()));
                    invoiceOcrDetail.setSE(productMap.get(OcrDataTypeEnum.VAT_INVOICE_TAX.getKey()));
                    invoiceOcrDetailList.add(invoiceOcrDetail);
                }
                invoiceOcrData.setFPMX(invoiceOcrDetailList);
            } else if ("motor_vehicle_sale_invoice".equals(type)) {
                // -------------机动车销售统一发票特殊字段--------------
                invoiceOcrData.setGMFMC(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_BUYER.getKey()));
                invoiceOcrData.setGMFSBH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_BUYER_ID.getKey()));
                invoiceOcrData.setCPXH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_CAR_MODEL.getKey()));
                invoiceOcrData.setHGZH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_CERT_ID.getKey()));
                invoiceOcrData.setFDJHM(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_ENGINE_ID.getKey()));
                invoiceOcrData.setCLSBH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_CAR_VIN.getKey()));
                invoiceOcrData.setHJJE(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_PRICE_WITHOUT_TAX.getKey()));
                invoiceOcrData.setZGSWJG(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_TAX_AUTHORITH.getKey()));
                invoiceOcrData.setZGSWJGDM(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_TAX_AUTHORITH_ID.getKey()));


                invoiceOcrData.setJSHJ(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_TOTAL_PRICE_DIGITS.getKey()));
                invoiceOcrData.setKPRQ(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_ISSUE_DATE.getKey()));
                invoiceOcrData.setFPDM(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_DAIMA.getKey()));
                invoiceOcrData.setFPHM(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_HAOMA.getKey()));
            } else if ("used_car_purchase_invoice".equals(type)) {
                // -------------二手车销售统一发票特殊字段--------------
                invoiceOcrData.setGMFMC(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_BUYER.getKey()));
                invoiceOcrData.setGMFSBH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_BUYER_ID.getKey()));
                invoiceOcrData.setXHFMC(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_SELLER.getKey()));
                invoiceOcrData.setXHFSBH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_SELLER_ID.getKey()));
                invoiceOcrData.setESCSC(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_MARKET.getKey()));
                invoiceOcrData.setCPZH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_PLATE_NUM.getKey()));
                invoiceOcrData.setDJZH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_CAR_MODEL.getKey()));
                invoiceOcrData.setCLSBH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_VIN.getKey()));
                invoiceOcrData.setCPXH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_CAR_MODEL.getKey()));
                invoiceOcrData.setESCNSRSBH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_MARKET_TAX_ID.getKey()));
                invoiceOcrData.setESCNSRSBH(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_MARKET_TAX_ID.getKey()));
                invoiceOcrData.setJSHJ(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_TOTAL_PRICE_DIGITS.getKey()));
                invoiceOcrData.setKPRQ(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_ISSUE_DATE.getKey()));
                invoiceOcrData.setFPDM(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_DAIMA.getKey()));
                invoiceOcrData.setFPHM(itemData.get(OcrDataTypeEnum.VEHICLE_INVOICE_HAOMA.getKey()));
            } else if("vehicle_toll".equals(type)){
                // -------------过路费特殊字段--------------
                invoiceOcrData.setJSHJ(itemData.get(OcrDataTypeEnum.MONEY.getKey()));
                invoiceOcrData.setKPRQ(itemData.get(OcrDataTypeEnum.DATE.getKey()));
                invoiceOcrData.setSJ(itemData.get(OcrDataTypeEnum.TIME.getKey()));
                invoiceOcrData.setFPDM(itemData.get(OcrDataTypeEnum.TOLL_CODE.getKey()));
                invoiceOcrData.setFPHM(itemData.get(OcrDataTypeEnum.TOLL_NUMBER.getKey()));

            }else if ("quota_invoice".equals(type)) {
                invoiceOcrData.setJSHJ(itemData.get(OcrDataTypeEnum.MONEY_SMALL.getKey()));
                invoiceOcrData.setFPDM(itemData.get(OcrDataTypeEnum.QUOTA_INVOICE_CODE.getKey()));
                invoiceOcrData.setFPHM(itemData.get(OcrDataTypeEnum.QUOTA_INVOICE_NUMBER.getKey()));

            } else if ("taxi_ticket".equals(type)) {
                // -------------出租车发票特殊字段--------------
                invoiceOcrData.setJSHJ(itemData.get(OcrDataTypeEnum.SUM.getKey()));
                invoiceOcrData.setFPDM(itemData.get(OcrDataTypeEnum.INVOICE_CODE.getKey()));
                invoiceOcrData.setFPHM(itemData.get(OcrDataTypeEnum.INVOICE_NO.getKey()));
                invoiceOcrData.setKPRQ(itemData.get(OcrDataTypeEnum.DATE.getKey()));
                invoiceOcrData.setSCSJ(itemData.get(OcrDataTypeEnum.BOARDING_TIME.getKey()));
                invoiceOcrData.setXCSJ(itemData.get(OcrDataTypeEnum.LANDING_TIME.getKey()));
                invoiceOcrData.setLC(itemData.get(OcrDataTypeEnum.MILEAGE.getKey()));
                invoiceOcrData.setSZD(itemData.get(OcrDataTypeEnum.LOCATION.getKey()));
                invoiceOcrData.setCPZH(itemData.get(OcrDataTypeEnum.TAXI_NO.getKey()));
            } else if("air_transport".equals(type)){
                invoiceOcrData.setXM(itemData.get(OcrDataTypeEnum.PASSENGER_NAME.getKey()));
                invoiceOcrData.setSFZH(itemData.get(OcrDataTypeEnum.ID_NO.getKey()));
                invoiceOcrData.setPZHM(itemData.get(OcrDataTypeEnum.E_TICKET_NO.getKey()));
                invoiceOcrData.setTKRQ(itemData.get(OcrDataTypeEnum.ISSUED_DATE.getKey()));
                invoiceOcrData.setTKDW(itemData.get(OcrDataTypeEnum.ISSUED_BY.getKey()));
                invoiceOcrData.setPJ(itemData.get(OcrDataTypeEnum.FARE.getKey()));
                invoiceOcrData.setMHFZJJ(itemData.get(OcrDataTypeEnum.CIVIL_AVIATION_FUND.getKey()));
                invoiceOcrData.setRYFJF(itemData.get(OcrDataTypeEnum.FUEL_SURCHARGE.getKey()));
                invoiceOcrData.setQTSF(itemData.get(OcrDataTypeEnum.OTHER_TAXES.getKey()));
                invoiceOcrData.setZE(itemData.get(OcrDataTypeEnum.TOTAL.getKey()));
                invoiceOcrData.setBXF(itemData.get(OcrDataTypeEnum.INSURANCE.getKey()));
            } else if("train_ticket".equals(type)){
                // -------------火车票特殊字段--------------
//                invoiceOcrData.setPZHM(itemData.get(OcrDataTypeEnum.VAT_INVOICE_TYPE.getKey()));
                invoiceOcrData.setKPRQ(itemData.get(OcrDataTypeEnum.DEPARTURE_DATE.getKey()));
                invoiceOcrData.setSJ(itemData.get(OcrDataTypeEnum.DEPARTURE_DATE.getKey()));
//                invoiceOcrData.setXM(itemData.get(OcrDataTypeEnum.VAT_INVOICE_TYPE.getKey()));
                invoiceOcrData.setCFZD(itemData.get(OcrDataTypeEnum.DEPARTURE_STATION.getKey()));
                invoiceOcrData.setDDZD(itemData.get(OcrDataTypeEnum.ARRIVAL_STATION.getKey()));
                invoiceOcrData.setCC(itemData.get(OcrDataTypeEnum.TRAIN_NUMBER.getKey()));
                invoiceOcrData.setZWDJ(itemData.get(OcrDataTypeEnum.CLASS.getKey()));
                invoiceOcrData.setXLH(itemData.get(OcrDataTypeEnum.SEAT_NUMBER.getKey()));
                invoiceOcrData.setSFZH(itemData.get(OcrDataTypeEnum.PASSENGER_ID.getKey()));
            } else if ("general_machine_invoice".equals(type) || "highway_passenger_invoice".equals(type) || "shipping_invoice".equals(type) || "passenger_transport_invoice".equals(type)) {
                //为 general_machine_invoice（通用机打发票）、 highway_passenger_invoice（公路客运发票）、shipping_invoice（船运客票）、passenger_transport_invoice（旅客运输普票）

                // -------------航空运输电子客票行程单特殊字段--------------
                invoiceOcrData.setJSHJ(itemData.get(OcrDataTypeEnum.MONEY.getKey()));
                invoiceOcrData.setFPDM(itemData.get(OcrDataTypeEnum.INVOICE_CODE.getKey()));
                invoiceOcrData.setFPHM(itemData.get(OcrDataTypeEnum.INVOICE_NUMBER.getKey()));
                invoiceOcrData.setKPRQ(itemData.get(OcrDataTypeEnum.DATE.getKey()));
                invoiceOcrData.setSJ(itemData.get(OcrDataTypeEnum.TIME.getKey()));
            } else if ("parking_invoice".equals(type)) {
                invoiceOcrData.setJSHJ(itemData.get(OcrDataTypeEnum.MONEY.getKey()));
                invoiceOcrData.setFPHM(itemData.get(OcrDataTypeEnum.INVOICE_NUMBER.getKey()));
            }
            openServiceOcr.setData(invoiceOcrData);
            openServiceOcr.setRegion(ocrDataListEntity.getPosition());
            openServices.add(openServiceOcr);
        }
        return openServices;
    }
}
