package com.ningdatech.pmapi.provincial.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.ningdatech.pmapi.common.config.ProvincialProperties;
import com.ningdatech.pmapi.provincial.model.dto.ProvincialProjectDTO;
import com.ningdatech.pmapi.provincial.service.IJoinReviewProvincialBureauService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Classname JointReviewProvincialBureauService
 * @Description 省局联审接口
 * @Date 2023/3/2 9:29
 * @Author PoffyZhang
 */
@Service
@Slf4j
public class JoinReviewProvincialBureauServiceImpl implements IJoinReviewProvincialBureauService {

    @Autowired
    private ProvincialProperties provincialProperties;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 推送/保存 重大接口到 省局联审
     * @return
     */
    @Override
    public String pushImportProject(ProvincialProjectDTO project){
        Long timeStamp = System.currentTimeMillis()/1000;
        String url = provincialProperties.getHost() + provincialProperties.getPushUrl()
                + "?timestamp=" + timeStamp;
        log.info("省局推送联审url {}",url);
        ResponseEntity<String> responseEntity = null;

        String signature = getSha256(timeStamp,provincialProperties.getPushUrl(),
                HttpMethod.POST.name());

        //发送post请求
        RequestEntity<ProvincialProjectDTO> requestEntity = RequestEntity
                .post(url)
                .header("Accept", MediaType.APPLICATION_JSON.toString())
                .header("X-Hmac-Auth-Key",provincialProperties.getKey())
                .header("X-Hmac-Auth-Signature",signature)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .body(project);

        try {
            responseEntity = restTemplate.exchange(requestEntity,String.class);
            log.info("省局联审 响应 :{}",responseEntity);
        } catch (Exception e) {
            log.error("[省局联审] http request error", e);
        }

        return responseEntity.getBody();
    }

    /**
     * 获取流程审批详情
     * @param projectId
     * @return
     */
    @Override
    public String processInfo(String projectId) {
        Long timeStamp = System.currentTimeMillis()/1000;
        String url = provincialProperties.getHost() + provincialProperties.getDetailUrl()
                + "?timestamp=" + timeStamp;

        log.info("省局获取审核详情 url {}",url);
        ResponseEntity<String> responseEntity = null;

        String signature = getSha256(timeStamp,provincialProperties.getDetailUrl(),
                HttpMethod.POST.name());

        //发送post请求
        RequestEntity<String> requestEntity = RequestEntity
                .post(url)
                .header("Accept", MediaType.APPLICATION_JSON.toString())
                .header("X-Hmac-Auth-Key",provincialProperties.getKey())
                .header("X-Hmac-Auth-Signature",signature)
                .contentType(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON)
                .body(projectId); //也可以是DTO

        try {
            responseEntity = restTemplate.exchange(requestEntity,String.class);
            log.info("获取审批详情 响应 :{}",responseEntity);
        } catch (Exception e) {
            log.error("[省局获取审核详情] http request error", e);
        }

        return responseEntity.getBody();
    }

    private String getSha256(Long timeStamp,String url,String method){
        String secret = provincialProperties.getSecret();
        String key = provincialProperties.getKey();
        String bytesToSign = method + StrUtil.LF + url + StrUtil.LF + timeStamp + StrUtil.LF + key;
        log.info("加密message :{}",bytesToSign);
        String res = SecureUtil.hmacSha256(secret).digestBase64(bytesToSign,false);
        log.info("加密结果 :{}",res);
        return res;
    }
}
