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

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.dto._constant.ResourceConstant;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.electronicInvoice.dto.request.DeliverRequest;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.electronicInvoice.dto.response.DeliverResponse;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.electronicInvoice.restapi.DeliverCallApi;
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.6_发票交付接口-
 * @Author: linjinyu
 * @CreateDate: 2021/12/23 下午5:12
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/12/23 下午5:12
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class DeliverCallService extends _SzhtxxElectronicInvoiceAuthService {

    @Autowired
    private DeliverCallApi deliverCallApi;

    @Autowired
    private ResourceConstant resourceConstant;

    public DeliverResponse callDeliverApi(DeliverRequest request) {

        super.addAuth(request);

        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.6_Deliver" +
                File.separator;

        String targetFullPath = excelExportPathRoot + excelExportRelativePathStorage;

        DeliverResponse response = null;

        try {

            String now = DateUtil.now();

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

            Call<DeliverResponse> callResponse = deliverCallApi.callDeliverApi(request);

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

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

//			log.info("response\n{}", JSONUtil.toJsonStr(response));

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

            retrofitException.printStackTrace();

            return null;

        } catch (IOException ioe) {

            ioe.printStackTrace();

            return null;
        }

        return response;
    }


}
