package com.hz.pm.api.irs.manage;

import com.alibaba.fastjson.JSONObject;
import com.ningdatech.irs.service.IRefreshTokenService;
import com.hz.pm.api.common.util.CryptUtils;
import com.hz.pm.api.common.util.HttpUtil;
import com.hz.pm.api.common.util.RefreshKeyUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

/**
 * @Classname ProjectIrsManage
 * @Description
 * @Date 2023/8/15 11:43
 * @Author PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class ProjectIrsManage {

    @Value("${irs.core-biz.appKey}")
    private String appKey;

    @Value("${irs.core-biz.appSecret}")
    private String appSecret;

    @Value("${irs.core-biz.url}")
    private String coreBizUrl;

    @Value("${irs.core-biz.interfaceName}")
    private String interfaceName;
    @Resource
    private IRefreshTokenService refreshTokenService;

    @Value("${irs.interface-refresh.request-token-url}")
    private String govRequestTokenUrl;
    @Value("${irs.interface-refresh.refresh-token-url}")
    private String govRefreshTokenUrl;

    /**
     * 查询核心业务
     *
     * @param businessName
     * @return
     * @throws NoSuchAlgorithmException
     */
    public JSONObject searchCoreBiz(String businessName,String orgCode,Integer limit,Integer page) {
        long timestamp = System.currentTimeMillis();
        String secret = refreshTokenService.refreshToken(appKey, appSecret, govRequestTokenUrl, govRefreshTokenUrl,
                HttpMethod.POST.name());
        String sign = null;
        try{
            sign = CryptUtils.MD5Encode(appKey + secret + timestamp);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        HttpComponentsClientHttpRequestFactory factory = HttpUtil.generateHttpRequestFactory();
        RestTemplate restTemplate;
        if(Objects.nonNull(factory)){
            restTemplate = new RestTemplate(factory);
        }else{
            restTemplate = new RestTemplate();
        }

        String url = coreBizUrl + "?" +
                "dingCode=" + (StringUtils.isNotBlank(orgCode)?orgCode:StringUtils.EMPTY) +
                "&matterName=" + (StringUtils.isNotBlank(businessName)?businessName:StringUtils.EMPTY) +
                "&oid=&useState=1&" +
                "limit=" + (Objects.isNull(limit) ? "10" : limit) + "&page=" + (Objects.isNull(page) ? "1" : page) +
                "&appKey=" + appKey + "&sign=" + sign + "&requestTime=" + timestamp;

        ResponseEntity<JSONObject> forEntity = restTemplate.getForEntity(url, JSONObject.class);
        JSONObject body = forEntity.getBody();
        log.info("core biz :{}",body);

        JSONObject datas = body.getJSONObject("datas");
        if(Objects.nonNull(datas)){
            return datas;
        }
        return forEntity.getBody();
    }
}
