package com.seeyon.apps.esign.service;

import com.seeyon.aicloud.common.JsonUtils;
import com.seeyon.apps.esign.config.EsignConfigProvider;
import com.seeyon.apps.esign.constants.EsignApiUrl;
import com.seeyon.apps.esign.constants.EsignConfigConstants;
import com.seeyon.apps.esign.po.esignapi.EsignApiHeader;
import com.seeyon.apps.esign.po.esignapi.EsignBaseResp;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.utils.http.HttpClient;

import java.util.Map;

public class EsignFlowQueryService {

    private TokenCacheManager tokenCacheManager = (TokenCacheManager) AppContext.getBean("tokenCacheManager");
    private EsignConfigProvider esignConfigProvider = EsignConfigProvider.getInstance();

    public Integer queryFlow(String orgName,String eFlowId){
        EsignApiHeader esignApiHeader = EsignApiHeader.build();
        String appId = tokenCacheManager.getAppIdByOrgName(orgName);
        esignApiHeader.token(tokenCacheManager.getToken(appId)).appId(appId);
        String url = esignConfigProvider.getBizConfigByKey(EsignConfigConstants.ESIGN_HOST) + EsignApiUrl.SIGN_FLOW_QUERY.replace("{signFlowId}",eFlowId);
        String respStr = HttpClient.httpGet(url, esignApiHeader.convert2Headers(), "UTF-8");
        EsignBaseResp esignBaseResp = JsonUtils.parseObject(respStr, EsignBaseResp.class);
        Map<String,Object> dataMap = (Map<String, Object>) esignBaseResp.getData();
        if(!"0".equals(esignBaseResp.getCode() + "")) {
            throw new RuntimeException("合同信息查询失败," + esignBaseResp.getMessage());
        }
        Integer flowStatus  = (Integer) dataMap.get("signFlowStatus");
        return flowStatus; //0 - 草稿 1 - 签署中 2 - 完成 3 - 撤销 5 - 过期（签署截至日期到期后触发）7 - 拒签
    }
}
