package cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.electronicInvoice.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.dto._constant.ResourceConstant;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.electronicInvoice.dto.request.SingleBillingRequest;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.electronicInvoice.dto.response.SingleBillingResponse;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.electronicInvoice.restapi.SingleBillingCallApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import retrofit2.Call;

import java.io.File;
import java.io.IOException;

/**
 * @Description: 1.1.1_单张开具接口-
 * @Author: linjinyu
 * @CreateDate: 2021/12/23 下午5:12
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/12/23 下午5:12
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class SingleBillingCallService extends _SzhtxxElectronicInvoiceAuthService {

    @Autowired
    private SingleBillingCallApi singleBillingCallApi;

    @Autowired
    private ResourceConstant resourceConstant;

    public SingleBillingResponse callSingleBillingApi(SingleBillingRequest request) {

        super.addAuth(request);

        /**
         * 1. api前
         * 2. api ---- 当前
         * 3. excel
         */
        String now     = DateUtil.now();
        String fpqqlsh = request.getKpContent().getFptxx().getFpqqlsh();

        String excelExportPathRoot             = resourceConstant.getPathRoot();
        String excelExportRelativePathTemplate = resourceConstant.getPathTemplate();
        String excelExportRelativePathStorage  = resourceConstant.getPathStorage();

        String relativePath = File.separator + "thirdpart" +
                File.separator + "szhtxx" +
                File.separator + "electronicInvoice" +
                File.separator + "1.1.1_SingleBilling" +
                File.separator + fpqqlsh +
                File.separator;

//		String templateRelativePath = excelExportPathRoot + excelExportRelativePathTemplate + relativePath;
//		String templateFullPath;

        String targetFullPath = excelExportPathRoot + excelExportRelativePathStorage;

//        String targetRequestFileName          = StrUtil.format("SingleBilling_request_{}.json", DateUtil.now());
//        String targetRequestWriteFileFullPath = targetFullPath + relativePath + targetRequestFileName;
//        FileUtil.touch(targetRequestWriteFileFullPath);
//        FileUtil.writeUtf8String(JSONUtil.toJsonPrettyStr(request), targetRequestWriteFileFullPath);// CharsetUtil.CHARSET_UTF_8

        SingleBillingResponse response = null;

        try {

            // 请求记录
            tmpLogWithFpqqlsh(
                    targetFullPath,
                    relativePath,
                    "request",
                    fpqqlsh,
                    JSONUtil.toJsonPrettyStr(request),
                    now
            );

            Call<SingleBillingResponse> callResponse = singleBillingCallApi.callSingleBillingApi(request);

            response = callResponse
                    .execute()
                    .body();

            // 返回记录
            tmpLogWithFpqqlsh(
                    targetFullPath,
                    relativePath,
                    "response",
                    fpqqlsh,
                    JSONUtil.toJsonPrettyStr(response),
                    now
            );

//            String targetResponseFileName          = StrUtil.format("SingleBilling_response_{}.json", DateUtil.now());
//            String targetResponseWriteFileFullPath = targetFullPath + relativePath + targetResponseFileName;
//            FileUtil.touch(targetResponseWriteFileFullPath);
//            FileUtil.writeUtf8String(JSONUtil.toJsonPrettyStr(response), targetResponseWriteFileFullPath);// CharsetUtil.CHARSET_UTF_8

        } catch (com.github.lianjiatech.retrofit.spring.boot.exception.RetrofitException retrofitException) {

            retrofitException.printStackTrace();
            return null;

        } catch (IORuntimeException iore) {

            iore.printStackTrace();
            return null;

        } catch (IOException ioe) {

            ioe.printStackTrace();
            return null;
        }

        return response;
    }


}