package cn.cslg.pas.service;

import cn.cslg.pas.common.model.PatentCell;
import cn.cslg.pas.common.model.dto.GetClaimsInfoParamsDTO;
import cn.cslg.pas.common.model.dto.GetDescriptionInfoParamsDTO;
import cn.cslg.pas.common.model.dto.GetFuTuParamsDTO;
import cn.cslg.pas.common.model.dto.GetSearchBiblioParamsDTO;
import cn.cslg.pas.domain.PubNo;
import com.google.gson.Gson;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 调用外部接口的Service类 PCS:权限系统
 *
 * @Author chenyu
 * @Date 2023/4/25
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class OutInterfaceService {
    @Value("${authorUrl}")
    private String PCSUrl;
    @Value("${OPSUrl}")
    private String OPSUrl;
    @Value("${PASUrl}")
    private String PASUrl;
    @Value("${RMSUrl}")
    private String RMSUrl;

    /**
     * 根据人员名称模糊查询人员ids
     *
     * @param personName 人员名称
     * @return 返回人员ids
     */
    public String getPersonIdByNamePCS(String personName) throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(PCSUrl + "/permission/api/system/getPersonIdByName?personName=" + personName)
                .get()
                .build();
        return Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
    }

    /**
     * 根据人员ids查询人员列表
     *
     * @param ids 人员ids
     * @return 返回装载着人员列表数据的data的String
     */
    public String getPersonnelByIdsFromPCS(List<Integer> ids) throws IOException {
        String param = new Gson().toJson(ids);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(PCSUrl + "/permission/api/system/getPersonnelByIds")
                .post(requestBody)
                .build();
        return Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
    }

    /**
     * 远程调用获取一批专利著录的接口
     *
     * @return
     */
    public String getSearchBiblio(GetSearchBiblioParamsDTO getSearchBiblioParamsDTO) throws IOException {
        //String param = new Gson().toJson(getSearchBiblioParamsDTO);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(OPSUrl + "/api/OPSQuery/SearchBiblio?query=" + getSearchBiblioParamsDTO.getQuery() + "&start=" + getSearchBiblioParamsDTO.getStart() + "&end=" + getSearchBiblioParamsDTO.getEnd())
                .get()
                .build();
        return Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
    }

    /**
     * 远程调用获取权要信息的接口
     *
     * @return
     */
    public String getClaimsInfo(GetClaimsInfoParamsDTO getClaimsInfoParamsDTO) throws IOException {
        //String param = new Gson().toJson(getClaimsInfoParamsDTO);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(OPSUrl + "/api/OPSQuery/GetClaimsInfo?cc=" + getClaimsInfoParamsDTO.getCc() + "&number=" + getClaimsInfoParamsDTO.getNumber() + "&kind=" + getClaimsInfoParamsDTO.getKind())
                .get()
                .build();
        return Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
    }

    /**
     * 远程调用获取说明书的接口
     *
     * @return
     */
    public String getDescriptionInfo(GetDescriptionInfoParamsDTO getDescriptionInfoParamsDTO) throws IOException {
        //String param = new Gson().toJson(getDescriptionInfoParamsDTO);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(OPSUrl + "/api/OPSQuery/GetDescriptionInfo?cc=" + getDescriptionInfoParamsDTO.getCc() + "&number=" + getDescriptionInfoParamsDTO.getNumber() + "&kind=" + getDescriptionInfoParamsDTO.getKind())
                .get()
                .build();
        return Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
    }

    /**
     * 远程调用获取Image信息的接口
     *
     * @return
     */
    public String getImagesInfo(PubNo pubNo) throws IOException {
        String pn = pubNo.getCountry() + pubNo.getNumber() + "." + pubNo.getKind();
        //String param = new Gson().toJson(pn);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(OPSUrl + "/api/OPSQuery/GetImagesInfo?Pn=" + pn)
                .get()
                .build();
        return Objects.requireNonNull(okHttpClient.newCall(request).execute().body()).string();
    }

    /**
     * 远程调用获取附件文件流的接口
     *
     * @return
     */
    public byte[] getPatentFile(GetFuTuParamsDTO getFuTuParamsDTO) throws IOException {
        //String param = new Gson().toJson(getFuTuParamsDTO);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(OPSUrl + "/api/OPSQuery/GetPatentFile?link=" + getFuTuParamsDTO.getLink() + "&page=" + getFuTuParamsDTO.getPage() + "&type=" + getFuTuParamsDTO.getType())
                .get()
                .build();
        byte[] buffer = okHttpClient.newCall(request).execute().body().bytes();
        return buffer;
    }

    /**
     * @params 传对象
     * @title 导入专利
     * @description 导入专利
     * @author lrj
     */
    public String importPatents(PatentCell patentCell) throws IOException {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .pingInterval(400, TimeUnit.SECONDS) // 设置 PING 帧发送间隔
                .connectTimeout(300, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(300, TimeUnit.SECONDS)//设置读取超时时间
                .build();
        String param = new Gson().toJson(patentCell);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), param);
        Request request = new Request.Builder()
                .url(PASUrl + "/api/v2/system/patentCellTODb")
                .post(requestBody)
                .build();
        return Objects.requireNonNull(httpClient.newCall(request).execute().body()).string();
    }

    public String importAssoReportPatentNo(Integer reportId, String patentNo,String name) throws IOException {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .pingInterval(400, TimeUnit.SECONDS) // 设置 PING 帧发送间隔
                .connectTimeout(300, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(300, TimeUnit.SECONDS)//设置读取超时时间
                .build();
        Request request = new Request.Builder()
                .url(RMSUrl + "/api/report/api/compare/addSingle?reportId=" + reportId + "&patentNo=" + patentNo+"&name="+name)
                .get()
                .build();
        return Objects.requireNonNull(httpClient.newCall(request).execute().body()).string();
    }

}
