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

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.core.dao.CheckTaskDao;
import com.dxhy.core.dao.ConfirmDao;
import com.dxhy.core.job.dao.TDxRecordInvoiceDetailJobDao;
import com.dxhy.core.job.dao.TDxRecordInvoiceStatisticsJobDao;
import com.dxhy.core.job.dao.TDxVehicleSaleInvoiceDao;
import com.dxhy.core.job.entity.InvoiceCheckVehicleDetailModel;
import com.dxhy.core.job.entity.TAcOrg;
import com.dxhy.core.job.entity.TDxRecordInvoiceStatisticsJobEntity;
import com.dxhy.core.job.pojo.*;
import com.dxhy.core.job.service.CheckTaskService;
import com.dxhy.core.job.service.TDxRecordInvoiceDetailJobService;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 功能描述
 *
 * @author 陈刚扬
 * @date 2020/10/28
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong"})
@Service("checkTaskService")
@Slf4j
public class CheckTaskServiceImpl implements CheckTaskService {

    @Value("${jxjk.checkUrl}")
    private String collectUrl;
    @Value("${jxjk.checkInvoice}")
    private String checkInvoice;
    @Value("${jxjk.enterpriseCode}")
    private String enterpriseCode;
    @Value("${jxjk.version}")
    private String version;
    @Value("${jxjk.appId}")
    private String appId;
    @Value("${jxjk.checkTax}")
    private String checkTaxNo;
    @Value("${jxjk.secretId}")
    private String secretId;
    @Value("${jxjk.secretKey}")
    private String secretKey;
    @Value("${jxjk.enterprisePermissionFlag}")
    public boolean enterprisePermissionFlag;
 
    @Resource
    private CheckTaskDao checkTaskDao;
    @Resource
    private ConfirmDao confirmDao;
    @Resource
    private TDxRecordInvoiceDetailJobDao tDxRecordInvoiceDetailDao;
    @Resource
    private TDxRecordInvoiceStatisticsJobDao tDxRecordInvoiceStatisticsDao;
    @Resource
    private TDxVehicleSaleInvoiceDao tDxVehicleSaleInvoiceDao;
    @Resource
    private TDxRecordInvoiceDetailJobService tDxRecordInvoiceDetailJobService;

    @Override
    public List<CheckNoDetailInvoice> getNoDetailInvoice() {
        return checkTaskDao.getNoDetailInvoice();
    }

    @Override
    public List<CheckNoDetailInvoice> getNoDetailInvoiceOld() {
        return checkTaskDao.getNoDetailInvoiceOld();
    }

    private String taxRate;

    /**
     * 查验
     *
     * @author 陈刚扬
     * @date 2020/10/28
     * @param checkNoDetailInvoice
     * @param db
     * @return void
     */
    @Override
    public void checkOne(CheckNoDetailInvoice checkNoDetailInvoice, String db) {
        // 构建请求数据
        final RequestData requestData = buildRequestData(checkNoDetailInvoice);
        // 调用接口验证
        final ResponseInvoice responseInvoice = sendRequest(requestData);
        // 查验成功
        DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE);
        if (responseInvoice != null) {
            if ("0001".equals(responseInvoice.getResultCode())) {
                List<InvoiceDetail> detailList = responseInvoice.getDetailList();
                // 如果不为机动车统计销售发票
                if (!"03".equals(responseInvoice.getInvoiceType())) {
                    // 如果明细不为空则保存明细,保存明细统计，更新主表
                    final List<InvoiceDetailInfo> invoiceDetailInfoList = buildDetailList(responseInvoice);
                    if (detailList != null && detailList.size() > 0) {
                        // 保存明细表 之前先删除
                        InvoiceDetailInfo invoiceDetail = invoiceDetailInfoList.get(0);
                        deleteDetail(invoiceDetail, db);
                    }
                    // 插入明细数据
                    insertDetail(db, invoiceDetailInfoList);
                    // 构建统计表发票明细
                    DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE);
                    final List<TDxRecordInvoiceStatisticsJobEntity> recordInvoiceStatisticsList =
                        buildRecordInvoiceStatisticsList(responseInvoice);
                    if (recordInvoiceStatisticsList.size() > 0) {
                        // 保存抵账统计明细
                        TDxRecordInvoiceStatisticsJobEntity recordInvoiceStatistics =
                            recordInvoiceStatisticsList.get(0);
                        tDxRecordInvoiceDetailDao.delDetailTaxRate(recordInvoiceStatistics.getInvoiceCode(),
                            recordInvoiceStatistics.getInvoiceNo());

                        for (TDxRecordInvoiceStatisticsJobEntity invoiceStatistics : recordInvoiceStatisticsList) {
                            // 需要用mybatisPlus转换一下插入数据库
                            tDxRecordInvoiceStatisticsDao.insert(invoiceStatistics);
                        }

                    }

                    // 构建主表实体
                    InvoiceCollectionInfo invoiceCollectionInfo =
                        buildInvoiceCollectionInfo(responseInvoice, checkNoDetailInvoice.getBuyerTaxNo(),taxRate);

                    int countdetail = getDetailCount(invoiceCollectionInfo, db);
                    // 更新主表
                    if (countdetail > 0) {
                        invoiceCollectionInfo.setDetailYesorno("1");
                    } else {
                        invoiceCollectionInfo.setDetailYesorno("0");
                    }
                    DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE);
                    checkTaskDao.updateRecordInvoice(invoiceCollectionInfo);
                } else {
                    // 机动车统一销售发票 构建机动车统一销售发票明细
                    List<InvoiceCheckVehicleDetailModel> vehicleDetailList = buildVehicleDetailList(responseInvoice);
                    if (vehicleDetailList.size() > 0) {
                        InvoiceCheckVehicleDetailModel invoiceCheckVehicleDetail = vehicleDetailList.get(0);
                        tDxRecordInvoiceDetailDao.delDetailJd(invoiceCheckVehicleDetail.getUuid());

                        for (InvoiceCheckVehicleDetailModel invoiceCheckVehicleDetailModel : vehicleDetailList) {
                            tDxVehicleSaleInvoiceDao.insert(invoiceCheckVehicleDetailModel);
                        }
                    }
                    tDxRecordInvoiceDetailDao.delDetailTaxRate(responseInvoice.getInvoiceCode(),
                        responseInvoice.getInvoiceNo());
                    // 保存抵账统计明细
                    // 设置税额统计表字段值
                    TDxRecordInvoiceStatisticsJobEntity tDxRecordInvoiceStatistics =
                        new TDxRecordInvoiceStatisticsJobEntity();
                    tDxRecordInvoiceStatistics.setInvoiceCode(responseInvoice.getInvoiceCode());
                    tDxRecordInvoiceStatistics.setInvoiceNo(responseInvoice.getInvoiceNo());
                    tDxRecordInvoiceStatistics.setDetailAmount(new BigDecimal(responseInvoice.getInvoiceAmount()));
                    if (responseInvoice.getTaxRate().contains("%")) {
                        responseInvoice.setTaxRate(responseInvoice.getTaxRate().replace("%", ""));
                    }
                    tDxRecordInvoiceStatistics.setTaxRate(new BigDecimal(responseInvoice.getTaxRate()));
                    try {
                        tDxRecordInvoiceStatistics.setTaxAmount(new BigDecimal(responseInvoice.getTaxAmount()));

                    } catch (Exception e) {
                        tDxRecordInvoiceStatistics.setTaxAmount(new BigDecimal("0"));
                    }
                    tDxRecordInvoiceStatistics.setTotalAmount(new BigDecimal(responseInvoice.getTotalAmount()));
                    tDxRecordInvoiceStatisticsDao.insert(tDxRecordInvoiceStatistics);

                    // 构建主表实体
                    InvoiceCollectionInfo invoiceCollectionInfo =
                        buildInvoiceCollectionInfo(responseInvoice, checkNoDetailInvoice.getBuyerTaxNo(),"");
                    DynamicContextHolder.push(db + DbConstant.BUSINESS_READ);
                    int countdetail = tDxRecordInvoiceDetailDao
                        .getDetailJd(invoiceCollectionInfo.getInvoiceCode() + invoiceCollectionInfo.getInvoiceNo());
                    // 更新主表
                    if (countdetail > 0) {
                        invoiceCollectionInfo.setDetailYesorno("1");
                    } else {
                        invoiceCollectionInfo.setDetailYesorno("0");
                    }
                    DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE);
                    // 更新主表
                    checkTaskDao.updateRecordInvoice(invoiceCollectionInfo);
                }
            }
        }

    }

    private int getDetailCount(InvoiceCollectionInfo invoiceCollectionInfo, String db) {
        DynamicContextHolder.push(db + DbConstant.DETAIL_WRITE);
        return tDxRecordInvoiceDetailDao
            .getDetailCount(invoiceCollectionInfo.getUuid());
    }

    private void insertDetail(String db, List<InvoiceDetailInfo> invoiceDetailInfoList) {
        if (invoiceDetailInfoList != null && invoiceDetailInfoList.size() > 0) {
            DynamicContextHolder.push(db + DbConstant.DETAIL_WRITE);
            tDxRecordInvoiceDetailJobService.saveBatch(invoiceDetailInfoList);
        }
    }

    private void deleteDetail(InvoiceDetailInfo invoiceDetail, String db) {
        DynamicContextHolder.push(db + DbConstant.DETAIL_WRITE);
        checkTaskDao.deleteDetail(invoiceDetail);
    }

    private RequestData buildRequestData(CheckNoDetailInvoice checkNoDetailInvoice) {
        // 请求数据
        final RequestData requestData = new RequestData();
        // 购方税号
        requestData.setBuyerTaxNo(checkNoDetailInvoice.getBuyerTaxNo());
        // 发票类型
        requestData.setInvoiceType(checkNoDetailInvoice.getInvoiceType());

        if("31".equals(checkNoDetailInvoice.getInvoiceType())
                || "32".equals(checkNoDetailInvoice.getInvoiceType())
                || "185".equals(checkNoDetailInvoice.getInvoiceType())
                || "186".equals(checkNoDetailInvoice.getInvoiceType())){
            // 发票代码
            requestData.setInvoiceCode("");
            // 发票号码
            requestData.setInvoiceNo(checkNoDetailInvoice.getInvoiceCode() + checkNoDetailInvoice.getInvoiceNo());
            // 金额
            requestData.setInvoiceAmount(checkNoDetailInvoice.getTotalAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString());

        }else {
            // 发票代码
            requestData.setInvoiceCode(checkNoDetailInvoice.getInvoiceCode());
            // 发票号码
            requestData.setInvoiceNo(checkNoDetailInvoice.getInvoiceNo());
            // 金额
            requestData.setInvoiceAmount(checkNoDetailInvoice.getInvoiceAmount().setScale(2, BigDecimal.ROUND_HALF_UP).toString());

        }
        // 开票时间
        String sdate = (new SimpleDateFormat("yyyyMMdd")).format(checkNoDetailInvoice.getInvoiceDate());
        requestData.setInvoiceDate(sdate);
        // 验证码
        requestData.setCheckCode(checkNoDetailInvoice.getCheckCode());

        return requestData;
    }

    public ResponseInvoice sendRequest(RequestData requestData) {
        String taxno = requestData.getBuyerTaxNo();
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        //区分secretId和secretKey是从配置文件来还是数据库读取
        if(enterprisePermissionFlag){
            // 通过税号获取id和key
            TAcOrg org = confirmDao.findIdAndKeyByTaxno(taxno);
            if(org!=null){
                secretId= org.getAceId();
                secretKey= org.getAceKey();
            }
        }
        // 定义返回值
        ResponseInvoice responseInvoice = null;

        // 请求通用头
        final GlobalInfo globalInfo = new GlobalInfo();
        // 请求通用
        final Authorize authorize = new Authorize();

        final BaseQueJson bqj = new BaseQueJson();

        log.info("-定时查验封装参数-");
        try {
            // 封装参数
            globalInfo.setAppId(appId);
            globalInfo.setVersion(version);
            String dataExchangeId = getDataExchangeId();
            globalInfo.setDataExchangeId(dataExchangeId);
            globalInfo.setInterfaceCode(checkInvoice);
            globalInfo.setEnterpriseCode(enterpriseCode);

            authorize.setAppSecId(secretId);

            String dataStr = JSONObject.toJSONString(requestData);

            log.info("data：{}", dataStr);
            String base64Data = Base64.encode(dataStr);

            String appSec = makeAppSec(base64Data, dataExchangeId, checkInvoice,secretId, secretKey);
            authorize.setAppSec(appSec);
            bqj.setAuthorize(authorize);
            bqj.setGlobalInfo(globalInfo);

            bqj.setData(base64Data);
            String collectDataStr = JSONObject.toJSONString(bqj);

            log.info("url:{}", collectUrl);
            log.info("请求参数：{}", collectDataStr);
            log.info("发送请求--");
            final String responseJson = HttpUtil.post(collectUrl, collectDataStr, 600000);

            log.info("返回报文:{}", responseJson);

            final BaseResJson baseResJson = JSONObject.parseObject(responseJson, BaseResJson.class);

            if ("0000".equals(baseResJson.getReturnStateInfo().getReturnCode())) {
                String dataJson = Base64.decodeStr(baseResJson.getData());

                log.info("data体：{}", dataJson);
                responseInvoice = JSONObject.parseObject(dataJson, ResponseInvoice.class);
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("手动验证失败:{}", e.getMessage());
        }
        return responseInvoice;
    }

    /**
     * 生成交换流水号
     *
     * @return
     */
    private String getDataExchangeId() {
        // enterpriseCode+8 位日期
        // (YYYYMMDD)+9 位序列号
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        StringBuilder sb = new StringBuilder();
        Random rand = new Random();
        for (int i = 0; i < 9; i++) {
            sb.append(rand.nextInt(10));
        }
        String data = sb.toString();
        return enterpriseCode + simpleDateFormat.format(new Date()) + data;
    }

    /**
     * 加密方法
     */
    public String makeAppSec(String data, String dataExchangeId, String interfaceCode, String secretId,
        String secretKey) {
        String srcStr =
            "POST/rest/invoice/dii?authorize={\"appSecId\":\"" + secretId + "\"}&globalInfo={\"appId\":\"" + appId
                + "\",\"version\":\"" + version + "\",\"interfaceCode\":\"" + interfaceCode + "\",\"enterpriseCode\":\""
                + enterpriseCode + "\",\"dataExchangeId\":\"" + dataExchangeId + "\"}&" + "data=" + data + "";
        log.info("待加密的请求：" + srcStr);
        SecretKeySpec keySpec = null;
        keySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "HmacSHA1");
        Mac mac = null;
        try {
            mac = Mac.getInstance("HmacSHA1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        try {
            mac.init(keySpec);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        byte[] singBytes = new byte[0];
        singBytes = mac.doFinal(srcStr.getBytes(StandardCharsets.UTF_8));
        String appSec = Base64.encode(singBytes);
        log.info("interfaceCode接口生成的加密串=appSec--");
        return appSec;
    }

    /**
     * 解析明細
     *
     * @param responseInvoice
     * @return
     */
    private List<InvoiceDetailInfo> buildDetailList(ResponseInvoice responseInvoice) {
        taxRate = "";
        // 构建返回值
        final List<InvoiceDetailInfo> invoiceDetailInfoList = new ArrayList<>();

        // 明细
        final List<InvoiceDetail> invoiceDetailList = responseInvoice.getDetailList();

        invoiceDetailList.forEach(invoiceDetail -> {
            final InvoiceDetailInfo invoiceDetailInfo = new InvoiceDetailInfo();
            // 税额
            invoiceDetailInfo.setTaxAmount(invoiceDetail.getTaxAmount());
            // 货物或应税劳务名称
            invoiceDetailInfo.setGoodsName(invoiceDetail.getGoodsName());
            // 发票号码
            invoiceDetailInfo.setInvoiceNo(responseInvoice.getInvoiceNo());
            // 发票代码
            invoiceDetailInfo.setInvoiceCode(responseInvoice.getInvoiceCode());
            // 数量
            invoiceDetailInfo.setNum(invoiceDetail.getNum());
            // 明细序号
            invoiceDetailInfo.setDetailNo(invoiceDetail.getDetailNo());
            // 单价
            invoiceDetailInfo.setUnitPrice(invoiceDetail.getUnitPrice());
            // 类型
            invoiceDetailInfo.setLx(invoiceDetail.getLx());
            // uuid唯一标识(发票代码+发票号码)
            invoiceDetailInfo.setUuid(responseInvoice.getInvoiceCode() + responseInvoice.getInvoiceNo());
            // 通行日期起
            invoiceDetailInfo.setTxrqq(invoiceDetail.getTxrqq());
            // 通行日期止
            invoiceDetailInfo.setTxrqz(invoiceDetail.getTxrqz());
            // 税率
            invoiceDetailInfo.setTaxRate(invoiceDetail.getTaxRate());

            if(StringUtils.isNotEmpty(invoiceDetail.getTaxRate()) && !taxRate.contains(invoiceDetail.getTaxRate())) {
                taxRate = taxRate + invoiceDetail.getTaxRate();
            }
            //山能项目 增加税码字段
            if(invoiceDetail.getTaxRate() != null){
                invoiceDetailInfo.setTaxCode(getZSM(invoiceDetail.getTaxRate()));
            }
            // 单位
            invoiceDetailInfo.setUnit(invoiceDetail.getUnit());
            // 金额
            invoiceDetailInfo.setDetailAmount(invoiceDetail.getDetailAmount());
            // 规格型号
            invoiceDetailInfo.setModel(invoiceDetail.getSpecificationModel());
            // 车牌号
            invoiceDetailInfo.setCph(invoiceDetail.getCph());
            // 放入集合
            invoiceDetailInfoList.add(invoiceDetailInfo);
        });
        return invoiceDetailInfoList;
    }

    private String getZSM(String taxRate) {

        String sm = "";

        switch (taxRate) {
            // 发票采集
            case "0":
                sm = "J0";
                break;
            case "不征税":
                sm = "J0";
                break;
            case "0.00":
                sm = "J0";
                break;
            case "0.000":
                sm = "J0";
                break;
            case "免税":
                sm = "J0";
                break;
            case "0.16":
                sm = "J1";
                break;
            case "0.13":
                sm = "J2";
                break;
            case "0.10":
                sm = "J3";
                break;
            case "0.09":
                sm = "J4";
                break;
            case "0.06":
                sm = "J5";
                break;
            case "0.05":
                sm = "J6";
                break;
            case "0.03":
                sm = "J7";
                break;
            case "0.02":
                sm = "J8";
                break;
            case "0.01":
                sm = "J9";
                break;
            case "0.17":
                sm = "JC";
                break;
            case "16":
                sm = "J1";
                break;
            case "13":
                sm = "J2";
                break;
            case "10":
                sm = "J3";
                break;
            case "9":
                sm = "J4";
                break;
            case "6":
                sm = "J5";
                break;
            case "5":
                sm = "J6";
                break;
            case "3":
                sm = "J7";
                break;
            case "2":
                sm = "J8";
                break;
            case "1.5":
                sm = "JA";
                break;
            case "1":
                sm = "J9";
                break;
            case "17":
                sm = "JC";
                break;
            default:
                sm = "";
                break;

        }
        return sm;
    }

    /**
     * 构建统计实体集
     *
     * @param responseInvoice
     *            响应实体
     * @return 抵账统计实体
     */
    @SuppressWarnings("AlibabaCollectionInitShouldAssignCapacity")
    private List<TDxRecordInvoiceStatisticsJobEntity>
        buildRecordInvoiceStatisticsList(ResponseInvoice responseInvoice) {
        // 定义返回值
        final List<TDxRecordInvoiceStatisticsJobEntity> recordInvoiceStatisticsList = new ArrayList<>();

        // 明细
        List<InvoiceDetail> invoiceDetailList;

        // 如果为机动车统计销售发票
        if ("03".equals(responseInvoice.getInvoiceType())) {
            invoiceDetailList = motorVehicleDetail(responseInvoice);
        } else {
            // 不为机动车统计销售发票
            invoiceDetailList = responseInvoice.getDetailList();
        }

        // 最终要的结果
        final Map<String, List<InvoiceDetail>> resultMap = new HashMap<>();
        // 按税率分组
        invoiceDetailList.forEach(dataItem -> {
            if (!"原价合计".equals(dataItem.getGoodsName().trim()) && !"折扣额合计".equals(dataItem.getGoodsName().trim())
                && !"(详见销货清单)".equals(dataItem.getGoodsName().trim())&& !"（详见销货清单）".equals(dataItem.getGoodsName().trim()) && StringUtils.isNotEmpty(dataItem.getTaxRate())) {
                if (resultMap.containsKey(dataItem.getTaxRate())) {
                    resultMap.get(dataItem.getTaxRate()).add(dataItem);
                } else {
                    final List<InvoiceDetail> list = new ArrayList<>();
                    list.add(dataItem);
                    resultMap.put(dataItem.getTaxRate(), list);
                }
            }
        });

        // 循环遍历map获取每组明细，并循环遍历每组里的明细组装抵账统计数据
        for (Map.Entry<String, List<InvoiceDetail>> entry : resultMap.entrySet()) {
            // 获取每组明细
            final List<InvoiceDetail> detailList = entry.getValue();
            // 定义抵账统计实体
            final TDxRecordInvoiceStatisticsJobEntity recordInvoiceStatistics =
                new TDxRecordInvoiceStatisticsJobEntity();
            // 税额
            BigDecimal taxAmount = new BigDecimal("0.0");
            // 金额
            BigDecimal detailAmount = new BigDecimal("0.0");
            // 循环赋值
            for (InvoiceDetail invoiceDetail : detailList) {
                BigDecimal detailTaxAmount;
                try {
                    detailTaxAmount = new BigDecimal(invoiceDetail.getTaxAmount());
                } catch (Exception e) {
                    detailTaxAmount = new BigDecimal(0);
                }
                taxAmount = taxAmount.add(detailTaxAmount);
                detailAmount = detailAmount.add(new BigDecimal(invoiceDetail.getDetailAmount()));

            }
            // 价税合计
            final BigDecimal totalAmount = taxAmount.add(detailAmount);
            // 税率
            try {

                recordInvoiceStatistics.setTaxRate(new BigDecimal(entry.getKey()));
            } catch (Exception e) {
                log.info("类型转换错误:{}", e.getMessage());
                recordInvoiceStatistics.setTaxRate(new BigDecimal("0"));
            }
            // 发票号码
            recordInvoiceStatistics.setInvoiceNo(responseInvoice.getInvoiceNo());
            // 发票代码
            recordInvoiceStatistics.setInvoiceCode(responseInvoice.getInvoiceCode());
            // 税额
            recordInvoiceStatistics.setTaxAmount(taxAmount);
            // 金额
            recordInvoiceStatistics.setDetailAmount(detailAmount);
            // 价税合计
            recordInvoiceStatistics.setTotalAmount(totalAmount);
            // 放入集合
            recordInvoiceStatisticsList.add(recordInvoiceStatistics);
        }
        // 返回
        return recordInvoiceStatisticsList;
    }

    /**
     * 机动车统一销售发票明细 这种发票没有detailList 故明细信息需从主体信息中获取
     *
     * @param responseInvoice
     *            查验响应实体
     * @return 机动车统一销售发票明细
     */
    private List<InvoiceDetail> motorVehicleDetail(ResponseInvoice responseInvoice) {
        final List<InvoiceDetail> invoiceDetailList = new ArrayList<>();
        final InvoiceDetail invoiceDetail = new InvoiceDetail();
        // 税率
        invoiceDetail.setTaxRate(responseInvoice.getTaxRate());
        // 税额
        invoiceDetail.setTaxAmount(responseInvoice.getTaxAmount());
        // 金额
        invoiceDetail.setDetailAmount(responseInvoice.getInvoiceAmount());

        invoiceDetailList.add(invoiceDetail);
        return invoiceDetailList;
    }

    /**
     * 构建抵账主体数据
     *
     * @param responseInvoice
     *            抵账主体数据
     * @return 主体数据
     */
    private InvoiceCollectionInfo buildInvoiceCollectionInfo(ResponseInvoice responseInvoice, String buyerTaxNo,String taxRate) {
        // 定义返回值
        final InvoiceCollectionInfo invoiceCollectionInfo = new InvoiceCollectionInfo();
        // 购方税号
        invoiceCollectionInfo.setGfTaxNo(responseInvoice.getBuyerTaxNo());
        // 购方名称
        invoiceCollectionInfo.setGfName(responseInvoice.getBuyerName());
        // 发票类型
        invoiceCollectionInfo.setInvoiceType(responseInvoice.getInvoiceType());
        // 发票代码
        invoiceCollectionInfo.setInvoiceCode(responseInvoice.getInvoiceCode());
        // 发票号码
        invoiceCollectionInfo.setInvoiceNo(responseInvoice.getInvoiceNo());
        // 价税合计
        invoiceCollectionInfo.setTotalAmount(responseInvoice.getTotalAmount());
        // 金额
        invoiceCollectionInfo.setInvoiceAmount(responseInvoice.getInvoiceAmount());
        // 税额
        try {
            BigDecimal taxAmount = new BigDecimal(responseInvoice.getTaxAmount());
            invoiceCollectionInfo.setTaxAmount(responseInvoice.getTaxAmount());
        } catch (Exception e) {
            invoiceCollectionInfo.setTaxAmount("0");
        }
        // 销方税号
        invoiceCollectionInfo.setXfTaxNo(responseInvoice.getSalerTaxNo());
        // 销方名称
        invoiceCollectionInfo.setXfName(responseInvoice.getSalerName());
        // 销方地址
        invoiceCollectionInfo.setXfAddressAndPhone(responseInvoice.getSalerAddressPhone());
        // 销方银行帐号
        invoiceCollectionInfo.setXfBankAndNo(responseInvoice.getSalerAccount());
        // 购方银行帐号
        invoiceCollectionInfo.setGfBankAndNo(responseInvoice.getBuyerAccount());
        // 购方地址
        invoiceCollectionInfo.setGfAddressAndPhone(responseInvoice.getBuyerAddressPhone());
        // 开票日期
        final DateTime invoiceDateTime =
            DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(responseInvoice.getInvoiceDate());
        invoiceCollectionInfo.setInvoiceDate(invoiceDateTime.toDate());
        // 备注
        invoiceCollectionInfo.setRemark(responseInvoice.getRemark());
        // 校验码
        invoiceCollectionInfo.setCheckCode(responseInvoice.getCheckCode());
        // 有明细
        invoiceCollectionInfo.setDetailYesorno("1");
        // uuid
        invoiceCollectionInfo.setUuid(responseInvoice.getInvoiceCode() + responseInvoice.getInvoiceNo());
        // 更新前的购方税号，放置不一致
        invoiceCollectionInfo.setBuyerTaxNo(buyerTaxNo);
        // 发票状态
        if (!"01".equals(invoiceCollectionInfo.getInvoiceType()) && !"03".equals(invoiceCollectionInfo.getInvoiceType())
            && !"14".equals(invoiceCollectionInfo.getInvoiceType())
            && !"08".equals(invoiceCollectionInfo.getInvoiceType())) {
            if ("N".equals(responseInvoice.getIsCancelled())) {
                invoiceCollectionInfo.setInvoiceStatus("0");
            } else if ("Y".equals(responseInvoice.getIsCancelled())) {
                invoiceCollectionInfo.setInvoiceStatus("2");
            } else {
                invoiceCollectionInfo.setInvoiceStatus(responseInvoice.getIsCancelled());
            }
        }

        // 机器编号
        invoiceCollectionInfo.setMachinecode(responseInvoice.getMachineNo());

        invoiceCollectionInfo.setQsType(responseInvoice.getQsType());

        invoiceCollectionInfo.setTaxRate(taxRate);
        // 返回
        return invoiceCollectionInfo;
    }

    /**
     * 构建机动车销售明细集
     *
     * @param responseInvoice
     *            查验响应实体
     * @return 机动车销售明细集
     */
    private List<InvoiceCheckVehicleDetailModel> buildVehicleDetailList(ResponseInvoice responseInvoice) {
        final List<InvoiceCheckVehicleDetailModel> vehicleDetailModelList = new ArrayList<>();
        final InvoiceCheckVehicleDetailModel invoiceCheckVehicleDetailModel = new InvoiceCheckVehicleDetailModel();
        invoiceCheckVehicleDetailModel.setUuid(responseInvoice.getInvoiceCode() + responseInvoice.getInvoiceNo());
        invoiceCheckVehicleDetailModel.setBuyerIdNum(responseInvoice.getBuyerIDNum());
        invoiceCheckVehicleDetailModel.setVehicleType(responseInvoice.getVehicleType());
        invoiceCheckVehicleDetailModel.setFactoryModel(responseInvoice.getFactoryModel());
        invoiceCheckVehicleDetailModel.setProductPlace(responseInvoice.getProductPlace());
        invoiceCheckVehicleDetailModel.setCertificate(responseInvoice.getCertificate());
        invoiceCheckVehicleDetailModel.setCertificateImport(responseInvoice.getCertificateImport());
        invoiceCheckVehicleDetailModel.setInspectionNum(responseInvoice.getInspectionNum());
        invoiceCheckVehicleDetailModel.setEngineNo(responseInvoice.getEngineNo());
        invoiceCheckVehicleDetailModel.setVehicleNo(responseInvoice.getVehicleNo());
        invoiceCheckVehicleDetailModel.setTaxRate(responseInvoice.getTaxRate());
        invoiceCheckVehicleDetailModel.setTaxCode(getZSM(responseInvoice.getTaxRate()));
        invoiceCheckVehicleDetailModel.setTaxBureauName(responseInvoice.getTaxBureauName());
        invoiceCheckVehicleDetailModel.setTaxBureauCode(responseInvoice.getTaxBureauCode());
        invoiceCheckVehicleDetailModel.setTaxRecords(responseInvoice.getTaxRecords());
        invoiceCheckVehicleDetailModel.setLimitPeople(responseInvoice.getLimitPeople());
        invoiceCheckVehicleDetailModel.setTonnage(responseInvoice.getTonnage());
        invoiceCheckVehicleDetailModel.setDeptId(responseInvoice.getDeptId());
        invoiceCheckVehicleDetailModel.setDeptName(responseInvoice.getDeptName());
        vehicleDetailModelList.add(invoiceCheckVehicleDetailModel);
        return vehicleDetailModelList;
    }
}
