package com.dxhy.jxpt.baseService.sign.service.singletable.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.api.model.db.TdxTdysMhxcdEntity;
import com.dxhy.api.model.db.TdxTdysMhxcdMxEntity;
import com.dxhy.api.model.db.TdxTdysTldzkpEntity;
import com.dxhy.api.model.invoice.invoiceCheck.TdxCheckInvoiceDetail;
import com.dxhy.api.model.sign.*;
import com.dxhy.api.model.sign.openservice.OpenServiceSingleInvoiceCheck;
import com.dxhy.api.model.sign.openservice.OpenServiceSingleInvoiceCheckJxpt;
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.service.DictdetaServcice;
import com.dxhy.common.util.NumberToCN;
import com.dxhy.common.util.ScanIdUtils;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.utils.CommonUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.jxpt.baseService.dao.TdxTdysMhxcdCoreDao;
import com.dxhy.jxpt.baseService.dao.TdxTdysMhxcdMxCoreDao;
import com.dxhy.jxpt.baseService.dao.TdxTdysTldzkpCoreDao;
import com.dxhy.jxpt.baseService.sign.service.singletable.SignCheckRecordInvoiceDetailService;
import com.dxhy.jxpt.baseService.sign.service.singletable.SignCheckRecordService;
import com.dxhy.jxpt.baseService.sign.service.singletable.SignCheckVehicleSaleInvoiceService;
import com.dxhy.jxpt.baseService.sign.service.singletable.SignSaveCheckInfoService;
import com.dxhy.jxpt.baseService.util.FpzlChangeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * 保存查验结果到查验相关记录表业务处理实现类
 *
 * @author ld
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@Service("signSaveCheckInfoService")
@Slf4j

public class SignSaveCheckInfoServiceImpl implements SignSaveCheckInfoService {

    @Resource
    private SignCheckRecordService signCheckRecordService;

    @Resource
    private DictdetaServcice dictdetaServcice;

    @Resource
    private SignCheckRecordInvoiceDetailService signCheckRecordInvoiceDetailService;

    @Resource
    private SignCheckVehicleSaleInvoiceService signCheckVehicleSaleInvoiceService;

    @Resource
    private ScanIdUtils scanIdUtils;

    @Resource
    private TdxTdysMhxcdCoreDao tdxTdysMhxcdCoreDao;
    @Resource
    private TdxTdysMhxcdMxCoreDao tdxTdysMhxcdMxCoreDao;

    @Resource
    private TdxTdysTldzkpCoreDao tdxTdysTldzkpCoreDao;

    @Override
    public void saveCheckInfo(JSONObject queryInvoiceResult, String userid, String dbName) {
        String resultCode = queryInvoiceResult.getString("resultCode");
        // 查验成功，结果一致
        /*
         * 查验成功状态码
         */
        String querySuccCode = "0001";
        if (querySuccCode.equals(resultCode)) {
            TDxCheckRecordInvoice checkRecord = getRecordInvoice(queryInvoiceResult, userid);
            DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
            TDxCheckRecordInvoice checkRecordTmp = signCheckRecordService.getCheckInvoiceByUuid(checkRecord.getUuid());
            if (checkRecordTmp != null) {
                if (StringUtils.isNotBlank(checkRecordTmp.getCycs())) {
                    int cycs = Integer.parseInt(checkRecordTmp.getCycs()) + 1;
                    checkRecord.setCycs(String.valueOf(cycs));
                } else {
                    checkRecord.setCycs("1");
                }
            } else {
                checkRecord.setCycs("1");
            }
            TDxCheckVehicleSale checkVehicleSale = getVehicleSaleInvoice(queryInvoiceResult);
            List<TdxCheckInvoiceDetail> checkRecordDetailList = getRecordInvoiceDetail(queryInvoiceResult);
            DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
            // 保存查验信息
            saveCheckMxxx(checkRecord, checkVehicleSale, checkRecordDetailList, null);
        }
    }

    /**
     * 保存查验信息
     *  @param checkRecord
     *            请求参数
     * @param checkVehicleSale
     *            请求参数
     * @param checkRecordDetailList
     * @param checkData     开放平台查验返回的数据结构。
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveCheckMxxx(TDxCheckRecordInvoice checkRecord, TDxCheckVehicleSale checkVehicleSale,
                              List<TdxCheckInvoiceDetail> checkRecordDetailList, OpenServiceSingleInvoiceCheckJxpt checkData) {
        String uuid = checkRecord.getUuid();
        {
            // 删除重复数据，防止重复的操作
            QueryWrapper<TDxCheckRecordInvoice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uuid", uuid);
            signCheckRecordService.remove(queryWrapper);
            // 保存数据
            signCheckRecordService.save(checkRecord);
        }

        if (checkVehicleSale != null) {
            // 删除重复数据，防止重复的操作
            QueryWrapper<TDxCheckVehicleSale> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uuid", uuid);
            signCheckVehicleSaleInvoiceService.remove(queryWrapper);
            // 保存数据
            signCheckVehicleSaleInvoiceService.save(checkVehicleSale);
        }

        //机动车，二手车，数电铁路，航空不再入库明细表
        if(!"03".equals(checkData.getFpzl())&&!"15".equals(checkData.getFpzl())&&!"161".equals(checkData.getFpzl())&&!"283".equals(checkData.getFpzl())){
            if (checkRecordDetailList != null && checkRecordDetailList.size() > 0) {
                // 删除重复数据，防止重复的操作
                QueryWrapper<TdxCheckInvoiceDetail> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("uuid", uuid);
                signCheckRecordInvoiceDetailService.remove(queryWrapper);
                // 保存数据
                /*
                 * 批量操作的大小
                 */
                int batchSize = 500;
                signCheckRecordInvoiceDetailService.saveBatch(checkRecordDetailList, batchSize);
            }
        }
        if(checkData!=null){
            //数电票航空 161
            if("161".equals(checkData.getFpzl())){
                log.info("更新特定要素-民航两个表的信息");
                updateTdysMhInfo(uuid,checkData);
            }
            //数电票铁路 283
            if("283".equals(checkData.getFpzl())){
                log.info("更新特定要素-铁路表的信息");
                updateTdysTldzkyInfo(uuid,checkData);
            }
        }

    }
    /**
    * @Description 更新特定要素-铁路两个表的信息
    * @param uuid
    * @param checkData
    * @Return void
    * @Author wangruwei
    * @Date 2024/5/10 16:30
    **/
    private void updateTdysTldzkyInfo(String uuid, OpenServiceSingleInvoiceCheck checkData) {
        log.info("删除t_dx_tdys_tldzkp表关联数据");
        tdxTdysTldzkpCoreDao.delete(new LambdaQueryWrapper<TdxTdysTldzkpEntity>().eq(TdxTdysTldzkpEntity::getUuid, uuid));
        log.info("扩展字段处理。保存t_dx_tdys_tldzkp表");
        TdxTdysTldzkpEntity tlkpInsert = new TdxTdysTldzkpEntity();
        tlkpInsert.setUuid(uuid);
        //tlkpInsert.setGmfjbr(invoiceTlkpInfoItem.getgm);
        //tlkpInsert.setJbrsfzjhm();
        //tlkpInsert.setJbrlxdh();
        //tlkpInsert.setKprsrrzdzxx();
        //tlkpInsert.setSjkpdzxx();
        //tlkpInsert.setHtbh();
        //tlkpInsert.setNsywfssj();
        //tlkpInsert.setJbrsfzjzlDm();
        //tlkpInsert.setSpsl();
        //tlkpInsert.setSfwzzfp();
        tlkpInsert.setFphm(checkData.getFphm());
        tlkpInsert.setZjhm(checkData.getZjhm());
        tlkpInsert.setKprq(checkData.getKprq());
        tlkpInsert.setYwlxmc(CommonUtil.changeTlkpYwlx(checkData.getYwlx()));
        tlkpInsert.setCfz(checkData.getCfz());
        //tlkpInsert.setCfzpy(invoiceTlkpInfoItem.getDepartureStationChinese());
        tlkpInsert.setDdz(checkData.getDdz());
        //tlkpInsert.setDdzpy(invoiceTlkpInfoItem.getDepartureStationChinese());
        tlkpInsert.setCccc(checkData.getCc());
        tlkpInsert.setRq(checkData.getCcrq());
        tlkpInsert.setCfsjl(checkData.getCfsj());
        tlkpInsert.setTldzkppzmc(checkData.getPz());
        tlkpInsert.setKttz(checkData.getKttz());
        tlkpInsert.setXbl(checkData.getXb());
        tlkpInsert.setCx(checkData.getCx());
        tlkpInsert.setXw(checkData.getXw());
        //tlkpInsert.setZfje();
        //tlkpInsert.setYtje(invoiceTlkpInfoItem.getRefundAmount());
        //tlkpInsert.setYppj();
        //tlkpInsert.setYpcfz();
        //tlkpInsert.setYpddz();
        tlkpInsert.setTaxRate(checkData.getSl());
        tlkpInsert.setDzkph(checkData.getDzkph());
        tlkpInsert.setXm(checkData.getXm());
        //tlkpInsert.setTlkpyhlx();
        //tlkpInsert.setYfphm();
        tlkpInsert.setCreateTime(new Date());
        tdxTdysTldzkpCoreDao.insert(tlkpInsert);
    }

    /**
    * @Description 更新特定要素-民航两个表的信息
    * @param uuid
    * @param checkData  开放平台查验返回的结构实体
    * @Return void
    * @Author wangruwei
    * @Date 2024/5/10 16:29
    **/
    private void updateTdysMhInfo(String uuid, OpenServiceSingleInvoiceCheck checkData) {
        log.info("删除t_dx_tdys_mhxcd表");
        tdxTdysMhxcdCoreDao.delete(new LambdaQueryWrapper<TdxTdysMhxcdEntity>().eq(TdxTdysMhxcdEntity::getUuid, uuid));
        log.info("入库t_dx_tdys_mhxcd表");
        TdxTdysMhxcdEntity mhxcdInsert = new TdxTdysMhxcdEntity();
        mhxcdInsert.setUuid(uuid);
        //mhxcdInsert.setGmfjbr();
        mhxcdInsert.setTicketPrice(checkData.getJshj());
        //mhxcdInsert.setFuelSurcharge(tdysMhxcd.getFuelSurcharge());
        mhxcdInsert.setValueAddedTaxAmount(checkData.getFpse());
        //mhxcdInsert.setVatRate(tdysMhxcd.getVatRate());
        //mhxcdInsert.setAviationDevelopmentFund(tdysMhxcd.getAviationDevelopmentFund());
        //mhxcdInsert.setOtherTaxes(tdysMhxcd.getOtherTaxes());
        //mhxcdInsert.setJbrsfzjhm();
        //mhxcdInsert.setJbrlxdh();
        //mhxcdInsert.setKprsrrzdzxx();
        //mhxcdInsert.setSjkpdzxx();
        //mhxcdInsert.setHtbh();
        //mhxcdInsert.setNsywfssj();
        //mhxcdInsert.setJbrsfzjzlDm();
        //mhxcdInsert.setSpsl();
        //mhxcdInsert.setSfwzzfp();
        mhxcdInsert.setFphm(checkData.getFphm());
        mhxcdInsert.setGngjbz(checkData.getGngjbs());
        mhxcdInsert.setGpdh(checkData.getGpdh());
        //mhxcdInsert.setSflzfpbz();
        mhxcdInsert.setXm(checkData.getXm());
        mhxcdInsert.setZjhm(checkData.getZjhm());
        //mhxcdInsert.setQz(tdysMhxcd.getEndorsement());
        mhxcdInsert.setDzkphm(checkData.getDzkph());
        //mhxcdInsert.setYzm(tdysMhxcd.getVerificationCode());
        //mhxcdInsert.setTsxx(tdysMhxcd.getPrompt());
        //mhxcdInsert.setBxf(tdysMhxcd.getInsuranceFee());
        //mhxcdInsert.setXswddh(tdysMhxcd.getOutletCode());
        //mhxcdInsert.setTkdw(tdysMhxcd.getIssuingEntity());
        mhxcdInsert.setKprq(checkData.getKprq());
        mhxcdInsert.setXsfmc(checkData.getXhfmc());
        mhxcdInsert.setXsfnsrsbh(checkData.getXhfsbh());
        mhxcdInsert.setGmfmc(checkData.getGmfmc());
        mhxcdInsert.setGmfnsrsbh(checkData.getGmfsbh());
        mhxcdInsert.setCreateTime(new Date());

        tdxTdysMhxcdCoreDao.insert(mhxcdInsert);


        log.info("删除t_dx_tdys_mhxcd_mx表");
        tdxTdysMhxcdMxCoreDao.delete(new LambdaQueryWrapper<TdxTdysMhxcdMxEntity>().eq(TdxTdysMhxcdMxEntity::getTdysId, uuid));
        if(!CollectionUtils.isEmpty(checkData.getDetailList())){
            log.info("入库t_dx_tdys_mhxcd_mx表");
            for (int i = 0; i < checkData.getDetailList().size(); i++) {
                OpenServiceSingleInvoiceCheck.SingleInvoiceDetail item = checkData.getDetailList().get(i);
                TdxTdysMhxcdMxEntity addItem = new TdxTdysMhxcdMxEntity();
                addItem.setTdysId(uuid);
                addItem.setSfz(item.getSfz());
                addItem.setMdz(item.getMdz());
                addItem.setHd(item.getHd());
                addItem.setCyr(item.getCyr());
                addItem.setHbh(item.getHbh());
                addItem.setZwdj(item.getZwdj());
                addItem.setCyrq(item.getCyrq());
                addItem.setQfsj(item.getQfsj());
                addItem.setKpjbkplb(item.getKpjb());
                //addItem.setKpsxrq(item.getkp);
                //addItem.setYxjzrq(item.getExpiryDate());
                //addItem.setMfxle(item.getBaggageWeight());
                addItem.setCreateTime(new Date());
                tdxTdysMhxcdMxCoreDao.insert(addItem);
            }
        }
    }

    @Override
    public List<TdxCheckInvoiceDetail> getRecordInvoiceDetail(JSONObject queryInvoiceResult) {
        if (queryInvoiceResult.containsKey("detailList")) {
            JSONArray dataArray = queryInvoiceResult.getJSONArray("detailList");
            if (dataArray != null && dataArray.size() > 0) {
                String invoiceType = queryInvoiceResult.getString("invoiceType");
                String invoiceCode = queryInvoiceResult.getString("invoiceCode");
                String invoiceNo = queryInvoiceResult.getString("invoiceNo");
                String uuid = invoiceCode + invoiceNo;
                List<TdxCheckInvoiceDetail> invoiceDetailList = new ArrayList<>(dataArray.size());
                for (int i = 0; i < dataArray.size(); i++) {
                    JSONObject invoiceDetailQuery = dataArray.getJSONObject(i);
                    TdxCheckInvoiceDetail invoiceDetail = new TdxCheckInvoiceDetail();
                    invoiceDetail.setUuid(uuid);
                    invoiceDetail.setDetailNo(invoiceDetailQuery.getString("detailNo"));
                    invoiceDetail.setGoodsName(invoiceDetailQuery.getString("goodsName"));
                    invoiceDetail.setModel(invoiceDetailQuery.getString("specificationModel"));
                    invoiceDetail.setUnit(invoiceDetailQuery.getString("unit"));
                    invoiceDetail.setNum(invoiceDetailQuery.getString("num"));
                    invoiceDetail.setUnitPrice(invoiceDetailQuery.getString("unitPrice"));
                    invoiceDetail.setDetailAmount(invoiceDetailQuery.getString("detailAmount"));
                    invoiceDetail.setTaxRate(invoiceDetailQuery.getString("taxRate"));
                    invoiceDetail.setTaxAmount(invoiceDetailQuery.getString("taxAmount"));
                    if (FplxEnum.TXF.getFplxDm().equals(invoiceType)) {
                        // 通行费发票
                        invoiceDetail.setCph(invoiceDetailQuery.getString("cph"));
                        invoiceDetail.setLx(invoiceDetailQuery.getString("lx"));
                        invoiceDetail.setTxrqq(invoiceDetailQuery.getString("txrqq"));
                        invoiceDetail.setTxrqz(invoiceDetailQuery.getString("txrqz"));
                    }
                    invoiceDetailList.add(invoiceDetail);
                }
                return invoiceDetailList;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public TDxCheckVehicleSale getVehicleSaleInvoice(JSONObject queryInvoiceResult) {
        String invoiceType = queryInvoiceResult.getString("invoiceType");
        if (FplxEnum.JDC.getFplxDm().equals(invoiceType)) {
            String invoiceCode = queryInvoiceResult.getString("invoiceCode");
            String invoiceNo = queryInvoiceResult.getString("invoiceNo");
            String uuid = invoiceCode + invoiceNo;
            TDxCheckVehicleSale vehicleSaleInvoice = new TDxCheckVehicleSale();
            vehicleSaleInvoice.setUuid(uuid);
            vehicleSaleInvoice.setBuyerIdNum(queryInvoiceResult.getString("buyerIDNum"));
            vehicleSaleInvoice.setVehicleType(queryInvoiceResult.getString("vehicleType"));
            vehicleSaleInvoice.setFactoryModel(queryInvoiceResult.getString("factoryModel"));
            vehicleSaleInvoice.setProductPlace(queryInvoiceResult.getString("productPlace"));
            vehicleSaleInvoice.setCertificate(queryInvoiceResult.getString("certificate"));
            vehicleSaleInvoice.setCertificateImport(queryInvoiceResult.getString("certificateImport"));
            vehicleSaleInvoice.setInspectionNum(queryInvoiceResult.getString("inspectionNum"));
            vehicleSaleInvoice.setEngineNo(queryInvoiceResult.getString("engineNo"));
            vehicleSaleInvoice.setVehicleNo(queryInvoiceResult.getString("vehicleNo"));
            vehicleSaleInvoice.setTaxBureauCode(queryInvoiceResult.getString("taxBureauCode"));
            vehicleSaleInvoice.setTaxBureauName(queryInvoiceResult.getString("taxBureauName"));
            vehicleSaleInvoice.setTaxRecords(queryInvoiceResult.getString("taxRecords"));
            vehicleSaleInvoice.setTonnage(queryInvoiceResult.getString("tonnage"));
            vehicleSaleInvoice.setLimitPeople(queryInvoiceResult.getString("limitPeople"));
            vehicleSaleInvoice.setTaxRate(queryInvoiceResult.getString("taxRate"));
            return vehicleSaleInvoice;
        }else if(FplxEnum.ESC.getFplxDm().equals(invoiceType)){
            String invoiceCode = queryInvoiceResult.getString("invoiceCode");
            String invoiceNo = queryInvoiceResult.getString("invoiceNo");
            String uuid = invoiceCode + invoiceNo;

            TDxCheckVehicleSale vehicleSaleInvoice = new TDxCheckVehicleSale();
            vehicleSaleInvoice.setUuid(uuid);
            vehicleSaleInvoice.setPlateNo(queryInvoiceResult.getString("licensePlate"));
            vehicleSaleInvoice.setRegistrationNo(queryInvoiceResult.getString("registrationNo"));
            if(StringUtils.isNotBlank(queryInvoiceResult.getString("vehicleType"))){
                vehicleSaleInvoice.setVehicleType(queryInvoiceResult.getString("vehicleType"));
            }
            if(StringUtils.isNotBlank(queryInvoiceResult.getString("bandModel"))){
                vehicleSaleInvoice.setFactoryModel(queryInvoiceResult.getString("bandModel"));
            }
            if(StringUtils.isNotBlank(queryInvoiceResult.getString("vehicleIdentificationNo"))){
                vehicleSaleInvoice.setVehicleNo(queryInvoiceResult.getString("vehicleIdentificationNo"));
            }
            vehicleSaleInvoice.setCarPrice(queryInvoiceResult.getString("carPrice"));
            vehicleSaleInvoice.setVehicleAdminOffice(queryInvoiceResult.getString("vehicleAdminOffice"));
            vehicleSaleInvoice.setBuyerUnit(queryInvoiceResult.getString("buyerUnitOrIndividual"));
            vehicleSaleInvoice.setBuyerUnitCode(queryInvoiceResult.getString("buyerUnitCodeOrIdNo"));
            vehicleSaleInvoice.setBuyerUnitAddress(queryInvoiceResult.getString("buyerUnitOrIndividualAddress"));
            vehicleSaleInvoice.setBuyerPhone(queryInvoiceResult.getString("buyerPhone"));
            vehicleSaleInvoice.setSellerUnit(queryInvoiceResult.getString("sellerUnitOrIndividual"));
            vehicleSaleInvoice.setSellerUnitCode(queryInvoiceResult.getString("sellerUnitCodeOrIdNo"));
            vehicleSaleInvoice.setSellerUnitAddress(queryInvoiceResult.getString("sellerUnitOrIndividualAddress"));
            vehicleSaleInvoice.setSellerPhone(queryInvoiceResult.getString("sellerPhone"));
            vehicleSaleInvoice.setAuctionUnit(queryInvoiceResult.getString("businessUnit"));
            vehicleSaleInvoice.setAuctionAddress(queryInvoiceResult.getString("businessUnitAddress"));
            vehicleSaleInvoice.setAuctionTaxNo(queryInvoiceResult.getString("businessUnitTaxNo"));
            vehicleSaleInvoice.setAuctionBank(queryInvoiceResult.getString("businessUnitBankAndAccount"));
            vehicleSaleInvoice.setAuctionPhone(queryInvoiceResult.getString("businessUnitPhone"));
            vehicleSaleInvoice.setCarMarket(queryInvoiceResult.getString("lemonMarket"));
            vehicleSaleInvoice.setCarMarketTaxNo(queryInvoiceResult.getString("lemonMarketTaxNo"));
            vehicleSaleInvoice.setCarMarketAddress(queryInvoiceResult.getString("lemonMarketAddress"));
            vehicleSaleInvoice.setCarMarketBank(queryInvoiceResult.getString("lemonMarketBankAndAccount"));
            vehicleSaleInvoice.setCarMarketPhone(queryInvoiceResult.getString("lemonMarketPhone"));
            return vehicleSaleInvoice;
        } else {
            return null;
        }
    }

    @Override
    public TDxCheckRecordInvoice getRecordInvoice(JSONObject queryInvoiceResult, String userid) {
        TDxCheckRecordInvoice invoice = new TDxCheckRecordInvoice();

        String invoiceType = queryInvoiceResult.getString("invoiceType");
        String invoiceCode = queryInvoiceResult.getString("invoiceCode");
        String invoiceNo = queryInvoiceResult.getString("invoiceNo");
        String uuid = invoiceCode + invoiceNo;

        invoice.setCyjgCode(queryInvoiceResult.getString("resultCode"));
        invoice.setCyjgTip(queryInvoiceResult.getString("resultTip"));
        invoice.setCycs(queryInvoiceResult.getString("checkCount"));
        invoice.setFpzl(invoiceType);
        if("31".equals(invoiceType) || "32".equals(invoiceType)){
            invoice.setFphm(uuid);
        }else {
            invoice.setFpdm(invoiceCode);
            invoice.setFphm(invoiceNo);
        }
        invoice.setUuid(uuid);
        invoice.setGfsh(queryInvoiceResult.getString("buyerTaxNo"));
        invoice.setGfmc(queryInvoiceResult.getString("buyerName"));
        invoice.setGfdzdh(queryInvoiceResult.getString("buyerAddressPhone"));
        invoice.setGfyhzh(queryInvoiceResult.getString("buyerAccount"));
        invoice.setXfdzdh(queryInvoiceResult.getString("salerAddressPhone"));
        invoice.setXfyhzh(queryInvoiceResult.getString("salerAccount"));
        invoice.setXfmc(queryInvoiceResult.getString("salerName"));
        invoice.setXfsh(queryInvoiceResult.getString("salerTaxNo"));
        invoice.setJe(queryInvoiceResult.getString("invoiceAmount"));
        try {
            new BigDecimal(queryInvoiceResult.getString("taxAmount"));
            invoice.setSe(queryInvoiceResult.getString("taxAmount"));
        } catch (Exception e) {
            invoice.setSe("0");
        }
        invoice.setJshj(queryInvoiceResult.getString("totalAmount"));
        if(FplxEnum.ESC.getFplxDm().equals(invoiceType)){
            invoice.setJe(queryInvoiceResult.getString("carPrice"));
            invoice.setJshj(queryInvoiceResult.getString("carPrice"));
            invoice.setGfmc(queryInvoiceResult.getString("buyerUnitOrIndividual"));
            invoice.setGfsh(queryInvoiceResult.getString("buyerUnitCodeOrIdNo"));
            invoice.setGfdzdh(queryInvoiceResult.getString("buyerUnitOrIndividualAddress"));
            invoice.setXfmc(queryInvoiceResult.getString("sellerUnitOrIndividual"));
            invoice.setXfsh(queryInvoiceResult.getString("sellerUnitCodeOrIdNo"));
            invoice.setXfdzdh(queryInvoiceResult.getString("sellerUnitOrIndividualAddress"));
        }
        invoice.setKprq(queryInvoiceResult.getString("invoiceDate"));
        invoice.setJqbh(queryInvoiceResult.getString("machineNo"));
        invoice.setBz(queryInvoiceResult.getString("remark"));
        invoice.setJym(queryInvoiceResult.getString("checkCode"));
        invoice.setTxfbz(queryInvoiceResult.getString("txfbz"));
        invoice.setCreateDate(new Date());
        invoice.setCheckUser(userid);
        invoice.setScanId(queryInvoiceResult.getString("scanId"));
        invoice.setZfbz(queryInvoiceResult.getString("isCancelled"));
        invoice.setCompany(queryInvoiceResult.getString("company"));
        invoice.setPdfMd5(Long.valueOf(scanIdUtils.createSerialNum()));
        return invoice;
    }

    @Override
    public JSONObject getCheckInfo(String invoiceCode, String invoiceNo, String dbName) {

        JSONObject checkInfo = null;

        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        String uuid = (StringUtils.isBlank(invoiceCode)?"":invoiceCode) + invoiceNo;
        TDxCheckRecordInvoice checkRecord = signCheckRecordService.getCheckInvoiceByUuid(uuid);

        if (checkRecord != null) {
            String invoiceType = checkRecord.getFpzl();
            TDxCheckVehicleSale checkVehicleSale = null;
            List<TdxCheckInvoiceDetail> checkRecordDetailList = null;
            if (FplxEnum.JDC.getFplxDm().equals(invoiceType) || FplxEnum.ESC.getFplxDm().equals(invoiceType)) {
                checkVehicleSale = signCheckVehicleSaleInvoiceService.getCheckInvoiceByUuid(uuid);
            } else {
                checkRecordDetailList = signCheckRecordInvoiceDetailService.getInvoiceDetailListByUuid(uuid);
            }
            checkInfo = this.createCheckInfo(checkRecord, checkVehicleSale, checkRecordDetailList);
        }
        return checkInfo;
    }

    /**
    * @Description 开放平台查验调用，保存查验记录
    * @param checkData
    * @param userid
    * @param dbName
    * @param scanId
     * @param company
     * @Return void
    * @Author wangruwei
    * @Date 2024/5/9 14:18
    **/
    @Override
    public void saveCheckInfoKfpt(OpenServiceSingleInvoiceCheckJxpt checkData, String userid, String dbName, String scanId, String company) {
        String resultCode = checkData.getCyjg();
        // 查验成功，结果一致
        /*
         * 查验成功状态码
         */
        String querySuccCode = "0001";
        if (querySuccCode.equals(resultCode)) {
            TDxCheckRecordInvoice checkRecord = getRecordInvoiceKfpt(checkData, userid,scanId,company);
            DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
            TDxCheckRecordInvoice checkRecordTmp = signCheckRecordService.getCheckInvoiceByUuid(checkRecord.getUuid());
            if (checkRecordTmp != null) {
                if (StringUtils.isNotBlank(checkRecordTmp.getCycs())) {
                    int cycs = Integer.parseInt(checkRecordTmp.getCycs()) + 1;
                    checkRecord.setCycs(String.valueOf(cycs));
                } else {
                    checkRecord.setCycs("1");
                }
            } else {
                checkRecord.setCycs("1");
            }
            TDxCheckVehicleSale checkVehicleSale = getVehicleSaleInvoiceKfpt(checkData);
            List<TdxCheckInvoiceDetail> checkRecordDetailList = getRecordInvoiceDetailKfpt(checkData);
            DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
            // 保存查验信息
            saveCheckMxxx(checkRecord, checkVehicleSale, checkRecordDetailList,checkData);
        }
    }

    /**
    * @Description 开放平台返回的数据组装 查验表明细实体
    * @param checkData
    * @Return java.util.List<com.dxhy.api.model.invoice.invoiceCheck.TdxCheckInvoiceDetail>
    * @Author wangruwei
    * @Date 2024/5/9 16:17
    **/
    private List<TdxCheckInvoiceDetail> getRecordInvoiceDetailKfpt(OpenServiceSingleInvoiceCheckJxpt checkData) {
        if (!CollectionUtils.isEmpty(checkData.getDetailList())) {
            List<OpenServiceSingleInvoiceCheck.SingleInvoiceDetail> detailList = checkData.getDetailList();
            if (detailList != null && detailList.size() > 0) {
                String invoiceType = FpzlChangeUtil.getInvoiceTypeKfptToJxpt(checkData.getFpzl(),checkData.getXdpbz());
                String invoiceCode = checkData.getFpdm();
                String invoiceNo = checkData.getFphm();
                String uuid = invoiceCode + invoiceNo;
                List<TdxCheckInvoiceDetail> invoiceDetailList = new LinkedList<>();
                for (int i = 0; i < detailList.size(); i++) {
                    OpenServiceSingleInvoiceCheck.SingleInvoiceDetail singleInvoiceDetail = detailList.get(i);
                    TdxCheckInvoiceDetail invoiceDetail = new TdxCheckInvoiceDetail();
                    invoiceDetail.setUuid(uuid);
                    invoiceDetail.setDetailNo(singleInvoiceDetail.getMxbh());
                    invoiceDetail.setGoodsName(singleInvoiceDetail.getHwmc());
                    invoiceDetail.setModel(singleInvoiceDetail.getGgxh());
                    invoiceDetail.setUnit(singleInvoiceDetail.getJldw());
                    invoiceDetail.setNum(singleInvoiceDetail.getSpsl());
                    invoiceDetail.setUnitPrice(singleInvoiceDetail.getBhsdj());
                    invoiceDetail.setDetailAmount(singleInvoiceDetail.getJe());
                    invoiceDetail.setTaxRate(singleInvoiceDetail.getSl());
                    //如果是0税率 0.000%，显示位 0%
                    if("0.000%".equals(invoiceDetail.getTaxRate())){
                        invoiceDetail.setTaxRate("0%");
                    }
                    invoiceDetail.setTaxAmount(singleInvoiceDetail.getSe());
                    if (FplxEnum.TXF.getFplxDm().equals(invoiceType)) {
                        // 通行费发票
                        invoiceDetail.setCph(singleInvoiceDetail.getCph());
                        invoiceDetail.setLx(singleInvoiceDetail.getLx());
                        invoiceDetail.setTxrqq(singleInvoiceDetail.getTxrqq());
                        invoiceDetail.setTxrqz(singleInvoiceDetail.getTxrqz());
                    }
                    invoiceDetailList.add(invoiceDetail);
                }
                return invoiceDetailList;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    /**
    * @Description 开放平台返回的数据组装查验表实体
    * @param checkData
    * @Return com.dxhy.api.model.sign.TDxCheckVehicleSale
    * @Author wangruwei
    * @Date 2024/5/9 15:45
    **/
    private TDxCheckVehicleSale getVehicleSaleInvoiceKfpt(OpenServiceSingleInvoiceCheckJxpt checkData) {
        String invoiceType = FpzlChangeUtil.getInvoiceTypeKfptToJxpt(checkData.getFpzl(),checkData.getXdpbz());
        if (FplxEnum.JDC.getFplxDm().equals(invoiceType)) {
            String invoiceCode = checkData.getFpdm();
            String invoiceNo = checkData.getFphm();
            String uuid = invoiceCode + invoiceNo;
            TDxCheckVehicleSale vehicleSaleInvoice = new TDxCheckVehicleSale();
            vehicleSaleInvoice.setUuid(uuid);
            vehicleSaleInvoice.setBuyerIdNum(checkData.getSfzh());
            vehicleSaleInvoice.setVehicleType(checkData.getCllx());
            vehicleSaleInvoice.setFactoryModel(checkData.getCpxh());
            vehicleSaleInvoice.setProductPlace(checkData.getCd());
            vehicleSaleInvoice.setCertificate(checkData.getHgzs());
            vehicleSaleInvoice.setCertificateImport(checkData.getJszmsh());
            vehicleSaleInvoice.setInspectionNum(checkData.getSjdh());
            vehicleSaleInvoice.setEngineNo(checkData.getFdjhm());
            vehicleSaleInvoice.setVehicleNo(checkData.getClsbh());
            vehicleSaleInvoice.setTaxBureauCode(checkData.getZgswjgdm());
            vehicleSaleInvoice.setTaxBureauName(checkData.getZgswjgmc());
            vehicleSaleInvoice.setTaxRecords(checkData.getWspzhm());
            vehicleSaleInvoice.setTonnage(checkData.getDw());
            vehicleSaleInvoice.setLimitPeople(checkData.getXcrs());
            vehicleSaleInvoice.setTaxRate(checkData.getSl());
            vehicleSaleInvoice.setPhone(checkData.getSmfdh());
            vehicleSaleInvoice.setSellerUnit(checkData.getSmfdwgr());
            vehicleSaleInvoice.setSellerUnitCode(checkData.getSmfdwdmsfzh());
            vehicleSaleInvoice.setSellerUnitAddress(checkData.getSmfdwgrzz());
            vehicleSaleInvoice.setSellerPhone(checkData.getSmfdh());
            vehicleSaleInvoice.setSellerKhzh(checkData.getKhzh());
            return vehicleSaleInvoice;
        }else if(FplxEnum.ESC.getFplxDm().equals(invoiceType)){
            String invoiceCode = checkData.getFpdm();
            String invoiceNo = checkData.getFphm();
            String uuid = invoiceCode + invoiceNo;

            TDxCheckVehicleSale vehicleSaleInvoice = new TDxCheckVehicleSale();
            vehicleSaleInvoice.setUuid(uuid);
            vehicleSaleInvoice.setPlateNo(checkData.getCpzh());
            vehicleSaleInvoice.setRegistrationNo(checkData.getDjzh());
            if(StringUtils.isNotBlank(checkData.getCllx())){
                vehicleSaleInvoice.setVehicleType(checkData.getCllx());
            }
            if(StringUtils.isNotBlank(checkData.getCpxh())){
                vehicleSaleInvoice.setFactoryModel(checkData.getCpxh());
            }
            if(StringUtils.isNotBlank(checkData.getClsbh())){
                vehicleSaleInvoice.setVehicleNo(checkData.getClsbh());
            }
            vehicleSaleInvoice.setPhone(checkData.getSmfdh());
            vehicleSaleInvoice.setBuyerIdNum(checkData.getMfdwdmsfzh());
            vehicleSaleInvoice.setGfPhone(checkData.getMfdh());

            vehicleSaleInvoice.setCarPrice(checkData.getCjhj());
            vehicleSaleInvoice.setVehicleAdminOffice(checkData.getZrdcgsmc());
            vehicleSaleInvoice.setBuyerUnit(checkData.getMfdwgr());
            vehicleSaleInvoice.setBuyerUnitCode(checkData.getMfdwdmsfzh());
            vehicleSaleInvoice.setBuyerUnitAddress(checkData.getMfdwgrdz());
            vehicleSaleInvoice.setBuyerPhone(checkData.getMfdh());
            vehicleSaleInvoice.setSellerUnit(checkData.getSmfdwgr());
            vehicleSaleInvoice.setSellerUnitCode(checkData.getSmfdwdmsfzh());
            vehicleSaleInvoice.setSellerUnitAddress(checkData.getSmfdwgrzz());
            vehicleSaleInvoice.setSellerPhone(checkData.getSmfdh());
            vehicleSaleInvoice.setAuctionUnit(checkData.getJypmdw());
            vehicleSaleInvoice.setAuctionAddress(checkData.getJypmdwdz());
            vehicleSaleInvoice.setAuctionTaxNo(checkData.getJypmdwzsbh());
            vehicleSaleInvoice.setAuctionBank(checkData.getKhyhjzh());
            vehicleSaleInvoice.setAuctionPhone(checkData.getJypmdwdh());
            vehicleSaleInvoice.setCarMarket(checkData.getEscsc());
            vehicleSaleInvoice.setCarMarketTaxNo(checkData.getEscscsbh());
            vehicleSaleInvoice.setCarMarketAddress(checkData.getEscscdz());
            vehicleSaleInvoice.setCarMarketBank(checkData.getEscsckhyhzh());
            vehicleSaleInvoice.setCarMarketPhone(checkData.getEscscdh());
            return vehicleSaleInvoice;
        } else {
            return null;
        }
    }

    /**
    * @Description 开放平台返回的数据组装查验表实体
    * @param checkData
    * @param userid
    * @param scanId
     * @param company
     * @Return com.dxhy.api.model.sign.TDxCheckRecordInvoice
    * @Author wangruwei
    * @Date 2024/5/9 14:26
    **/
    private TDxCheckRecordInvoice getRecordInvoiceKfpt(OpenServiceSingleInvoiceCheckJxpt checkData, String userid, String scanId, String company) {
        TDxCheckRecordInvoice invoice = new TDxCheckRecordInvoice();

        String invoiceType = FpzlChangeUtil.getInvoiceTypeKfptToJxpt(checkData.getFpzl(),checkData.getXdpbz());
        String invoiceCode = checkData.getFpdm();
        String invoiceNo = checkData.getFphm();
        String uuid = invoiceCode + invoiceNo;

        invoice.setCyjgCode(checkData.getCyjg());
        invoice.setCyjgTip(checkData.getCyjgxx());
        invoice.setCycs(checkData.getCycs());
        invoice.setFpzl(invoiceType);
        //如果号码是20位，拆分代码号码
        if(checkData.getFphm().length()==20){
            invoiceCode = checkData.getFphm().substring(0, 12);
            invoiceNo = checkData.getFphm().substring(12, 20);
        }
        if(StringUtils.isNotBlank(checkData.getEleInvoiceNo())){
            invoice.setEleInvoiceNo(checkData.getEleInvoiceNo());
        }

        invoice.setFpdm(invoiceCode);
        invoice.setFphm(invoiceNo);

        invoice.setFpzt(checkData.getFpzt());

        invoice.setUuid(uuid);
        invoice.setGfsh(checkData.getGmfsbh());
        invoice.setGfmc(checkData.getGmfmc());
        invoice.setGfdzdh(checkData.getGmfdzdh().replaceAll("null", ""));
        invoice.setGfyhzh(checkData.getGmfyhzh().replaceAll("null", ""));
        invoice.setXfdzdh(checkData.getXhfdzdh().replaceAll("null", ""));
        invoice.setXfyhzh(checkData.getXhfyhzh().replaceAll("null", ""));
        invoice.setXfmc(checkData.getXhfmc());
        invoice.setXfsh(checkData.getXhfsbh());
        invoice.setJe(checkData.getFpje());
        try {
            new BigDecimal(checkData.getFpse());
            invoice.setSe(checkData.getFpse());
        } catch (Exception e) {
            invoice.setSe("0");
        }
        invoice.setJshj(checkData.getJshj());
        if(FplxEnum.JDC.getFplxDm().equals(invoiceType)){
            invoice.setJe(checkData.getFpje());
            invoice.setJshj(checkData.getJshj());
            invoice.setGfsh(checkData.getMfdwdmsfzh());
            invoice.setGfdzdh((StringUtils.isBlank(checkData.getMfdwgrdz())?"":checkData.getMfdwgrdz())+(StringUtils.isBlank(checkData.getMfdh())?"":checkData.getMfdh()));
            invoice.setXfmc(checkData.getXhfmc());
            invoice.setXfdzdh((StringUtils.isBlank(checkData.getSmfdwgrzz())?"":checkData.getSmfdwgrzz())+(StringUtils.isBlank(checkData.getSmfdh())?"":checkData.getSmfdh()));
            invoice.setXfyhzh(checkData.getKhyh());
        }
        if(FplxEnum.ESC.getFplxDm().equals(invoiceType)){
            invoice.setJe(checkData.getCjhj());
            invoice.setJshj(checkData.getCjhj());
            invoice.setGfmc(checkData.getMfdwgr());
            invoice.setGfsh(checkData.getMfdwdmsfzh());
            invoice.setGfdzdh(StringUtils.isBlank(checkData.getMfdwgrdz())?"":checkData.getMfdwgrdz()+checkData.getMfdh());
            invoice.setXfmc(checkData.getXhfmc());
            invoice.setXfsh(checkData.getSmfdwdmsfzh());
            invoice.setXfdzdh(StringUtils.isBlank(checkData.getSmfdwgrzz())?"":checkData.getSmfdwgrzz()+checkData.getSmfdh());
            invoice.setXfyhzh(checkData.getKhyh());
        }
        invoice.setKprq(checkData.getKprq());
        invoice.setJqbh(checkData.getJqbh());
        invoice.setBz(checkData.getBz());
        invoice.setJym(checkData.getJym());
        invoice.setTxfbz(checkData.getTxfbz());
        invoice.setCreateDate(new Date());
        invoice.setCheckUser(userid);
        invoice.setScanId(scanId);
        invoice.setZfbz(checkData.getZfbz());
        invoice.setCompany(company);
        invoice.setPdfMd5(Long.valueOf(scanIdUtils.createSerialNum()));
        return invoice;
    }

    /**
     * 根据数据拼出需要返回页面的报文
     *
     * @param checkRecord
     *            查验表信息
     * @param checkVehicleSale
     *            查验机动车表信息
     * @param checkRecordDetailList
     *            查验明细表信息
     * @return 返回结果
     *
     */
    private JSONObject createCheckInfo(TDxCheckRecordInvoice checkRecord, TDxCheckVehicleSale checkVehicleSale,
        List<TdxCheckInvoiceDetail> checkRecordDetailList) {

        JSONObject checkInfo = new JSONObject();
        CheckRecordInvoiceVO checkRecordVo = this.convertToCheckRecordVo(checkRecord);

        // 取发票上的价税合计
        String totalAmount = checkRecord.getJshj();
        // 价税合计小写
        checkInfo.put("detailAmountTotal", totalAmount);
        // 价税合计大写
        checkInfo.put("stringTotalAmount",
            (StringUtils.isBlank(totalAmount)) ? "" : NumberToCN.number2CNMontrayUnit(new BigDecimal(totalAmount)));

        String invoiceType = checkRecord.getFpzl();
        String uuid = checkRecord.getUuid();
        //如果是161  283.返回对应数据
        if (FplxEnum.HKDZFP.getFplxDm().equals(invoiceType)){
            QueryWrapper<TdxTdysMhxcdEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uuid", uuid);
            TdxTdysMhxcdEntity tdxTdysMhxcd = tdxTdysMhxcdCoreDao.selectOne(queryWrapper);
            if(ObjectUtils.isNotEmpty(tdxTdysMhxcd)){
                tdxTdysMhxcd.setKprq(DateUtils.strToStrDate(tdxTdysMhxcd.getKprq()));
            }
            QueryWrapper<TdxTdysMhxcdMxEntity> queryMxWrapper = new QueryWrapper<>();
            queryMxWrapper.eq("tdys_id", uuid);
            List<TdxTdysMhxcdMxEntity> tdxTdysMhxcdList = tdxTdysMhxcdMxCoreDao.selectList(queryMxWrapper);
            checkRecordVo.setTdysMhxcd(tdxTdysMhxcd);
            checkRecordVo.setTdysMhxcdMx(tdxTdysMhxcdList);
            checkRecordVo.setInvoiceStatus(dictdetaServcice.queryDictName(DictConstant.FPCY_KFPT_MHXCD_KPZT, checkRecord.getFpzt()));
        }else if (FplxEnum.TLKP.getFplxDm().equals(invoiceType)){
            QueryWrapper<TdxTdysTldzkpEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uuid", uuid);
            TdxTdysTldzkpEntity tdxTdysTldzkp = tdxTdysTldzkpCoreDao.selectOne(queryWrapper);
            if(ObjectUtils.isNotEmpty(tdxTdysTldzkp)){
                if(StringUtils.isNotBlank(tdxTdysTldzkp.getKprq())){
                    tdxTdysTldzkp.setKprq(DateUtils.strToStrDate(tdxTdysTldzkp.getKprq()));
                }
                if(StringUtils.isNotBlank(tdxTdysTldzkp.getRq())){
                    tdxTdysTldzkp.setRq(DateUtils.strToStrChina(DateUtils.strToStrDate(tdxTdysTldzkp.getRq())));
                }
            }
            checkRecordVo.setTdysTldzkp(tdxTdysTldzkp);
        }

        // 发票票面信息
        checkInfo.put("invoiceSubjectList", JSON.toJSON(checkRecordVo));
        if (checkVehicleSale != null) {
            CheckVehicleSaleVO checkVehicleSaleVo = this.convertToVehicleSaleVo(checkVehicleSale,checkRecord);
            checkInfo.put("vehicle", JSON.toJSON(checkVehicleSaleVo));
        } else {
            checkInfo.put("vehicle", new JSONObject());
        }



        String se = "";
        if (checkRecordDetailList != null && checkRecordDetailList.size() > 0) {
            int count = checkRecordDetailList.size();
            JSONArray detail = new JSONArray(count);
            for (TdxCheckInvoiceDetail tDxCheckInvoiceDetail : checkRecordDetailList) {
                CheckRecordInvoiceDetailVO checkRecordDetailVo = this.convertToRecordDetailVo(tDxCheckInvoiceDetail);
                detail.add(JSON.toJSON(checkRecordDetailVo));
                // 明细框 税额合计
                try {
                    Double.parseDouble(checkRecordDetailVo.getTaxAmount());
                } catch (Exception e) {
                    se = checkRecordDetailVo.getTaxAmount();
                }
            }
            checkInfo.put("invoiceDetails", detail);
        } else {
            checkInfo.put("invoiceDetails", new JSONArray());
        }

        // 明细框 金额合计
        checkInfo.put("detailAmountSum", checkRecord.getJe());
        // 明细框 税额合计
        if (StringUtils.isNotBlank(se)) {
            checkInfo.put("taxAmountSum", se);
        } else {
            checkInfo.put("taxAmountSum", checkRecord.getSe());
        }
        return checkInfo;
    }

    /**
     *
     * 把数据库查询查验明细表对象转换为页面显示需要对象
     *
     * @param tDxCheckRecordInvoiceDetail
     *            请求参数
     * @return 返回结果
     *
     */
    private CheckRecordInvoiceDetailVO convertToRecordDetailVo(TdxCheckInvoiceDetail tDxCheckRecordInvoiceDetail) {
        CheckRecordInvoiceDetailVO checkRecordDetailVo = new CheckRecordInvoiceDetailVO();
        checkRecordDetailVo.setCph(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getCph()));
        checkRecordDetailVo.setDetailAmount(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getDetailAmount()));
        checkRecordDetailVo.setDetailNo(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getDetailNo()));
        checkRecordDetailVo.setGoodsName(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getGoodsName()));
        checkRecordDetailVo.setGoodsNum(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getGoodsNum()));
        checkRecordDetailVo.setLx(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getLx()));
        checkRecordDetailVo.setModel(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getModel()));
        checkRecordDetailVo.setNum(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getNum()));
        checkRecordDetailVo.setTaxAmount(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getTaxAmount()));
        if (StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getTaxRate()).matches("[0-9]{1,}")) {
            checkRecordDetailVo.setTaxRate(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getTaxRate()) + "%");
        } else {
            checkRecordDetailVo.setTaxRate(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getTaxRate()));
        }
        checkRecordDetailVo.setTxrqq(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getTxrqq()));
        checkRecordDetailVo.setTxrqz(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getTxrqz()));
        checkRecordDetailVo.setUnit(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getUnit()));
        checkRecordDetailVo.setUnitPrice(StringHelper.trimEnter(tDxCheckRecordInvoiceDetail.getUnitPrice()));
        return checkRecordDetailVo;
    }

    /**
     *
     * 把数据库查询查验机动车对象转换为页面显示需要对象
     *
     * @param checkVehicleSale
     *            请求参数
     * @return 返回结果
     *
     */
    private CheckVehicleSaleVO convertToVehicleSaleVo(TDxCheckVehicleSale checkVehicleSale,TDxCheckRecordInvoice checkRecord) {
        String invoiceType = checkRecord.getFpzl();
        CheckVehicleSaleVO checkVehicleSaleVo = new CheckVehicleSaleVO();
        checkVehicleSaleVo.setBuyerBank(StringHelper.trimEnter(checkVehicleSale.getBuyerBank()));
        checkVehicleSaleVo.setBuyerIdNum(StringHelper.trimEnter(checkVehicleSale.getBuyerIdNum()));
        checkVehicleSaleVo.setCertificate(StringHelper.trimEnter(checkVehicleSale.getCertificate()));
        checkVehicleSaleVo.setCertificateImport(StringHelper.trimEnter(checkVehicleSale.getCertificateImport()));
        checkVehicleSaleVo.setEngineNo(StringHelper.trimEnter(checkVehicleSale.getEngineNo()));
        checkVehicleSaleVo.setFactoryModel(StringHelper.trimEnter(checkVehicleSale.getFactoryModel()));
        checkVehicleSaleVo.setInspectionNum(StringHelper.trimEnter(checkVehicleSale.getInspectionNum()));
        checkVehicleSaleVo.setLimitPeople(StringHelper.trimEnter(checkVehicleSale.getLimitPeople()));
        checkVehicleSaleVo.setPhone(StringHelper.trimEnter(checkVehicleSale.getSellerPhone()));
        checkVehicleSaleVo.setProductPlace(StringHelper.trimEnter(checkVehicleSale.getProductPlace()));
        checkVehicleSaleVo.setTaxBureauCode(StringHelper.trimEnter(checkVehicleSale.getTaxBureauCode()));
        checkVehicleSaleVo.setTaxBureauName(StringHelper.trimEnter(checkVehicleSale.getTaxBureauName()));
        checkVehicleSaleVo.setTaxRate(StringHelper.trimEnter(checkVehicleSale.getTaxRate()));
        checkVehicleSaleVo.setTaxRecords(StringHelper.trimEnter(checkVehicleSale.getTaxRecords()));
        checkVehicleSaleVo.setTonnage(StringHelper.trimEnter(checkVehicleSale.getTonnage()));
        checkVehicleSaleVo.setVehicleNo(StringHelper.trimEnter(checkVehicleSale.getVehicleNo()));
        checkVehicleSaleVo.setVehicleType(StringHelper.trimEnter(checkVehicleSale.getVehicleType()));
        checkVehicleSaleVo.setXfBankAndNo(StringHelper.trimEnter(checkRecord.getXfyhzh()));
        checkVehicleSaleVo.setXfAddressAndPhone(StringHelper.trimEnter(checkVehicleSale.getSellerUnitAddress()));
        checkVehicleSaleVo.setAccount(StringHelper.trimEnter(checkVehicleSale.getSellerKhzh()));
        //二手车相关信息
        if(FplxEnum.ESC.getFplxDm().equals(invoiceType)){
            checkVehicleSaleVo.setLicensePlate(StringHelper.trimEnter(checkVehicleSale.getPlateNo()));
            checkVehicleSaleVo.setRegistrationNo(StringHelper.trimEnter(checkVehicleSale.getRegistrationNo()));
            checkVehicleSaleVo.setVehicleAdminOffice(StringHelper.trimEnter(checkVehicleSale.getVehicleAdminOffice()));
            checkVehicleSaleVo.setAuctionUnit(StringHelper.trimEnter(checkVehicleSale.getAuctionUnit()));
            checkVehicleSaleVo.setAuctionAddress(StringHelper.trimEnter(checkVehicleSale.getAuctionAddress()));
            checkVehicleSaleVo.setAuctionTaxNo(StringHelper.trimEnter(checkVehicleSale.getAuctionTaxNo()));
            checkVehicleSaleVo.setAuctionBank(StringHelper.trimEnter(checkVehicleSale.getAuctionBank()));
            checkVehicleSaleVo.setAuctionPhone(StringHelper.trimEnter(checkVehicleSale.getAuctionPhone()));
            checkVehicleSaleVo.setCarMarket(StringHelper.trimEnter(checkVehicleSale.getCarMarket()));
            checkVehicleSaleVo.setCarMarketTaxNo(StringHelper.trimEnter(checkVehicleSale.getCarMarketTaxNo()));
            checkVehicleSaleVo.setCarMarketAddress(StringHelper.trimEnter(checkVehicleSale.getCarMarketAddress()));
            checkVehicleSaleVo.setCarMarketBank(StringHelper.trimEnter(checkVehicleSale.getCarMarketBank()));
            checkVehicleSaleVo.setCarMarketPhone(StringHelper.trimEnter(checkVehicleSale.getCarMarketPhone()));
            checkVehicleSaleVo.setCarPrice(StringHelper.trimEnter(checkVehicleSale.getCarPrice()));
            checkVehicleSaleVo.setBuyerUnitOrIndividual(StringHelper.trimEnter(checkVehicleSale.getBuyerUnit()));
            checkVehicleSaleVo.setBuyerUnitCodeOrIdNo(StringHelper.trimEnter(checkVehicleSale.getBuyerUnitCode()));
            checkVehicleSaleVo.setBuyerUnitOrIndividualAddress(StringHelper.trimEnter(checkVehicleSale.getBuyerUnitAddress()));
            checkVehicleSaleVo.setBuyerPhone(StringHelper.trimEnter(checkVehicleSale.getBuyerPhone()));
            checkVehicleSaleVo.setSellerUnitOrIndividual(StringHelper.trimEnter(checkVehicleSale.getSellerUnit()));
            checkVehicleSaleVo.setSellerUnitCodeOrIdNo(StringHelper.trimEnter(checkVehicleSale.getSellerUnitCode()));
            checkVehicleSaleVo.setSellerUnitOrIndividualAddress(StringHelper.trimEnter(checkVehicleSale.getSellerUnitAddress()));
            checkVehicleSaleVo.setSellerPhone(StringHelper.trimEnter(checkVehicleSale.getSellerPhone()));
        }
        return checkVehicleSaleVo;
    }

    /**
     * 把数据库查询查验记录对象转换为页面显示需要对象
     *
     * @param checkRecord
     *            请求参数
     * @return 返回结果
     *
     */
    private CheckRecordInvoiceVO convertToCheckRecordVo(TDxCheckRecordInvoice checkRecord) {
        String fpzlMc = this.getFpzlMc(checkRecord.getFpzl());
        CheckRecordInvoiceVO checkRecordVo = new CheckRecordInvoiceVO();
        checkRecordVo.setRemark(StringHelper.trimEnter(checkRecord.getBz()));
        checkRecordVo.setCycs(StringHelper.trimEnter(checkRecord.getCycs()));
        checkRecordVo.setCyjgCode(StringHelper.trimEnter(checkRecord.getCyjgCode()));
        checkRecordVo.setCyjgTip(StringHelper.trimEnter(checkRecord.getCyjgTip()));
        checkRecordVo.setCyrmc(StringHelper.trimEnter(checkRecord.getCyrmc()));
        checkRecordVo.setCyrsbh(StringHelper.trimEnter(checkRecord.getCyrsbh()));
        checkRecordVo.setCzch(StringHelper.trimEnter(checkRecord.getCzch()));
        checkRecordVo.setFhr(StringHelper.trimEnter(checkRecord.getFhr()));
        checkRecordVo.setFhrmc(StringHelper.trimEnter(checkRecord.getFhrmc()));
        checkRecordVo.setFhrsbh(StringHelper.trimEnter(checkRecord.getFhrsbh()));
        checkRecordVo.setInvoiceCode(StringHelper.trimEnter(checkRecord.getFpdm()));
        checkRecordVo.setInvoiceNo(StringHelper.trimEnter(checkRecord.getFphm()));
        checkRecordVo.setEleInvoiceNo(StringHelper.trimEnter(checkRecord.getEleInvoiceNo()));
        checkRecordVo.setInvoiceType(StringHelper.trimEnter(checkRecord.getFpzl()));
        checkRecordVo.setInvoiceString(StringHelper.trimEnter(fpzlMc));
        checkRecordVo.setGfAddressAndPhone(StringHelper.trimEnter(checkRecord.getGfdzdh()));
        checkRecordVo.setGfName(StringHelper.trimEnter(checkRecord.getGfmc()));
        checkRecordVo.setGfTaxNo(StringHelper.trimEnter(checkRecord.getGfsh()));
        checkRecordVo.setGfBankAndNo(StringHelper.trimEnter(checkRecord.getGfyhzh()));
        checkRecordVo.setInvoiceAmount(StringHelper.trimEnter(checkRecord.getJe()));
        checkRecordVo.setJqbh(StringHelper.trimEnter(checkRecord.getJqbh()));
        checkRecordVo.setTotalAmount(StringHelper.trimEnter(checkRecord.getJshj()));
        checkRecordVo.setCheckCode(StringHelper.trimEnter(checkRecord.getJym()));
        checkRecordVo.setKpr(StringHelper.trimEnter(checkRecord.getKpr()));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        if (StringUtils.isNotBlank(checkRecord.getKprq())) {
            String invoiceDate = checkRecord.getKprq().replaceAll("-", "").replaceAll("/", "");
            try {
                checkRecordVo.setInvoiceDate(sdf.format(new SimpleDateFormat("yyyyMMdd").parse(invoiceDate)));
            } catch (Exception e) {
                checkRecordVo.setInvoiceDate(invoiceDate);
            }
        } else {
            checkRecordVo.setInvoiceDate("");
        }
        checkRecordVo.setLslbz(StringHelper.trimEnter(checkRecord.getLslbz()));
        checkRecordVo.setLzfpdm(StringHelper.trimEnter(checkRecord.getLzfpdm()));
        checkRecordVo.setLzfphm(StringHelper.trimEnter(checkRecord.getLzfphm()));
        checkRecordVo.setQyd(StringHelper.trimEnter(checkRecord.getQyd()));
        checkRecordVo.setTaxAmount(StringHelper.trimEnter(checkRecord.getSe()));
        checkRecordVo.setSfzhm(StringHelper.trimEnter(checkRecord.getSfzhm()));
        checkRecordVo.setShrmc(StringHelper.trimEnter(checkRecord.getShrmc()));
        checkRecordVo.setShrsbh(StringHelper.trimEnter(checkRecord.getShrsbh()));
        checkRecordVo.setSkr(StringHelper.trimEnter(checkRecord.getSkr()));
        checkRecordVo.setSl(StringHelper.trimEnter(checkRecord.getSl()));
        checkRecordVo.setSpfmc(StringHelper.trimEnter(checkRecord.getSpfmc()));
        checkRecordVo.setSpfsbh(StringHelper.trimEnter(checkRecord.getSpfsbh()));
        checkRecordVo.setTxfbz(StringHelper.trimEnter(checkRecord.getTxfbz()));
        checkRecordVo.setXfAddressAndPhone(StringHelper.trimEnter(checkRecord.getXfdzdh()));
        checkRecordVo.setXfName(StringHelper.trimEnter(checkRecord.getXfmc()));
        checkRecordVo.setXfTaxNo(StringHelper.trimEnter(checkRecord.getXfsh()));
        checkRecordVo.setXfBankAndNo(StringHelper.trimEnter(checkRecord.getXfyhzh()));
        checkRecordVo.setYshwxx(StringHelper.trimEnter(checkRecord.getYshwxx()));
        checkRecordVo.setZfbz(StringHelper.trimEnter(checkRecord.getZfbz()));
        return checkRecordVo;
    }

    private String getFpzlMc(String invoiceType) {
        String fplx = "";
        if (FplxEnum.DZP.getFplxDm().equals(invoiceType)) {
            fplx = FplxEnum.DZP.getFplxMc(FplxEnum.DZP.getIndex());
        } else if (FplxEnum.JDC.getFplxDm().equals(invoiceType)) {
            fplx = FplxEnum.JDC.getFplxMc(FplxEnum.JDC.getIndex());
        } else if (FplxEnum.JSP.getFplxDm().equals(invoiceType)) {
            fplx = FplxEnum.JSP.getFplxMc(FplxEnum.JSP.getIndex());
        } else if (FplxEnum.PP.getFplxDm().equals(invoiceType)) {
            fplx = FplxEnum.PP.getFplxMc(FplxEnum.PP.getIndex());
        } else if (FplxEnum.TXF.getFplxDm().equals(invoiceType)) {
            fplx = FplxEnum.TXF.getFplxMc(FplxEnum.TXF.getIndex());
        } else if (FplxEnum.ZP.getFplxDm().equals(invoiceType)) {
            fplx = FplxEnum.ZP.getFplxMc(FplxEnum.ZP.getIndex());
        }
        return fplx;
    }

}
