package com.dxhy.portrait.business.service.impl;

import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.FplxEnum;
import com.dxhy.common.enums.FpqsjgEnum;
import com.dxhy.common.enums.FpztEnum;
import com.dxhy.common.enums.QsfsEnum;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.service.InvoiceLogService;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.FileUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.vo.InvoiceLog;
import com.dxhy.oss.model.FileModel;
import com.dxhy.oss.service.FileService;
import com.dxhy.portrait.business.dao.MixedSignatureDao;
import com.dxhy.portrait.business.dao.TDxInvoiceDao;
import com.dxhy.portrait.business.dao.TDxRecordInvoiceDao;
import com.dxhy.portrait.business.entity.*;
import com.dxhy.portrait.business.entity.ocr.OpenServiceOcr;
import com.dxhy.portrait.business.fegin.OcrInterfaceClient;
import com.dxhy.portrait.business.service.IInvoiceDetail;
import com.dxhy.portrait.business.service.ISaveInvoiceInfoService;
import com.dxhy.portrait.business.service.IScanInvoiceLogService;
import com.dxhy.portrait.business.service.MixedSignatureService;
import com.dxhy.portrait.business.util.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dxhy
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong"})
@Service
@Slf4j
public class MixedSignatureServiceImpl extends MpBaseServiceImpl<MixedSignatureDao, TDxInvoiceReimburse>
    implements MixedSignatureService {

    @Resource
    private IScanInvoiceLogService scanInvoiceLogService;

    @Resource
    private InvoiceQueryUtil invoiceQueryUtil;
    @Resource
    private TDxInvoiceDao invoiceDao;
    @Resource
    private TDxRecordInvoiceDao recordInvoiceDao;
    @Resource
    private ISaveInvoiceInfoService saveInvoiceInfoService;
    @Resource
    private MixedScanOcrService mixedScanOcrService;
    @Resource
    private FileService fileService;
    @Value("${oldOrgName}")
    private String oldOrgName;
    @Value("${ocrs.userid}")
    private String userIdModel;
    @Resource
    private IInvoiceDetail invoiceDetail;
    @Resource
    private OcrInterfaceClient ocrInterfaceClient;
    @Resource
    private InvoiceLogService invoiceLogService;
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public String selectQsfpList(Map<String, Object> pramsMap, int curr, int size) {

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        JSONObject json;

        if (pramsMap.get("isBusiness") != null) {
            String[] titlelist =
                {"序号", "扫描描述", "单据类型", "发票类型", "签收状态", "确认状态", "扫描日期", "发票代码", "发票号码", "开票日期", "购方名称", "购方税号", "销方名称",
                    "销方税号", "金额", "税额", "价税合计", "客票号码", "车次/航班号", "出发城市", "到达城市", "乘坐人", "校验码", "备注", "业务单元"};
            String[] titleprop = {"xh", "notes", "fileTypeName", "invoiceTypeName", "qsStatusName", "qzStatusName",
                "createDate", "invoiceCode", "invoiceNo", "invoiceDate", "gfName", "gfTaxNo", "xfName", "xfTaxNo",
                "invoiceAmount", "taxAmount", "totalAmount", "ticketNumber", "trainNumber", "departureCity",
                "reachingCity", "passenger", "checkCode", "remark", "businessName"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
        } else {
            String[] titlelist = {"序号", "扫描描述", "单据类型", "发票类型", "签收状态", "确认状态", "扫描日期", "发票代码", "发票号码", "开票日期", "购方名称",
                "购方税号", "销方名称", "销方税号", "金额", "税额", "价税合计", "客票号码", "车次/航班号", "出发城市", "到达城市", "乘坐人", "校验码", "备注"};
            String[] titleprop = {"xh", "notes", "fileTypeName", "invoiceTypeName", "qsStatusName", "qzStatusName",
                "createDate", "invoiceCode", "invoiceNo", "invoiceDate", "gfName", "gfTaxNo", "xfName", "xfTaxNo",
                "invoiceAmount", "taxAmount", "totalAmount", "ticketNumber", "trainNumber", "departureCity",
                "reachingCity", "passenger", "checkCode", "remark"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
        }

        PageHelper.startPage(curr, size);
        List<SmyqsWqsfpVO> pageList = this.baseMapper.selectQsfpList(pramsMap);
        PageInfo<SmyqsWqsfpVO> pageInfo = new PageInfo<>(pageList);
        List<SmyqsWqsfpVO> resultList = pageInfo.getList();

        // 设置记录总数
        json.put("total", pageInfo.getTotal());

        if (resultList != null && resultList.size() > 0) {

            int start = (int)pageInfo.getStartRow();

            for (int i = 0; i < resultList.size(); i++) {
                resultList.get(i).setXh(String.valueOf(start + i));
                String invoiceAmount = resultList.get(i).getInvoiceAmount();
                String taxAmount = resultList.get(i).getTaxAmount();
                String totalAmount = resultList.get(i).getTotalAmount();
                if (StringUtils.isNotBlank(invoiceAmount)) {
                    resultList.get(i).setInvoiceAmount(
                        new BigDecimal(invoiceAmount).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
                }
                if (StringUtils.isNotBlank(taxAmount)) {
                    resultList.get(i)
                        .setTaxAmount(new BigDecimal(taxAmount).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
                }
                if (StringUtils.isNotBlank(totalAmount)) {
                    resultList.get(i).setTotalAmount(totalAmount == null ? null
                        : new BigDecimal(totalAmount).setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
                }
                if ("0".equals(resultList.get(i).getFileType())) {
                    resultList.get(i).setFileTypeName("封面");
                } else if ("1".equals(resultList.get(i).getFileType())) {
                    resultList.get(i).setFileTypeName("票据");
                } else if ("2".equals(resultList.get(i).getFileType())) {
                    resultList.get(i).setFileTypeName("附件");
                } else {
                    resultList.get(i).setFileTypeName("其他");
                }

                // 字典转换
                pageList.get(i)
                    .setInvoiceTypeName(InvoiceTypeUtils.invoiceTypeName(resultList.get(i).getInvoiceType()));
                // if ("01".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("增值税专用发票");
                // } else if ("03".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("机动车销售统一发票");
                // } else if ("08".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("增值税电子专用发票");
                // } else if ("04".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("增值税普通发票");
                // } else if ("10".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("增值税电子普通发票");
                // } else if ("11".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("增值税普通发票(卷票)");
                // } else if ("14".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("增值税电子普通发票(通行费)");
                // } else if ("91".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("出租车票");
                // } else if ("92".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("火车票");
                // } else if ("94".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("汽车票");
                // } else if ("95".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("定额发票");
                // } else if ("96".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("长途汽车票");
                // } else if ("97".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("通用机打发票");
                // } else if ("98".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("过路过桥");
                // } else if ("90".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("航空客运电子客票行程单");
                // } else if ("89".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("公路运输客票(实名)");
                // } else if ("88".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("水路运输客票(实名)");
                // } else if ("87".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("其他运输客票(实名)");
                // } else if ("00".equals(resultList.get(i).getInvoiceType())) {
                // resultList.get(i).setInvoiceTypeName("其他");
                // } else {
                // resultList.get(i).setInvoiceTypeName("");
                // }

                String qrStatusName =
                    dictdetaServcice.queryDictName(DictConstant.QR_STATUS, resultList.get(i).getQzStatus());
                resultList.get(i).setQzStatusName(qrStatusName);

                String qsStatusName =
                    dictdetaServcice.queryDictName(DictConstant.QS_STATUS, resultList.get(i).getQsStatus());
                resultList.get(i).setQsStatusName(qsStatusName);
                if ("1".equals(resultList.get(i).getQsStatus())) {
                    resultList.get(i).setQzStatusName("已确认");
                }
            }
        }
        json.put("datalist", resultList);
        return super.responseResult(json);
    }

    @Override
    public String mixedSign(Map<String, String> pramsMapAll) {
        JSONObject json;
        String businessCode = pramsMapAll.get("businessCode");
        String company = pramsMapAll.get("company");
        if (org.apache.commons.lang3.StringUtils.isBlank(businessCode)) {
            String[] titlelist = {"序号", "扫描描述", "单据类型", "发票类型", "签收状态", "确认状态", "扫描日期", "发票代码", "发票号码", "开票日期", "金额",
                "税额", "价税合计", "客票号码", "车次/航班号", "出发城市", "到达城市", "乘坐人", "校验码"};
            String[] titleprop = {"xh", "notes", "fileTypeName", "invoiceTypeName", "qsStatusName", "qzStatusName",
                "createDate", "invoiceCode", "invoiceNo", "invoiceDate", "invoiceAmount", "taxAmount", "totalAmount",
                "ticketNumber", "trainNumber", "departureCity", "reachingCity", "passenger", "checkCode"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
        } else {
            String[] titlelist = {"序号", "扫描描述", "单据类型", "发票类型", "签收状态", "确认状态", "扫描日期", "发票代码", "发票号码", "开票日期", "金额",
                "税额", "价税合计", "客票号码", "车次/航班号", "出发城市", "到达城市", "乘坐人", "校验码", "业务单元"};
            String[] titleprop = {"xh", "notes", "fileTypeName", "invoiceTypeName", "qsStatusName", "qzStatusName",
                "createDate", "invoiceCode", "invoiceNo", "invoiceDate", "invoiceAmount", "taxAmount", "totalAmount",
                "ticketNumber", "trainNumber", "departureCity", "reachingCity", "passenger", "checkCode",
                "businessName"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
        }
        String gfTaxNo = pramsMapAll.get("gfTaxNo");

        TDxInvoiceReimburse tDxInvoice = null;

        DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_READ);
        // 从扫描记录表获取扫描信息
        String pch = pramsMapAll.get("pch");
        String[] split = pch.split("-");
        String loginName = split[0];
        QueryWrapper<TDxScanInvoiceLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pch", pch);
        TDxScanInvoiceLog scanInvoiceLog = scanInvoiceLogService.getOne(queryWrapper);

        String scanInvoiceText = "";
        if (scanInvoiceLog != null) {
            scanInvoiceText = scanInvoiceLog.getInvoiceText();
        }
        JSONObject ocrData = JSONObject.parseObject(scanInvoiceText);
        JSONArray datas = ocrData.getJSONArray("invoiceList");

        // 校验扫描信息
        List<SmyqsWqsfpVO> returnList = new ArrayList<>();
        int xh = 0;
        // 扫描仪每次上传多张发票，因此需要根据上传pch循环处理本批上传的发票
        for (Object o : datas) {
            JSONObject data = (JSONObject)o;
            if ("0".equals(data.getString("fileType"))) {
                pramsMapAll.put("ewm", data.getString("ewm"));
                DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_READ);
                tDxInvoice = this.baseMapper.getScaninfoByEwm(data.getString("ewm"));
                if (tDxInvoice == null) {
                    tDxInvoice = new TDxInvoiceReimburse();
                    tDxInvoice.setEwm(data.getString("ewm"));
                    tDxInvoice.setQsDate(new Date());
                    tDxInvoice.setQzStatus("0");
                    tDxInvoice.setQsStatus("0");
                    tDxInvoice.setQsName(pramsMapAll.get("userName"));
                    tDxInvoice.setDeptId(pramsMapAll.get("businessCode"));
                    tDxInvoice.setDeptName(pramsMapAll.get("businessName"));
                    tDxInvoice.setGfTaxNo(gfTaxNo);
                    tDxInvoice.setGfName(pramsMapAll.get("gfName"));
                    tDxInvoice.setTaxNo(pramsMapAll.get("taxNo"));
                    tDxInvoice.setScanId(data.getString("scanid"));
                    tDxInvoice.setUuid(data.getString("ewm"));
                    tDxInvoice.setFileType(data.getString("fileType"));
                    tDxInvoice.setCreateDate(new Date());
                    tDxInvoice.setNotes("封面扫描成功");
                    tDxInvoice.setCompany(company);
                    DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_WRITE);
                    this.baseMapper.insert(tDxInvoice);
                } else {
                    tDxInvoice.setQsDate(new Date());
                    tDxInvoice.setEwm(data.getString("ewm"));
                    tDxInvoice.setScanId(data.getString("scanid"));
                    tDxInvoice.setUuid(data.getString("ewm"));
                    tDxInvoice.setGfTaxNo(gfTaxNo);
                    tDxInvoice.setGfName(pramsMapAll.get("gfName"));
                    tDxInvoice.setNotes("封面扫描成功");
                    tDxInvoice.setCompany(company);
                    DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_WRITE);
                    this.baseMapper.updateById(tDxInvoice);
                }
                xh++;
                SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, null, xh);
                returnList.add(qsjg);
            } else {
                pramsMapAll.put("fileType", data.getString("fileType"));
                String scanid = data.getString("scanid").trim();
                pramsMapAll.put("scanId", scanid);
                TDxInvoiceImg img = this.baseMapper.getImgByScanId(scanid);
                if (img == null) {
                    throw new RuntimeException();
                }
                String imagPath = img.getImagePath();

                String localPath = fileService.getLocalPathDefault() + DateUtils.dateToStr(new Date()) + "/"
                    + new File(imagPath).getName();
                // 下载文件
                String base64img;
                try {
                    FileModel fileModel = fileService.downFile(imagPath, localPath);
                    base64img = FileUtil.fileToBase64(new File(fileModel.getFilePath()));
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException();
                }
                Map<String, String> ocrMap = new HashMap<>();
                ocrMap.put("picture", base64img);
                ocrMap.put("loginName", loginName);
                List<OpenServiceOcr> ocrList = ocrInterfaceClient.getInvoiceOcrResult(ocrMap);
                if (ocrList == null) {
                    // 识别识别的单独处理
                    tDxInvoice = new TDxInvoiceReimburse();
                    tDxInvoice.setEwm(pramsMapAll.get("ewm"));
                    tDxInvoice.setQzStatus("0");
                    tDxInvoice.setQsStatus("0");
                    tDxInvoice.setQsName(pramsMapAll.get("userName"));
                    tDxInvoice.setTaxNo(pramsMapAll.get("gfTaxNo"));
                    tDxInvoice.setGfTaxNo(pramsMapAll.get("gfTaxNo"));
                    tDxInvoice.setGfName(pramsMapAll.get("gfName"));
                    tDxInvoice.setDeptId(pramsMapAll.get("businessCode"));
                    tDxInvoice.setDeptName(pramsMapAll.get("businessName"));
                    tDxInvoice.setScanId(scanid);
                    tDxInvoice.setEwm(pramsMapAll.get("ewm"));
                    tDxInvoice.setFileType("3");
                    tDxInvoice.setUuid(scanid);
                    tDxInvoice.setCreateDate(new Date());
                    tDxInvoice.setQsDate(new Date());
                    tDxInvoice.setOcrStatus("2");
                    // 不存在扫描记录
                    tDxInvoice.setNotes("处理失败，请重新识别");
                    tDxInvoice.setCompany(company);
                    DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_WRITE);
                    this.baseMapper.insert(tDxInvoice);
                    xh++;
                    SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, null, xh);
                    returnList.add(qsjg);
                    continue;
                }
                // 校验扫描信息
                List<Map<String, String>> pramsMapList = this.validateScanInfo(ocrList, pramsMapAll);
                for (Map<String, String> map : pramsMapList) {
                    xh++;
                    Boolean isExits = false;
                    String userName = map.get("userName");
                    String invoiceType = map.get("invoiceType");
                    // 增加类型为85的
                    // if (invoiceType == null || "".equals(invoiceType) || "00".equals(invoiceType)) {
                    if (invoiceType == null || "".equals(invoiceType)) {
                        tDxInvoice = new TDxInvoiceReimburse();
                        tDxInvoice.setEwm(pramsMapAll.get("ewm"));
                        tDxInvoice.setQsDate(new Date());
                        tDxInvoice.setQzStatus("0");
                        tDxInvoice.setQsStatus("0");
                        tDxInvoice.setQsName(pramsMapAll.get("userName"));
                        tDxInvoice.setTaxNo(pramsMapAll.get("gfTaxNo"));
                        tDxInvoice.setGfTaxNo(pramsMapAll.get("gfTaxNo"));
                        tDxInvoice.setGfName(pramsMapAll.get("gfName"));
                        tDxInvoice.setDeptId(pramsMapAll.get("businessCode"));
                        tDxInvoice.setDeptName(pramsMapAll.get("businessName"));
                        tDxInvoice.setScanId(scanid);
                        tDxInvoice.setEwm(pramsMapAll.get("ewm"));
                        tDxInvoice.setFileType("2");
                        tDxInvoice.setUuid(scanid);
                        tDxInvoice.setCreateDate(new Date());
                        // 不存在扫描记录
                        tDxInvoice.setNotes("附件扫描成功");
                        tDxInvoice.setCompany(company);
                        DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_WRITE);
                        this.baseMapper.insert(tDxInvoice);
                        SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, null, xh);
                        returnList.add(qsjg);
                        continue;
                    }
                    String invoiceCode = map.get("invoiceCode");
                    String invoiceNo = map.get("invoiceNo");
                    String invoiceDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
                    if (StringUtils.isNotBlank(map.get("invoiceDate"))) {
                        invoiceDate = map.get("invoiceDate");
                    } else {
                        // 当前系统时间
                        map.put("invoiceDate", invoiceDate);
                    }
                    String uuid = map.get("uuid");
                    map.put("uuid", uuid);
                    tDxInvoice = this.initQsjl(map);
                    tDxInvoice.setTaxNo(pramsMapAll.get("gfTaxNo"));
                    try {
                        tDxInvoice.setInvoiceDate(new SimpleDateFormat("yyyyMMdd").parse(invoiceDate));
                    } catch (ParseException e) {
                        tDxInvoice.setInvoiceDate(new Date());
                    }
                    tDxInvoice.setEwm(pramsMapAll.get("ewm"));
                    tDxInvoice.setInvoiceType(invoiceType);
                    tDxInvoice.setFileType("1");
                    tDxInvoice.setXfName(map.get("xfName"));
                    tDxInvoice.setGfName(map.get("gfName"));
                    tDxInvoice.setGfTaxNo(map.get("gfTaxNo"));
                    tDxInvoice.setXfTaxNo(map.get("xfTaxNo"));
                    tDxInvoice.setCompany(company);
                    if (map.get("taxAmount") != null) {
                        try {
                            tDxInvoice.setTaxAmount(new BigDecimal(map.get("taxAmount")));
                        } catch (Exception e) {
                            tDxInvoice.setTaxAmount(null);
                        }
                    }
                    if (map.get("invoiceAmount") != null) {
                        try {
                            tDxInvoice.setInvoiceAmount(new BigDecimal(map.get("invoiceAmount")));
                        } catch (Exception e) {
                            tDxInvoice.setInvoiceAmount(null);
                        }
                    }
                    if (map.get("totalAmount") != null) {
                        try {
                            tDxInvoice.setTotalAmount(new BigDecimal(map.get("totalAmount")));
                        } catch (Exception e) {
                            tDxInvoice.setTotalAmount(null);
                        }
                    }

                    map.put("dbName", pramsMapAll.get("dbName"));
                    map.put("gfmcAll", pramsMapAll.get("gfmcAll"));
                    // 查看扫描表是否存在
                    TDxInvoiceReimburse invoiceScanInfo = this.getScaninfo(uuid, pramsMapAll);
                    if (invoiceScanInfo != null) {
                        tDxInvoice = invoiceScanInfo;
                        tDxInvoice.setNotes("重复扫描");
                        tDxInvoice.setDoubleStatus("1");
                        SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, invoiceDate, xh);
                        returnList.add(qsjg);
                        continue;
                    }
                    DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_READ);
                    TDxInvoice scanInvoice = this.baseMapper.getInvoiceByUuid(uuid);
                    boolean scanIsExit = false;
                    if (scanInvoice != null) {
                        scanIsExit = true;

                        tDxInvoice = new TDxInvoiceReimburse();
                        tDxInvoice.setFileType("1");
                        tDxInvoice.setInvoiceType(scanInvoice.getInvoiceType());

                        tDxInvoice.setCreateDate(scanInvoice.getCreateDate());
                        tDxInvoice.setInvoiceCode(scanInvoice.getInvoiceCode());
                        tDxInvoice.setInvoiceNo(scanInvoice.getInvoiceNo());

                        tDxInvoice.setInvoiceDate(scanInvoice.getInvoiceDate());
                        tDxInvoice.setInvoiceAmount(scanInvoice.getInvoiceAmount());
                        tDxInvoice.setTaxAmount(scanInvoice.getTaxAmount());
                        tDxInvoice.setTotalAmount(scanInvoice.getTotalAmount());
                        tDxInvoice.setQsStatus(scanInvoice.getQsStatus());
                        tDxInvoice.setQsDate(scanInvoice.getQsDate());
                        tDxInvoice.setDeptId(pramsMapAll.get("businessCode"));
                        tDxInvoice.setDeptName(pramsMapAll.get("businessName"));
                        tDxInvoice.setNotes("重复扫描");
                        tDxInvoice.setDoubleStatus("1");
                        SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, invoiceDate, xh);
                        returnList.add(qsjg);
                        continue;

                    }
                    tDxInvoice.setCreateDate(new Date());
                    if (!gfTaxNo.equals(tDxInvoice.getGfTaxNo())) {
                        tDxInvoice.setNotes("发票购方税号与所选税号不匹配");
                        tDxInvoice.setQzStatus("0");
                        tDxInvoice.setQsStatus("0");
                        tDxInvoice.setDoubleStatus("2");
                        SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, invoiceDate, xh);
                        returnList.add(qsjg);
                        continue;
                    }
                    // 如果是专票、机动车票、通行费票：普票、电票、卷票：
                    if (FplxEnum.ZP.getFplxDm().equals(invoiceType) || FplxEnum.TXF.getFplxDm().equals(invoiceType)
                        || FplxEnum.JDC.getFplxDm().equals(invoiceType) || FplxEnum.PP.getFplxDm().equals(invoiceType)
                        || FplxEnum.DZP.getFplxDm().equals(invoiceType) || FplxEnum.JSP.getFplxDm().equals(invoiceType)
                        || FplxEnum.DZZP.getFplxDm().equals(invoiceType)
                        || FplxEnum.ESC.getFplxDm().equals(invoiceType)) {

                        String notes = this.validateInvoiceInfo(invoiceCode, invoiceNo, invoiceDate);
                        if (notes != null) {
                            // 信息校验失败
                            // 未确认
                            tDxInvoice.setNotes(notes);
                            tDxInvoice.setQsStatus("0");
                            qsHandsMoth(returnList, tDxInvoice, isExits, map, xh, invoiceDate);
                        } else {
                            if (FplxEnum.ZP.getFplxDm().equals(invoiceType)
                                || FplxEnum.TXF.getFplxDm().equals(invoiceType)
                                || FplxEnum.JDC.getFplxDm().equals(invoiceType)
                                || FplxEnum.DZZP.getFplxDm().equals(invoiceType)) {
                                // 专票、机动车票、通行费票
                                //
                                if (pramsMapAll.containsKey("gfshAll")
                                    && StringUtils.isNotBlank(pramsMapAll.get("gfshAll"))
                                    && !pramsMapAll.get("gfshAll").contains(tDxInvoice.getGfTaxNo())) {
                                    // 查验成功但是信息不一致，直接在扫描表添加一条记录
                                    tDxInvoice.setQsStatus(FpqsjgEnum.WQS.getQsjgDm());
                                    tDxInvoice.setQzStatus("0");
                                    tDxInvoice.setNotes("无税号权限");
                                    qsHandsMoth(returnList, tDxInvoice, isExits, map, xh, invoiceDate);
                                } else {

                                    TDxRecordInvoice invoice = this.baseMapper.getRecordByUuid(uuid);
                                    if (invoice == null) {
                                        tDxInvoice.setQsStatus(FpqsjgEnum.WQS.getQsjgDm());
                                        tDxInvoice.setQzStatus("0");
                                        tDxInvoice.setNotes("无底账信息");
                                        qsHandsMoth(returnList, tDxInvoice, isExits, map, xh, invoiceDate);

                                    } else {
                                        notes = this.compareInvoiceInfo(invoice, map);
                                        if (notes != null) {
                                            // 信息比对不一致
                                            // 未确认
                                            tDxInvoice.setNotes(notes);
                                            tDxInvoice.setQsStatus("0");
                                            qsHandsMoth(returnList, tDxInvoice, isExits, map, xh, invoiceDate);

                                        } else if (!"0".equals(invoice.getInvoiceStatus())) {
                                            tDxInvoice.setNotes("发票状态异常");
                                            tDxInvoice.setQsStatus("0");
                                            tDxInvoice.setQzStatus("0");
                                            qsHandsMoth(returnList, tDxInvoice, isExits, map, xh, invoiceDate);
                                        } else {
                                            // 直接签收
                                            // 确认
                                            tDxInvoice.setQsStatus("1");
                                            tDxInvoice.setNotes("验真成功");

                                            // 通行费没有校验码、明细的时候补明细
                                            if (FplxEnum.TXF.getFplxDm().equals(invoiceType)
                                                && StringUtils.isBlank(invoice.getCheckCode())) {
                                                List<TDxRecordInvoiceDetail> recordInvoiceDetail = new ArrayList<>();

                                                String checkDate = DateUtils.dateToStr(invoice.getInvoiceDate());
                                                String invoiceAmount = map.getOrDefault("invoiceAmount", "");
                                                String checkCode = map.getOrDefault("checkCode", "");
                                                // 调用查验接口进行发票签收
                                                JSONObject queryInvoiceResult = invoiceQueryUtil.queryInvoice(
                                                    invoiceCode, invoiceNo, checkDate, invoiceAmount, checkCode);
                                                if (!queryInvoiceResult.containsKey("returnCode")) {

                                                    String resultCode = queryInvoiceResult.getString("resultCode");
                                                    if ("0001".equals(resultCode)) {
                                                        updateRecordInvoice(queryInvoiceResult, invoice);
                                                        recordInvoiceDetail = saveInvoiceInfoService
                                                            .getRecordInvoiceDetail(queryInvoiceResult);
                                                    }
                                                }

                                                if (recordInvoiceDetail != null && recordInvoiceDetail.size() > 0) {
                                                    boolean deleteDetail = invoiceDetail.deleteDetail(invoice.getUuid(),
                                                        map.get("dbName"));
                                                    if (deleteDetail) {
                                                        invoiceDetail.saveInvoiceDetail(recordInvoiceDetail,
                                                            map.get("dbName"));
                                                    }
                                                }
                                            }
                                            qsHandsMothConfirm(returnList, tDxInvoice, isExits, map, xh, invoiceDate,
                                                scanIsExit, invoice);
                                            SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, invoiceDate, xh);
                                            returnList.add(qsjg);
                                        }
                                    }
                                }
                            } else {

                                if ("1".equals(pramsMapAll.get("productType"))) {
                                    TDxRecordInvoice invoice = this.baseMapper.getRecordByUuid(uuid);

                                    if (invoice != null) {
                                        tDxInvoice.setSourceSystem(invoice.getSourceSystem());
                                        // 直接签收
                                        // 确认
                                        tDxInvoice.setQsStatus("1");
                                        tDxInvoice.setNotes("验真成功");
                                        qsHandsMothConfirm(returnList, tDxInvoice, isExits, map, xh, invoiceDate,
                                            scanIsExit, invoice);
                                        SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, invoiceDate, xh);
                                        returnList.add(qsjg);
                                        continue;
                                    }
                                }
                                // 当天的票不查验
                                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                                // 设置日期格式
                                String nowtime = df.format(new Date());
                                if (nowtime.equals(invoiceDate)) {
                                    tDxInvoice.setNotes("当天发票无法查验，请隔天修改后查验");
                                    tDxInvoice.setQsStatus("0");
                                    qsHandsMoth(returnList, tDxInvoice, isExits, map, xh, invoiceDate);
                                    continue;
                                }
                                // 调用查验接口进行发票签收
                                tDxInvoice = this.qsSinglefpByInvoiceQuery(tDxInvoice, invoiceCode, invoiceNo, map,
                                    pramsMapAll, isExits, QsfsEnum.SMYQS.getQsfsDm(), scanIsExit);
                                tDxInvoice.setEwm(pramsMapAll.get("ewm"));
                                tDxInvoice.setFileType("1");
                                SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, null, xh);
                                returnList.add(qsjg);
                            }
                        }

                    } else {
                        // 飞机票、火车票、定额发票、出租车票、其他：
                        // 未确认
                        tDxInvoice.setNotes("扫描成功");
                        qsHandsMoth(returnList, tDxInvoice, isExits, map, xh, invoiceDate);

                    }
                }
            }
        }
        json.put("datalist", returnList);
        return super.responseResult(json);
    }

    /**
     * 根据查验结果进行发票签收
     *
     * @param tDxInvoice
     * @param invoiceCode
     * @param invoiceNo
     * @param pramsMap
     * @return @
     *
     */
    private TDxInvoiceReimburse qsSinglefpByInvoiceQuery(TDxInvoiceReimburse tDxInvoice, String invoiceCode,
        String invoiceNo, Map<String, String> pramsMap, Map<String, String> pramsMapAll, Boolean isExits, String qsType,
        Boolean scanIsExit) {

        String invoiceDate = pramsMap.get("invoiceDate");
        if (invoiceDate.length() == 8) {
            invoiceDate =
                invoiceDate.substring(0, 4) + "-" + invoiceDate.substring(4, 6) + "-" + invoiceDate.substring(6);
        }
        String invoiceAmount = pramsMap.getOrDefault("invoiceAmount", "");
        String checkCode = pramsMap.getOrDefault("checkCode", "");

        tDxInvoice.setQsDate(DateUtils.getSqlDateLong());
        tDxInvoice.setQsType(qsType);
        if (StringUtils.isNotBlank(invoiceAmount)) {
            tDxInvoice.setInvoiceAmount(new BigDecimal(invoiceAmount));
        }
        tDxInvoice.setCheckCode(checkCode);
        try {
            tDxInvoice.setInvoiceDate(new SimpleDateFormat("yyyy-MM-dd").parse(invoiceDate));
        } catch (ParseException e) {
            tDxInvoice.setInvoiceDate(new Date());
        }
        tDxInvoice.setQsDate(DateUtils.getSqlDateLong());
        tDxInvoice.setUuid(invoiceCode + invoiceNo);
        tDxInvoice.setSelectId(tDxInvoice.getUuid());
        // 调用查验接口查询发票信息
        JSONObject queryInvoiceResult =
            invoiceQueryUtil.queryInvoice(invoiceCode, invoiceNo, invoiceDate, invoiceAmount, checkCode);
        if (queryInvoiceResult.containsKey("returnCode")) {
            // 调用查验接口失败，进入查验失败处理流程
            tDxInvoice.setNotes("查验失败");
            tDxInvoice.setQzStatus("0");
            tDxInvoice.setQsStatus(FpqsjgEnum.WQS.getQsjgDm());
        } else {
            String resultCode = queryInvoiceResult.getString("resultCode");
            // 查验成功且信息一致，直接保存到底账表并签收
            if ("0001".equals(resultCode)) {
                // 查验返回的购方税号
                String checkGfsh = queryInvoiceResult.getString("buyerTaxNo");
                checkGfsh = checkGfsh == null ? "" : checkGfsh;
                // 查验返回的购方名称
                String checkGfmc = queryInvoiceResult.getString("buyerName");
                checkGfmc = checkGfmc == null ? "" : checkGfmc;
                // 查验返回的作废标志
                String zfbz = queryInvoiceResult.getString("isCancelled");
                // 查验返回的税额
                String taxAmount = queryInvoiceResult.getString("taxAmount");
                if (StringUtils.isNotBlank(taxAmount)) {
                    if (taxAmount.contains("*")) {
                        taxAmount = "0";
                    }
                    tDxInvoice.setTaxAmount(new BigDecimal(taxAmount));
                }
                if (pramsMapAll.containsKey("gfshAll") && StringUtils.isNotBlank(pramsMapAll.get("gfshAll"))
                    && !pramsMapAll.get("gfshAll").contains(checkGfsh)) {
                    // 查验成功但是信息不一致，直接在扫描表添加一条记录
                    tDxInvoice.setQzStatus("0");
                    tDxInvoice.setQsStatus(FpqsjgEnum.WQS.getQsjgDm());
                    tDxInvoice.setNotes("无税号权限");
                } else if (pramsMapAll.containsKey("gfmcAll") && StringUtils.isNotBlank(pramsMapAll.get("gfmcAll"))
                    && !CompareNameUtils.changeBrackets(pramsMapAll.get("gfmcAll"))
                        .contains(CompareNameUtils.changeBrackets(checkGfmc.replaceAll(" ", "")))
                    && !CompareNameUtils.changeBrackets(oldOrgName)
                        .contains(CompareNameUtils.changeBrackets(checkGfmc.replaceAll(" ", "")))) {
                    // 查验成功但是信息不一致，直接在扫描表添加一条记录
                    tDxInvoice.setQzStatus("0");
                    tDxInvoice.setQsStatus(FpqsjgEnum.WQS.getQsjgDm());
                    tDxInvoice.setNotes("登录人的公司名称与发票购方名称不一致,查验返回的购方名称为：" + checkGfmc);
                } else if (!("N".equals(zfbz) || "0".equals(zfbz))) {
                    // 作废发票无法签收，发票查验接口只返回了作废状态，未返回其它异常状态
                    tDxInvoice.setQsStatus(FpqsjgEnum.WQS.getQsjgDm());
                    tDxInvoice.setQzStatus("0");
                    tDxInvoice.setNotes("发票状态异常，签收失败！");

                } else {
                    tDxInvoice.setGfTaxNo(queryInvoiceResult.getString("buyerTaxNo"));
                    tDxInvoice.setGfName(queryInvoiceResult.getString("buyerName"));
                    tDxInvoice.setGfAddressAndPhone(queryInvoiceResult.getString("buyerAddressPhone"));
                    tDxInvoice.setGfBankAndNo(queryInvoiceResult.getString("buyerAccount"));
                    tDxInvoice.setXfTaxNo(queryInvoiceResult.getString("salerTaxNo"));
                    tDxInvoice.setXfName(queryInvoiceResult.getString("salerName"));
                    tDxInvoice.setXfAddressAndPhone(queryInvoiceResult.getString("salerAddressPhone"));
                    tDxInvoice.setXfBankAndNo(queryInvoiceResult.getString("salerAccount"));
                    tDxInvoice.setRemark(queryInvoiceResult.getString("remark"));
                    if ("Y".equals(queryInvoiceResult.getString("isCancelled"))) {
                        tDxInvoice.setInvoiceStatus(FpztEnum.ZF.getFpztDm(FpztEnum.ZF.getIndex()));
                    } else if ("N".equals(queryInvoiceResult.getString("isCancelled"))) {
                        tDxInvoice.setInvoiceStatus(FpztEnum.ZC.getFpztDm(FpztEnum.ZC.getIndex()));
                    } else {
                        tDxInvoice.setInvoiceStatus(queryInvoiceResult.getString("isCancelled"));
                    }
                    if (!"".equals(queryInvoiceResult.getString("invoiceAmount"))
                        && queryInvoiceResult.getString("invoiceAmount") != null
                        && !(" ").equals(queryInvoiceResult.getString("invoiceAmount"))) {
                        tDxInvoice.setInvoiceAmount(new BigDecimal(queryInvoiceResult.getString("invoiceAmount")));
                    }
                    if (!"".equals(queryInvoiceResult.getString("totalAmount"))
                        && queryInvoiceResult.getString("totalAmount") != null
                        && !(" ").equals(queryInvoiceResult.getString("totalAmount"))) {
                        tDxInvoice.setTotalAmount(new BigDecimal(queryInvoiceResult.getString("totalAmount")));
                    }
                    tDxInvoice.setInvoiceType(queryInvoiceResult.getString("invoiceType"));
                    System.out.printf("invoiceType======" + queryInvoiceResult.getString("invoiceType"));
                    tDxInvoice.setQsStatus(FpqsjgEnum.YQS.getQsjgDm());
                    tDxInvoice.setQzStatus("1");
                    tDxInvoice.setNotes("验真成功");
                }
                tDxInvoice.setGfName(checkGfmc);
                tDxInvoice.setGfTaxNo(checkGfsh);
                tDxInvoice.setXfName(queryInvoiceResult.getString("salerName"));
                tDxInvoice.setXfTaxNo(queryInvoiceResult.getString("salerTaxNo"));
                tDxInvoice.setTotalAmount(queryInvoiceResult.getBigDecimal("totalAmount"));
                if ("10".equals(tDxInvoice.getInvoiceType())) {
                    tDxInvoice.setIsDeduction("1");
                    JSONArray dataArray = queryInvoiceResult.getJSONArray("detailList");
                    if (dataArray != null && dataArray.size() > 0) {
                        String goodName = dataArray.getJSONObject(0).getString("goodName");
                        if (goodName != null && goodName.contains("运输服务") && new SimpleDateFormat("yyyy-MM-dd")
                            .format(tDxInvoice.getInvoiceDate()).compareTo("2019-04-01") > -1) {
                            String taxRate = dataArray.getJSONObject(0).getString("taxRate");
                            tDxInvoice.setIsDeduction("0");
                            tDxInvoice.setTag(goodName);
                            tDxInvoice.setTaxRate(taxRate);
                        }
                    }
                }

            } else {
                // 查验成功但是信息不一致，直接在扫描表添加一条记录
                tDxInvoice.setNotes(queryInvoiceResult.getString("resultTip"));
                tDxInvoice.setQsStatus(FpqsjgEnum.WQS.getQsjgDm());
                tDxInvoice.setQzStatus("0");
            }
        }

        // 扫描表是否存在 true 存在
        if (!isExits) {

            TDxInvoiceReimburse invoiceScanInfo = this.getScaninfo(tDxInvoice.getUuid(), pramsMapAll);
            DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_WRITE);
            if (null != invoiceScanInfo && StringUtils.isNotBlank(invoiceScanInfo.getUuid())) {
                // 已存在扫描记录

                tDxInvoice.setQsDate(new Date());
                QueryWrapper<TDxInvoiceReimburse> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("uuid", tDxInvoice.getUuid());
                this.baseMapper.update(tDxInvoice, queryWrapper);
            } else {
                // 不存在扫描记录
                this.baseMapper.insert(tDxInvoice);
            }
        } else {
            // 已存在扫描记录
            DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_WRITE);
            tDxInvoice.setQsDate(new Date());
            QueryWrapper<TDxInvoiceReimburse> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uuid", tDxInvoice.getUuid());
            this.baseMapper.update(tDxInvoice, queryWrapper);
        }
        if ("1".equals(tDxInvoice.getQsStatus())) {
            TDxInvoice scanInvoice = new TDxInvoice();
            scanInvoice.setInvoiceType(tDxInvoice.getInvoiceType());
            scanInvoice.setInvoiceCode(tDxInvoice.getInvoiceCode());
            scanInvoice.setInvoiceNo(tDxInvoice.getInvoiceNo());
            scanInvoice.setGfTaxNo(tDxInvoice.getGfTaxNo());
            scanInvoice.setGfName(tDxInvoice.getGfName());
            scanInvoice.setXfTaxNo(tDxInvoice.getXfTaxNo());
            scanInvoice.setXfName(tDxInvoice.getXfName());
            scanInvoice.setInvoiceAmount(tDxInvoice.getInvoiceAmount());
            scanInvoice.setTaxAmount(tDxInvoice.getTaxAmount());
            scanInvoice.setTotalAmount(tDxInvoice.getTotalAmount());
            try {
                scanInvoice.setInvoiceDate(tDxInvoice.getInvoiceDate() != null ? tDxInvoice.getInvoiceDate() : null);
            } catch (Exception e) {
                e.printStackTrace();
            }
            scanInvoice.setUserAccount(tDxInvoice.getQsName());
            scanInvoice.setUserName(tDxInvoice.getQsName());
            scanInvoice.setQsType(tDxInvoice.getQsType());
            scanInvoice.setQsStatus(tDxInvoice.getQsStatus());
            scanInvoice.setValid("1");
            scanInvoice.setUuid(tDxInvoice.getUuid());
            scanInvoice.setCreateDate(new Date());
            scanInvoice.setQsDate(new Date());
            scanInvoice.setScanId(tDxInvoice.getScanId());
            scanInvoice.setNotes(tDxInvoice.getNotes());
            scanInvoice.setDeptId(tDxInvoice.getDeptId());
            scanInvoice.setDeptName(tDxInvoice.getDeptName());
            scanInvoice.setCheckCode(tDxInvoice.getCheckCode());
            scanInvoice.setInvoiceStatus(tDxInvoice.getInvoiceStatus());
            scanInvoice.setQsName(tDxInvoice.getQsName());
            scanInvoice.setCompany(tDxInvoice.getCompany());
            scanInvoice.setInvoiceRemark(queryInvoiceResult.getString("remark"));
            DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_WRITE);
            if (!scanIsExit) {
                invoiceDao.insert(scanInvoice);
            } else {
                QueryWrapper<TDxInvoice> invoiceWrapper = new QueryWrapper();
                invoiceWrapper.eq("uuid", scanInvoice.getUuid());
                invoiceDao.update(scanInvoice, invoiceWrapper);
            }
            queryInvoiceResult.put("company", pramsMapAll.get("company"));
            saveInvoiceInfoService.saveQueryInvoiceResult(queryInvoiceResult, pramsMapAll.get("userName"), qsType,
                pramsMapAll.get("dbName"));

        }
        return tDxInvoice;
    }

    /**
     * //确认
     *
     * @param returnList
     * @param tDxInvoice
     * @param isExits
     * @param pramsMap
     * @param i
     * @param invoiceDate
     */
    public void qsHandsMothConfirm(List<SmyqsWqsfpVO> returnList, TDxInvoiceReimburse tDxInvoice, Boolean isExits,
        Map<String, String> pramsMap, int i, String invoiceDate, boolean scanIsExit, TDxRecordInvoice invoice) {
        tDxInvoice.setRemark(invoice.getRemark());
        tDxInvoice.setQsDate(new Date());
        tDxInvoice.setQsStatus(FpqsjgEnum.YQS.getQsjgDm());
        tDxInvoice.setQsType(QsfsEnum.SMYQS.getQsfsDm());
        tDxInvoice.setQzStatus("1");
        tDxInvoice.setSelectId(tDxInvoice.getUuid());
        if (StringUtils.isNotBlank(invoice.getGfName())) {
            tDxInvoice.setGfName(invoice.getGfName());
        }
        tDxInvoice.setGfTaxNo(invoice.getGfTaxNo());
        tDxInvoice.setXfName(invoice.getXfName());
        tDxInvoice.setXfTaxNo(invoice.getXfTaxNo());
        tDxInvoice.setCheckCode(invoice.getCheckCode());
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_WRITE);
        if (!isExits) {
            tDxInvoice.setQsDate(new Date());
            // 不存在扫描记录
            this.baseMapper.insert(tDxInvoice);

        } else {
            tDxInvoice.setQsDate(new Date());
            QueryWrapper<TDxInvoiceReimburse> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uuid", tDxInvoice.getUuid());
            this.baseMapper.update(tDxInvoice, queryWrapper);
        }
        if (FplxEnum.ZP.getFplxDm().equals(tDxInvoice.getInvoiceType())
            || FplxEnum.TXF.getFplxDm().equals(tDxInvoice.getInvoiceType())
            || FplxEnum.JDC.getFplxDm().equals(tDxInvoice.getInvoiceType())
            || FplxEnum.PP.getFplxDm().equals(tDxInvoice.getInvoiceType())
            || FplxEnum.DZP.getFplxDm().equals(tDxInvoice.getInvoiceType())
            || FplxEnum.JSP.getFplxDm().equals(tDxInvoice.getInvoiceType())
            || FplxEnum.DZZP.getFplxDm().equals(tDxInvoice.getInvoiceType())) {
            TDxInvoice scanInvoice = new TDxInvoice();
            scanInvoice.setInvoiceType(invoice.getInvoiceType());
            scanInvoice.setInvoiceCode(invoice.getInvoiceCode());
            scanInvoice.setInvoiceNo(invoice.getInvoiceNo());
            scanInvoice.setGfTaxNo(invoice.getGfTaxNo());
            scanInvoice.setGfName(invoice.getGfName());
            scanInvoice.setXfTaxNo(invoice.getXfTaxNo());
            scanInvoice.setXfName(invoice.getXfName());
            scanInvoice.setInvoiceAmount(invoice.getInvoiceAmount());
            scanInvoice.setTaxAmount(invoice.getTaxAmount());
            scanInvoice.setTotalAmount(invoice.getTotalAmount());
            try {
                scanInvoice.setInvoiceDate(tDxInvoice.getInvoiceDate() != null ? tDxInvoice.getInvoiceDate() : null);
            } catch (Exception e) {
                e.printStackTrace();
            }
            scanInvoice.setUserAccount(tDxInvoice.getQsName());
            scanInvoice.setUserName(tDxInvoice.getQsName());
            scanInvoice.setQsType(tDxInvoice.getQsType());
            scanInvoice.setQsStatus(tDxInvoice.getQsStatus());
            scanInvoice.setValid("1");
            scanInvoice.setUuid(invoice.getUuid());
            scanInvoice.setCreateDate(new Date());
            scanInvoice.setQsDate(new Date());
            scanInvoice.setScanId(tDxInvoice.getScanId());
            scanInvoice.setNotes(tDxInvoice.getNotes());
            scanInvoice.setDeptId(tDxInvoice.getDeptId());
            scanInvoice.setDeptName(tDxInvoice.getDeptName());
            scanInvoice.setCheckCode(invoice.getCheckCode());
            scanInvoice.setInvoiceStatus(invoice.getInvoiceStatus());
            scanInvoice.setQsName(tDxInvoice.getQsName());
            scanInvoice.setCompany(tDxInvoice.getCompany());
            scanInvoice.setInvoiceRemark(invoice.getRemark());
            if (!scanIsExit) {
                invoiceDao.insert(scanInvoice);
            } else {
                QueryWrapper<TDxInvoice> invoiceWrapper = new QueryWrapper();
                invoiceWrapper.eq("uuid", scanInvoice.getUuid());
                invoiceDao.update(scanInvoice, invoiceWrapper);
            }
            if (invoice != null) {
                // TDxRecordInvoice invoiceUpdate = new TDxRecordInvoice();
                invoice.setId(invoice.getId());
                invoice.setQsStatus(FpqsjgEnum.YQS.getQsjgDm());
                invoice.setQsType(scanInvoice.getQsType());
                invoice.setQsDate(DateUtils.getSqlDateLong());
                invoice.setLastUpdateDate(new Date());
                invoice.setQsName(scanInvoice.getQsName());
                if (tDxInvoice.getDeptId() != null) {
                    invoice.setDeptId(tDxInvoice.getDeptId());
                    invoice.setDeptName(tDxInvoice.getDeptName());
                }
                invoice.setCompany(tDxInvoice.getCompany());
                recordInvoiceDao.updateById(invoice);
            }

            // 增加发票签收日志
            InvoiceLog log = new InvoiceLog();
            log.setInputName(scanInvoice.getQsName());
            log.setInputStatus("1");
            log.setInvoiceCode(scanInvoice.getInvoiceCode());
            log.setInvoiceNo(scanInvoice.getInvoiceNo());
            log.setQsType("1");
            log.setType("3");
            log.setCreateDate(new Date());
            log.setRemark("发票混扫");
            List<InvoiceLog> logs = new ArrayList<>();
            logs.add(log);
            invoiceLogService.toInvoiceLog(logs);
        }

        /*
         * SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice,invoiceDate,i+1);
         * returnList.add(qsjg);
         */
    }

    /**
     * 对比扫描信息和底账表信息是否一致
     *
     * @param tDxRecordInvoice
     *            底账表信息
     * @param pramsMap
     *            扫描传入信息
     * @return
     *
     */
    private String compareInvoiceInfo(TDxRecordInvoice tDxRecordInvoice, Map<String, String> pramsMap) {
        String notes = null;
        if (!pramsMap.get("invoiceDate").equals(DateUtils.dateToStr(tDxRecordInvoice.getInvoiceDate()))) {
            notes = "扫描的开票日期与底账开票日期不一致";
        } else {
            if (!pramsMap.get("gfTaxNo").equals(tDxRecordInvoice.getGfTaxNo())) {
                notes = "扫描的购方税号与底账购方税号不一致";
            } else {
                if (StringUtils.isNotBlank(tDxRecordInvoice.getGfName())) {
                    if (pramsMap.containsKey("gfmcAll")
                        && org.apache.commons.lang3.StringUtils.isNotBlank(pramsMap.get("gfmcAll"))
                        && org.apache.commons.lang3.StringUtils.isNoneBlank(tDxRecordInvoice.getGfName()) &&
                        // 底账购方名称为空不对比购方
                        !CompareNameUtils.changeBrackets(pramsMap.get("gfmcAll")).contains(
                            CompareNameUtils.changeBrackets(tDxRecordInvoice.getGfName().replaceAll(" ", "")))) {
                        if (StringUtils.isBlank(oldOrgName) || !CompareNameUtils.changeBrackets(oldOrgName).contains(
                            CompareNameUtils.changeBrackets(tDxRecordInvoice.getGfName().replaceAll(" ", "")))) {
                            // 发票上的购方名称不在登录账号能操作的购方税号范围内
                            notes = "登录人公司名称与发票的购方名称不一致";
                            return notes;
                        }
                    }
                } else if (pramsMap.containsKey("gfmcAll")
                    && org.apache.commons.lang3.StringUtils.isNotBlank(pramsMap.get("gfmcAll"))
                    && org.apache.commons.lang3.StringUtils.isNoneBlank(pramsMap.get("gfName")) &&
                    // 底账购方名称为空不对比购方
                    !CompareNameUtils.changeBrackets(pramsMap.get("gfmcAll"))
                        .contains(CompareNameUtils.changeBrackets(pramsMap.get("gfName").replaceAll(" ", "")))) {
                    if (StringUtils.isBlank(oldOrgName) || !CompareNameUtils.changeBrackets(oldOrgName)
                        .contains(CompareNameUtils.changeBrackets(pramsMap.get("gfName").replaceAll(" ", "")))) {
                        // 发票上的购方名称不在登录账号能操作的购方税号范围内
                        notes = "登录人公司名称与发票的购方名称不一致";
                        return notes;
                    }
                }
                if (!pramsMap.get("xfTaxNo").equals(tDxRecordInvoice.getXfTaxNo())) {
                    notes = "扫描的销方税号与底账销方税号不一致";
                } else {
                    if (!pramsMap.get("invoiceAmount")
                        .equals(MathUtil.round(tDxRecordInvoice.getInvoiceAmount().toPlainString()))) {
                        notes = "扫描的金额与底账金额不一致";
                    } else {
                        // 处理空和0的比较 因为ocr返回的是"" 库里存的可能是0
                        String ocrtaxAmount = pramsMap.get("taxAmount");
                        if (StringUtils.isBlank(ocrtaxAmount)) {
                            ocrtaxAmount = "0";
                        }
                        ocrtaxAmount = new BigDecimal(ocrtaxAmount).stripTrailingZeros().toPlainString();
                        log.info("ocrtaxAmount==" + ocrtaxAmount);
                        String recordtaxAmount = tDxRecordInvoice.getTaxAmount().toPlainString();
                        if (StringUtils.isBlank(recordtaxAmount)) {
                            recordtaxAmount = "0";
                        }
                        recordtaxAmount = new BigDecimal(recordtaxAmount).stripTrailingZeros().toPlainString();
                        log.info("recordtaxAmount==" + recordtaxAmount);
                        if (!ocrtaxAmount.equals(recordtaxAmount)) {
                            notes = "扫描的税额与底账税额不一致";
                        } else {
                            if (!pramsMap.get("totalAmount")
                                .equals(MathUtil.round(tDxRecordInvoice.getTotalAmount().toPlainString()))) {
                                notes = "扫描的价税合计与底账价税合计不一致";
                            }
                        }
                    }
                }
            }
        }
        return notes;
    }

    /**
     * 校验扫描基本信息是否正确
     *
     * @param invoiceDate
     * @param invoiceNo
     * @param invoiceCode
     *
     * @return
     *
     */
    private String validateInvoiceInfo(String invoiceCode, String invoiceNo, String invoiceDate) {
        String notes = null;
        InvoiceUtil invoiceUtil = new InvoiceUtil(invoiceCode);
        if (!invoiceUtil.fpdmValid() || invoiceUtil.getFplxdm() == null) {
            notes = "发票代码不正确";
        } else {
            invoiceUtil.setFphm(invoiceNo);
            if (!invoiceUtil.fphmValid()) {
                notes = "发票号码不正确";
            } else {
                invoiceUtil.setKprq(invoiceDate);
                if (!invoiceUtil.kprqValid(null)) {
                    notes = "开票日期不正确";
                }
            }
        }
        return notes;
    }

    /**
     * //未确认
     *
     * @param returnList
     * @param tDxInvoice
     * @param isExits
     * @param pramsMap
     * @param i
     * @param invoiceDate
     */
    public void qsHandsMoth(List<SmyqsWqsfpVO> returnList, TDxInvoiceReimburse tDxInvoice, Boolean isExits,
        Map<String, String> pramsMap, int i, String invoiceDate) {

        tDxInvoice.setQsStatus(FpqsjgEnum.WQS.getQsjgDm());
        tDxInvoice.setQsType(QsfsEnum.SMYQS.getQsfsDm());
        tDxInvoice.setQzStatus("0");
        try {
            tDxInvoice.setInvoiceDate(new SimpleDateFormat("yyyyMMdd").parse(invoiceDate));
        } catch (ParseException e) {
            tDxInvoice.setInvoiceDate(new Date());
        }
        tDxInvoice.setSelectId(tDxInvoice.getUuid());
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_WRITE);
        if (!isExits) {
            tDxInvoice.setQsDate(new Date());
            // 不存在扫描记录
            this.baseMapper.insert(tDxInvoice);
        } else {
            tDxInvoice.setQsDate(new Date());
            QueryWrapper<TDxInvoiceReimburse> updateWrapper = new QueryWrapper<>();
            updateWrapper.eq("uuid", tDxInvoice.getUuid());
            this.baseMapper.update(tDxInvoice, updateWrapper);
        }
        SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, invoiceDate, i);
        returnList.add(qsjg);
    }

    /**
     * 根据uuid查询扫描信息
     *
     * @param uuid
     * @return
     *
     */
    private TDxInvoiceReimburse getScaninfo(String uuid, Map<String, String> pramsMapAll) {
        // 直接查询扫描表是否存在信息
        DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_READ);
        QueryWrapper<TDxInvoiceReimburse> queryWrapper = new QueryWrapper();
        queryWrapper.eq("uuid", uuid);
        TDxInvoiceReimburse invoiceSm = this.baseMapper.selectOne(queryWrapper);
        return invoiceSm;
    }

    private TDxInvoiceReimburse getOtherScaninfo(String uuid, Map<String, String> pramsMapAll) {
        // 直接查询扫描表是否存在信息
        DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_READ);
        QueryWrapper<TDxInvoiceReimburse> queryWrapper = new QueryWrapper();
        queryWrapper.eq("uuid", uuid);
        queryWrapper.ne("id", pramsMapAll.get("id"));
        TDxInvoiceReimburse invoiceSm = this.baseMapper.selectOne(queryWrapper);
        return invoiceSm;
    }

    /**
     * 初始化签收记录信息
     *
     *
     * @param pramsMap
     * @return
     *
     */
    private TDxInvoiceReimburse initQsjl(Map<String, String> pramsMap) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        TDxInvoiceReimburse tDxInvoice = new TDxInvoiceReimburse();
        tDxInvoice.setEwm(pramsMap.get("ewm"));
        String invoiceCode = pramsMap.get("invoiceCode");
        String invoiceNo = pramsMap.get("invoiceNo");
        tDxInvoice.setInvoiceType(pramsMap.get("invoiceType"));
        tDxInvoice.setInvoiceCode(invoiceCode);
        tDxInvoice.setInvoiceNo(invoiceNo);
        tDxInvoice.setSourceSystem("1");
        tDxInvoice.setUuid(pramsMap.get("uuid"));
        tDxInvoice.setQsName(pramsMap.get("userName"));

        tDxInvoice.setUuid(pramsMap.get("uuid"));
        tDxInvoice.setDeptId(pramsMap.get("businessCode"));
        tDxInvoice.setDeptName(pramsMap.get("businessName"));
        tDxInvoice.setScanId(pramsMap.get("scanId"));
        tDxInvoice.setFileType(pramsMap.get("documentsType"));

        tDxInvoice.setTicketNumber(pramsMap.get("ticketNumber"));
        tDxInvoice.setPassenger(pramsMap.get("passenger"));

        tDxInvoice.setOrientation(pramsMap.get("orientation"));
        tDxInvoice.setRegion(pramsMap.get("region"));
        try {
            tDxInvoice.setInvoiceAmount(new BigDecimal(pramsMap.get("invoiceAmount")));
        } catch (Exception e) {
            tDxInvoice.setInvoiceAmount(null);
        }
        try {
            tDxInvoice.setTaxAmount(new BigDecimal(pramsMap.get("taxAmount")));
        } catch (Exception e) {
            tDxInvoice.setTaxAmount(null);
        }
        try {
            tDxInvoice.setTotalAmount(new BigDecimal(pramsMap.get("totalAmount")));
        } catch (Exception e) {
            tDxInvoice.setTotalAmount(null);
        }
        tDxInvoice.setInvoiceStatus(pramsMap.get("invoiceStatus"));
        tDxInvoice.setDqskssq(pramsMap.get("dqskssq"));
        tDxInvoice.setDepartureCity(pramsMap.get("departureCity"));
        tDxInvoice.setReachingCity(pramsMap.get("reachingCity"));
        tDxInvoice.setRelatedBusinessDocuments(pramsMap.get("relatedBusinessDocuments"));
        tDxInvoice.setBusinessDocumentMaker(pramsMap.get("businessDocumentMaker"));
        tDxInvoice.setBusinessDocumentNumber(pramsMap.get("businessDocumentNumber"));
        tDxInvoice.setReimbursementTime(pramsMap.get("reimbursementTime"));
        tDxInvoice.setReimbursementStatus(pramsMap.get("reimbursementStatus"));
        tDxInvoice.setTrainNumber(pramsMap.get("trainNumber"));
        tDxInvoice.setPassengerSFZH(pramsMap.get("passengerSfzh"));
        tDxInvoice.setGfTaxNo(pramsMap.get("gfTaxNo"));
        tDxInvoice.setGfAddressAndPhone(pramsMap.get("gfAddressAndPhone"));
        tDxInvoice.setGfBankAndNo(pramsMap.get("gfBankAndNo"));
        tDxInvoice.setXfAddressAndPhone(pramsMap.get("xfAddressAndPhone"));
        tDxInvoice.setXfBankAndNo(pramsMap.get("xfBankAndNo"));
        tDxInvoice.setGfName(pramsMap.get("gfName"));
        tDxInvoice.setXfTaxNo(pramsMap.get("xfTaxNo"));
        tDxInvoice.setXfName(pramsMap.get("xfName"));
        tDxInvoice.setTaxRate(pramsMap.get("taxRate"));
        tDxInvoice.setCreateDate(new Date());
        tDxInvoice.setRzhYesorno(pramsMap.get("rzhYesorno"));
        tDxInvoice.setTag(pramsMap.get("tag"));
        String invoiceType = pramsMap.get("invoiceType");
        if ("01".equals(invoiceType) || "03".equals(invoiceType) || "14".equals(invoiceType) || "08".equals(invoiceType)
            || "87".equals(invoiceType) || "88".equals(invoiceType) || "89".equals(invoiceType)
            || "90".equals(invoiceType) || "92".equals(invoiceType)) {
            String invoiceDate = pramsMap.get("invoiceDate");
            String nowDate = "2019-04-01";
            if (invoiceDate.compareTo(nowDate) < 0) {
                tDxInvoice.setIsDeduction("1");
            } else {
                tDxInvoice.setIsDeduction("0");
            }

        } else if ("10".equals(invoiceType)) {
            tDxInvoice.setIsDeduction(pramsMap.get("isDeduction"));
        } else {
            tDxInvoice.setIsDeduction("1");
        }

        tDxInvoice.setIsDomestic("0");

        tDxInvoice.setSelectId(pramsMap.get("selectId"));

        tDxInvoice.setScanId(pramsMap.get("scanId"));
        tDxInvoice.setFuelSurcharge(pramsMap.get("fuelSurcharge"));
        tDxInvoice.setQsDate(new Date());
        tDxInvoice.setCaacDevelopmentFund(pramsMap.get("caacDevelopmentFund"));
        // 如果是飞机票 取飞机票上的票价 其他票为null
        try {
            tDxInvoice.setFare(new BigDecimal(pramsMap.get("fare")));
        } catch (Exception e) {
            tDxInvoice.setFare(null);
        }

        try {
            tDxInvoice.setTicketDepartureTime(sdf.format(sdf.parse(pramsMap.get("ticketDepartureTime"))));
            tDxInvoice.setTicketArrivalTime(sdf.format(sdf.parse(pramsMap.get("ticketArrivalTime"))));
        } catch (Exception e) {
        }
        return tDxInvoice;
    }

    private List<Map<String, String>> validateScanInfo(List<OpenServiceOcr> ocrList, Map<String, String> pramsMap) {

        List<Map<String, String>> pramsMapList = new ArrayList<>();

        // JSONArray ocrData = JSONObject.parseArray(invoiceText);
        if (ocrList != null) {
            for (OpenServiceOcr data : ocrList) {
                OpenServiceOcr.InvoiceOcrData ocrData = data.getData();
                Map<String, String> validateMap = new HashMap<>(16);
                validateMap.put("userId", pramsMap.get("userId"));
                validateMap.put("userName", pramsMap.get("userName"));
                validateMap.put("gfshAll", pramsMap.get("gfshAll"));
                validateMap.put("gfmcAll", pramsMap.get("gfmcAll"));
                validateMap.put("businessCode", pramsMap.get("businessCode"));
                validateMap.put("businessName", pramsMap.get("businessName"));
                validateMap.put("company", pramsMap.get("company"));
                validateMap.put("scanId", pramsMap.get("scanId"));
                validateMap.put("taxNo", pramsMap.get("gfTaxNo"));
                validateMap.put("gfTaxNo", pramsMap.get("gfTaxNo"));
                validateMap.put("gfName", pramsMap.get("gfName"));
                if (null == pramsMap.get("fileType") || "".equals(pramsMap.get("fileType"))
                    || "".equals(pramsMap.get("fileType"))) {
                    validateMap.put("fileType", "1");
                } else {
                    validateMap.put("fileType", pramsMap.get("fileType"));
                }
                OpenServiceOcr.ExtralInfo extralInfo = data.getData().getFJXX();
                if (extralInfo != null) {
                    validateMap.put("ewm", extralInfo.getEWM());
                }
                validateMap.put("caacDevelopmentFund", ocrData.getMHFZJJ());

                String invoiceCode = ocrData.getFPDM();
                String invoiceType = data.getFPLXDM();
                if (null == invoiceType) {
                    invoiceType = "";
                }

                String invoiceNo = ocrData.getFPHM();
                String invoiceDate = ocrData.getKPRQ();
                // 飞机的开具日期
                if ("93".equals(invoiceType)) {
                    invoiceDate = ocrData.getTKRQ();
                }
                if (StringUtils.isBlank(invoiceDate)) {
                    invoiceDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
                }
                String ticketNumber = ocrData.getPZHM();
                String passenger = ocrData.getXM();
                String invoiceAmount = ocrData.getHJJE();
                if (null == invoiceAmount || "".equals(invoiceAmount) || "".equals(invoiceAmount)) {
                    invoiceAmount = "0";
                }
                String taxAmount = ocrData.getHJSE();
                if (null == taxAmount || "".equals(taxAmount) || "".equals(taxAmount)) {
                    taxAmount = "0";
                }
                if (taxAmount.contains("*")) {
                    taxAmount = "0";
                }
                String totalAmount = ocrData.getJSHJ();
                if (null == totalAmount || "".equals(totalAmount) || "".equals(totalAmount)) {
                    totalAmount = "0";
                }
                String fuelSurcharge = ocrData.getRYFJF();
                if (null == fuelSurcharge || "".equals(fuelSurcharge) || "".equals(fuelSurcharge)) {
                    fuelSurcharge = "0";
                }
                String caacDevelopmentFund = ocrData.getMHFZJJ();
                if (null == caacDevelopmentFund || "".equals(caacDevelopmentFund) || "".equals(caacDevelopmentFund)) {
                    caacDevelopmentFund = "0";
                }
                // 飞机票价
                String fare = ocrData.getPJ();
                if (null == fare || "".equals(fare) || "".equals(fare)) {
                    fare = "0";
                }
                String ze = ocrData.getZE();
                if (StringUtils.isBlank(ze)) {
                    ze = "0";
                }
                switch (invoiceType) {
                    case "93":
                        // 飞机票设置为航空客运电子客票行程单
                        invoiceType = "90";
                        break;
                    case "98":
                        // 过路过桥代码转换
                        invoiceType = "101";
                        break;
                    case "85":
                        // 可报销其他发票代码转换
                        invoiceType = "00";
                        break;
                    case "87":
                        // 完税证明代码转换
                        invoiceType = "102";
                        break;
                    case "96":
                        // 国际小票代码转换
                        invoiceType = "99";
                        break;
                }
                // switch (invoiceType) {
                // case "98":
                // // 过路过桥设置为通用机打发票
                // invoiceType = "97";
                // break;
                // case "93":
                // // 飞机票设置为航空客运电子客票行程单
                // invoiceType = "90";
                // break;
                // case "94":
                // // 汽车票设置为公路运输判断是否有乘车人有则实名无则非实名
                // if (StringUtils.isNotBlank(passenger)) {
                // invoiceType = "89";
                // }
                // break;
                // case "88":
                // // 船票设置为实名制客票
                // invoiceType = "89";
                // break;
                // case "85":
                // // 其他可报销发票调整为其他运输客票
                // invoiceType = "87";
                // break;
                // default:
                // break;
                // }

                switch (invoiceType) {
                    case "92": {
                        // totalAmount=invoiceAmount;
                        BigDecimal totalAmountBigDecimal = new BigDecimal(totalAmount);
                        taxAmount = totalAmountBigDecimal.divide(new BigDecimal("1.09"), 5, BigDecimal.ROUND_HALF_UP)
                            .multiply(new BigDecimal("0.09")).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                        invoiceAmount =
                            totalAmountBigDecimal.divide(new BigDecimal("1.09"), 5, BigDecimal.ROUND_HALF_UP)
                                .setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                        break;
                    }
                    case "90": {
                        // 价税合计
                        totalAmount = ze;
                        BigDecimal totalAmountBigDecimal = new BigDecimal(ze);
                        // 金额和燃油附加费
                        BigDecimal invoiceAmountAndFuelSurcharge =
                            new BigDecimal(fare).add(new BigDecimal(fuelSurcharge));
                        // 税额
                        taxAmount =
                            invoiceAmountAndFuelSurcharge.divide(new BigDecimal("1.09"), 5, BigDecimal.ROUND_HALF_UP)
                                .multiply(new BigDecimal("0.09")).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                        // 金额
                        invoiceAmount = totalAmountBigDecimal.subtract(new BigDecimal(taxAmount)).toString();
                        break;
                    }
                    case "89":
                    case "88":
                    case "102": {
                        BigDecimal totalAmountBigDecimal = new BigDecimal(totalAmount);
                        taxAmount = totalAmountBigDecimal.divide(new BigDecimal("1.03"), 5, BigDecimal.ROUND_HALF_UP)
                            .multiply(new BigDecimal("0.03")).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                        invoiceAmount =
                            totalAmountBigDecimal.divide(new BigDecimal("1.03"), 5, BigDecimal.ROUND_HALF_UP)
                                .setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                        break;
                    }
                    default:
                        break;
                }

                // String invoiceStatus = ocrData.getfp;
                // String dqskssq = data.getString("dqskssq");
                // String sourceSystem = data.getString("sourceSystem");
                String departureCity = ocrData.getCFZD();
                String reachingCity = ocrData.getDDZD();

                String trainNumber = ocrData.getCC();

                String ticketDepartureTime = ocrData.getSCSJ();
                String ticketArrivalTime = ocrData.getXCSJ();
                String passengerSfzh = ocrData.getSFZH();
                String uuid = UUID.randomUUID().toString();
                if (invoiceCode != null && invoiceNo != null) {
                    uuid = invoiceCode + invoiceNo;
                } else if (invoiceCode == null && invoiceNo != null) {
                    uuid = invoiceNo;
                }
                if ("92".equals(invoiceType)) {
                    uuid = ocrData.getXLH();
                    ticketNumber = ocrData.getPZHM();
                }
                String flightNumber = null;
                if ("90".equals(invoiceType)) {
                    uuid = ocrData.getPZHM();
                    ticketNumber = ocrData.getPZHM();
                    // JSONArray flights = data.getJSONArray("flights");
                    List<OpenServiceOcr.PlaneInfo> planeInfos = ocrData.getFJXC();
                    if (planeInfos != null) {
                        OpenServiceOcr.PlaneInfo planeInfo = planeInfos.get(0);
                        departureCity = planeInfo.getCFZD();
                        reachingCity = planeInfo.getDDZD();
                        flightNumber = planeInfo.getHBH();
                    }
                }
                String gfTaxNo = ocrData.getGMFSBH();
                String gfAddressAndPhone = ocrData.getGMFDZDH();
                String gfBankAndNo = ocrData.getGMFYHZH();
                String xfAddressAndPhone = ocrData.getXHFDZDH();
                String xfBankAndNo = ocrData.getXHFYHZH();
                String gfName = ocrData.getGMFMC();
                String xfTaxNo = ocrData.getXHFSBH();
                String xfName = ocrData.getXHFMC();
                // String taxRate = data.getString("taxRate");
                // String rzhYesorno = data.getString("legalizeState");
                String fileType = pramsMap.get("fileType");
                String checkCode = ocrData.getJYM();
                String ewm = pramsMap.get("ewm");

                String orientation = data.getXZJD();
                String region = data.getRegion();
                // 添加座位等级
                if (StringUtils.isNotBlank(ocrData.getZWDJ())) {
                    validateMap.put("seatLevel", ocrData.getZWDJ());
                }
                // 添加消费类型
                if (StringUtils.isNotBlank(ocrData.getXFLX())) {
                    validateMap.put("trafficType", ocrData.getXFLX());
                }
                validateMap.put("orientation", orientation);
                validateMap.put("region", region);

                validateMap.put("invoiceType", invoiceType);
                validateMap.put("invoiceCode", invoiceCode);
                validateMap.put("invoiceNo", invoiceNo);
                validateMap.put("invoiceDate", toDate(invoiceDate));
                validateMap.put("ticketNumber", ticketNumber);
                validateMap.put("passenger", passenger);
                validateMap.put("invoiceAmount", invoiceAmount);
                validateMap.put("taxAmount", taxAmount);
                validateMap.put("totalAmount", totalAmount);
                // validateMap.put("invoiceStatus", );
                // validateMap.put("dqskssq", dqskssq);
                // validateMap.put("sourceSystem", sourceSystem);
                validateMap.put("departureCity", departureCity);
                validateMap.put("reachingCity", reachingCity);

                if ("92".equals(invoiceType)) {
                    validateMap.put("trainNumber", trainNumber);
                }
                if ("90".equals(invoiceType)) {
                    validateMap.put("trainNumber", flightNumber);
                }
                validateMap.put("fuelSurcharge", fuelSurcharge);
                validateMap.put("ticketDepartureTime", ticketDepartureTime);
                validateMap.put("ticketArrivalTime", ticketArrivalTime);
                validateMap.put("passengerSfzh", passengerSfzh);
                validateMap.put("uuid", uuid);
                if (StringUtils.isNotBlank(gfTaxNo)) {
                    validateMap.put("gfTaxNo", gfTaxNo);
                }
                validateMap.put("gfAddressAndPhone", gfAddressAndPhone);
                validateMap.put("gfBankAndNo", gfBankAndNo);
                validateMap.put("xfAddressAndPhone", xfAddressAndPhone);
                validateMap.put("xfBankAndNo", xfBankAndNo);
                if (StringUtils.isNotBlank(gfName)) {
                    validateMap.put("gfName", gfName);
                }
                validateMap.put("xfTaxNo", xfTaxNo);
                validateMap.put("xfName", xfName);
                // validateMap.put("taxRate", taxRate);
                // validateMap.put("rzhYesorno", rzhYesorno);
                validateMap.put("fileType", fileType);
                validateMap.put("checkCode", checkCode);
                validateMap.put("ewm", ewm);
                validateMap.put("fare", fare);

                pramsMapList.add(validateMap);
            }
        }
        return pramsMapList;
    }

    public String toDate(String invoiceDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (invoiceDate.length() > 10) {
                invoiceDate = invoiceDate.substring(0, 10);
            }
            if (StringUtils.isNotBlank(invoiceDate) && Arrays.asList(8, 10).contains(invoiceDate.length())) {
                if (invoiceDate.startsWith("2")) {

                    Date parse = dateFormat.parse(invoiceDate.replaceAll("-", ""));
                    return format.format(parse);
                } else {
                    return format.format(new Date());
                }
            } else if (StringUtils.isNotBlank(invoiceDate) && invoiceDate.length() == 6) {
                Date parse = dateFormat.parse("20" + invoiceDate);
                return format.format(parse);
            } else {
                return format.format(new Date());
            }
        } catch (Exception e) {
            return format.format(new Date());
        }
    }

    /**
     * 转换扫描仪结果对象列表
     *
     * @param tDxInvoice
     * @param invoiceDate
     * @param i
     * @return
     */
    private SmyqsWqsfpVO convertToSmyqsView(TDxInvoiceReimburse tDxInvoice, String invoiceDate, int i) {
        SmyqsWqsfpVO qsjg = new SmyqsWqsfpVO();
        qsjg.setXh(String.valueOf(i));
        qsjg.setId(tDxInvoice.getId() + "");
        qsjg.setFileType(tDxInvoice.getFileType());
        if ("0".equals(tDxInvoice.getFileType())) {
            qsjg.setFileTypeName("封面");
        } else if ("1".equals(tDxInvoice.getFileType())) {
            qsjg.setFileTypeName("票据");
        } else if ("2".equals(tDxInvoice.getFileType())) {
            qsjg.setFileTypeName("附件");
        } else {
            qsjg.setFileTypeName("其他");
        }
        qsjg.setInvoiceType(tDxInvoice.getInvoiceType());
        String invoiceType = tDxInvoice.getInvoiceType();
        qsjg.setInvoiceTypeName(InvoiceTypeUtils.invoiceTypeName(invoiceType));
        // if ("01".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("增值税专用发票");
        // } else if ("03".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("机动车销售统一发票");
        // } else if ("08".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("增值税电子专用发票");
        // } else if ("04".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("增值税普通发票");
        // } else if ("10".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("增值税电子普通发票");
        // } else if ("11".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("增值税普通发票(卷票)");
        // } else if ("14".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("增值税电子普通发票(通行费)");
        // } else if ("91".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("出租车票");
        // } else if ("92".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("火车票");
        // } else if ("94".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("汽车票");
        // } else if ("95".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("定额发票");
        // } else if ("96".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("长途汽车票");
        // } else if ("97".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("通用机打发票");
        // } else if ("98".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("过路过桥");
        // } else if ("90".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("航空客运电子客票行程单");
        // } else if ("89".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("公路运输客票(实名)");
        // } else if ("88".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("水路运输客票(实名)");
        // } else if ("87".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("其他运输客票(实名)");
        // } else if ("00".equals(invoiceType)) {
        // qsjg.setInvoiceTypeName("其他");
        // } else {
        // qsjg.setInvoiceTypeName("");
        // }
        qsjg.setQzStatus(tDxInvoice.getQzStatus());
        if ("1".equals(tDxInvoice.getQzStatus())) {
            qsjg.setQzStatusName("已确认");
        } else {
            qsjg.setQzStatusName("未确认");
        }
        qsjg.setQsStatus(tDxInvoice.getQsStatus());
        if ("1".equals(tDxInvoice.getQsStatus())) {
            qsjg.setQsStatusName("已签收");
        } else {
            qsjg.setQsStatusName("未签收");
        }
        qsjg.setQsDate(new SimpleDateFormat("yyyy-MM-dd").format(tDxInvoice.getQsDate()));
        qsjg.setInvoiceNo(tDxInvoice.getInvoiceNo());
        qsjg.setInvoiceCode(tDxInvoice.getInvoiceCode());
        if (tDxInvoice.getInvoiceDate() != null) {
            qsjg.setInvoiceDate(new SimpleDateFormat("yyyy-MM-dd").format(tDxInvoice.getInvoiceDate()));
        }
        qsjg.setInvoiceAmount(tDxInvoice.getInvoiceAmount() == null ? null
            : String.valueOf(tDxInvoice.getInvoiceAmount().setScale(2, BigDecimal.ROUND_HALF_UP)));
        qsjg.setTaxAmount(tDxInvoice.getTaxAmount() == null ? null
            : String.valueOf(tDxInvoice.getTaxAmount().setScale(2, BigDecimal.ROUND_HALF_UP)));
        qsjg.setTrainNumber(tDxInvoice.getTrainNumber());
        qsjg.setDepartureCity(tDxInvoice.getDepartureCity());
        qsjg.setReachingCity(tDxInvoice.getReachingCity());
        qsjg.setPassenger(tDxInvoice.getPassenger());
        qsjg.setScanId(tDxInvoice.getScanId());
        qsjg.setUuid(tDxInvoice.getUuid());
        qsjg.setQsName(tDxInvoice.getQsName());
        qsjg.setGfTaxNo(tDxInvoice.getGfTaxNo());
        qsjg.setGfName(tDxInvoice.getGfName());
        qsjg.setXfName(tDxInvoice.getGfName());
        qsjg.setXfTaxNo(tDxInvoice.getXfTaxNo());
        qsjg.setCheckCode(tDxInvoice.getCheckCode());
        qsjg.setOrientation(tDxInvoice.getOrientation());
        qsjg.setRegion(tDxInvoice.getRegion());
        if (tDxInvoice.getTotalAmount() != null) {
            qsjg.setTotalAmount(tDxInvoice.getTotalAmount() == null ? null
                : tDxInvoice.getTotalAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString());
        }
        if (tDxInvoice.getCreateDate() != null) {
            qsjg.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(tDxInvoice.getCreateDate()));
        }
        qsjg.setEwm(tDxInvoice.getEwm());
        qsjg.setFuelSurcharge(tDxInvoice.getFuelSurcharge());
        qsjg.setDoubleStatus(tDxInvoice.getDoubleStatus());
        qsjg.setTicketNumber(tDxInvoice.getTicketNumber());
        qsjg.setPassengerSfzh(tDxInvoice.getPassengerSFZH());
        qsjg.setNotes(tDxInvoice.getNotes());
        qsjg.setCaacDevelopmentFund(tDxInvoice.getCaacDevelopmentFund());
        qsjg.setFare(tDxInvoice.getFare());
        qsjg.setOcrStatus(tDxInvoice.getOcrStatus());
        qsjg.setBusinessName(tDxInvoice.getDeptName());
        qsjg.setIsHunsao("1");
        return qsjg;
    }

    @Override
    public String updateReimbursement(Map<String, String> param) {
        // sql还没写 看具体的参数
        String invoiceCode = param.getOrDefault("invoiceCode", null);
        String invoiceNo = param.getOrDefault("invoiceNo", null);
        String invoiceType = param.getOrDefault("invoiceType", "");
        String ticketNumber = param.getOrDefault("ticketNumber", "");
        String invoiceDate = param.get("invoiceDate");
        param.put("ticketNumber", ticketNumber);
        String uuid = UUID.randomUUID().toString();
        if (StringUtils.isNotBlank(invoiceCode) && StringUtils.isNotBlank(invoiceNo)) {
            uuid = invoiceCode + invoiceNo;
        } else if (StringUtils.isBlank(invoiceCode) && StringUtils.isNotBlank(invoiceNo)) {
            uuid = invoiceNo;
        }
        if ("93".equals(invoiceType) || "90".equals(invoiceType)) {
            uuid = ticketNumber;
        }
        if (!"92".equals(invoiceType)) {
            param.put("uuid", uuid);
            param.put("selectId", uuid);
        }
        JSONObject queryInvoiceResult = null;
        TDxRecordInvoice invoice = this.baseMapper.getRecordByUuid(uuid);
        if ("1".equals(param.get("fileType"))) {
            // 增加火车票和飞机票税额金额的计算公式

            if ("92".equals(invoiceType)) {
                String totalAmount = param.getOrDefault("totalAmount", "");
                BigDecimal totalAmountBigDecimal = new BigDecimal(totalAmount);
                String taxAmount = totalAmountBigDecimal.divide(new BigDecimal("1.09"), 5, BigDecimal.ROUND_HALF_UP)
                    .multiply(new BigDecimal("0.09")).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                String invoiceAmount = totalAmountBigDecimal.divide(new BigDecimal("1.09"), 5, BigDecimal.ROUND_HALF_UP)
                    .setScale(2, BigDecimal.ROUND_HALF_UP).toString();

                param.put("totalAmount", totalAmount);
                param.put("taxAmount", taxAmount);
                param.put("invoiceAmount", invoiceAmount);

            } else if ("93".equals(invoiceType) || "90".equals(invoiceType)) {
                String totalAmount = param.getOrDefault("totalAmount", "0");
                String fare = param.getOrDefault("fare", "0");
                String fuelSurcharge = param.get("fuelSurcharge");
                String caacDevelopmentFund = param.get("caacDevelopmentFund");
                if (null == fuelSurcharge || "".equals(fuelSurcharge) || "".equals(fuelSurcharge)) {
                    fuelSurcharge = "0";
                }
                if (StringUtils.isBlank(caacDevelopmentFund)) {
                    caacDevelopmentFund = "0";
                }
                BigDecimal totalAmountBigDecimal = new BigDecimal(totalAmount);
                BigDecimal fareAndFuelSurcharge = new BigDecimal(fare).add(new BigDecimal(fuelSurcharge));
                String taxAmount = fareAndFuelSurcharge.divide(new BigDecimal("1.09"), 5, BigDecimal.ROUND_HALF_UP)
                    .multiply(new BigDecimal("0.09")).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                String invoiceAmount = totalAmountBigDecimal.subtract(new BigDecimal(taxAmount)).toString();
                param.put("fuelSurcharge", fuelSurcharge);
                param.put("caacDevelopmentFund", caacDevelopmentFund);
                param.put("fare", fare);
                param.put("totalAmount", totalAmount);
                param.put("taxAmount", taxAmount);
                param.put("invoiceAmount", invoiceAmount);
            } else if ("89".equals(invoiceType) || "88".equals(invoiceType) || "87".equals(invoiceType)
                || "102".equals(invoiceType)) {
                String totalAmount = param.getOrDefault("totalAmount", "0");
                BigDecimal totalAmountBigDecimal = new BigDecimal(totalAmount);
                String taxAmount = totalAmountBigDecimal.divide(new BigDecimal("1.03"), 5, BigDecimal.ROUND_HALF_UP)
                    .multiply(new BigDecimal("0.03")).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                String invoiceAmount = totalAmountBigDecimal.divide(new BigDecimal("1.03"), 5, BigDecimal.ROUND_HALF_UP)
                    .setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                param.put("totalAmount", totalAmount);
                param.put("taxAmount", taxAmount);
                param.put("invoiceAmount", invoiceAmount);
            }
            // 查看扫描表是否存在
            boolean doubleStatus = false;
            TDxInvoiceReimburse invoiceScanInfo = this.getOtherScaninfo(uuid, param);
            if (invoiceScanInfo != null) {
                param.put("notes", "重复签收");
                param.put("doubleStatus", "1");
                doubleStatus = true;
            } else
            // 如果是专票、机动车票、通行费票：普票、电票、卷票：
            if (FplxEnum.ZP.getFplxDm().equals(invoiceType) || FplxEnum.TXF.getFplxDm().equals(invoiceType)
                || FplxEnum.JDC.getFplxDm().equals(invoiceType) || FplxEnum.PP.getFplxDm().equals(invoiceType)
                || FplxEnum.DZP.getFplxDm().equals(invoiceType) || FplxEnum.JSP.getFplxDm().equals(invoiceType)
                || FplxEnum.DZZP.getFplxDm().equals(invoiceType) || FplxEnum.ESC.getFplxDm().equals(invoiceType)) {
                TDxInvoice dxinvoice = this.baseMapper.getInvoiceByUuid(uuid);
                if (dxinvoice != null) {
                    param.put("notes", "重复签收");
                    param.put("doubleStatus", "1");
                    doubleStatus = true;
                } else
                // 票据增值税验真
                if (FplxEnum.ZP.getFplxDm().equals(invoiceType) || FplxEnum.TXF.getFplxDm().equals(invoiceType)
                    || FplxEnum.JDC.getFplxDm().equals(invoiceType) || FplxEnum.DZZP.getFplxDm().equals(invoiceType)) {
                    // 专票、机动车票、通行费票
                    //
                    if (param.containsKey("gfshAll") && StringUtils.isNotBlank(param.get("gfshAll"))
                        && !param.get("gfshAll").contains(param.get("gfTaxNo"))) {
                        param.put("notes", "无税号权限");
                        param.put("qsStatus", FpqsjgEnum.WQS.getQsjgDm());
                        param.put("qsStatusName", "未签收");
                        param.put("qzStatusName", "未确认");
                    } else {
                        if (invoice == null) {
                            // 底账表不存在对应发票
                            param.put("notes", "无底账信息");
                            param.put("qsStatus", FpqsjgEnum.WQS.getQsjgDm());
                            param.put("qsStatusName", "未签收");
                            param.put("qzStatusName", "未确认");
                        } else {
                            String notes = this.compareInvoiceInfo(invoice, param);

                            if (notes != null) {
                                // 信息比对不一致
                                // 未确认
                                param.put("notes", notes);

                            } else if (!"0".equals(invoice.getInvoiceStatus())) {
                                param.put("notes", "发票状态异常");
                                param.put("qsStatus", FpqsjgEnum.WQS.getQsjgDm());
                                param.put("qsStatusName", "未签收");
                                param.put("qzStatusName", "未确认");
                            } else {
                                // 直接签收
                                // 确认
                                if (StringUtils.isNotBlank(invoice.getGfName())) {
                                    param.put("gfName", invoice.getGfName());
                                }
                                param.put("xfName", invoice.getXfName());
                                param.put("invoiceStatus", invoice.getInvoiceStatus());
                                param.put("remark", invoice.getRemark());
                                param.put("qsType", "1");
                                param.put("notes", "修改验真成功");
                                param.put("qsStatus", "1");
                                param.put("qsStatusName", "已签收");
                                param.put("qzStatus", "1");
                                param.put("qzStatusName", "已确认");
                                param.put("qsDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                            }
                        }
                    }
                } else {

                    if ("1".equals(param.get("productType")) && invoice != null) {
                        // 直接签收
                        if (StringUtils.isNotBlank(invoice.getGfName())) {
                            param.put("gfName", invoice.getGfName());
                        }
                        param.put("sourceSystem", invoice.getSourceSystem());
                        param.put("xfName", invoice.getXfName());
                        param.put("invoiceStatus", invoice.getInvoiceStatus());
                        param.put("remark", invoice.getRemark());
                        param.put("qsType", "1");
                        param.put("notes", "修改验真成功");
                        param.put("qsStatus", "1");
                        param.put("qsStatusName", "已签收");
                        param.put("qzStatus", "1");
                        param.put("qzStatusName", "已确认");
                        param.put("qsDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

                    } else {

                        // 调用查验接口查询发票信息
                        queryInvoiceResult = invoiceQueryUtil.queryInvoice(invoiceCode, invoiceNo,
                            param.get("invoiceDate"), param.get("invoiceAmount"), param.get("checkCode"));
                        if (queryInvoiceResult.containsKey("returnCode")) {
                            // 调用查验接口失败，进入查验失败处理流程
                            param.put("qzStatus", "0");
                            param.put("notes", "查验失败");
                            param.put("qsStatusName", "未签收");
                            param.put("qzStatusName", "未确认");
                        } else {
                            String resultCode = queryInvoiceResult.getString("resultCode");
                            // 查验成功且信息一致，直接保存到底账表并签收
                            if ("0001".equals(resultCode)) {
                                // 查验返回的购方税号
                                String checkGfsh = queryInvoiceResult.getString("buyerTaxNo");
                                checkGfsh = checkGfsh == null ? "" : checkGfsh;
                                // 查验返回的购方名称
                                String checkGfmc = queryInvoiceResult.getString("buyerName");
                                checkGfmc = checkGfmc == null ? "" : checkGfmc;
                                // 查验返回的作废标志
                                String zfbz = queryInvoiceResult.getString("isCancelled");

                                // 查验返回的税额
                                if (param.containsKey("gfshAll") && StringUtils.isNotBlank(param.get("gfshAll"))
                                    && !param.get("gfshAll").contains(param.get("gfTaxNo"))) {
                                    param.put("notes", "无税号权限");
                                    param.put("qsStatusName", "未签收");
                                    param.put("qzStatusName", "未确认");
                                } else if (param.containsKey("gfmcAll") && StringUtils.isNotBlank(param.get("gfmcAll"))
                                    && !CompareNameUtils.changeBrackets(param.get("gfmcAll"))
                                        .contains(CompareNameUtils.changeBrackets(checkGfmc.replaceAll(" ", "")))
                                    && !CompareNameUtils.changeBrackets(oldOrgName)
                                        .contains(CompareNameUtils.changeBrackets(checkGfmc.replaceAll(" ", "")))) {
                                    // 查验成功但是信息不一致，直接在扫描表添加一条记录
                                    param.put("notes", "登录人的公司名称与发票购方名称不一致,查验返回的购方名称为：" + checkGfmc);
                                    param.put("qsStatusName", "未签收");
                                    param.put("qzStatusName", "未确认");
                                } else if ("2".equals(zfbz) || "Y".equals(zfbz)) {
                                    // 作废发票无法签收，发票查验接口只返回了作废状态，未返回其它异常状态
                                    param.put("notes", "发票已作废，无法签收！");
                                    param.put("qsStatusName", "未签收");
                                    param.put("qzStatusName", "未确认");
                                } else {
                                    param.put("gfTaxNo", queryInvoiceResult.getString("buyerTaxNo"));
                                    param.put("gfName", queryInvoiceResult.getString("buyerName"));
                                    param.put("gfAddressAndPhone", queryInvoiceResult.getString("buyerAddressPhone"));
                                    param.put("gfBankAndNo", queryInvoiceResult.getString("buyerAccount"));
                                    param.put("xfTaxNo", queryInvoiceResult.getString("salerTaxNo"));
                                    param.put("xfName", queryInvoiceResult.getString("salerName"));
                                    param.put("xfAddressAndPhone", queryInvoiceResult.getString("salerAddressPhone"));
                                    param.put("xfBankAndNo", queryInvoiceResult.getString("salerAccount"));
                                    param.put("remark", queryInvoiceResult.getString("remark"));
                                    if ("Y".equals(queryInvoiceResult.getString("isCancelled"))) {
                                        param.put("invoiceStatus", FpztEnum.ZF.getFpztDm(FpztEnum.ZF.getIndex()));
                                    } else if ("N".equals(queryInvoiceResult.getString("isCancelled"))) {
                                        param.put("invoiceStatus", FpztEnum.ZC.getFpztDm(FpztEnum.ZC.getIndex()));
                                    } else {
                                        param.put("invoiceStatus", queryInvoiceResult.getString("isCancelled"));
                                    }
                                    if (!"".equals(queryInvoiceResult.getString("invoiceAmount"))
                                        && queryInvoiceResult.getString("invoiceAmount") != null
                                        && !(" ").equals(queryInvoiceResult.getString("invoiceAmount"))) {
                                        param.put("invoiceAmount", queryInvoiceResult.getString("invoiceAmount"));
                                    }
                                    if (!"".equals(queryInvoiceResult.getString("taxAmount"))
                                        && queryInvoiceResult.getString("taxAmount") != null
                                        && !(" ").equals(queryInvoiceResult.getString("taxAmount"))) {
                                        param.put("taxAmount", queryInvoiceResult.getString("taxAmount"));
                                        if (queryInvoiceResult.getString("taxAmount").contains("*")) {
                                            param.put("taxAmount", "0");
                                        }

                                    }
                                    if (!"".equals(queryInvoiceResult.getString("totalAmount"))
                                        && queryInvoiceResult.getString("totalAmount") != null
                                        && !(" ").equals(queryInvoiceResult.getString("totalAmount"))) {
                                        param.put("totalAmount", queryInvoiceResult.getString("totalAmount"));
                                    }
                                    param.put("qsStatus", FpqsjgEnum.YQS.getQsjgDm());
                                    param.put("qsStatusName", "已签收");
                                    param.put("qzStatus", "1");
                                    param.put("qzStatusName", "已确认");
                                    param.put("notes", "修改验真成功");
                                    param.put("invoiceType", queryInvoiceResult.getString("invoiceType"));
                                }
                                param.put("gfName", checkGfmc);
                                param.put("gfTaxNo", checkGfsh);
                                param.put("xfName", queryInvoiceResult.getString("salerName"));
                                param.put("xfTaxNo", queryInvoiceResult.getString("salerTaxNo"));
                                param.put("totalAmount", queryInvoiceResult.getString("totalAmount"));

                            } else {
                                // 查验成功但是信息不一致，直接在扫描表添加一条记录
                                param.put("notes", queryInvoiceResult.getString("resultTip"));
                                param.put("qsStatus", FpqsjgEnum.WQS.getQsjgDm());
                                param.put("qsStatusName", "未签收");
                                param.put("qzStatusName", "未确认");
                            }
                        }
                    }
                }
            } else {
                param.put("notes", "修改成功");
            }
            if ("01".equals(invoiceType) || "03".equals(invoiceType) || "14".equals(invoiceType)
                || "08".equals(invoiceType) || "87".equals(invoiceType) || "88".equals(invoiceType)
                || "89".equals(invoiceType) || "90".equals(invoiceType) || "92".equals(invoiceType)) {
                if ("01".equals(invoiceType) || "03".equals(invoiceType) || "14".equals(invoiceType)
                    || "08".equals(invoiceType) || FplxEnum.ESC.getFplxDm().equals(invoiceType)) {
                    param.put("isDeduction", "0");
                } else {
                    param.put("isDeduction", "1");
                    if (StringUtils.isNotBlank(invoiceDate)) {
                        if (invoiceDate.compareTo("2019-04-01") > -1) {
                            param.put("isDeduction", "0");
                        }
                    }
                }

            } else if ("10".equals(invoiceType)) {
                JSONArray dataArray = queryInvoiceResult.getJSONArray("detailList");
                param.put("isDeduction", "1");
                if (dataArray != null && dataArray.size() > 0) {
                    String goodName = dataArray.getJSONObject(0).getString("goodName");
                    if (goodName != null && goodName.contains("运输服务") && invoiceDate.compareTo("2019-04-01") > -1) {
                        param.put("isDeduction", "0");
                        param.put("tag", goodName);
                        param.put("taxRate", dataArray.getJSONObject(0).getString("taxRate"));
                    }
                }
            } else {
                param.put("isDeduction", "1");
            }
            if (!doubleStatus) {
                DynamicContextHolder.push(param.get("dbName") + DbConstant.BUSINESS_WRITE);
                this.baseMapper.updateReimbursement(param);
            }
        } else {
            if ("0".equals(param.get("fileType"))) {
                param.put("uuid", param.get("ewm"));
            }
            DynamicContextHolder.push(param.get("dbName") + DbConstant.BUSINESS_WRITE);
            this.baseMapper.updateReimbursementByFj(param);
        }
        if (FplxEnum.ZP.getFplxDm().equals(invoiceType) || FplxEnum.TXF.getFplxDm().equals(invoiceType)
            || FplxEnum.JDC.getFplxDm().equals(invoiceType) || FplxEnum.PP.getFplxDm().equals(invoiceType)
            || FplxEnum.DZP.getFplxDm().equals(invoiceType) || FplxEnum.JSP.getFplxDm().equals(invoiceType)
            || FplxEnum.DZZP.getFplxDm().equals(invoiceType) || FplxEnum.ESC.getFplxDm().equals(invoiceType)) {
            DynamicContextHolder.push(param.get("dbName") + DbConstant.BUSINESS_READ);
            TDxInvoiceReimburse reimburse = this.baseMapper.selectById(param.get("id"));
            if ("1".equals(param.get("qsStatus"))) {
                TDxInvoice scanInvoice = new TDxInvoice();
                scanInvoice.setInvoiceType(param.get("invoiceType"));
                scanInvoice.setInvoiceCode(param.get("invoiceCode"));
                scanInvoice.setInvoiceNo(param.get("invoiceNo"));
                scanInvoice.setGfTaxNo(param.get("gfTaxNo"));
                scanInvoice.setGfName(param.get("gfName"));
                scanInvoice.setXfTaxNo(param.get("xfTaxNo"));
                scanInvoice.setXfName(param.get("xfName"));
                scanInvoice.setInvoiceAmount(new BigDecimal(param.get("invoiceAmount")));
                scanInvoice.setTaxAmount(new BigDecimal(param.get("taxAmount")));
                scanInvoice.setTotalAmount(new BigDecimal(param.get("totalAmount")));
                try {
                    scanInvoice.setInvoiceDate(StringUtils.isNotBlank(param.get("invoiceDate"))
                        ? new SimpleDateFormat("yyyy-MM-dd").parse(param.get("invoiceDate")) : null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                scanInvoice.setUserAccount(param.get("userName"));
                scanInvoice.setUserName(param.get("userName"));
                scanInvoice.setQsType(param.get("qsType"));
                scanInvoice.setQsStatus(param.get("qsStatus"));
                scanInvoice.setValid("1");
                scanInvoice.setUuid(param.get("invoiceCode") + param.get("invoiceNo"));
                scanInvoice.setCreateDate(new Date());
                scanInvoice.setQsDate(new Date());
                scanInvoice.setScanId(reimburse.getScanId());
                scanInvoice.setNotes(param.get("notes"));
                scanInvoice.setDeptId(reimburse.getDeptId());
                scanInvoice.setDeptName(reimburse.getDeptName());
                scanInvoice.setCheckCode(param.get("checkCode"));
                scanInvoice.setInvoiceStatus(param.get("invoiceStatus"));
                scanInvoice.setQsName(param.get("userName"));
                scanInvoice.setCompany(param.get("company"));
                scanInvoice.setInvoiceRemark(param.get("remark"));
                DynamicContextHolder.push(param.get("dbName") + DbConstant.BUSINESS_WRITE);
                invoiceDao.insert(scanInvoice);
                DynamicContextHolder.push(param.get("dbName") + DbConstant.BUSINESS_READ);
                if (invoice != null) {
                    TDxRecordInvoice invoiceUpdate = new TDxRecordInvoice();
                    invoiceUpdate.setId(invoice.getId());
                    invoiceUpdate.setQsStatus(FpqsjgEnum.YQS.getQsjgDm());
                    invoiceUpdate.setQsType(scanInvoice.getQsType());
                    invoiceUpdate.setQsDate(DateUtils.getSqlDateLong());
                    invoiceUpdate.setLastUpdateDate(new Date());
                    invoiceUpdate.setQsName(scanInvoice.getQsName());
                    if (scanInvoice.getDeptId() != null) {
                        invoiceUpdate.setDeptId(scanInvoice.getDeptId());
                        invoiceUpdate.setDeptName(scanInvoice.getDeptName());
                    }
                    DynamicContextHolder.push(param.get("dbName") + DbConstant.BUSINESS_WRITE);
                    recordInvoiceDao.updateById(invoiceUpdate);
                } else {
                    if (!"1".equals(param.get("productType"))) {

                        saveInvoiceInfoService.saveQueryInvoiceResult(queryInvoiceResult, param.get("userName"),
                            param.get("qsType"), param.get("dbName"));
                    }
                }
            }
        }
        JSONObject json = new JSONObject();
        json.put("data", param);
        return super.responseResult(json);
    }

    @Override
    public boolean confirm(String id, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        this.baseMapper.confirm(id);
        return true;
    }

    @Override
    public boolean deleReimbursement(Map<String, String> pramsMap) {
        String id = pramsMap.get("id");
        String isHunsao = pramsMap.get("isHunsao");
        String dbName = pramsMap.get("dbName");
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        if ("1".equals(isHunsao)) {
            this.baseMapper.deletemixed(id);
        } else {
            this.baseMapper.deleteInvoice(id);
        }
        return true;

    }

    /**
     * 重新调用OCR
     *
     * @param
     * @return
     */
    /*    @Override
    public List<SmyqsWqsfpVO> reSendOcr(Map<String, String> pramsMapAll) {
        List<SmyqsWqsfpVO> returnList = new ArrayList<>();
        // 通过id找图片
        DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_READ);
        TDxInvoiceReimburse reimburse = this.baseMapper.selectById(pramsMapAll.get("id"));
    
        TDxInvoiceImg tDxInvoiceImg = this.baseMapper.getImgByScanId(reimburse.getScanId());
        if (null == tDxInvoiceImg) {
            return returnList;
        }
        pramsMapAll.put("scanId", tDxInvoiceImg.getScanId());
        pramsMapAll.put("fileType", null);
        String imagPath = tDxInvoiceImg.getImagePath();
    
        if (StringUtils.isBlank(imagPath)) {
            throw new RuntimeException();
        }
        String base64img;
        try {
            base64img = FileUtil.fileToBase64(new File(imagPath));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        final String userid = UUID.randomUUID().toString().replace("-", "");
        Map<String, Object> resultMaps = mixedScanOcrService.discernInvoice("13716563288", userid, base64img);
    
        String invoiceText = (String)resultMaps.get("invoice");
    
        // 校验扫描信息
        List<Map<String, String>> pramsMapList = this.validateScanInfo(invoiceText, pramsMapAll);
    
        boolean r = pramsMapList.size() == 0
            || (pramsMapList.size() == 1 && StringUtils.isBlank(pramsMapList.get(0).get("invoiceType")));
        if (!"0000".contentEquals(resultMaps.get("returnCode").toString()) || r) {
            return returnList;
        }
    
        for (int i = 0; i < pramsMapList.size(); i++) {
            TDxInvoiceReimburse tDxInvoice = new TDxInvoiceReimburse();
            Boolean isExits = false;
            Map<String, String> pramsMap = pramsMapList.get(i);
            String userName = pramsMap.get("userName");
            String invoiceType = pramsMap.get("invoiceType");
            // 增加类型为85的
            if (invoiceType == null || "".equals(invoiceType) || "00".equals(invoiceType) || "85".equals(invoiceType)) {
                tDxInvoice.setEwm(pramsMapAll.get("ewm"));
                tDxInvoice.setQsDate(new Date());
                tDxInvoice.setQzStatus("0");
                tDxInvoice.setQsStatus("0");
                tDxInvoice.setQsName(pramsMapAll.get("userName"));
                tDxInvoice.setTaxNo(pramsMapAll.get("taxNo"));
                tDxInvoice.setScanId(pramsMapAll.get("scanId"));
                tDxInvoice.setUuid(pramsMapAll.get("scanId"));
                tDxInvoice.setCreateDate(new Date());
                tDxInvoice.setFileType("2");
                // 不存在扫描记录
                tDxInvoice.setNotes("附件扫描成功");
                DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_WRITE);
                this.baseMapper.insert(tDxInvoice);
                SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, null, i + 1);
                returnList.add(qsjg);
                continue;
            }
            String invoiceCode = pramsMap.get("invoiceCode");
            String invoiceNo = pramsMap.get("invoiceNo");
            String invoiceDate = DateUtils.dateToStr(DateUtils.getSqlDate());
            if (pramsMap.get("invoiceDate") != null && !"".equals(pramsMap.get("invoiceDate"))) {
                invoiceDate = pramsMap.get("invoiceDate");
            } else {
                // 当前系统时间
                pramsMap.put("invoiceDate", invoiceDate);
            }
            String uuid = pramsMap.get("uuid");
            pramsMap.put("uuid", uuid);
            tDxInvoice = this.initQsjl(pramsMap);
            tDxInvoice.setTaxNo(pramsMapAll.get("taxNo"));
    //            tDxInvoice.setInvoiceDate(invoiceDate);
            tDxInvoice.setEwm(pramsMapAll.get("ewm"));
            tDxInvoice.setInvoiceType(invoiceType);
            tDxInvoice.setFileType("1");
            tDxInvoice.setXfName(pramsMap.get("xfName"));
            tDxInvoice.setGfName(pramsMap.get("gfName"));
            tDxInvoice.setGfTaxNo(pramsMap.get("gfTaxNo"));
            tDxInvoice.setXfTaxNo(pramsMap.get("xfTaxNo"));
            if (pramsMap.get("taxAmount") != null) {
                try {
                    tDxInvoice.setTaxAmount(new BigDecimal(pramsMap.get("taxAmount")));
                } catch (Exception e) {
                    tDxInvoice.setTaxAmount(null);
                }
            }
            if (pramsMap.get("invoiceAmount") != null) {
                try {
                    tDxInvoice.setInvoiceAmount(new BigDecimal(pramsMap.get("invoiceAmount")));
                } catch (Exception e) {
                    tDxInvoice.setInvoiceAmount(null);
                }
            }
            // 查看扫描表是否存在
            TDxInvoiceReimburse invoiceScanInfo = this.getScaninfo(uuid, pramsMapAll);
            if (invoiceScanInfo != null) {
                tDxInvoice = invoiceScanInfo;
                tDxInvoice.setNotes("重复扫描");
                tDxInvoice.setDoubleStatus("1");
                SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, invoiceDate, i + 1);
                returnList.add(qsjg);
                continue;
            }
            DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_READ);
            TDxInvoice scanInvoice = this.baseMapper.getInvoiceByUuid(uuid);
            boolean scanIsExit = false;
            if (scanInvoice != null) {
                scanIsExit = true;
                if ("1".equals(scanInvoice.getQsStatus())) {
                    tDxInvoice = new TDxInvoiceReimburse();
                    tDxInvoice.setFileType("1");
                    tDxInvoice.setInvoiceType(scanInvoice.getInvoiceType());
    //                    tDxInvoice.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(scanInvoice.getCreateDate()));
                    tDxInvoice.setInvoiceCode(scanInvoice.getInvoiceCode());
                    tDxInvoice.setInvoiceNo(scanInvoice.getInvoiceNo());
    //                    tDxInvoice.setInvoiceDate(new SimpleDateFormat("yyyy-MM-dd").format(scanInvoice.getInvoiceDate()));
                    tDxInvoice.setInvoiceAmount(scanInvoice.getInvoiceAmount());
                    tDxInvoice.setTaxAmount(scanInvoice.getTaxAmount());
                    tDxInvoice.setTotalAmount(scanInvoice.getTotalAmount());
                    tDxInvoice.setQsStatus("1");
                    tDxInvoice.setNotes("发票已签收");
                    tDxInvoice.setDoubleStatus("1");
                    SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, invoiceDate, i + 1);
                    returnList.add(qsjg);
                    continue;
                }
            }
    //            tDxInvoice.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            // 如果是专票、机动车票、通行费票：普票、电票、卷票：
            if (FplxEnum.ZP.getFplxDm().equals(invoiceType) || FplxEnum.TXF.getFplxDm().equals(invoiceType)
                || FplxEnum.JDC.getFplxDm().equals(invoiceType) || FplxEnum.PP.getFplxDm().equals(invoiceType)
                || FplxEnum.DZP.getFplxDm().equals(invoiceType) || FplxEnum.JSP.getFplxDm().equals(invoiceType)) {
    
                String notes = this.validateInvoiceInfo(invoiceCode, invoiceNo, invoiceDate);
                if (notes != null) {
                    // 信息校验失败
                    // 未确认
                    tDxInvoice.setNotes(notes);
                    tDxInvoice.setQsStatus("0");
                    qsHandsMoth(returnList, tDxInvoice, isExits, pramsMap, i, invoiceDate);
                } else {
                    if (FplxEnum.ZP.getFplxDm().equals(invoiceType) || FplxEnum.TXF.getFplxDm().equals(invoiceType)
                        || FplxEnum.JDC.getFplxDm().equals(invoiceType)) {
                        // 专票、机动车票、通行费票
                        if (pramsMapAll.containsKey("gfshAll") && StringUtils.isNotBlank(pramsMapAll.get("gfshAll"))
                            && !pramsMapAll.get("gfshAll").contains(tDxInvoice.getGfTaxNo())) {
                            // 查验成功但是信息不一致，直接在扫描表添加一条记录
                            tDxInvoice.setQsStatus(FpqsjgEnum.WQS.getQsjgDm());
                            tDxInvoice.setQzStatus("0");
                            tDxInvoice.setNotes("无税号权限");
                            qsHandsMoth(returnList, tDxInvoice, isExits, pramsMap, i, invoiceDate);
                        } else {
                            DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_READ);
                            TDxRecordInvoice invoice = this.baseMapper.getRecordByUuid(uuid);
                            if (invoice == null) {
                                tDxInvoice.setQsStatus(FpqsjgEnum.WQS.getQsjgDm());
                                tDxInvoice.setQzStatus("0");
                                tDxInvoice.setNotes("无底账信息");
                                qsHandsMoth(returnList, tDxInvoice, isExits, pramsMap, i, invoiceDate);
                            } else {
                                notes = this.compareInvoiceInfo(invoice, pramsMap);
    
                                if (notes != null) {
                                    // 信息比对不一致
                                    // 未确认
                                    tDxInvoice.setNotes(notes);
                                    tDxInvoice.setQsStatus("0");
                                    qsHandsMoth(returnList, tDxInvoice, isExits, pramsMap, i, invoiceDate);
    
                                } else {
                                    // 直接签收
                                    // 确认
                                    tDxInvoice.setQsStatus("1");
                                    tDxInvoice.setNotes("验真成功");
                                    qsHandsMothConfirm(returnList, tDxInvoice, isExits, pramsMap, i, invoiceDate,
                                        scanIsExit, invoice);
                                    SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, invoiceDate, i + 1);
                                    returnList.add(qsjg);
                                }
                            }
                        }
                    } else {
                        if ("1".equals(pramsMapAll.get("productType"))) {
                            TDxRecordInvoice invoice = this.baseMapper.getRecordByUuid(uuid);
    
                            if (invoice != null) {
                                tDxInvoice.setSourceSystem(invoice.getSourceSystem());
                                // 直接签收
                                // 确认
                                tDxInvoice.setQsStatus("1");
                                tDxInvoice.setNotes("验真成功");
                                qsHandsMothConfirm(returnList, tDxInvoice, isExits, pramsMap, i, invoiceDate,
                                    scanIsExit, invoice);
                                SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, invoiceDate, i + 1);
                                returnList.add(qsjg);
                                continue;
                            }
                        }
                        // 当天的票不查验
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                        // 设置日期格式
                        String nowtime = df.format(new Date());
                        if (nowtime.equals(invoiceDate)) {
                            tDxInvoice.setNotes("当天发票无法查验，请隔天修改后查验");
                            tDxInvoice.setQsStatus("0");
                            qsHandsMoth(returnList, tDxInvoice, isExits, pramsMap, i, invoiceDate);
                            continue;
                        }
                        // 调用查验接口进行发票签收
                        tDxInvoice = this.qsSinglefpByInvoiceQuery(tDxInvoice, invoiceCode, invoiceNo, pramsMap,
                            pramsMapAll, isExits, QsfsEnum.SMYQS.getQsfsDm(), scanIsExit);
                        tDxInvoice.setEwm(pramsMapAll.get("ewm"));
                        tDxInvoice.setFileType("1");
                        SmyqsWqsfpVO qsjg = this.convertToSmyqsView(tDxInvoice, null, i + 1);
                        returnList.add(qsjg);
                    }
                }
    
            } else {
                // 飞机票、火车票、定额发票、出租车票、其他：
                // 未确认
                tDxInvoice.setNotes("扫描成功");
                qsHandsMoth(returnList, tDxInvoice, isExits, pramsMap, i, invoiceDate);
    
            }
        }
        // 处理完删除原数据
        String id = pramsMapAll.get("id");
        DynamicContextHolder.push(pramsMapAll.get("dbName") + DbConstant.BUSINESS_WRITE);
        this.baseMapper.deletemixed(id);
        return returnList;
    }*/

    public TDxRecordInvoice updateRecordInvoice(JSONObject queryInvoiceResult, TDxRecordInvoice invoice) {

        invoice.setInvoiceAmount(queryInvoiceResult.getBigDecimal("invoiceAmount"));
        invoice.setTaxAmount(queryInvoiceResult.getBigDecimal("taxAmount"));
        invoice.setTotalAmount(queryInvoiceResult.getBigDecimal("totalAmount"));
        invoice.setGfAddressAndPhone(queryInvoiceResult.getString("buyerAddressPhone"));
        invoice.setGfBankAndNo(queryInvoiceResult.getString("buyerAccount"));
        invoice.setGfName(queryInvoiceResult.getString("buyerName"));
        invoice.setGfTaxNo(queryInvoiceResult.getString("buyerTaxNo"));
        invoice.setXfAddressAndPhone(queryInvoiceResult.getString("salerAddressPhone"));
        invoice.setXfBankAndNo(queryInvoiceResult.getString("buyerAccount"));
        invoice.setXfName(queryInvoiceResult.getString("salerName"));
        invoice.setXfTaxNo(queryInvoiceResult.getString("salerTaxNo"));
        invoice.setMachinecode(queryInvoiceResult.getString("machineNo"));
        invoice.setRemark(queryInvoiceResult.getString("remark"));
        invoice.setCheckCode(queryInvoiceResult.getString("checkCode"));
        invoice.setTxfbz(queryInvoiceResult.getString("txfbz"));
        if (queryInvoiceResult.containsKey("detailList")) {
            JSONArray dataArray = queryInvoiceResult.getJSONArray("detailList");
            if (dataArray != null && dataArray.size() > 0) {
                invoice.setDetailYesorno("1");

            } else {
                invoice.setDetailYesorno("0");
            }
        } else {
            invoice.setDetailYesorno("0");
        }
        invoice.setLastUpdateDate(new Date());
        return invoice;
    }
}
