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

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.dxhy.sign.entity.TDxVehicleSaleInvoice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
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.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.FplxEnum;
import com.dxhy.common.util.NumberToCN;
import com.dxhy.common.util.ScanIdUtils;
import com.dxhy.common.util.StringHelper;
import com.dxhy.sign.entity.TDxCheckInvoiceDetail;
import com.dxhy.sign.entity.TDxCheckRecordInvoice;
import com.dxhy.sign.entity.TDxCheckVehicleSale;
import com.dxhy.sign.model.CheckRecordInvoiceDetailVO;
import com.dxhy.sign.model.CheckRecordInvoiceVO;
import com.dxhy.sign.model.CheckVehicleSaleVO;
import com.dxhy.sign.service.singletable.SignCheckRecordInvoiceDetailService;
import com.dxhy.sign.service.singletable.SignCheckRecordService;
import com.dxhy.sign.service.singletable.SignCheckVehicleSaleInvoiceService;
import com.dxhy.sign.service.singletable.SignSaveCheckInfoService;

import lombok.extern.slf4j.Slf4j;

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

public class SignSaveCheckInfoServiceImpl implements SignSaveCheckInfoService {

    @Resource
    private SignCheckRecordService signCheckRecordService;

    @Resource
    private SignCheckRecordInvoiceDetailService signCheckRecordInvoiceDetailService;

    @Resource
    private SignCheckVehicleSaleInvoiceService signCheckVehicleSaleInvoiceService;

    @Resource
    private ScanIdUtils scanIdUtils;

    @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);
        }
    }

    /**
     * 保存查验信息
     *
     * @param checkRecord
     *            请求参数
     * @param checkVehicleSale
     *            请求参数
     * @param checkRecordDetailList
     *            请求参数
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveCheckMxxx(TDxCheckRecordInvoice checkRecord, TDxCheckVehicleSale checkVehicleSale,
        List<TDxCheckInvoiceDetail> checkRecordDetailList) {
        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 (checkRecordDetailList != null && checkRecordDetailList.size() > 0) {
            // 删除重复数据，防止重复的操作
            QueryWrapper<TDxCheckInvoiceDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uuid", uuid);
            signCheckRecordInvoiceDetailService.remove(queryWrapper);
            // 保存数据
            /*
             * 批量操作的大小
             */
            int batchSize = 500;
            signCheckRecordInvoiceDetailService.saveBatch(checkRecordDetailList, batchSize);
        }
    }

    @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;
    }

    /**
     * 根据数据拼出需要返回页面的报文
     *
     * @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)));

        // 发票票面信息
        checkInfo.put("invoiceSubjectList", JSON.toJSON(checkRecordVo));
        if (checkVehicleSale != null) {
            CheckVehicleSaleVO checkVehicleSaleVo = this.convertToVehicleSaleVo(checkVehicleSale,checkRecord.getFpzl());
            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,String invoiceType) {
        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.getPhone()));
        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()));
        //二手车相关信息
        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.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;
    }

}
