package com.dxhy.erp.service.impl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.ReturnCodeEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.pojo.Authorize;
import com.dxhy.common.util.pojo.GlobalInfo;
import com.dxhy.erp.dao.MryxInterfaceDao;
import com.dxhy.erp.dao.SysDeptDao;
import com.dxhy.erp.dao.TdxRecordInvoiceDao;
import com.dxhy.erp.entity.*;
import com.dxhy.erp.model.*;
import com.dxhy.erp.model.auth.InterfaceCode;
import com.dxhy.erp.service.MryxInterfaceService;
import com.github.pagehelper.util.StringUtil;

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

/**
 * @author dxhy
 */
@SuppressWarnings("AlibabaMethodTooLong")
@Service("mryxInterfaceService")
@Slf4j
public class MryxInterfaceServiceImpl extends MpBaseServiceImpl<TdxRecordInvoiceDao, TdxRecordInvoice>
    implements MryxInterfaceService {

    @Resource
    private SysDeptDao sysDeptDao;
    @Resource
    private MryxInterfaceDao mryxInterfaceDao;

    /**
     * 每日优鲜全票面信息获取接口
     *
     * @param globalInfo
     * @param map
     * @param request
     * @param response
     * @param authorize
     * @return
     */
    @Override
    public String getAllInfo(GlobalInfo globalInfo, Map<String, String> map, HttpServletRequest request,
        HttpServletResponse response, Authorize authorize) {
        InterfaceCode interfaceCode = new InterfaceCode();
        ReturnStateInfo run = new ReturnStateInfo();
        List<TdxRecordInvoice> list;
        // 01增值税专用发票明细list
        List<InvoiceDetailInfo> detailList;
        // 03机动车list
        List<VehicleSaleInvoices> vehicleList;
        CollectData data = null;
        try {
            data = JSON.parseObject(map.get("data"), CollectData.class);
            run = getCheck(data);
            if (run.getReturnCode() != null || run.getReturnMessage() != null) {
                interfaceCode.setGlobalInfo(globalInfo);
                interfaceCode.setReturnStateInfo(run);
                return JSON.toJSONString(interfaceCode);
            }
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            SysDeptEntity deptEntity = sysDeptDao.getOrg(data.getBuyerTaxNo());
            String dbName = null;
            if (deptEntity != null && deptEntity.getDbName() != null) {
                dbName = deptEntity.getDbName();
            } else {
                ReturnStateInfo runInfo = new ReturnStateInfo();
                runInfo.setReturnCode(ReturnCodeEnum.JTSHBF.getFhzDm());
                runInfo.setReturnMessage(ReturnCodeEnum.JTSHBF.getFhzMc(ReturnCodeEnum.JTSHBF.getIndex()));
                interfaceCode.setGlobalInfo(globalInfo);
                interfaceCode.setReturnStateInfo(runInfo);
                return JSON.toJSONString(interfaceCode);
            }
            // 发票号码集合
            List<InvoiceSavePo> invoiceNoList = data.getInvoices();
            // 01回传信息
            List<InvoiceMryxInfo> invoices = new ArrayList<>();
            // 03机动车回传信息
            List<VehicleSaleInvoiceInfos> vehicleInvoiceInfos = new ArrayList<>();
            List<String> invoiceUuidList = new ArrayList<>();
            // 查询抵帐表数据（主信息）
            for (InvoiceSavePo uuList : invoiceNoList) {
                String uuid1 = uuList.getInvoiceCode() + uuList.getInvoiceNo();
                invoiceUuidList.add(uuid1);
            }
            DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
            list = mryxInterfaceDao.getInvoiceList(data.getBuyerTaxNo(), data.getInvoiceType(), invoiceUuidList);
            for (TdxRecordInvoice info : list) {
                String uuid = info.getInvoiceCode() + info.getInvoiceNo();
                if ("03".equals(data.getInvoiceType())) {
                    DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
                    vehicleList = selectVehicle(uuid);
                    if (vehicleList != null && vehicleList.size() > 0) {
                        for (VehicleSaleInvoices vehicle : vehicleList) {
                            VehicleSaleInvoiceInfos vehicleInfo = getVehicleInfo(vehicle, info);
                            vehicleInvoiceInfos.add(vehicleInfo);
                        }
                    }
                } else {
                    DynamicContextHolder.push(dbName + DbConstant.DETAIL_READ);
                    detailList = getDetailInfo(uuid);
                    List<InvoiceDetailInfo> listInfo = new ArrayList<>();
                    if (detailList != null && detailList.size() > 0) {
                        for (InvoiceDetailInfo detail : detailList) {
                            InvoiceDetailInfo detailInfo = getInvoiceDetailInfo(detail);
                            listInfo.add(detailInfo);
                        }
                    }
                    InvoiceMryxInfo invoiceInfo = getInvoiceInfo(info);
                    invoiceInfo.setDetailList(listInfo);
                    invoices.add(invoiceInfo);
                }
            }
            GatherData gather = new GatherData();
            gather.setInvoiceType(data.getInvoiceType());
            gather.setTotalRows(list.size());
            if (vehicleInvoiceInfos.size() > 0) {
                gather.setInvoices(vehicleInvoiceInfos);
            }
            if (invoices.size() > 0) {
                gather.setInvoices(invoices);
            }
            String result = JSON.toJSONString(gather);
            run.setReturnCode(ReturnCodeEnum.CLCG.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.CLCG.getFhzMc(ReturnCodeEnum.CLCG.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            interfaceCode.setData(Base64.encode(result));
            return JSON.toJSONString(interfaceCode);
        } catch (Exception e) {
            e.printStackTrace();
            run.setReturnCode("2011");
            run.setReturnMessage("其他异常");
            interfaceCode.setReturnStateInfo(run);
            interfaceCode.setGlobalInfo(globalInfo);
            return JSON.toJSONString(interfaceCode);
        }
    }

    /**
     * 每日优鲜签收状态获取接口
     *
     * @param globalInfo
     * @param map
     * @param request
     * @param response
     * @param authorize
     * @return
     */
    @Override
    public String getSignState(GlobalInfo globalInfo, Map<String, String> map, HttpServletRequest request,
        HttpServletResponse response, Authorize authorize) {
        InterfaceCode interfaceCode = new InterfaceCode();
        ReturnStateInfo run = new ReturnStateInfo();
        List<InvoiceElectron> list = null;
        CollectData data = null;
        try {
            data = JSON.parseObject(map.get("data"), CollectData.class);
            run = getSignCheck(data);
            if (run.getReturnCode() != null || run.getReturnMessage() != null) {
                interfaceCode.setGlobalInfo(globalInfo);
                interfaceCode.setReturnStateInfo(run);
                return JSON.toJSONString(interfaceCode);
            }
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            SysDeptEntity deptEntity = sysDeptDao.getOrg(data.getBuyerTaxNo());
            String dbName = null;
            if (deptEntity != null && deptEntity.getDbName() != null) {
                dbName = deptEntity.getDbName();
            } else {
                ReturnStateInfo runInfo = new ReturnStateInfo();
                runInfo.setReturnCode(ReturnCodeEnum.JTSHBF.getFhzDm());
                runInfo.setReturnMessage(ReturnCodeEnum.JTSHBF.getFhzMc(ReturnCodeEnum.JTSHBF.getIndex()));
                interfaceCode.setGlobalInfo(globalInfo);
                interfaceCode.setReturnStateInfo(runInfo);
                return JSON.toJSONString(interfaceCode);
            }
            // 发票号码集合
            List<InvoiceSavePo> invoiceNoList = data.getInvoices();
            // 签收状态回传信息
            List<SignInvoiceInfo> invoices = new ArrayList<>();
            List<String> invoiceUuidList = new ArrayList<>();
            // 查询抵帐表数据（主信息）
            for (InvoiceSavePo uuList : invoiceNoList) {
                String uuid1 = uuList.getInvoiceCode() + uuList.getInvoiceNo();
                invoiceUuidList.add(uuid1);
            }
            DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
            // 获取签收状态信息
            list = mryxInterfaceDao.getSignState(data.getBuyerTaxNo(), invoiceUuidList);
            for (InvoiceElectron signInfo : list) {
                // 转换发票实体
                SignInvoiceInfo invoiceInfo = this.getSignInfo(signInfo);
                invoices.add(invoiceInfo);
            }
            GatherData gather = new GatherData();
            gather.setTotalRows(list.size());
            if (invoices.size() > 0) {
                gather.setInvoices(invoices);
            }
            String result = JSON.toJSONString(gather);
            run.setReturnCode(ReturnCodeEnum.CLCG.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.CLCG.getFhzMc(ReturnCodeEnum.CLCG.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            interfaceCode.setData(Base64.encode(result));
            return JSON.toJSONString(interfaceCode);
        } catch (Exception e) {
            e.printStackTrace();
            run.setReturnCode("2011");
            run.setReturnMessage("其他异常");
            interfaceCode.setReturnStateInfo(run);
            interfaceCode.setGlobalInfo(globalInfo);
            return JSON.toJSONString(interfaceCode);
        }
    }

    /**
     * 每日优鲜付款信息同步接口
     *
     * @param globalInfo
     * @param map
     * @param request
     * @param response
     * @param authorize
     * @return
     */
    @Override
    public String synPayInfo(GlobalInfo globalInfo, Map<String, String> map, HttpServletRequest request,
        HttpServletResponse response, Authorize authorize) {
        InterfaceCode interfaceCode = new InterfaceCode();
        ReturnStateInfo run = new ReturnStateInfo();
        PayInfo data = null;
        try {
            data = JSON.parseObject(map.get("data"), PayInfo.class);
            run = getPayCheck(data);
            if (run.getReturnCode() != null || run.getReturnMessage() != null) {
                interfaceCode.setGlobalInfo(globalInfo);
                interfaceCode.setReturnStateInfo(run);
                return JSON.toJSONString(interfaceCode);
            }
            DynamicContextHolder.push(DbConstant.BASICS_READ);
            SysDeptEntity deptEntity = sysDeptDao.getOrg(data.getTaxNo());
            String dbName = null;
            if (deptEntity != null && deptEntity.getDbName() != null) {
                dbName = deptEntity.getDbName();
            } else {
                ReturnStateInfo runInfo = new ReturnStateInfo();
                runInfo.setReturnCode(ReturnCodeEnum.JTSHBF.getFhzDm());
                runInfo.setReturnMessage(ReturnCodeEnum.JTSHBF.getFhzMc(ReturnCodeEnum.JTSHBF.getIndex()));
                interfaceCode.setGlobalInfo(globalInfo);
                interfaceCode.setReturnStateInfo(runInfo);
                return JSON.toJSONString(interfaceCode);
            }
            // 付款信息参数集合
            List<PayInfoSyn> payList = data.getInvoices();
            List<ResSignInfo> rlist = new ArrayList<>();
            int totalRows = payList.size();
            int contentRows = 0;
            for (PayInfoSyn payInfoSyn : payList) {
                String uuid = payInfoSyn.getInvoiceCode() + payInfoSyn.getInvoiceNo();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
                int a = mryxInterfaceDao.updatePay(uuid, payInfoSyn.getPaymentDate(), payInfoSyn.getPaymentStatus(),
                    payInfoSyn.getPaymentSum(), payInfoSyn.getVoucherNumber());
                // 回传付款信息说明
                ResSignInfo resSignInfo = new ResSignInfo();
                if (a > 0) {
                    contentRows = contentRows + 1;
                    resSignInfo.setInvoiceCode(payInfoSyn.getInvoiceCode());
                    resSignInfo.setInvoiceNo(payInfoSyn.getInvoiceNo());
                    resSignInfo.setFailReason("");
                    resSignInfo.setDealStatus("1");
                } else {
                    resSignInfo.setInvoiceCode(payInfoSyn.getInvoiceCode());
                    resSignInfo.setInvoiceNo(payInfoSyn.getInvoiceNo());
                    resSignInfo.setFailReason("无底账数据");
                    resSignInfo.setDealStatus("0");
                }
                rlist.add(resSignInfo);
            }
            GatherData gather = new GatherData();
            gather.setTotalRows(rlist.size());
            gather.setContentRows(contentRows);
            if (rlist.size() > 0) {
                gather.setInvoices(rlist);
            }
            String result = JSON.toJSONString(gather);
            run.setReturnCode(ReturnCodeEnum.CLCG.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.CLCG.getFhzMc(ReturnCodeEnum.CLCG.getIndex()));
            interfaceCode.setGlobalInfo(globalInfo);
            interfaceCode.setReturnStateInfo(run);
            interfaceCode.setData(Base64.encode(result));
            return JSON.toJSONString(interfaceCode);
        } catch (Exception e) {
            e.printStackTrace();
            run.setReturnCode("2011");
            run.setReturnMessage("其他异常");
            interfaceCode.setReturnStateInfo(run);
            interfaceCode.setGlobalInfo(globalInfo);
            return JSON.toJSONString(interfaceCode);
        }
    }

    /**
     * 数据校验
     *
     * @param data
     * @return
     */
    public ReturnStateInfo getCheck(CollectData data) {
        ReturnStateInfo run = new ReturnStateInfo();
        String taxno = data.getBuyerTaxNo();
        String invoType = data.getInvoiceType();
        if (StringUtil.isEmpty(taxno)) {
            run.setReturnCode(ReturnCodeEnum.SHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.SHBNWK.getFhzMc(ReturnCodeEnum.SHBNWK.getIndex()));
            return run;
        }
        if (StringUtil.isEmpty(invoType)) {
            run.setReturnCode(ReturnCodeEnum.FPLXWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.FPLXWK.getFhzMc(ReturnCodeEnum.FPLXWK.getIndex()));
            return run;
        }
        if (data.getInvoices() == null || data.getInvoices().size() <= 0) {
            run.setReturnCode(ReturnCodeEnum.NRYCY.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.NRYCY.getFhzMc(ReturnCodeEnum.NRYCY.getIndex()));
            return run;
        }
        for (InvoiceSavePo uuList : data.getInvoices()) {
            if (StringUtil.isEmpty(uuList.getInvoiceCode())) {
                run.setReturnCode(ReturnCodeEnum.FPDMBNWK.getFhzDm());
                run.setReturnMessage(ReturnCodeEnum.FPDMBNWK.getFhzMc(ReturnCodeEnum.FPDMBNWK.getIndex()));
                return run;
            }
            if (StringUtil.isEmpty(uuList.getInvoiceNo())) {
                run.setReturnCode(ReturnCodeEnum.FPHMBNWK.getFhzDm());
                run.setReturnMessage(ReturnCodeEnum.FPHMBNWK.getFhzMc(ReturnCodeEnum.FPHMBNWK.getIndex()));
                return run;
            }
        }
        return run;
    }

    /**
     * 根据uuid查询机动车信息
     *
     * @param uuid
     * @return
     */
    public List<VehicleSaleInvoices> selectVehicle(String uuid) {
        return super.baseMapper.selectVehicle(uuid);
    }

    /**
     * 机动车信息转换
     *
     * @param vehicleSaleInvoice
     * @param veinfo
     * @return
     */
    private VehicleSaleInvoiceInfos getVehicleInfo(VehicleSaleInvoices vehicleSaleInvoice, TdxRecordInvoice veinfo) {
        DecimalFormat df = new DecimalFormat("#0.00");
        VehicleSaleInvoiceInfos info = new VehicleSaleInvoiceInfos();
        info.setInvoiceCode(veinfo.getInvoiceCode());
        info.setInvoiceNo(veinfo.getInvoiceNo());
        if (StringUtil.isEmpty(veinfo.getDqskssq())) {
            info.setCurrentTaxPeriod("");
        } else {
            info.setCurrentTaxPeriod(veinfo.getDqskssq());
        }
        if (StringUtil.isEmpty(veinfo.getGxjzr())) {
            info.setLegalizeEndDate("");
        } else {
            info.setLegalizeEndDate(veinfo.getGxjzr());
        }
        if (StringUtil.isEmpty(veinfo.getGxfwq())) {
            info.setLegalizeInvoiceDateBegin("");
        } else {
            info.setLegalizeInvoiceDateBegin(veinfo.getGxfwq());
        }
        if (StringUtil.isEmpty(veinfo.getGxfwz())) {
            info.setLegalizeInvoiceDateEnd("");
        } else {
            info.setLegalizeInvoiceDateEnd(veinfo.getGxfwz());
        }
        info.setInvoiceType(veinfo.getInvoiceType());
        info.setBuyerName(veinfo.getGfName());
        info.setInvoiceDate(new SimpleDateFormat("yyyyMMdd").format(veinfo.getInvoiceDate()));
        if (StringUtil.isEmpty(vehicleSaleInvoice.getBuyerIdNum())) {
            info.setBuyerIdNum("");
        } else {
            info.setBuyerIdNum(vehicleSaleInvoice.getBuyerIdNum());
        }
        info.setBuyerTaxNo(veinfo.getGfTaxNo());
        if (StringUtil.isEmpty(vehicleSaleInvoice.getVehicleType())) {
            info.setVehicleType("");
        } else {
            info.setVehicleType(vehicleSaleInvoice.getVehicleType());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getFactoryModel())) {
            info.setFactoryModel("");
        } else {
            info.setFactoryModel(vehicleSaleInvoice.getFactoryModel());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getProductPlace())) {
            info.setProductPlace("");
        } else {
            info.setProductPlace(vehicleSaleInvoice.getProductPlace());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getCertificate())) {
            info.setCertificate("");
        } else {
            info.setCertificate(vehicleSaleInvoice.getCertificate());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getCertificateImport())) {
            info.setCertificateImport("");
        } else {
            info.setCertificateImport(vehicleSaleInvoice.getCertificateImport());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getInspectionNum())) {
            info.setInspectionNum("");
        } else {
            info.setInspectionNum(vehicleSaleInvoice.getInspectionNum());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getEngineNo())) {
            info.setEngineNo("");
        } else {
            info.setEngineNo(vehicleSaleInvoice.getEngineNo());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getVehicleNo())) {
            info.setVehicleNo("");
        } else {
            info.setVehicleNo(vehicleSaleInvoice.getVehicleNo());
        }
        if (veinfo.getTotalAmount() != null) {
            info.setTotalAmount(df.format(veinfo.getTotalAmount()));
        } else {
            info.setTotalAmount("0");
        }
        info.setSalerName(veinfo.getXfName());
        info.setTaxNum(veinfo.getXfTaxNo());
        if (StringUtil.isEmpty(vehicleSaleInvoice.getPhone())) {
            info.setPhone("");
        } else {
            info.setPhone(vehicleSaleInvoice.getPhone());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getBuyerBank())) {
            info.setBuyerBank("");
        } else {
            info.setBuyerBank(vehicleSaleInvoice.getBuyerBank());
        }
        if (StringUtil.isEmpty(veinfo.getXfBankAndNo())) {
            info.setBuyerAcount("");
        } else {
            info.setBuyerAcount(veinfo.getXfBankAndNo());
        }
        if (StringUtil.isEmpty(veinfo.getXfAddressAndPhone())) {
            info.setBuyerAddressPhone("");
        } else {
            info.setBuyerAddressPhone(veinfo.getXfAddressAndPhone());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getTaxRate())) {
            info.setTaxRate("");
        } else {
            String taxrate = vehicleSaleInvoice.getTaxRate().replaceAll("[%]", "");
            info.setTaxRate(taxrate);
        }
        if (veinfo.getTaxAmount() != null) {
            info.setTaxAmount(df.format(veinfo.getTaxAmount()));
        } else {
            info.setTaxAmount("0");
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getTaxBureauCode())) {
            info.setTaxBureauCode("");
        } else {
            info.setTaxBureauCode(vehicleSaleInvoice.getTaxBureauCode());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getTaxBureauName())) {
            info.setTaxBureauName("");
        } else {
            info.setTaxBureauName(vehicleSaleInvoice.getTaxBureauName());
        }
        if (veinfo.getInvoiceAmount() != null) {
            info.setInvoiceAmount(df.format(veinfo.getInvoiceAmount()));
        } else {
            info.setInvoiceAmount("0");
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getTaxRecords())) {
            info.setTaxRecords("");
        } else {
            info.setTaxRecords(vehicleSaleInvoice.getTaxRecords());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getTonnage())) {
            info.setTonnage("");
        } else {
            info.setTonnage(vehicleSaleInvoice.getTonnage());
        }
        if (StringUtil.isEmpty(vehicleSaleInvoice.getLimitPeople())) {
            info.setLimitPeople("");
        } else {
            info.setLimitPeople(vehicleSaleInvoice.getLimitPeople());
        }
        info.setInvoiceStatus(veinfo.getInvoiceStatus());
        info.setCheckStatus(veinfo.getSfygx());
        info.setLegalizeState(veinfo.getRzhYesorno());
        if (veinfo.getRzhDate() == null) {
            info.setLegalizeDate("");
        } else {
            info.setLegalizeDate(new SimpleDateFormat("yyyyMMdd").format(veinfo.getRzhDate()));
        }
        if (StringUtil.isEmpty(veinfo.getRzhBelongDate())) {
            info.setTaxPeriod("");
        } else {
            info.setTaxPeriod(veinfo.getRzhBelongDate());
        }
        if (StringUtil.isEmpty(veinfo.getRzhType())) {
            info.setLegalizeType("");
        } else {
            info.setLegalizeType(veinfo.getRzhType());
        }
        if (StringUtil.isEmpty(veinfo.getSfdbts())) {
            info.setSfdbts("");
        } else {
            info.setSfdbts(veinfo.getSfdbts());
        }
        if (StringUtil.isEmpty(veinfo.getRzlx())) {
            info.setRzlx("");
        } else {
            info.setRzlx(veinfo.getRzlx());
        }
        return info;
    }

    /**
     * 根据uuid查询明细
     *
     * @param uuid
     * @return
     */
    @Override
    public List<InvoiceDetailInfo> getDetailInfo(String uuid) {
        return super.baseMapper.selectDetailInfo(uuid);
    }

    /**
     * 明细信息转换
     *
     * @param detail
     * @return
     */
    private InvoiceDetailInfo getInvoiceDetailInfo(InvoiceDetailInfo detail) {
        InvoiceDetailInfo detailInfo = new InvoiceDetailInfo();
        detailInfo.setDetailNo(detail.getDetailNo() + "");
        detailInfo.setGoodsName(detail.getGoodsName());
        if (StringUtil.isEmpty(detail.getModel())) {
            detailInfo.setSpecificationModel("");
        } else {
            detailInfo.setSpecificationModel(detail.getModel());
        }
        if (StringUtil.isEmpty(detail.getGoodsNum())) {
            detailInfo.setGoodsNum("");
        } else {
            detailInfo.setGoodsNum(detail.getGoodsNum());
        }
        if (StringUtil.isEmpty(detail.getNum())) {
            detailInfo.setNum("0");
        } else {
            detailInfo.setNum(detail.getNum() + "");
        }
        if (StringUtil.isEmpty(detail.getUnit())) {
            detailInfo.setUnit("");
        } else {
            detailInfo.setUnit(detail.getUnit());
        }
        if (StringUtil.isEmpty(detail.getUnitPrice())) {
            detailInfo.setUnitPrice("");
        } else {
            detailInfo.setUnitPrice(detail.getUnitPrice());
        }
        if (StringUtil.isEmpty(detail.getDetailAmount())) {
            detailInfo.setDetailAmount("");
        } else {
            detailInfo.setDetailAmount(detail.getDetailAmount());
        }
        if (StringUtil.isEmpty(detail.getTaxRate())) {
            detailInfo.setTaxRate("");
        } else {
            detailInfo.setTaxRate(detail.getTaxRate() + "");
        }
        if (StringUtil.isEmpty(detail.getTaxAmount())) {
            detailInfo.setTaxAmount("");
        } else {
            detailInfo.setTaxAmount(detail.getTaxAmount());
        }
        if (StringUtil.isEmpty(detail.getCph())) {
            detailInfo.setCph("");
        } else {
            detailInfo.setCph(detail.getCph());
        }
        if (StringUtil.isEmpty(detail.getLx())) {
            detailInfo.setLx("");
        } else {
            detailInfo.setLx(detail.getLx());
        }
        if (StringUtil.isEmpty(detail.getTxrqq())) {
            detailInfo.setTxrqq("");
        } else {
            detailInfo.setTxrqq(detail.getTxrqq());
        }
        if (StringUtil.isEmpty(detail.getTxrqz())) {
            detailInfo.setTxrqz("");
        } else {
            detailInfo.setTxrqz(detail.getTxrqz());
        }
        return detailInfo;
    }

    /**
     * 发票信息转换
     *
     * @param invoice
     * @return
     */
    private InvoiceMryxInfo getInvoiceInfo(TdxRecordInvoice invoice) {
        InvoiceMryxInfo info = new InvoiceMryxInfo();
        info.setInvoiceCode(invoice.getInvoiceCode());
        info.setInvoiceNo(invoice.getInvoiceNo());
        info.setInvoiceDate(new SimpleDateFormat("yyyyMMdd").format(invoice.getInvoiceDate()));
        info.setBuyerAcount(invoice.getGfBankAndNo());
        info.setBuyerAddressPhone(invoice.getGfAddressAndPhone());
        info.setBuyerName(invoice.getGfName());
        info.setBuyerTaxNo(invoice.getGfTaxNo());
        info.setSalerAcount(invoice.getXfBankAndNo());
        info.setSalerAddressPhone(invoice.getXfAddressAndPhone());
        info.setSalerName(invoice.getXfName());
        info.setSalerTaxNo(invoice.getXfTaxNo());
        if (invoice.getInvoiceAmount() != null) {
            info.setInvoiceAmount(invoice.getInvoiceAmount().toString());
        } else {
            info.setInvoiceAmount("0");
        }
        if (invoice.getTaxAmount() != null) {
            info.setTaxAmount(invoice.getTaxAmount().toString());
        } else {
            info.setTaxAmount("0");
        }
        if (invoice.getTotalAmount() != null) {
            info.setTotalAmount(invoice.getTotalAmount().toString());
        } else {
            info.setTotalAmount("0");
        }
        if (StringUtil.isEmpty(invoice.getRemark())) {
            info.setRemark("");
        } else {
            info.setRemark(invoice.getRemark());
        }
        if (StringUtil.isEmpty(invoice.getDqskssq())) {
            info.setCurrentTaxPeriod("");
        } else {
            info.setCurrentTaxPeriod(invoice.getDqskssq());
        }
        if (StringUtil.isEmpty(invoice.getGxjzr())) {
            info.setLegalizeEndDate("");
        } else {
            info.setLegalizeEndDate(invoice.getGxjzr());
        }
        if (StringUtil.isEmpty(invoice.getGxfwq())) {
            info.setLegalizeInvoiceDateBegin("");
        } else {
            info.setLegalizeInvoiceDateBegin(invoice.getGxfwq());
        }
        if (StringUtil.isEmpty(invoice.getGxfwz())) {
            info.setLegalizeInvoiceDateEnd("");
        } else {
            info.setLegalizeInvoiceDateEnd(invoice.getGxfwz());
        }
        if (invoice.getRzhDate() == null) {
            info.setLegalizeDate("");
        } else {
            info.setLegalizeDate(new SimpleDateFormat("yyyyMMdd").format(invoice.getRzhDate()));
        }
        info.setLegalizeState(invoice.getRzhYesorno());
        info.setInvoiceStatus(invoice.getInvoiceStatus());
        info.setInvoiceType(invoice.getInvoiceType());
        if (StringUtil.isEmpty(invoice.getRzhBelongDate())) {
            info.setLegalizeBlongDate("");
        } else {
            info.setLegalizeBlongDate(invoice.getRzhBelongDate());
        }
        if (StringUtil.isEmpty(invoice.getSfdbts())) {
            info.setSfdbts("");
        } else {
            info.setSfdbts(invoice.getSfdbts());
        }
        if (StringUtil.isEmpty(invoice.getRzlx())) {
            info.setRzlx("");
        } else {
            info.setRzlx(invoice.getRzlx());
        }
        if (StringUtil.isEmpty(invoice.getCheckCode())) {
            info.setCheckCode("");
        } else {
            info.setCheckCode(invoice.getCheckCode());
        }
        if (StringUtil.isEmpty(invoice.getTxfbz())) {
            info.setTxfbz("");
        } else {
            info.setTxfbz(invoice.getTxfbz());
        }
        if (StringUtil.isEmpty(invoice.getLslbz())) {
            info.setLslbz("");
        } else {
            info.setLslbz(invoice.getLslbz());
        }
        if (StringUtil.isEmpty(invoice.getRzhType())) {
            info.setLegalizeType("");
        } else {
            info.setLegalizeType(invoice.getRzhType());
        }
        return info;
    }

    /**
     * 数据校验
     *
     * @param data
     * @return
     */
    private ReturnStateInfo getSignCheck(CollectData data) {
        ReturnStateInfo run = new ReturnStateInfo();
        String taxno = data.getBuyerTaxNo();
        if (StringUtil.isEmpty(taxno)) {
            run.setReturnCode(ReturnCodeEnum.SHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.SHBNWK.getFhzMc(ReturnCodeEnum.SHBNWK.getIndex()));
            return run;
        }
        if (data.getInvoices() == null || data.getInvoices().size() <= 0) {
            run.setReturnCode(ReturnCodeEnum.NRYCY.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.NRYCY.getFhzMc(ReturnCodeEnum.NRYCY.getIndex()));
            return run;
        }
        for (InvoiceSavePo uuList : data.getInvoices()) {
            if (StringUtil.isEmpty(uuList.getInvoiceCode())) {
                run.setReturnCode(ReturnCodeEnum.FPDMBNWK.getFhzDm());
                run.setReturnMessage(ReturnCodeEnum.FPDMBNWK.getFhzMc(ReturnCodeEnum.FPDMBNWK.getIndex()));
                return run;
            }
            if (StringUtil.isEmpty(uuList.getInvoiceNo())) {
                run.setReturnCode(ReturnCodeEnum.FPHMBNWK.getFhzDm());
                run.setReturnMessage(ReturnCodeEnum.FPHMBNWK.getFhzMc(ReturnCodeEnum.FPHMBNWK.getIndex()));
                return run;
            }
        }
        return run;
    }

    /**
     * 签收转换发票实体
     *
     * @param signInfo
     * @return
     */
    private SignInvoiceInfo getSignInfo(InvoiceElectron signInfo) {
        SignInvoiceInfo signInvoiceInfo = new SignInvoiceInfo();
        signInvoiceInfo.setBuyerTaxno(signInfo.getGfTaxNo());
        signInvoiceInfo.setInvoiceCode(signInfo.getInvoiceCode());
        signInvoiceInfo.setInvoiceNo(signInfo.getInvoiceNo());
        if (signInfo.getQsDate() != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            signInvoiceInfo.setQsDate(sdf.format(signInfo.getQsDate()));
        } else {
            signInvoiceInfo.setQsDate("");
        }
        if (StringUtil.isEmpty(signInfo.getUserName())) {
            signInvoiceInfo.setQsName("");
        } else {
            signInvoiceInfo.setQsName(signInfo.getUserName());
        }
        if (StringUtil.isEmpty(signInfo.getQsStatus())) {
            signInvoiceInfo.setQsStatus("");
        } else {
            signInvoiceInfo.setQsStatus(signInfo.getQsStatus());
        }
        if (StringUtil.isEmpty(signInfo.getQsType())) {
            signInvoiceInfo.setQsType("");
        } else {
            signInvoiceInfo.setQsType(signInfo.getQsType());
        }
        return signInvoiceInfo;
    }

    /**
     * 数据校验
     *
     * @param data
     * @return
     */
    private ReturnStateInfo getPayCheck(PayInfo data) {
        ReturnStateInfo run = new ReturnStateInfo();
        String taxno = data.getTaxNo();
        if (StringUtil.isEmpty(taxno)) {
            run.setReturnCode(ReturnCodeEnum.SHBNWK.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.SHBNWK.getFhzMc(ReturnCodeEnum.SHBNWK.getIndex()));
            return run;
        }
        if (data.getInvoices() == null || data.getInvoices().size() <= 0) {
            run.setReturnCode(ReturnCodeEnum.NRYCY.getFhzDm());
            run.setReturnMessage(ReturnCodeEnum.NRYCY.getFhzMc(ReturnCodeEnum.NRYCY.getIndex()));
            return run;
        }
        for (PayInfoSyn payList : data.getInvoices()) {
            if (StringUtil.isEmpty(payList.getInvoiceCode())) {
                run.setReturnCode(ReturnCodeEnum.FPDMBNWK.getFhzDm());
                run.setReturnMessage(ReturnCodeEnum.FPDMBNWK.getFhzMc(ReturnCodeEnum.FPDMBNWK.getIndex()));
                return run;
            }
            if (StringUtil.isEmpty(payList.getInvoiceNo())) {
                run.setReturnCode(ReturnCodeEnum.FPHMBNWK.getFhzDm());
                run.setReturnMessage(ReturnCodeEnum.FPHMBNWK.getFhzMc(ReturnCodeEnum.FPHMBNWK.getIndex()));
                return run;
            }
        }
        return run;
    }
}
