package com.jianshui.invoice.service.impl.adapter.request;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.enums.ErrorCode;
import com.jianshui.common.exception.jianshui.JianshuiParamErrorException;
import com.jianshui.common.utils.IdcardUtils;
import com.jianshui.common.utils.StringUtils;
import com.jianshui.common.utils.encrypt.AisinoInvoiceDecryptUtil;
import com.jianshui.invoice.domain.BillDetail;
import com.jianshui.invoice.domain.BillInfo;
import com.jianshui.invoice.domain.Redinfo;
import com.jianshui.invoice.domain.Redinfodetail;
import com.jianshui.invoice.domain.dto.*;
import com.jianshui.invoice.domain.dto.adapter.request.aisino_jn.HxBillDetailDTO;
import com.jianshui.invoice.domain.dto.adapter.request.aisino_jn.HxBillInfoDTO;
import com.jianshui.invoice.domain.dto.adapter.request.aisino_jn.HxRedInfoDTO;
import com.jianshui.invoice.domain.dto.adapter.request.aisino_jn.HxRedInfoDetailsDTO;
import com.jianshui.invoice.service.IInvoiceRequestService;
import com.jianshui.invoice.utils.elephant.ElephantUtils;
import com.jianshui.system.service.ICompanyserviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description 航信入口报文适配器
 * @Author 巩权林
 * @Date 2022/3/19 21:28
 **/
@Slf4j
@Component("jn_aisino_invoice_request_adapter")
public class AisinoInvoiceRequestAdapterImpl implements IInvoiceRequestService {

    @Autowired
    private ICompanyserviceService companyserviceService;

    /**
     * 解密函数
     *
     * @param request
     * @param companyservice
     * @return
     */
    @Override
    public JSONObject decrypt(HttpServletRequest request, Companyservice companyservice, String serviceKey) {
        // 根据servletRequest获取原始请求报文
        // String rawInput = HttpHelper.getBodyString(request);
        // if (StringUtils.isEmpty(rawInput)) {
        //     throw new JianshuiParamErrorException(ErrorCode.EMPTY_PARAMS,"invoice);
        // }
        // JSONObject rawJson = JSONObject.parseObject(rawInput);
        // 因为航信的报文都是Form，所以这里要按form的来
        //方式一：getParameterMap()，获得请求参数map
        Map<String, String[]> map = request.getParameterMap();
        //参数名称
        Set<String> key = map.keySet();
        //参数迭代器
        Iterator<String> iterator = key.iterator();
        // JSONObject rawJson = JSONObject.parseObject(JSONObject.toJSONString(params));
        JSONObject rawJson = new JSONObject();
        while (iterator.hasNext()) {
            String k = iterator.next();
            rawJson.put(k, map.get(k)[0]);
        }


        if (StringUtils.isEmpty(rawJson)) {
            throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
        }

        String order = rawJson.getString("order");
        String identity = rawJson.getString("identity");

        log.info("【航信转invoice请求适配器】客户请求报文原始数据：{}，销方id：{}，serviceKey：{}", rawJson, companyservice.getCompanyid(), serviceKey);

        // 开始解析报文
        if (order == null || "".equals(order.trim())) {
            throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
        }

        // // 查询企业验证信息
        // Companyservice companyservice = new Companyservice();
        // if (StringUtils.isEmpty(identity)) {
        //     throw new JianshuiParamErrorException(ErrorCode.EMPTY_IDENTITY,"invoice);
        // }
        // companyservice = companyserviceService.selectCompanyserviceByCompanyid(Long.parseLong(identity));
        // if (companyservice == null) {
        //     throw new JianshuiParamErrorException(ErrorCode.COMPANY_NOT_FOUND,"invoice);
        // }

        String JKey = companyservice.getSecret(); // 解密用的secret
        if (StringUtils.isEmpty(JKey)) {
            throw new JianshuiParamErrorException(ErrorCode.IDENTITY_NOT_SET, companyservice, "invoice");
        }

        // 平台解密
        try {
            // TODO: 2023/9/20
            order = AisinoInvoiceDecryptUtil.decrypt(order, JKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new JianshuiParamErrorException(ErrorCode.DECRYPT_ERROR, companyservice, "invoice");
        }


        log.info("销项报文：identity=" + identity + ",order=" + order);

        // 报文内容体
        JSONObject json = new JSONObject();
        try {
            // 根据用户封装参数进行区别处理
            JSONObject orderJson = JSONObject.parseObject(order);
            String myIdentity = orderJson.getString("identity");
            if (myIdentity != null) {
                // order里面有identity的情况，取出来order
                JSONObject tempJson = new JSONObject();
                tempJson = orderJson.getJSONObject("order");
                if (tempJson != null) {
                    orderJson = tempJson;
                }
            }

            json = orderJson;
            // json = JSONObject.parseObject(order);

            // if (json == null || json.getString("taxNo") == null) {
            //     throw new JianshuiParamErrorException(ErrorCode.TAXCODE_NOT_FOUNC,"invoice);
            // }
            // // 校验企业资质
            // if (!json.getString("taxNo").equals(companyservice.getSellertax())) {
            //     throw new JianshuiParamErrorException(ErrorCode.IDENTITY_NOT_MATCH,"invoice);
            // }
            // 红字查询
            if (StringUtils.equals(serviceKey, "find_redinfo")) {
                return json;
            }
            // 如果是开票或者查询
            if (StringUtils.equals(serviceKey, "add") || StringUtils.equals(serviceKey, "query")) {
                // 开始把航信报文转成billInfo
                HxBillInfoDTO hxBillInfo = json.toJavaObject(HxBillInfoDTO.class);
                BillInfo billInfo = new BillInfo();
                BeanUtils.copyProperties(hxBillInfo, billInfo);
                if (StringUtils.isNotEmpty(hxBillInfo.getEmail())) {
                    billInfo.setBuyerEmail(hxBillInfo.getEmail());
                }

                // TODO: 2023/11/24 银行处理
                /*if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                    String bankNo = IdcardUtils.getBankNo(billInfo.getBuyerAccount());
                    if (StringUtils.isNotEmpty(bankNo)) {
                        String raw = billInfo.getBuyerAccount();
                        billInfo.setBuyerAccount(bankNo);
                        billInfo.setBuyerBank(raw.replace(bankNo, ""));
                    }
                }*/
                // 银行处理
                if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                    Map<String, String> bank = IdcardUtils.getBankNoAndName(billInfo.getBuyerAccount());
                    billInfo.setBuyerAccount(bank.getOrDefault("bankNo", ""));
                    billInfo.setBuyerBank(bank.getOrDefault("bankName", ""));
                }


                // 处理detail
                List<HxBillDetailDTO> hxBillDetailList = hxBillInfo.getDetail();
                List<BillDetail> detailList = new ArrayList<>();
                if (hxBillDetailList != null && hxBillDetailList.size() > 0) {
                    for (HxBillDetailDTO detial : hxBillDetailList) {
                        BillDetail temp = new BillDetail();
                        BeanUtils.copyProperties(detial, temp);
                        detailList.add(temp);
                    }
                    billInfo.setBillDetailList(detailList);
                }

                /*if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                    String bankNo = IdcardUtils.getBankNo(billInfo.getBuyerAccount());
                    if (StringUtils.isNotEmpty(bankNo)) {
                        String raw = billInfo.getBuyerAccount();
                        billInfo.setBuyerAccount(bankNo);
                        billInfo.setBuyerBank(raw.replace(bankNo, ""));
                    }
                }*/
                // 银行处理
                if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                    Map<String, String> bank = IdcardUtils.getBankNoAndName(billInfo.getBuyerAccount());
                    billInfo.setBuyerAccount(bank.getOrDefault("bankNo", ""));
                    billInfo.setBuyerBank(bank.getOrDefault("bankName", ""));
                }
                // 处理销方信息
                if (StringUtils.isEmpty(billInfo.getSellerBank()) && StringUtils.isNotEmpty(billInfo.getSellerAccount())) {
                    String bankNo = IdcardUtils.getBankNo(billInfo.getSellerAccount());
                    if (StringUtils.isNotEmpty(bankNo)) {
                        String raw = billInfo.getSellerAccount();
                        billInfo.setSellerAccount(bankNo);
                        billInfo.setSellerBank(raw.replace(bankNo, ""));
                    }
                }

                if (StringUtils.isBlank(billInfo.getSellerBank())){
                    billInfo.setSellerBank(json.getString("xsfkhh"));
                }

                if (StringUtils.isBlank(billInfo.getSellerAccount())){
                    billInfo.setSellerAccount(json.getString("xsfyhzh"));
                }

                json = (JSONObject) JSONObject.toJSON(billInfo);
            }

            if (StringUtils.equals(serviceKey, "addBatch")) {
                JSONArray billInfoList = JSONUtil.parseArray(json.get("billInfo"));
                JSONArray billInfoResult = new JSONArray();
                for (Object billInfoTemp : billInfoList) {

                    // 开始把航信报文转成billInfo
                    JSONObject orderJsonTemp = JSONObject.parseObject(String.valueOf(billInfoTemp));
                    HxBillInfoDTO hxBillInfo = orderJsonTemp.toJavaObject(HxBillInfoDTO.class);
                    BillInfo billInfo = new BillInfo();
                    BeanUtils.copyProperties(hxBillInfo, billInfo);
                    if (StringUtils.isNotEmpty(hxBillInfo.getEmail())) {
                        billInfo.setBuyerEmail(hxBillInfo.getEmail());
                    }
                    /*if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                        String bankNo = IdcardUtils.getBankNo(billInfo.getBuyerAccount());
                        if (StringUtils.isNotEmpty(bankNo)) {
                            String raw = billInfo.getBuyerAccount();
                            billInfo.setBuyerAccount(bankNo);
                            billInfo.setBuyerBank(raw.replace(bankNo, ""));
                        }
                    }*/
                    // 银行处理
                    if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                        Map<String, String> bank = IdcardUtils.getBankNoAndName(billInfo.getBuyerAccount());
                        billInfo.setBuyerAccount(bank.getOrDefault("bankNo", ""));
                        billInfo.setBuyerBank(bank.getOrDefault("bankName", ""));
                    }
                    // 处理detail
                    List<HxBillDetailDTO> hxBillDetailList = hxBillInfo.getDetail();
                    List<BillDetail> detailList = new ArrayList<>();
                    if (hxBillDetailList != null && hxBillDetailList.size() > 0) {
                        for (HxBillDetailDTO detial : hxBillDetailList) {
                            BillDetail temp = new BillDetail();
                            BeanUtils.copyProperties(detial, temp);
                            detailList.add(temp);
                        }
                        billInfo.setBillDetailList(detailList);
                    }

                    /*if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                        String bankNo = IdcardUtils.getBankNo(billInfo.getBuyerAccount());
                        if (StringUtils.isNotEmpty(bankNo)) {
                            String raw = billInfo.getBuyerAccount();
                            billInfo.setBuyerAccount(bankNo);
                            billInfo.setBuyerBank(raw.replace(bankNo, ""));
                        }
                    }*/
                    // 银行处理
                    if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                        Map<String, String> bank = IdcardUtils.getBankNoAndName(billInfo.getBuyerAccount());
                        billInfo.setBuyerAccount(bank.getOrDefault("bankNo", ""));
                        billInfo.setBuyerBank(bank.getOrDefault("bankName", ""));
                    }
                    // 处理销方信息
                    if (StringUtils.isEmpty(billInfo.getSellerBank()) && StringUtils.isNotEmpty(billInfo.getSellerAccount())) {
                        String bankNo = IdcardUtils.getBankNo(billInfo.getSellerAccount());
                        if (StringUtils.isNotEmpty(bankNo)) {
                            String raw = billInfo.getSellerAccount();
                            billInfo.setSellerAccount(bankNo);
                            billInfo.setSellerBank(raw.replace(bankNo, ""));
                        }
                    }

                    json = (JSONObject) JSONObject.toJSON(billInfo);
                    billInfoResult.add(json);
                }
                // 清除数据，重新组装
                json.clear();
                json.put("billInfo", billInfoResult);

            }

            // 如果是redinfo
            if (StringUtils.equals(serviceKey, "add_redinfo")) {
                // 开始把航信报文转成billInfo
                HxRedInfoDTO hxBillInfo = json.toJavaObject(HxRedInfoDTO.class);
                Redinfo billInfo = new Redinfo();
                BeanUtils.copyProperties(hxBillInfo, billInfo);


                // 处理detail
                List<HxRedInfoDetailsDTO> hxBillDetailList = hxBillInfo.getRedinfodetailList();
                List<Redinfodetail> detailList = new ArrayList<>();

                if (CollectionUtil.isNotEmpty(hxBillDetailList)) {
                    for (int i = 0; i < hxBillDetailList.size(); i++) {
                        HxRedInfoDetailsDTO detial = hxBillDetailList.get(i);
                        Redinfodetail temp = new Redinfodetail();
                        BeanUtils.copyProperties(detial, temp);
                        temp.setIndex(i);
                        detailList.add(temp);
                    }
                    billInfo.setRedinfodetailList(detailList);
                }
                json = (JSONObject) JSONObject.toJSON(billInfo);
            }

            // 如果是发票作废
            if (StringUtils.equals(serviceKey, "deprecate")) {
                return json;
            }

            // 如果是红字信息表下载
            if (StringUtils.equals(serviceKey, "download_redinfo")) {
                DownloadRedInfoDTO downloadRedInfoDTO = json.toJavaObject(DownloadRedInfoDTO.class);
                json = (JSONObject) JSONObject.toJSON(downloadRedInfoDTO);
                return json;
            }

            // 如果是发票打印
            if (StringUtils.equals(serviceKey, "batch_print")) {
                // JSONArray detail = json.getJSONArray("dyfpxx");
                // for (int i = 0; i < detail.size(); i++) {
                //     JSONObject temp = (JSONObject) detail.get(i);
                //     String fplxdm = ElephantUtils.transElephantType(temp.getString("fpzldm"), 1);
                //     temp.put("fpzldm", fplxdm);
                //     detail.set(i, temp);
                // }
                return json;
            }

        } catch (JianshuiParamErrorException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new JianshuiParamErrorException(ErrorCode.DECRYPT_ERROR, companyservice, "invoice");
        }

        return json;
    }

    @Override
    public JSONObject decryptZq(String identity, String order, Companyservice companyservice, String serviceKey) {


        log.info("【航信转invoice请求适配器】客户请求报文原始数据：{}，销方id：{}，serviceKey：{}，identity={}", order, companyservice.getCompanyid(), serviceKey, identity);

        // 开始解析报文
        if (order == null || "".equals(order.trim())) {
            throw new JianshuiParamErrorException(ErrorCode.ERROR_PARAMS, companyservice, "invoice");
        }

        String JKey = companyservice.getSecret(); // 解密用的secret
        if (StringUtils.isEmpty(JKey)) {
            throw new JianshuiParamErrorException(ErrorCode.IDENTITY_NOT_SET, companyservice, "invoice");
        }

        // 平台解密
        try {
            // TODO: 2023/9/20
            order = AisinoInvoiceDecryptUtil.decrypt(order, JKey);
        } catch (Exception e) {
            e.printStackTrace();
            throw new JianshuiParamErrorException(ErrorCode.DECRYPT_ERROR, companyservice, "invoice");
        }

        log.info("销项报文：identity=" + identity + ",order=" + order);

        // 报文内容体
        JSONObject json = new JSONObject();
        try {
            // 根据用户封装参数进行区别处理
            JSONObject orderJson = JSONObject.parseObject(order);
            String myIdentity = orderJson.getString("identity");
            if (myIdentity != null) {
                // order里面有identity的情况，取出来order
                JSONObject tempJson = new JSONObject();
                tempJson = orderJson.getJSONObject("order");
                if (tempJson != null) {
                    orderJson = tempJson;
                }
            }

            json = orderJson;
            // 红字查询
            if (StringUtils.equals(serviceKey, "find_redinfo")) {
                return json;
            }
            // 如果是开票或者查询
            if (StringUtils.equals(serviceKey, "add") || StringUtils.equals(serviceKey, "query")) {
                // 开始把航信报文转成billInfo
                HxBillInfoDTO hxBillInfo = json.toJavaObject(HxBillInfoDTO.class);
                BillInfo billInfo = new BillInfo();
                BeanUtils.copyProperties(hxBillInfo, billInfo);
                if (StringUtils.isNotEmpty(hxBillInfo.getEmail())) {
                    billInfo.setBuyerEmail(hxBillInfo.getEmail());
                }
                /*if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                    String bankNo = IdcardUtils.getBankNo(billInfo.getBuyerAccount());
                    if (StringUtils.isNotEmpty(bankNo)) {
                        String raw = billInfo.getBuyerAccount();
                        billInfo.setBuyerAccount(bankNo);
                        billInfo.setBuyerBank(raw.replace(bankNo, ""));
                    }
                }*/
                // 银行处理
                if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                    Map<String, String> bank = IdcardUtils.getBankNoAndName(billInfo.getBuyerAccount());
                    billInfo.setBuyerAccount(bank.getOrDefault("bankNo", ""));
                    billInfo.setBuyerBank(bank.getOrDefault("bankName", ""));
                }
                // 处理detail
                List<HxBillDetailDTO> hxBillDetailList = hxBillInfo.getDetail();
                List<BillDetail> detailList = new ArrayList<>();
                if (hxBillDetailList != null && hxBillDetailList.size() > 0) {
                    for (HxBillDetailDTO detial : hxBillDetailList) {
                        BillDetail temp = new BillDetail();
                        BeanUtils.copyProperties(detial, temp);
                        detailList.add(temp);
                    }
                    billInfo.setBillDetailList(detailList);
                }

                /*if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                    String bankNo = IdcardUtils.getBankNo(billInfo.getBuyerAccount());
                    if (StringUtils.isNotEmpty(bankNo)) {
                        String raw = billInfo.getBuyerAccount();
                        billInfo.setBuyerAccount(bankNo);
                        billInfo.setBuyerBank(raw.replace(bankNo, ""));
                    }
                }*/
                // 银行处理
                if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                    Map<String, String> bank = IdcardUtils.getBankNoAndName(billInfo.getBuyerAccount());
                    billInfo.setBuyerAccount(bank.getOrDefault("bankNo", ""));
                    billInfo.setBuyerBank(bank.getOrDefault("bankName", ""));
                }
                // 处理销方信息
                if (StringUtils.isEmpty(billInfo.getSellerBank()) && StringUtils.isNotEmpty(billInfo.getSellerAccount())) {
                    String bankNo = IdcardUtils.getBankNo(billInfo.getSellerAccount());
                    if (StringUtils.isNotEmpty(bankNo)) {
                        String raw = billInfo.getSellerAccount();
                        billInfo.setSellerAccount(bankNo);
                        billInfo.setSellerBank(raw.replace(bankNo, ""));
                    }
                }

                json = (JSONObject) JSONObject.toJSON(billInfo);
            }

            if (StringUtils.equals(serviceKey, "addBatch")) {
                JSONArray billInfoList = JSONUtil.parseArray(json.get("billInfo"));

                //开票条数限制
                /*if(billInfoList.size() > 2000){
                    throw new JianshuiParamErrorException("最大支持开票条数2000条", companyservice, "invoice");
                }*/

                JSONArray billInfoResult = new JSONArray();
                for (Object billInfoTemp : billInfoList) {

                    // 开始把航信报文转成billInfo
                    JSONObject orderJsonTemp = JSONObject.parseObject(String.valueOf(billInfoTemp));
                    HxBillInfoDTO hxBillInfo = orderJsonTemp.toJavaObject(HxBillInfoDTO.class);
                    BillInfo billInfo = new BillInfo();
                    BeanUtils.copyProperties(hxBillInfo, billInfo);
                    if (StringUtils.isNotEmpty(hxBillInfo.getEmail())) {
                        billInfo.setBuyerEmail(hxBillInfo.getEmail());
                    }
                    /*if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                        String bankNo = IdcardUtils.getBankNo(billInfo.getBuyerAccount());
                        if (StringUtils.isNotEmpty(bankNo)) {
                            String raw = billInfo.getBuyerAccount();
                            billInfo.setBuyerAccount(bankNo);
                            billInfo.setBuyerBank(raw.replace(bankNo, ""));
                        }
                    }*/
                    // 银行处理
                    if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                        Map<String, String> bank = IdcardUtils.getBankNoAndName(billInfo.getBuyerAccount());
                        billInfo.setBuyerAccount(bank.getOrDefault("bankNo", ""));
                        billInfo.setBuyerBank(bank.getOrDefault("bankName", ""));
                    }
                    // 处理detail
                    List<HxBillDetailDTO> hxBillDetailList = hxBillInfo.getDetail();
                    List<BillDetail> detailList = new ArrayList<>();
                    if (hxBillDetailList != null && hxBillDetailList.size() > 0) {
                        for (HxBillDetailDTO detial : hxBillDetailList) {
                            BillDetail temp = new BillDetail();
                            BeanUtils.copyProperties(detial, temp);
                            detailList.add(temp);
                        }
                        billInfo.setBillDetailList(detailList);
                    }

                    /*if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                        String bankNo = IdcardUtils.getBankNo(billInfo.getBuyerAccount());
                        if (StringUtils.isNotEmpty(bankNo)) {
                            String raw = billInfo.getBuyerAccount();
                            billInfo.setBuyerAccount(bankNo);
                            billInfo.setBuyerBank(raw.replace(bankNo, ""));
                        }
                    }*/
                    // 银行处理
                    if (StringUtils.isEmpty(billInfo.getBuyerBank()) && StringUtils.isNotEmpty(billInfo.getBuyerAccount())) {
                        Map<String, String> bank = IdcardUtils.getBankNoAndName(billInfo.getBuyerAccount());
                        billInfo.setBuyerAccount(bank.getOrDefault("bankNo", ""));
                        billInfo.setBuyerBank(bank.getOrDefault("bankName", ""));
                    }
                    // 处理销方信息
                    if (StringUtils.isEmpty(billInfo.getSellerBank()) && StringUtils.isNotEmpty(billInfo.getSellerAccount())) {
                        String bankNo = IdcardUtils.getBankNo(billInfo.getSellerAccount());
                        if (StringUtils.isNotEmpty(bankNo)) {
                            String raw = billInfo.getSellerAccount();
                            billInfo.setSellerAccount(bankNo);
                            billInfo.setSellerBank(raw.replace(bankNo, ""));
                        }
                    }

                    json = (JSONObject) JSONObject.toJSON(billInfo);
                    billInfoResult.add(json);
                }
                // 清除数据，重新组装
                json.clear();
                json.put("billInfo", billInfoResult);

            }

            // 如果是redinfo
            if (StringUtils.equals(serviceKey, "add_redinfo")) {
                // 开始把航信报文转成billInfo
                HxRedInfoDTO hxBillInfo = json.toJavaObject(HxRedInfoDTO.class);
                Redinfo billInfo = new Redinfo();
                BeanUtils.copyProperties(hxBillInfo, billInfo);


                // 处理detail
                List<HxRedInfoDetailsDTO> hxBillDetailList = hxBillInfo.getRedinfodetailList();
                List<Redinfodetail> detailList = new ArrayList<>();

                if (CollectionUtil.isNotEmpty(hxBillDetailList)) {
                    for (int i = 0; i < hxBillDetailList.size(); i++) {
                        HxRedInfoDetailsDTO detial = hxBillDetailList.get(i);
                        Redinfodetail temp = new Redinfodetail();
                        BeanUtils.copyProperties(detial, temp);
                        temp.setIndex(i);
                        detailList.add(temp);
                    }
                    billInfo.setRedinfodetailList(detailList);
                }
                json = (JSONObject) JSONObject.toJSON(billInfo);
            }

            // 如果是发票作废
            if (StringUtils.equals(serviceKey, "deprecate")) {
                return json;
            }

            // 如果是红字信息表下载
            if (StringUtils.equals(serviceKey, "download_redinfo")) {
                DownloadRedInfoDTO downloadRedInfoDTO = json.toJavaObject(DownloadRedInfoDTO.class);
                json = (JSONObject) JSONObject.toJSON(downloadRedInfoDTO);
                return json;
            }

            // 如果是发票打印
            if (StringUtils.equals(serviceKey, "batch_print")) {
                // JSONArray detail = json.getJSONArray("dyfpxx");
                // for (int i = 0; i < detail.size(); i++) {
                //     JSONObject temp = (JSONObject) detail.get(i);
                //     String fplxdm = ElephantUtils.transElephantType(temp.getString("fpzldm"), 1);
                //     temp.put("fpzldm", fplxdm);
                //     detail.set(i, temp);
                // }
                return json;
            }

        } catch (JianshuiParamErrorException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new JianshuiParamErrorException(ErrorCode.DECRYPT_ERROR, companyservice, "invoice");
        }

        return json;
    }


    @Override
    public String covertMethodName(String method) {
        return null;
    }
}
