package com.jianshui.invoice.service.impl.api;

import java.util.Date;

import cn.hutool.core.collection.CollectionUtil;
import com.jianshui.common.core.redis.RedisCache;
import com.jianshui.common.utils.BeanToMapUtils;
import com.jianshui.common.utils.spring.SpringUtils;
import com.jianshui.invoice.domain.dto.api.*;
import com.jianshui.invoice.domain.dto.api.jcsk.*;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jianshui.common.constant.WebServiceConstant;
import com.jianshui.common.core.domain.AjaxResult;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.enums.ErrorCode;
import com.jianshui.common.utils.StringUtils;
import com.jianshui.common.utils.TimeUtil;
import com.jianshui.common.utils.ValidateUtils;
import com.jianshui.common.utils.jcsk.ApiHttp;
import com.jianshui.common.utils.uuid.IdUtils;
import com.jianshui.framework.manager.AsyncManager;
import com.jianshui.invoice.domain.*;
import com.jianshui.invoice.domain.dto.*;
import com.jianshui.invoice.domain.dto.api.jcsk.add.InvoiceAllAddInfoTwoDTO;
import com.jianshui.invoice.domain.dto.api.jcsk.add.InvoiceAllAddTwoDTO;
import com.jianshui.invoice.domain.dto.api.jcsk.add.InvoiceAllAddmxListTwoDTO;
import com.jianshui.invoice.domain.dto.api.jcsk.red.AddRedInvoiceTwoDTO;
import com.jianshui.invoice.domain.ele.EleNewMessage;
import com.jianshui.invoice.domain.vo.InvoiceAllQycodeResultElEVO;
import com.jianshui.invoice.domain.vo.jcsk.*;
import com.jianshui.invoice.mapper.*;
import com.jianshui.invoice.service.IInvoiceApiService;
import com.jianshui.system.domain.InvoiceAllApiLog;
import com.jianshui.system.domain.InvoiceAllYhdj;
import com.jianshui.system.mapper.InvoiceAllApiLogMapper;
import com.jianshui.system.mapper.InvoiceAllYhdjMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 金财数科
 */
@Component("jcsk_invoice")
@Slf4j
public class JcskInvoiceApiServiceImpl implements IInvoiceApiService {

    @Autowired
    private BillInfoMapper billInfoMapper;
    @Autowired
    private InvoiceMapper invoiceMapper;

    @Autowired
    private SkDyjMapper skDyjMapper;

    @Autowired
    private RedinfoMapper redinfoMapper;

    @Autowired
    private InvoiceAllApiLogMapper allApiLogMapper;

    @Autowired
    private InvoiceAllYhdjMapper yhdjMapper;


    /**
     * 发票开具
     *
     * @param billInfo
     * @param companyservice
     * @return com.jianshui.invoice.domain.dto.HXResponse
     * @author dxk
     * @date 2022/6/27 16:50
     */
    @Override
    public HXResponse addInvoice(BillInfo billInfo, Companyservice companyservice) throws IllegalAccessException, InstantiationException {

        // 请求billInfo
        Long billInfoID = 0L;

        // kptype 1蓝票 2红票
        String kpType = billInfo.getKptype();

        // 查询用户登记参数
        InvoiceAllYhdj yhdj = getUserInfo(companyservice);
        if (BeanUtil.isEmpty(yhdj)) {
            return new HXResponse("未查询到登记信息！");
        }

        QueryTwoDTO queryDTO = new QueryTwoDTO();
        queryDTO.setDqbm(yhdj.getDqbm());
        queryDTO.setAsync("false");
        queryDTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());
        queryDTO.setNsrsbh(yhdj.getNsrsbh());
        queryDTO.setRequestId("tycxjk");

        if ("1".equals(kpType)) {
            // 开票实体转换,校验
            InvoiceAllAddTwoDTO invoiceAllAddTwoDTO = BeanUtil.copyProperties(billInfo, InvoiceAllAddTwoDTO.class);
            invoiceAllAddTwoDTO.setDqbm(queryDTO.getDqbm());
            invoiceAllAddTwoDTO.setBsrysfzjhm(queryDTO.getBsrysfzjhm());
            invoiceAllAddTwoDTO.setNsrsbh(queryDTO.getNsrsbh());
            invoiceAllAddTwoDTO.setAsync("true");
            ValidateUtils.validate(invoiceAllAddTwoDTO);

            // 忽略明细属性
            InvoiceAllAddInfoTwoDTO invoiceAllAddInfoTwoDTO = BeanUtil.copyProperties(billInfo, InvoiceAllAddInfoTwoDTO.class, "fjxxList", "mxList", "tdywxx", "ext");
            // 发票类型处理
            ValidateUtils.validate(invoiceAllAddInfoTwoDTO);

            // 明细集合
            List<InvoiceAllAddmxListTwoDTO> mxList = BeanUtil.copyToList(billInfo.getBillDetailList(), InvoiceAllAddmxListTwoDTO.class);
            if (CollectionUtils.isEmpty(mxList)) {
                return new HXResponse("mxList明细集合不存在！");
            }
            for (InvoiceAllAddmxListTwoDTO addmxListDTO : mxList) {
                ValidateUtils.validate(addmxListDTO);
            }

            String invoiceType = getInvoiceType(billInfo.getInvoiceType(), false);
            if (StrUtil.isEmpty(invoiceType)) {
                return new HXResponse("发票类型代码不存在！");
            }
            invoiceAllAddInfoTwoDTO.setFplxdm(invoiceType);
            invoiceAllAddInfoTwoDTO.setMxList(mxList);
            invoiceAllAddTwoDTO.setJsonData(invoiceAllAddInfoTwoDTO);

            // 报文转换存储
            billInfo.setCompanyId(companyservice.getCompanyid());
            billInfo.setServiceSupplierKey("JCSK");

            billInfo.setInvoiceType(billInfo.getInvoiceType());
            String systemOrderno = IdUtils.randomSystemOrderno();
            billInfo.setSystemOrderno(systemOrderno);
            billInfo.setOutTradeOrderno(systemOrderno);
            if (BeanUtil.isEmpty(billInfo.getClerk())) {
                billInfo.setClerk("");
            }
            try {
                int resBillInfo = billInfoMapper.insertBillInfo(billInfo);
                if (resBillInfo > 0) {
                    billInfoID = billInfo.getId();
                    if (BeanUtil.isEmpty(billInfo.getId())) {
                        log.error("【金四服务类】【数科】【蓝字发票开具】API请求异常，billInfoId返回存储错误。");
                        return new HXResponse("蓝字发票开具内部错误！");
                    }
                    //数据库存储
                    List<BillDetail> billDetailList = billInfo.getBillDetailList();
                    Long index = 0L;
                    for (BillDetail billDetail : billDetailList) {
                        billDetail.setBillInfoId(billInfoID);
                        billDetail.setIndex(++index);
                    }
                    int billDetailResult = billInfoMapper.batchBillDetail(billDetailList);
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("【金四服务类】【数科】【蓝字发票开具】API请求异常，billInfo存储错误。错误信息:{}", e.getMessage());
                return new HXResponse("蓝字发票开具内部错误！");
            }

            cn.hutool.json.JSONObject result = null;

            try {
                Map<String, Object> map = BeanToMapUtils.fastJsonBean2Map(invoiceAllAddTwoDTO);
                result = ApiHttp.request(WebServiceConstant.LZFPKJ, WebServiceConstant.URL, JSONUtil.parse(map), companyservice);

                String code = result.get("code") != null ? result.get("code").toString() : "";
                String msg = result.get("msg") != null ? result.get("msg").toString() : "";
                String data = result.get("data") != null ? result.get("data").toString() : "";

                if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                    cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                    String requestId = json.getOrDefault("requestId", "").toString();
                    if (StrUtil.isEmpty(requestId)) {
                        return new HXResponse(msg);
                    }
                    BillInfo billInfoRes = billInfoMapper.selectBillInfoById(billInfoID);
                    billInfoRes.setOutTradeOrderno(requestId);
                    billInfoMapper.updateBillInfo(billInfoRes);

                    // 保存日志
                    InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                    allApiLog.setUrl(WebServiceConstant.LZFPKJ);
                    allApiLog.setSendMsg("");
                    allApiLog.setResultMsg("");
                    allApiLog.setCompany(companyservice.getSellertax());
                    allApiLog.setIdentityId(companyservice.getIdentity());
                    allApiLog.setRequestId(requestId);
                    allApiLog.setCreateTime(new Date());
                    AsyncManager.me().execute(new TimerTask() {
                        @Override
                        public void run() {
                            allApiLogMapper.insertInvoiceAllApiLog(allApiLog);
                        }
                    });

                    HXResponse response = new HXResponse("0000", "同步成功");
                    response.put("fpqqlsh", requestId);
                    return response;
                } else {
                    return new HXResponse(msg);
                }
            } catch (Exception e) {
                log.error("【金四服务类】【数科】【蓝字发票开具】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
                return new HXResponse("蓝字发票开具异常");
            }

        } else if ("2".equals(kpType)) {
            //billinfo => 红字报文
            AddRedInvoiceTwoDTO addRedInvoiceDTO = BeanUtil.copyProperties(billInfo, AddRedInvoiceTwoDTO.class);
            ValidateUtils.validate(addRedInvoiceDTO);
            addRedInvoiceDTO.setAsync("true");
            addRedInvoiceDTO.setBsrysfzjhm(queryDTO.getBsrysfzjhm());
            addRedInvoiceDTO.setDqbm(queryDTO.getDqbm());
            addRedInvoiceDTO.setNsrsbh(queryDTO.getNsrsbh());

            QueryWrapper<Redinfo> redinfoQueryWrapper = new QueryWrapper<>();
            redinfoQueryWrapper.eq("red_info_no", addRedInvoiceDTO.getHzfpxxqrdid());
            Redinfo redinfo = redinfoMapper.selectOne(redinfoQueryWrapper);

            cn.hutool.json.JSONObject result = null;
            try {
                result = ApiHttp.request(WebServiceConstant.ADD_RED_INVOICE, WebServiceConstant.URL, addRedInvoiceDTO, companyservice);
            } catch (Exception e) {
                e.printStackTrace();
                log.error("【金四服务类】【数科】【获取二维码】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
                return new HXResponse(ErrorCode.INCOME_ERROR.toString());
            }

            // 处理请求
            if (!StringUtils.equals("0000", result.getStr("code"))) {
                log.error("【金四服务类】【数科】【获取二维码】API请求异常，外部报文返回code非0000。返回信息:{}", result);
                return new HXResponse(result.getStr("message"));
            }

            cn.hutool.json.JSONObject data = result.getJSONObject("data");
            if (data == null) {
                return new HXResponse(ErrorCode.INCOME_ERROR.toString());
            }
            String requestId = data.getStr("requestId");

            HXResponse response = new HXResponse("0000", "同步成功");
            response.put("fpqqlsh", requestId);
            return response;
        } else {
            return new HXResponse(ErrorCode.INCOME_ERROR + "未获取到开票类型！".toString());
        }

    }



    /**
     * 发票打印
     *
     * @param invoice
     * @param companyservice
     * @return com.jianshui.common.core.domain.AjaxResult
     * @author dxk
     * @date 2022/6/29 13:48
     */
    @Override
    public AjaxResult printInvoice(Invoice invoice, Companyservice companyservice) {
        return null;
    }


    /**
     * 发票作废
     *
     * @param invoice
     * @param companyservice
     * @return com.jianshui.invoice.domain.dto.HXResponse
     * @author dxk
     * @date 2022/6/30 9:43
     */
    @Override
    public HXResponse deprecateInvoice(JSONObject invoice, Companyservice companyservice) {
        return null;
    }


    @Override
    public AjaxResult reInvoice(BillInfo billInfo, Companyservice companyservice) {
        return null;
    }

    /**
     * 单个发票查询
     *
     * @param billInfo
     * @param companyservice
     * @return
     */
    @Override
    public HXResponse queryInvoice(QueryBillInfoDTO billInfo, Companyservice companyservice) {

        return null;

    }

    /**
     * 数科报文=》简税报文转换
     *
     * @param redInvoiceQueryAllTWOVO
     * @return
     */
    private List<Redinfo> parseRedINFO(RedInvoiceQueryAllTWOVO redInvoiceQueryAllTWOVO) {
        List<Redinfo> redinfos = new ArrayList<>();

        // 页数
        int pageNumber = redInvoiceQueryAllTWOVO.getPageNumber();
        // 每页条数
        int pageSize = redInvoiceQueryAllTWOVO.getPageSize();
        // 总条数
        int total = redInvoiceQueryAllTWOVO.getTotal();
        ;
        //发票集合
        List<RedInvoiceQueryTWOVO> redInvoiceQueryTWOVOS = redInvoiceQueryAllTWOVO.getFpList();

        for (RedInvoiceQueryTWOVO redInvoiceQueryTWOVO : redInvoiceQueryTWOVOS) {

            RedinfoCopyVO redinfoVO = BeanUtil.copyProperties(redInvoiceQueryTWOVO, RedinfoCopyVO.class);
            Redinfo redinfo = BeanUtil.copyProperties(BeanToMapUtils.fastJsonBean2Map(redinfoVO), Redinfo.class);

            // TODO: 2023/4/6 明细接口数科还没对
            // redinfo.setRedinfodetailList(null);

            redinfos.add(redinfo);
        }

        return redinfos;
    }


    /**
     * 发票批量查询
     *
     * @param billInfo
     * @param byWhat
     * @param companyservice
     * @return
     */
    @Override
    public HXResponse batchQueryInvoices(BillInfo billInfo, String byWhat, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse scopeQueryInvoices(JSONObject queryJson, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse queryRedConfirmationList(JSONObject decryptResult, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse queryRedConfirmationItemList(JSONObject decryptResult, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse redConfirmationApply(JSONObject decryptResult, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse redConfirmationConfirm(JSONObject decryptResult, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse rpaLogin(JSONObject decryptResult, Companyservice companyservice) {
        return null;
    }



    @Override
    public HXResponse getRpaAuthStatus(JSONObject decryptResult, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse queryRpaTaxAccount(JSONObject decryptResult, Companyservice companyservice) {
        return null;
    }

    /**
     * 红字信息表上传
     */
    @Override
    public HXResponse uploadRedInfo(Redinfo redinfo, Companyservice companyservice) {

        // 金财数科数据处理 start
        // 查询用户登记参数
        InvoiceAllYhdj yhdj = getUserInfo(companyservice);
        if (BeanUtil.isEmpty(yhdj)) {
            return new HXResponse("未查询到登记信息！");
        }

        SqhzTwoDTO sqhzDTO = BeanUtil.copyProperties(redinfo, SqhzTwoDTO.class);
        sqhzDTO.setAsync("true");
        sqhzDTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());
        sqhzDTO.setDqbm(yhdj.getDqbm());
        sqhzDTO.setNsrsbh(yhdj.getNsrsbh());
        ValidateUtils.validate(sqhzDTO);

        SqhzJsonDataTwoDTO sqhzJsonDataTwoDTO = BeanUtil.copyProperties(redinfo, SqhzJsonDataTwoDTO.class);
        ValidateUtils.validate(sqhzJsonDataTwoDTO);

        // redInfo 前置处理 start
        redinfo.setCompanyId(companyservice.getCompanyid());
        String id = IdUtils.randomSystemOrderno();
        redinfo.setSystemOrderno(id);
        redinfo.setOutTradeOrderno(id);
        redinfo.setFpqqlsh(id);
        redinfo.setRedInfoStatus("0");
        redinfo.setSellerName(companyservice.getSellername());
        redinfo.setSellerTaxnum(companyservice.getSellertax());
        redinfo.setBillInputTime(TimeUtil.dateFormat(new Date(), "yyyyMMddHHmmss"));
        // 纸票为必填
        redinfo.setBuyerName("全电");
        redinfo.setBuyerTaxnum("全电");
        redinfo.setSellerName("全电");
        redinfo.setSellerTaxnum("全电");
        redinfoMapper.insertRedinfo(redinfo);
        int index = 0;
        List<Redinfodetail> redinfodetailList = redinfo.getRedinfodetailList();
        if (CollectionUtil.isNotEmpty(redinfodetailList)) {
            redinfoMapper.batchRedinfodetail(redinfodetailList);
        }
        // end


        List<SqhzJsonDataDetailTwoDTO> sqhzJsonDataDetailTwoDTOS = BeanUtil.copyToList(redinfo.getRedinfodetailList(), SqhzJsonDataDetailTwoDTO.class);
        if (CollectionUtil.isNotEmpty(sqhzJsonDataDetailTwoDTOS)) {
            sqhzJsonDataTwoDTO.setHzqrxxmxlist(sqhzJsonDataDetailTwoDTOS);
        }
        sqhzDTO.setJsonData(sqhzJsonDataTwoDTO);

        cn.hutool.json.JSONObject result = null;
        try {
            result = ApiHttp.request(WebServiceConstant.SQHZFPXXQRD, WebServiceConstant.URL, JSONUtil.parseObj(BeanToMapUtils.fastJsonBean2Map(sqhzDTO)), companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                String requestId = json.getOrDefault("requestId", "").toString();
                if (StrUtil.isEmpty(requestId)) {
                    return new HXResponse(msg);
                }
                InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                allApiLog.setUrl(WebServiceConstant.SQHZFPXXQRD);
                allApiLog.setSendMsg(sqhzDTO.toString());
                allApiLog.setResultMsg(JSONUtil.toJsonStr(result));
                allApiLog.setCompany(companyservice.getSellertax());
                allApiLog.setIdentityId(companyservice.getIdentity());
                allApiLog.setRequestId(requestId);
                allApiLog.setCreateTime(new Date());
                allApiLogMapper.insertInvoiceAllApiLog(allApiLog);

                // 更新响应ID
                redinfo.setOutTradeOrderno(requestId);
                redinfoMapper.updateRedinfo(redinfo);

                HXResponse response = new HXResponse("0000", "申请成功");
                response.put("queryid", requestId);
                return response;

            } else {
                return new HXResponse(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【申请红字信息表】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("通用查询接口异常");

        }
    }

    @Override
    public AjaxResult queryRedInfo(Redinfo redinfo, Companyservice companyservice) {
        return null;
    }

    /**
     * 红字信息表下载
     */
    @Override
    public HXResponse downloadRedInfo(DownloadRedInfoDTO downloadRedInfoDTO, Companyservice companyservice) {

        // 查找之前的红票
        Redinfo redinfo = new Redinfo();
        redinfo.setRedInfoNo(downloadRedInfoDTO.getBillInfoNo());
        redinfo.setCompanyId(companyservice.getCompanyid());
        QueryWrapper<Redinfo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(downloadRedInfoDTO.getInvoiceLine())) {
            queryWrapper.eq("invoice_type", downloadRedInfoDTO.getInvoiceLine());
        }
        if (StringUtils.isNotEmpty(downloadRedInfoDTO.getBuyerTaxNo())) {
            queryWrapper.eq("buyer_taxnum", downloadRedInfoDTO.getBuyerTaxNo());
        }
        if (StringUtils.isNotEmpty(downloadRedInfoDTO.getExtensionNumber())) {
            queryWrapper.eq("fjh", downloadRedInfoDTO.getExtensionNumber());
        }
        if (StringUtils.isNotEmpty(downloadRedInfoDTO.getTerminalNumber())) {
            queryWrapper.eq("terminal_number", downloadRedInfoDTO.getTerminalNumber());
        }
        if (StringUtils.isNotEmpty(downloadRedInfoDTO.getBillTimeBegin())) {
            queryWrapper.ge("bill_input_time", downloadRedInfoDTO.getBillTimeBegin());
        }
        if (StringUtils.isNotEmpty(downloadRedInfoDTO.getBillTimeEnd())) {
            queryWrapper.le("bill_input_time", downloadRedInfoDTO.getBillTimeEnd());
        }
        if (StringUtils.isNotEmpty(downloadRedInfoDTO.getBillNo())) {
            queryWrapper.le("out_trade_orderno", downloadRedInfoDTO.getBillNo());
        }
        queryWrapper.orderByDesc("id");
        queryWrapper.last(" limit 1 ");
        Redinfo redInfoResult = redinfoMapper.selectOne(queryWrapper);
        if (redInfoResult == null) {
            HXResponse response = new HXResponse("9999", "未查到指定红字信息");
            return response;
        }


        // 查询用户登记参数
        InvoiceAllYhdj yhdj = getUserInfo(companyservice);
        if (BeanUtil.isEmpty(yhdj)) {
            return new HXResponse("未查询到登记信息！");
        }

        // 这里直接用传入的报文进行转换
        CxhzTwoDTO cxhzDTO = BeanUtil.copyProperties(downloadRedInfoDTO, CxhzTwoDTO.class);
        cxhzDTO.setAsync("true");
        cxhzDTO.setNsrsbh(yhdj.getNsrsbh());
        cxhzDTO.setDqbm(yhdj.getDqbm());
        cxhzDTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());

        CxhzJsonDataTwoDTO cxhzJsonDataDTO = BeanUtil.copyProperties(downloadRedInfoDTO, CxhzJsonDataTwoDTO.class);

        ValidateUtils.validate(cxhzDTO);
        ValidateUtils.validate(cxhzJsonDataDTO);

        cxhzDTO.setJsonData(cxhzJsonDataDTO);


        cn.hutool.json.JSONObject result = null;
        try {
            Map<String,Object> mapParam = BeanToMapUtils.fastJsonBean2Map(cxhzDTO);
            result = ApiHttp.request(WebServiceConstant.CXHZFPXXQRD, WebServiceConstant.URL, mapParam, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                String requestId = json.getOrDefault("requestId", "").toString();
                if (StrUtil.isEmpty(requestId)) {
                    return new HXResponse(msg);
                }

                InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                allApiLog.setUrl(WebServiceConstant.CXHZFPXXQRD);
                allApiLog.setSendMsg(cxhzDTO.toString());
                allApiLog.setResultMsg(JSONUtil.toJsonStr(result));
                allApiLog.setCompany(companyservice.getSellertax());
                allApiLog.setIdentityId(companyservice.getIdentity());
                allApiLog.setRequestId(requestId);
                allApiLog.setCreateTime(new Date());
                allApiLogMapper.insertInvoiceAllApiLog(allApiLog);

                HXResponse response = new HXResponse("0000", "申请成功");
                response.put("queryid", requestId);
                return response;

            } else {
                return new HXResponse(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【查询红字信息表】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("查询红字信息表接口异常");

        }


    }


    @Override
    public HXResponse deprecateQueryInvoice(DeprecateQueryInvoiceDTO invoice, Companyservice companyservice) {
        return null;
    }


    /**
     * 打印机查询
     *
     * @param queryPrinterDTO
     * @param companyservice
     * @return
     */
    @Override
    public HXResponse queryPrinter(QueryPrinterDTO queryPrinterDTO, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse pushData(BillInfo billInfo, Companyservice companyservice) {
        return null;
    }

    /**
     * 发票打印
     *
     * @param printDTO
     * @param companyservice
     * @return
     */
    @Override
    public HXResponse batchPrint(PrintDTO printDTO, Companyservice companyservice) {

        return null;

    }

    @Override
    public AjaxResult syncStockInfo(Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse signInvoice(SignESealDTO signESealDTO, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse createESeal(CreateESealDTO createESealDTO, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse registerESeal(RegisterESealDTO createESealDTO, Companyservice companyservice) {
        return null;
    }

    @Override
    public AjaxResult SummaryReport(SummaryReportQueryDTO summaryReportQueryDTO, Companyservice companyservice) {
        return null;
    }

    @Override
    public Object callback(HttpServletRequest request) {
        return null;
    }


    /**
     * 简税转换总部控制台发票类型
     *
     * @param invoiceType
     * @return java.lang.Integer
     * @author dxk
     * @date 2022/6/28 20:17
     */
    public static Integer CovertInvoiceType(String invoiceType) {

        /**
         * 发票种类 invoice_type（ p电子增值税普通发票 c增值税普通发票 s 增值税专用发票 e 收购发票 f 收购发票 r增值税普通发票）
         */
        if (StrUtil.isEmpty(invoiceType)) {
            return -1;
        }
        if ("p".equals(invoiceType) || "c".equals(invoiceType) || "e".equals(invoiceType) || "f".equals(invoiceType) || "r".equals(invoiceType)) {
            return 2;
        } else if ("s".equals(invoiceType)) {
            return 0;
        }

        return -1;
    }

    /**
     * 总部控制台转简税发票类型
     *
     * @param invoiceType
     * @return java.lang.String
     * @author dxk
     * @date 2022/6/29 17:12
     */
    public static String CovertInvoiceTypeReverse(String invoiceType) {

        if (invoiceType.equals("2")) {
            return "p";
        } else if (invoiceType.equals("0")) {
            return "s";
        }
        return "";
    }


    /**
     * 金财数科新增接口
     **/

    @Override
    public Object addCallback(HttpServletRequest request) {
        return null;
    }

    @Override
    public Object decryptCallback(HttpServletRequest request) {
        return null;
    }


    @Override
    public HXResponse getFileUrl(InvoiceAllFileAcquisitionTWODTO invoiceAllFileAcquisitionTWODTO, Companyservice companyservice) {

        FileAcquisitionTwoDTO fileAcquisitionDTO = BeanUtil.copyProperties(invoiceAllFileAcquisitionTWODTO, FileAcquisitionTwoDTO.class);

        // 查询用户信息组装
        InvoiceAllYhdj yhdj = getUserInfo(companyservice);
        if (BeanUtil.isEmpty(yhdj)) {
            return new HXResponse("未查询到登记信息！");
        }

        fileAcquisitionDTO.setAsync("true");
        fileAcquisitionDTO.setNsrsbh(yhdj.getNsrsbh());
        fileAcquisitionDTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());
        fileAcquisitionDTO.setDqbm(yhdj.getDqbm());

        Map<String, Object> map = new JSONObject();
        map.put("fphm", fileAcquisitionDTO.getFphm());
        map.put("kprq", fileAcquisitionDTO.getKprq());
        fileAcquisitionDTO.setJsonData(map);

        ValidateUtils.validate(fileAcquisitionDTO);

        cn.hutool.json.JSONObject result = null;
        try {
            result = ApiHttp.request(WebServiceConstant.FPEWMXZ, WebServiceConstant.URL, fileAcquisitionDTO, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                String requestId = json.getOrDefault("requestId", "").toString();
                if (StrUtil.isEmpty(requestId)) {
                    return new HXResponse(msg);
                }
                InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                allApiLog.setUrl(WebServiceConstant.FPEWMXZ);
                allApiLog.setSendMsg(fileAcquisitionDTO.toString());
                allApiLog.setResultMsg(JSONUtil.toJsonStr(result));
                allApiLog.setCompany(companyservice.getSellertax());
                allApiLog.setIdentityId(companyservice.getIdentity());
                allApiLog.setRequestId(requestId);
                allApiLog.setCreateTime(new Date());
                allApiLogMapper.insertInvoiceAllApiLog(allApiLog);

                HXResponse response = new HXResponse("0000", "同步成功");
                response.put("data", JSONUtil.parse(data));
                return response;
            } else {
                return new HXResponse(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【获取发票下载地址】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("获取发票下载地址错误！");

        }


    }

    @Override
    public HXResponse getFileAcquisitionOn(InvoiceAllFileAcquisitionOnTWODTO invoiceAllFileAcquisitionOnTWODTO, Companyservice companyservice) {

        FileAcquisitionOnTwoDTO fileAcquisitionOnDTO = new FileAcquisitionOnTwoDTO();
        FileAcquisitionOnDetailTwoDTO fileAcquisitionOnDetailDTO = BeanUtil.copyProperties(invoiceAllFileAcquisitionOnTWODTO, FileAcquisitionOnDetailTwoDTO.class);
        fileAcquisitionOnDTO.setJsonData(fileAcquisitionOnDetailDTO);

        // 查询用户信息组装
        InvoiceAllYhdj yhdj = getUserInfo(companyservice);
        if (BeanUtil.isEmpty(yhdj)) {
            return new HXResponse("未查询到登记信息！");
        }
        fileAcquisitionOnDTO.setAsync("true");
        fileAcquisitionOnDTO.setNsrsbh(yhdj.getNsrsbh());
        fileAcquisitionOnDTO.setDqbm(yhdj.getDqbm());
        fileAcquisitionOnDTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());

        ValidateUtils.validate(fileAcquisitionOnDTO);
        ValidateUtils.validate(fileAcquisitionOnDetailDTO);

        cn.hutool.json.JSONObject result = null;
        try {
            result = ApiHttp.request(WebServiceConstant.QDFPWJHQ, WebServiceConstant.URL, fileAcquisitionOnDTO, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                String requestId = json.getOrDefault("requestId", "").toString();
                if (StrUtil.isEmpty(requestId)) {
                    return new HXResponse(msg);
                }
                InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                allApiLog.setUrl(WebServiceConstant.QDFPWJHQ);
                allApiLog.setSendMsg(fileAcquisitionOnDTO.toString());
                allApiLog.setResultMsg(JSONUtil.toJsonStr(result));
                allApiLog.setCompany(companyservice.getSellertax());
                allApiLog.setIdentityId(companyservice.getIdentity());
                allApiLog.setRequestId(requestId);
                allApiLog.setCreateTime(new Date());
                allApiLogMapper.insertInvoiceAllApiLog(allApiLog);

                HXResponse response = new HXResponse("0000", "同步成功", JSONUtil.parse(data));
                return response;
            } else {
                return new HXResponse(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【全电安排文件获取】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("全电发票文件获取错误！");

        }
    }

    /**
     * 获取人脸识别二维码
     */
    @Override
    public HXResponse qrcode(JSONObject decryptResult, Companyservice companyservice) {
        cn.hutool.json.JSONObject result = null;
        try {
            result = ApiHttp.request(WebServiceConstant.QRCODE, WebServiceConstant.URL, decryptResult, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                String requestId = json.getOrDefault("requestId", "").toString();
                if (StrUtil.isEmpty(requestId)) {
                    return new HXResponse(msg);
                }

                InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                allApiLog.setUrl(WebServiceConstant.QRCODE);
                allApiLog.setSendMsg(JSONUtil.toJsonStr(decryptResult));
                allApiLog.setResultMsg(JSONUtil.toJsonStr(result));
                allApiLog.setCompany(companyservice.getSellertax());
                allApiLog.setIdentityId(companyservice.getIdentity());
                allApiLog.setRequestId(requestId);
                allApiLog.setCreateTime(new Date());
                allApiLogMapper.insertInvoiceAllApiLog(allApiLog);

                HXResponse response = new HXResponse("0000", "请求成功", json);
                return response;
            } else {
                return new HXResponse(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【获取二维码】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("获取二维码错误！");

        }

        // TODO 处理日志
//        log.info("【金四服务类】【数科】【获取二维码】API请求成功，外部报文返回code。返回信息:{}", result);

//        return AjaxResult.success(result);
    }

    @Override
    public HXResponse query(InvoiceAllFileQueryTWODTO invoiceAllFileQueryTWODTO, Companyservice companyservice) {

        FileQueryTWODTO fileQueryTWODTO = new FileQueryTWODTO();
        FileQueryJsonDataTWODTO fileQueryJsonDataTWODTO = BeanUtil.copyProperties(invoiceAllFileQueryTWODTO, FileQueryJsonDataTWODTO.class);
        fileQueryTWODTO.setJsonData(fileQueryJsonDataTWODTO);

        // 查询用户信息组装
        InvoiceAllYhdj yhdj = getUserInfo(companyservice);
        if (BeanUtil.isEmpty(yhdj)) {
            return new HXResponse("未查询到登记信息！");
        }

        fileQueryTWODTO.setDqbm(yhdj.getDqbm());
        fileQueryTWODTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());
        fileQueryTWODTO.setNsrsbh(yhdj.getNsrsbh());
        fileQueryTWODTO.setAsync("true");

        cn.hutool.json.JSONObject result = null;
        try {
            result = ApiHttp.request(WebServiceConstant.QUERY, WebServiceConstant.URL, fileQueryTWODTO, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                String requestId = json.getOrDefault("requestId", "").toString();
                if (StrUtil.isEmpty(requestId)) {
                    return new HXResponse(msg);
                }
                InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                allApiLog.setUrl(WebServiceConstant.QUERY);
                allApiLog.setSendMsg(fileQueryTWODTO.toString());
                allApiLog.setResultMsg(JSONUtil.toJsonStr(result));
                allApiLog.setCompany(companyservice.getSellertax());
                allApiLog.setIdentityId(companyservice.getIdentity());
                allApiLog.setRequestId(requestId);
                allApiLog.setCreateTime(new Date());
                allApiLogMapper.insertInvoiceAllApiLog(allApiLog);

                HXResponse response = new HXResponse("0000", "同步成功", JSONUtil.parse(data));
                return response;
            } else {
                return new HXResponse(msg);
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【单张发票查询】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("发票查询错误");
        }

    }

    @Override
    public HXResponse getPrintXML(InvoiceAllPrintXMLTWODTO invoiceAllPrintXMLTWODTO, Companyservice companyservice) {

        // 查询用户信息组装
        InvoiceAllYhdj yhdj = getUserInfo(companyservice);
        if (BeanUtil.isEmpty(yhdj)) {
            return new HXResponse("未查询到登记信息！");
        }

        PrintXMLTWODTO printXMLTWODTO = new PrintXMLTWODTO();
        PrintXMLJsonDataTWODTO jsonDataTWODTO = BeanUtil.copyProperties(invoiceAllPrintXMLTWODTO, PrintXMLJsonDataTWODTO.class);
        jsonDataTWODTO.setNsrsbh(yhdj.getNsrsbh());
        printXMLTWODTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());
        printXMLTWODTO.setNsrsbh(yhdj.getNsrsbh());
        printXMLTWODTO.setAsync("true");
        printXMLTWODTO.setDqbm(yhdj.getDqbm());
        printXMLTWODTO.setJsonData(jsonDataTWODTO);

        ValidateUtils.validate(printXMLTWODTO);
        ValidateUtils.validate(jsonDataTWODTO);

        cn.hutool.json.JSONObject result = null;
        try {
            result = ApiHttp.request(WebServiceConstant.GET_PRINT_XML, WebServiceConstant.URL, printXMLTWODTO, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                String requestId = json.getOrDefault("requestId", "").toString();
                if (StrUtil.isEmpty(requestId)) {
                    return new HXResponse(msg);
                }
                InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                allApiLog.setUrl(WebServiceConstant.GET_PRINT_XML);
                allApiLog.setSendMsg(printXMLTWODTO.toString());
                allApiLog.setResultMsg(JSONUtil.toJsonStr(result));
                allApiLog.setCompany(companyservice.getSellertax());
                allApiLog.setIdentityId(companyservice.getIdentity());
                allApiLog.setRequestId(requestId);
                allApiLog.setCreateTime(new Date());
                allApiLogMapper.insertInvoiceAllApiLog(allApiLog);

                HXResponse response = new HXResponse("0000", "同步成功", JSONUtil.parse(data));
                return response;
            } else {
                return new HXResponse(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【获取发票打印XML】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("获取发票打印XML错误！");

        }

    }

    @Override
    public HXResponse issueFastRedInvoice(InvoiceAllKshcJsonDataTWODTO invoiceAllKshcDTO, Companyservice companyservice) {

        // 查询用户信息组装
        InvoiceAllYhdj yhdj = getUserInfo(companyservice);
        if (BeanUtil.isEmpty(yhdj)) {
            return new HXResponse("未查询到登记信息！");
        }

        KshcTwoDTO kshcDTO = new KshcTwoDTO();
        KshcJsonDataTwoDTO jsonDataTwoDTO = BeanUtil.copyProperties(invoiceAllKshcDTO, KshcJsonDataTwoDTO.class);
        kshcDTO.setAsync(true);
        kshcDTO.setNsrsbh(yhdj.getNsrsbh());
        kshcDTO.setDqbm(yhdj.getDqbm());
        kshcDTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());
        kshcDTO.setJsonData(jsonDataTwoDTO);

        ValidateUtils.validate(kshcDTO);
        ValidateUtils.validate(jsonDataTwoDTO);

        // redInfo 前置处理 start
        Redinfo redinfo = new Redinfo();
        redinfo.setCompanyId(companyservice.getCompanyid());
        String id = IdUtils.randomSystemOrderno();
        redinfo.setSystemOrderno(id);
        redinfo.setOutTradeOrderno(id);
        redinfo.setFpqqlsh(id);
        redinfo.setRedInfoStatus("0");
        redinfo.setSellerName(companyservice.getSellername());
        redinfo.setSellerTaxnum(companyservice.getSellertax());
        redinfo.setBillInputTime(TimeUtil.dateFormat(new Date(), "yyyyMMddHHmmss"));
        // 纸票为必填
        redinfo.setBuyerName("全电");
        redinfo.setBuyerTaxnum("全电");
        redinfo.setSellerName("全电");
        redinfo.setSellerTaxnum("全电");
        redinfo.setServiceSupplierKey("0");

        redinfo.setOriginFphm(invoiceAllKshcDTO.getYfphm());
        redinfo.setApplyRemark(invoiceAllKshcDTO.getChyydm());
        redinfoMapper.insertRedinfo(redinfo);
        // end

        cn.hutool.json.JSONObject result = null;
        try {
            result = ApiHttp.request(WebServiceConstant.KSHC, WebServiceConstant.URL, kshcDTO, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                String requestId = json.getOrDefault("requestId", "").toString();
                if (StrUtil.isEmpty(requestId)) {
                    return new HXResponse(msg);
                }
                InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                allApiLog.setUrl(WebServiceConstant.KSHC);
                allApiLog.setSendMsg(kshcDTO.toString());
                allApiLog.setResultMsg(JSONUtil.toJsonStr(result));
                allApiLog.setCompany(companyservice.getSellertax());
                allApiLog.setIdentityId(companyservice.getIdentity());
                allApiLog.setRequestId(requestId);
                allApiLog.setCreateTime(new Date());
                allApiLogMapper.insertInvoiceAllApiLog(allApiLog);

                redinfo.setOutTradeOrderno(requestId);
                redinfoMapper.updateRedinfo(redinfo);

                HXResponse response = new HXResponse("0000", "同步成功", JSONUtil.parse(data));
                return response;

            } else {
                return new HXResponse(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【快速红冲】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("快速红冲错误！");

        }


    }

    @Override
    public HXResponse invoiceStatic(JSONObject decryptResult, Companyservice companyservice) {
        // com.alibaba.fastjson.JSONObject jsonObj = requestBody.getJSONObject("jsonData");
        // if (jsonObj == null) {
        //     return AjaxResult.error(ErrorCode.INVOICE_NOT_EXISTS);
        // }

        // 查询用户信息组装
        InvoiceAllYhdj yhdj = getUserInfo(companyservice);
        if (BeanUtil.isEmpty(yhdj)) {
            return new HXResponse("未查询到登记信息！");
        }
        StaticTWODTO staticTWODTO = new StaticTWODTO();
        staticTWODTO.setDqbm(yhdj.getDqbm());
        staticTWODTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());
        staticTWODTO.setNsrsbh(yhdj.getNsrsbh());
        staticTWODTO.setAsync(true);

        ValidateUtils.validate(staticTWODTO);

        cn.hutool.json.JSONObject result = null;
        try {
            result = ApiHttp.request(WebServiceConstant.INVOICE_STATIC, WebServiceConstant.URL, staticTWODTO, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                String requestId = json.getOrDefault("requestId", "").toString();
                if (StrUtil.isEmpty(requestId)) {
                    return new HXResponse(msg);
                }
                InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                allApiLog.setUrl(WebServiceConstant.INVOICE_STATIC);
                allApiLog.setSendMsg(staticTWODTO.toString());
                allApiLog.setResultMsg(JSONUtil.toJsonStr(result));
                allApiLog.setCompany(companyservice.getSellertax());
                allApiLog.setIdentityId(companyservice.getIdentity());
                allApiLog.setRequestId(requestId);
                allApiLog.setCreateTime(new Date());
                allApiLogMapper.insertInvoiceAllApiLog(allApiLog);

                HXResponse response = new HXResponse("0000", "同步成功", JSONUtil.parse(data));
                return response;
            } else {
                return new HXResponse(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【蓝字统计】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("发票统计错误！");
        }
    }

    @Override
    public HXResponse auditRedInfo(InvoiceAllShhzJsonDataTWODTO invoiceAllShhzTWODTO, Companyservice companyservice) {

        // 查询用户信息组装
        InvoiceAllYhdj yhdj = getUserInfo(companyservice);
        if (BeanUtil.isEmpty(yhdj)) {
            return new HXResponse("未查询到登记信息！");
        }

        ShhzTwoDTO shhzDTO = new ShhzTwoDTO();
        shhzDTO.setAsync("true");
        shhzDTO.setNsrsbh(yhdj.getNsrsbh());
        shhzDTO.setDqbm(yhdj.getDqbm());
        shhzDTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());
        ShhzJsonDataTwoDTO shhzJsonDataDTO = BeanUtil.copyProperties(invoiceAllShhzTWODTO, ShhzJsonDataTwoDTO.class);
        shhzJsonDataDTO.setXsfnsrsbh(yhdj.getNsrsbh());
        ValidateUtils.validate(shhzDTO);
        ValidateUtils.validate(shhzJsonDataDTO);

        shhzDTO.setJsonData(shhzJsonDataDTO);


        cn.hutool.json.JSONObject result = null;
        try {
            result = ApiHttp.request(WebServiceConstant.SHHZFPXXQRD, WebServiceConstant.URL, shhzDTO, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                String requestId = json.getOrDefault("requestId", "").toString();
                if (StrUtil.isEmpty(requestId)) {
                    return new HXResponse(msg);
                }
                InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                allApiLog.setUrl(WebServiceConstant.SHHZFPXXQRD);
                allApiLog.setSendMsg(shhzDTO.toString());
                allApiLog.setResultMsg(JSONUtil.toJsonStr(result));
                allApiLog.setCompany(companyservice.getSellertax());
                allApiLog.setIdentityId(companyservice.getIdentity());
                allApiLog.setRequestId(requestId);
                allApiLog.setCreateTime(new Date());
                allApiLogMapper.insertInvoiceAllApiLog(allApiLog);


                HXResponse response = new HXResponse("0000", "同步成功", result);
                return response;
            } else {
                return new HXResponse(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【审核红字信息表】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("审核红字信息表错误！");
        }

    }

    /**
     * 查询红字信息表明细
     *
     * @param invoiceAllRedInvoiceDetailDTO
     * @param companyservice
     * @return
     */
    @Override
    public HXResponse queryRedInfoDetail(InvoiceAllRedInvoiceDetailTWODTO invoiceAllRedInvoiceDetailDTO, Companyservice companyservice) {

        RedInvoiceDetailTWODTO redInvoiceDetailDTO = BeanUtil.copyProperties(invoiceAllRedInvoiceDetailDTO, RedInvoiceDetailTWODTO.class);
        InvoiceAllRedInvoiceDetailJsonDataTWODTO redInvoiceDetailJsonDataDTO = BeanUtil.copyProperties(invoiceAllRedInvoiceDetailDTO.getJsonData(), InvoiceAllRedInvoiceDetailJsonDataTWODTO.class);
        redInvoiceDetailDTO.setJsonData(redInvoiceDetailJsonDataDTO);
        ValidateUtils.validate(redInvoiceDetailDTO);
        ValidateUtils.validate(redInvoiceDetailJsonDataDTO);

        cn.hutool.json.JSONObject result = null;
        try {
            result = ApiHttp.request(WebServiceConstant.CXHZFPXXQRDMX, WebServiceConstant.URL, redInvoiceDetailDTO, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                String requestId = json.getOrDefault("requestId", "").toString();
                if (StrUtil.isEmpty(requestId)) {
                    return new HXResponse(msg);
                }
                InvoiceAllApiLog allApiLog = new InvoiceAllApiLog();
                allApiLog.setUrl(WebServiceConstant.QDFPWJHQ);
                allApiLog.setSendMsg(redInvoiceDetailDTO.toString());
                allApiLog.setResultMsg(JSONUtil.toJsonStr(result));
                allApiLog.setCompany(companyservice.getSellertax());
                allApiLog.setIdentityId(companyservice.getIdentity());
                allApiLog.setRequestId(requestId);
                allApiLog.setCreateTime(new Date());
                allApiLogMapper.insertInvoiceAllApiLog(allApiLog);

            } else {
                return new HXResponse(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【查询红字申请表明细】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("查询红字申请表明细错误！");

        }
        HXResponse response = new HXResponse("0000", "同步成功", result);
        return response;
    }


    /**
     * 通用查询接口
     *
     * @param invoiceAllYhdjDTO
     * @param companyservice
     * @return
     */
    @Override
    public HXResponse queryResultInfo(InvoiceAllQueryTWODTO invoiceAllYhdjDTO, Companyservice companyservice) {

        String systemOrderNo = invoiceAllYhdjDTO.getRequestId();

        // 查询用户登记参数
        InvoiceAllYhdj yhdj = getUserInfo(companyservice);
        if (BeanUtil.isEmpty(yhdj)) {
            return new HXResponse("未查询到登记信息！");
        }

        QueryTwoDTO queryDTO = new QueryTwoDTO();
        queryDTO.setDqbm(yhdj.getDqbm());
        queryDTO.setAsync("false");
        queryDTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());
        queryDTO.setNsrsbh(yhdj.getNsrsbh());
        queryDTO.setServiceId("tycxjk");
        queryDTO.setRequestId(systemOrderNo);

        ValidateUtils.validate(queryDTO);

        String requestId = queryDTO.getRequestId();
        String serviceId = "";

        cn.hutool.json.JSONObject result = null;
        try {
            result = ApiHttp.request(WebServiceConstant.TYCXJK, WebServiceConstant.URL, queryDTO, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {

                QueryWrapper<InvoiceAllApiLog> apiLogQueryWrapper = new QueryWrapper<>();
                apiLogQueryWrapper.eq("request_id", requestId);
                InvoiceAllApiLog allApiLog = allApiLogMapper.selectOne(apiLogQueryWrapper);
                if (BeanUtil.isNotEmpty(allApiLog) && StrUtil.isNotEmpty(allApiLog.getRequestId())) {
                    serviceId = allApiLog.getUrl();
                }

                if (BeanUtil.isEmpty(allApiLog)) {
                    log.error("金四日志表未记录，requestId={},返回结果={}", requestId, data);
                }

                HXResponse response = new HXResponse("0000", "查询成功");

                String finalServiceId = serviceId;

                if (WebServiceConstant.LZFPKJ.equals(finalServiceId)) {
                    // 蓝字发票开具
                    AddInvoiceTWOVO addInvoiceVO = BeanUtil.copyProperties(JSONUtil.parse(data), AddInvoiceTWOVO.class);

                    if (BeanUtil.isNotEmpty(addInvoiceVO)) {
                        QueryWrapper<BillInfo> billInfoQueryWrapper = new QueryWrapper<>();
                        billInfoQueryWrapper.eq("out_trade_orderno", requestId);
                        BillInfo billInfoR = billInfoMapper.selectOne(billInfoQueryWrapper);
                        if (BeanUtil.isNotEmpty(billInfoR)) {
                            QueryWrapper<Invoice> invoiceQueryWrapper = new QueryWrapper<>();
                            invoiceQueryWrapper.eq("out_trade_orderno", requestId);
                            Invoice invoice = invoiceMapper.selectOne(invoiceQueryWrapper);

                            // 根据
                            BillInfo billInfoResult = billInfoMapper.selectByOutTradeOrderno(companyservice.getCompanyid(), systemOrderNo);
                            if (BeanUtil.isNotEmpty(billInfoResult)) {
                                if (BeanUtil.isNotEmpty(invoice)) {
                                    invoice = BeanUtil.copyProperties(billInfoResult, Invoice.class);
                                    invoice.setBillInfoId(billInfoResult.getId());
                                    invoice.setFphm(addInvoiceVO.getFphm());
                                    invoice.setKprq(DateUtil.parse(addInvoiceVO.getKprq()));
                                    invoice.setZzfpdm(addInvoiceVO.getZzfpdm());
                                    invoice.setZzfphm(addInvoiceVO.getZzfphm());
                                    //二维码=二维码下载地址
                                    invoice.setQrCode(addInvoiceVO.getEwmUrl());
                                    //发票 pdf 地址=PDF格式下载地址
                                    invoice.setInvoicePdfUrl(addInvoiceVO.getPdfUrl());
                                    //发票详情地址=OFD格式下载地址
                                    invoice.setInvoiceJpgUrl(addInvoiceVO.getOfdUrl());
                                    //ofdUrl=XML格式下载地址
                                    invoice.setcOfdUrl(addInvoiceVO.getXmlUrl());
                                    invoice.setState(2);
                                    invoiceMapper.updateInvoice(invoice);
                                    billInfoR.setOriginFphm(addInvoiceVO.getFphm());
                                    billInfoR.setState(2);
                                    billInfoMapper.updateById(billInfoR);
                                } else {
                                    invoice = BeanUtil.copyProperties(billInfoResult, Invoice.class);
                                    invoice.setBillInfoId(billInfoResult.getId());
                                    invoice.setFphm(addInvoiceVO.getFphm());
                                    invoice.setKprq(DateUtil.parse(addInvoiceVO.getKprq()));
                                    invoice.setZzfpdm(addInvoiceVO.getZzfpdm());
                                    invoice.setZzfphm(addInvoiceVO.getZzfphm());
                                    //二维码=二维码下载地址
                                    invoice.setQrCode(addInvoiceVO.getEwmUrl());
                                    //发票 pdf 地址=PDF格式下载地址
                                    invoice.setInvoicePdfUrl(addInvoiceVO.getPdfUrl());
                                    //发票详情地址=OFD格式下载地址
                                    invoice.setInvoiceJpgUrl(addInvoiceVO.getOfdUrl());
                                    //ofdUrl=XML格式下载地址
                                    invoice.setcOfdUrl(addInvoiceVO.getXmlUrl());
                                    invoice.setState(2);
                                    invoiceMapper.insertInvoice(invoice);
                                    billInfoR.setState(2);
                                    billInfoR.setOriginFphm(addInvoiceVO.getFphm());
                                    billInfoMapper.updateById(billInfoR);
                                }
                            }
                            // 返回数据
                            response.put("data", invoice);
                        } else {
                            log.error("【金四服务类】【数科】【通用查询接口】API请求异常，billInfo未查询到，requestId={}。", requestId);
                            return new HXResponse("查询错误！");
                        }
                    }

                } else if (WebServiceConstant.FPEWMXZ.equals(finalServiceId)) {
                    // 获取发票下载地址
                    cn.hutool.json.JSONObject json = JSONUtil.parseObj(data);
                    String ewmUrl = json.getOrDefault("ewmUrl", "").toString();
                    String pdfUrl = json.getOrDefault("pdfUrl", "").toString();
                    String ofdUrl = json.getOrDefault("ofdUrl", "").toString();
                    String xmlUrl = json.getOrDefault("xmlUrl", "").toString();
                    if (StrUtil.isNotEmpty(requestId)) {
                        QueryWrapper<Invoice> invoiceQueryWrapper = new QueryWrapper<>();
                        invoiceQueryWrapper.eq("out_trade_orderno", requestId);
                        Invoice invoice = invoiceMapper.selectOne(invoiceQueryWrapper);
                        if (BeanUtil.isNotEmpty(invoice)) {
                            invoice.setInvoicePdfUrl(pdfUrl);
                            invoice.setcOfdUrl(ofdUrl);
                            invoiceMapper.updateInvoice(invoice);
                        }
                    }
                    // TODO: 2023/3/29 返回数据处理
                    response.put("data", JSONUtil.parse(data));
                } else if (WebServiceConstant.ADD_RED_INVOICE.equals(finalServiceId)) {
                    // 查找原发票信息
                    AddRedInvoiceTWOVO addRedInvoiceVO = BeanUtil.copyProperties(data, AddRedInvoiceTWOVO.class);
                    QueryWrapper<BillInfo> billInfoQueryWrapper = new QueryWrapper<>();
                    billInfoQueryWrapper.eq("out_trade_orderno", requestId);
                    BillInfo billInfoR = billInfoMapper.selectOne(billInfoQueryWrapper);
                    if (billInfoR != null && StrUtil.isNotEmpty(billInfoR.getOriginFphm())) {

                        // 查找原发票信息
                        String originFphm = billInfoR.getOriginFphm();
                        QueryWrapper<Invoice> invoiceQueryWrapper = new QueryWrapper<>();
                        invoiceQueryWrapper.eq("fphm", originFphm);
                        Invoice invoice = invoiceMapper.selectOne(invoiceQueryWrapper);

                        invoice.setId(null);
                        invoice.setFphm(addRedInvoiceVO.getFphm());
                        invoice.setBillInfoId(billInfoR.getId());
                        invoice.setSystemOrderno(billInfoR.getSystemOrderno());
                        invoice.setOutTradeOrderno(requestId);
                        invoice.setFpqqlsh(billInfoR.getFpqqlsh());
                        invoice.setKprq(DateUtil.parse(addRedInvoiceVO.getKprq()));
                        invoice.setZzfpdm(addRedInvoiceVO.getZzfpdm());
                        invoice.setZzfphm(addRedInvoiceVO.getZzfphm());
                        //二维码=二维码下载地址
                        invoice.setQrCode(addRedInvoiceVO.getEwmUrl());
                        //发票 pdf 地址=PDF格式下载地址
                        invoice.setInvoicePdfUrl(addRedInvoiceVO.getPdfUrl());
                        //发票详情地址=OFD格式下载地址
                        invoice.setInvoiceJpgUrl(addRedInvoiceVO.getOfdUrl());
                        //ofdUrl=XML格式下载地址
                        invoice.setcOfdUrl(addRedInvoiceVO.getXmlUrl());
                        invoice.setState(2);
                        invoiceMapper.insertInvoice(invoice);
                        billInfoR.setState(2);
                        billInfoMapper.updateById(billInfoR);

                        response.put("data", invoice);
                    }
                } else if (WebServiceConstant.KSHC.equals(finalServiceId)) {
                    // 快速冲红
                    KschTWOVO kschVO = BeanUtil.copyProperties(JSONUtil.parse(data),KschTWOVO.class);

                    QueryWrapper<Redinfo> billInfoQueryWrapper = new QueryWrapper<>();
                    billInfoQueryWrapper.eq("out_trade_orderno", requestId);
                    Redinfo billInfoR = redinfoMapper.selectOne(billInfoQueryWrapper);
                    if (billInfoR != null && StrUtil.isNotEmpty(billInfoR.getOriginFphm())) {

                        // 查找原发票信息
                        String originFphm = billInfoR.getOriginFphm();
                        QueryWrapper<Invoice> invoiceQueryWrapper = new QueryWrapper<>();
                        invoiceQueryWrapper.eq("fphm", originFphm);
                        Invoice invoice = invoiceMapper.selectOne(invoiceQueryWrapper);

                        if (BeanUtil.isNotEmpty(kschVO)) {

                            //更新蓝字发票状态为冲红 99
                            invoice.setState(99);
                            invoiceMapper.updateInvoice(invoice);

                            invoice.setId(null);
                            invoice.setOriginFphm(invoice.getFphm());
                            invoice.setFphm(kschVO.getFphm());
                            invoice.setBillInfoId(billInfoR.getId());
                            invoice.setSystemOrderno(billInfoR.getSystemOrderno());
                            invoice.setOutTradeOrderno(requestId);
                            invoice.setFpqqlsh(billInfoR.getFpqqlsh());
                            invoice.setKprq(DateUtil.parse(kschVO.getKprq()));
                            invoice.setZzfpdm(kschVO.getZzfpdm());
                            invoice.setZzfphm(kschVO.getZzfphm());
                            //二维码=二维码下载地址
                            invoice.setQrCode(kschVO.getEwmUrl());
                            //发票 pdf 地址=PDF格式下载地址
                            invoice.setInvoicePdfUrl(kschVO.getPdfUrl());
                            //发票详情地址=OFD格式下载地址
                            invoice.setInvoiceJpgUrl(kschVO.getOfdUrl());
                            //ofdUrl=XML格式下载地址
                            invoice.setcOfdUrl(kschVO.getXmlUrl());
                            invoice.setState(2);
                            invoiceMapper.insertInvoice(invoice);
                        }
                        billInfoR.setBillInputTime(kschVO.getKprq());
                        redinfoMapper.updateById(billInfoR);
                    }
                    response.put("data", JSONUtil.parse(data));

                } else if (WebServiceConstant.CXHZFPXXQRD.equals(finalServiceId)) {
                    // 查询红字信息表

                    RedInvoiceQueryAllTWOVO redInvoiceQueryAllTWOVO = BeanUtil.copyProperties(JSONUtil.parse(data), RedInvoiceQueryAllTWOVO.class);
                    // 返回报文转换
                    List<Redinfo> resultDownload = parseRedINFO(redInvoiceQueryAllTWOVO);
                    // TODO: 2023/4/6 数据存储
                    response.put("data", resultDownload);
                } else if (WebServiceConstant.SHHZFPXXQRD.equals(finalServiceId)) {
                    // 审核红字信息表
                    response.put("data", JSONUtil.parse(data));
                } else if (WebServiceConstant.QDFPWJHQ.equals(finalServiceId)) {
                    // 审核红字信息表
                    // TODO: 全电发票文件获取
                    response.put("data", JSONUtil.parse(data));
                } else if (WebServiceConstant.QRCODE.equals(finalServiceId)) {
                    response.put("data", JSONUtil.parse(data));
                } else if (WebServiceConstant.SQHZFPXXQRD.equals(finalServiceId)) {
                    // 申请红字信息表
                    AddRedInfoInvoiceTWOVO addRedInfoInvoiceTWOVO = BeanUtil.copyProperties(JSONUtil.parse(data), AddRedInfoInvoiceTWOVO.class);

                    QueryWrapper<Redinfo> billInfoQueryWrapper = new QueryWrapper<>();
                    billInfoQueryWrapper.eq("out_trade_orderno", requestId);
                    Redinfo billInfoR = redinfoMapper.selectOne(billInfoQueryWrapper);
                    if (billInfoR != null && StrUtil.isNotEmpty(billInfoR.getOriginFphm())) {

                        // 查找原发票信息
                        String originFphm = billInfoR.getOriginFphm();
                        QueryWrapper<Invoice> invoiceQueryWrapper = new QueryWrapper<>();
                        invoiceQueryWrapper.eq("fphm", originFphm);
                        Invoice invoice = invoiceMapper.selectOne(invoiceQueryWrapper);

                        AddRedInfoInvoiceJsonDataTWOVO addRedInfoInvoiceJsonDataTWOVO = addRedInfoInvoiceTWOVO.getHzfpxx();
                        if (BeanUtil.isNotEmpty(addRedInfoInvoiceJsonDataTWOVO)) {

                            //更新蓝字发票状态为冲红 99
                            invoice.setState(99);
                            invoiceMapper.updateInvoice(invoice);

                            invoice.setId(null);
                            invoice.setOriginFphm(invoice.getFphm());
                            invoice.setFphm(addRedInfoInvoiceJsonDataTWOVO.getFphm());
                            invoice.setBillInfoId(billInfoR.getId());
                            invoice.setSystemOrderno(billInfoR.getSystemOrderno());
                            invoice.setOutTradeOrderno(requestId);
                            invoice.setFpqqlsh(billInfoR.getFpqqlsh());
                            invoice.setKprq(DateUtil.parse(addRedInfoInvoiceJsonDataTWOVO.getKprq()));
                            invoice.setZzfpdm(addRedInfoInvoiceJsonDataTWOVO.getZzfpdm());
                            invoice.setZzfphm(addRedInfoInvoiceJsonDataTWOVO.getZzfphm());
                            //二维码=二维码下载地址
                            invoice.setQrCode(addRedInfoInvoiceJsonDataTWOVO.getEwmUrl());
                            //发票 pdf 地址=PDF格式下载地址
                            invoice.setInvoicePdfUrl(addRedInfoInvoiceJsonDataTWOVO.getPdfUrl());
                            //发票详情地址=OFD格式下载地址
                            invoice.setInvoiceJpgUrl(addRedInfoInvoiceJsonDataTWOVO.getOfdUrl());
                            //ofdUrl=XML格式下载地址
                            invoice.setcOfdUrl(addRedInfoInvoiceJsonDataTWOVO.getXmlUrl());
                            invoice.setState(2);
                            invoiceMapper.insertInvoice(invoice);
                        }
                        billInfoR.setRedInfoNo(addRedInfoInvoiceTWOVO.getHzfpxxqrdbh());
                        billInfoR.setRedInfoStatus(addRedInfoInvoiceTWOVO.getHzfpxxqrdZtdm());
                        billInfoR.setBillInputTime(addRedInfoInvoiceTWOVO.getLrrq());
                        redinfoMapper.updateById(billInfoR);

                        response.put("data", addRedInfoInvoiceTWOVO);
                    }

                } else {
                    response.put("data", JSONUtil.parse(data));
                }
                return response;
            } else {
                // TODO: 2023/5/9 这里更新到日志表记录

                return new HXResponse(msg);
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【通用查询接口】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("通用查询接口异常");
        }
    }

    @Override
    public EleNewMessage findRedInfo(FindRedInfo findRedInfo, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse generateDynamicCode(JSONObject decryptResult, Companyservice companyservice) {
        return null;
    }

    @Override
    public HXResponse exportVehicleInvoice(JSONObject decryptResult, Companyservice companyservice) {
        return null;
    }

    /** 获取人脸识别结果 */
    @Override
    public HXResponse qrcodeResult(JSONObject decryptResult, Companyservice companyservice) {

        cn.hutool.json.JSONObject result = null;
        try {
            // 同步
            InvoiceAllQycodeResultDTO qycodeResultDTO = BeanUtil.copyProperties(decryptResult,InvoiceAllQycodeResultDTO.class);
            if(BeanUtil.isEmpty(qycodeResultDTO)){
                return new HXResponse("参数为空！");
            }

            // 查询用户登记参数
            InvoiceAllYhdj yhdj = getUserInfo(companyservice);
            if (BeanUtil.isEmpty(yhdj)) {
                return new HXResponse("未查询到登记信息！");
            }

           // 组装报文
            QycodeResultTWODTO qycodeResultTWODTO = new QycodeResultTWODTO();
            qycodeResultTWODTO.setAsync("false");
            qycodeResultTWODTO.setBsrysfzjhm(yhdj.getBsrysfzjhm());
            qycodeResultTWODTO.setDqbm(yhdj.getDqbm());
            qycodeResultTWODTO.setNsrsbh(yhdj.getNsrsbh());
            QycodeResultJsonDataTWODTO qycodeResultJsonDataTWODTO = BeanUtil.copyProperties(qycodeResultDTO,QycodeResultJsonDataTWODTO.class);
            qycodeResultTWODTO.setJsonData(qycodeResultJsonDataTWODTO);

            result = ApiHttp.request(WebServiceConstant.HQRLSBJG, WebServiceConstant.URL, qycodeResultTWODTO, companyservice);

            String code = result.get("code") != null ? result.get("code").toString() : "";
            String msg = result.get("msg") != null ? result.get("msg").toString() : "";
            String data = result.get("data") != null ? result.get("data").toString() : "";

            if ("0000".equals(code) && StrUtil.isNotEmpty(data)) {
                InvoiceAllQycodeResultVO qycodeResultVO = BeanUtil.copyProperties(JSONUtil.parse(data), InvoiceAllQycodeResultVO.class);
                qycodeResultVO.setRzid(qycodeResultJsonDataTWODTO.getRzid());
                qycodeResultVO.setYhm(qycodeResultJsonDataTWODTO.getYhm());
                HXResponse response = new HXResponse("0000", "请求成功", qycodeResultVO);
                return response;
            } else {
                return new HXResponse(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【金四服务类】【数科】【获取人脸识别结果】API请求异常，外部报文返回code非0000。错误信息:{}", e.getMessage());
            return new HXResponse("获取二维码错误！");

        }
    }

    @Override
    public void callBackAisino(InvoiceBack invoiceBack, String callBackUrl, String identity,Long id) {

    }

    @Override
    public HXResponse mailDelivery(InvoiceMailDeliveryDTO mailDeliveryDTO, Companyservice companyservice) {
        return null;
    }

    /**
     * 简税和金财数科 发票类型互换
     *
     * @param invoiceType 发票种类
     * @param jcsk2js     是否通过金财数科转换为简税
     * @return
     */
    private static String getInvoiceType(String invoiceType, boolean jcsk2js) {
        /**
         * 全电发票（铁路电子客票）：51
         * 全电发票（航空运输电子客票行程单） 61
         * 机动车销售电子统一发票 83
         * 二手车销售电子统一发票 84
         * 纸质专票(全电开具) 85
         * 纸质普票(全电开具) 86
         * 货物运输业增值税专用发票 02
         * 机动车销售统一发票 03
         * 二手车销售统一发票 15
         *
         * 纸质发票（机动车销售统一发票） 87
         * 纸质发票（二手车销售统一发票） 88
         * 增值税专票 01
         * 增值税普票  04
         * 增值税电专 08
         * 增值税电普 10
         * 全电专票 81
         * 全电普票 82
         */
        // TODO: 2023/4/26 整理所有发票代码统一管理

//        a、d、e、f、g、h、i、k、l、m、n、o、q、t、v、w、x、y、z。z
//        p/c/s/b/j/u/q/r

        Map<String, String> map = new HashMap<>();
        map.put("10", "p"); //电子增值税普通发票 10
        map.put("04", "c"); //增值税普通发票 04
        map.put("01", "s"); //增值税专用发票 01
        map.put("08", "b"); //增值税电子专用发票 08
        map.put("87", "j"); //机动车发票 87
        map.put("88", "u"); //二手车发票 88

        map.put("82", "q"); //全电普票
        map.put("81", "r"); //全电专票


        if (jcsk2js) {
            String resp = map.get(invoiceType);
            return StringUtils.isNotEmpty(resp) ? resp : invoiceType;
        }

        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (entry.getValue().equals(invoiceType)) {
                return entry.getKey();
            }
            // 兼容一下数科的发票类型
            if("81".equals(invoiceType) ||  "82".equals(invoiceType)){
                return invoiceType;
            }
        }

        return invoiceType;
    }


    /**
     * 获取登记用户信息
     */
    public InvoiceAllYhdj getUserInfo(Companyservice companyservice) {

        String identity = companyservice.getIdentity();
        String sellertax = companyservice.getSellertax();

        String key = WebServiceConstant.TOKEN_KEY + sellertax + "_"+ identity;

        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        cn.hutool.json.JSONObject yhdjObj = redisCache.getCacheObject(key);

        if(JSONUtil.isNull(yhdjObj)){
            // 查询用户登记参数
            QueryWrapper<InvoiceAllYhdj> yhdjQueryWrapper = new QueryWrapper<>();
            yhdjQueryWrapper.eq("nsrsbh", sellertax);
            yhdjQueryWrapper.eq("identity", identity);
            InvoiceAllYhdj invoiceAllYhdj = yhdjMapper.selectOne(yhdjQueryWrapper);

            cn.hutool.json.JSONObject obj = JSONUtil.parseObj(invoiceAllYhdj);
            Long exprieTime = System.currentTimeMillis(); // 过期时间
            obj.put("exprieTime", exprieTime);
            redisCache.setCacheObject(key, obj);

            yhdjObj = obj;
        }

        return BeanUtil.copyProperties(yhdjObj,InvoiceAllYhdj.class);
    }













}
