package com.jianshui.invoice.utils;


import com.alibaba.fastjson.JSONObject;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.enums.ErrorCode;
import com.jianshui.common.exception.jianshui.JianshuiParamErrorException;
import com.jianshui.common.utils.StringUtils;
import com.jianshui.common.utils.spring.SpringUtils;
import com.jianshui.invoice.domain.BillInfo;
import com.jianshui.invoice.domain.dto.HXResponse;
import com.jianshui.invoice.service.IInvoiceApiService;
import com.jianshui.system.mapper.CompanyserviceMapper;
import com.jianshui.system.mapper.ServiceManageMapper;
import com.jianshui.system.service.IServiceManageService;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description API请求工具类
 * @Author 巩权林
 * @Date 2022/4/13 15:25
 **/
public class ApiRequestUtils {

    /**
     * 根据header解析对应的identity
     *
     * @param request
     * @return
     */
    public static String getIdentityByHeader(HttpServletRequest request) {
        // 如果是取的identity
        String identityRaw = request.getHeader("identity");
        if (StringUtils.isNotEmpty(identityRaw)) {
            return identityRaw;
        }

        // 如果是取得诺诺2.0的userTax，兼容诺诺2.0
        String sellertax = request.getHeader("userTax");
        if (StringUtils.isNotEmpty(sellertax)) {
            CompanyserviceMapper companyserviceMapper = SpringUtils.getBean(CompanyserviceMapper.class);
            Companyservice companyservice = companyserviceMapper.selectFirstCompanyserviceBySellerTax(sellertax);
            if (companyservice != null) {
                return companyservice.getIdentity();
            }
        }

        // 如果是取得诺诺2.0的userTax，兼容诺诺2.0
        sellertax = request.getHeader("usertax");
        if (StringUtils.isNotEmpty(sellertax)) {
            CompanyserviceMapper companyserviceMapper = SpringUtils.getBean(CompanyserviceMapper.class);
            Companyservice companyservice = companyserviceMapper.selectFirstCompanyserviceBySellerTax(sellertax);
            if (companyservice != null) {
                return companyservice.getIdentity();
            }
        }

        // 坑比诺诺2.0
        sellertax = request.getHeader("taxNum");
        if (StringUtils.isNotEmpty(sellertax)) {
            CompanyserviceMapper companyserviceMapper = SpringUtils.getBean(CompanyserviceMapper.class);
            Companyservice companyservice = companyserviceMapper.selectFirstCompanyserviceBySellerTax(sellertax);
            if (companyservice != null) {
                return companyservice.getIdentity();
            }
        }

        // 大坑比诺诺2.0
        sellertax = request.getHeader("taxnum");
        if (StringUtils.isNotEmpty(sellertax)) {
            CompanyserviceMapper companyserviceMapper = SpringUtils.getBean(CompanyserviceMapper.class);
            Companyservice companyservice = companyserviceMapper.selectFirstCompanyserviceBySellerTax(sellertax);
            if (companyservice != null) {
                return companyservice.getIdentity();
            }
        }

        // 否则就是找不到identity
        return null;
    }

    /**
     * 根据请求获得对应的服务器请求，比如开票、查询等
     *
     * @param request
     * @return
     */
    public static String getApiMethods(HttpServletRequest request) {
        return request.getHeader("method");
    }

}
