package com.cim.idm.utils;

import com.cim.idm.framework.IDMFrameServiceProxy;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
  
public class DingTalkUtils {  
  
    private static final String APP_KEY = "dingdz6no5d2igfucd8h";   //AppKey
    private static final String APP_SECRET = "YB0ieklneXHEUZ0dyRxu36w9AKSbjqVpS7z6iPE1_3YzTvS3yeQ-jieeP4Al0sIW";  
  
    public static String getAccessToken() throws IOException {  
        OkHttpClient client = new OkHttpClient();  
        String url = "https://oapi.dingtalk.com/gettoken?appkey=" + APP_KEY + "&appsecret=" + APP_SECRET;  
  
        Request request = new Request.Builder()  
                .url(url)  
                .build();  
  
        try (Response response = client.newCall(request).execute()) {  
            if (!response.isSuccessful()) throw new IOException("Failed to retrieve access token");  
  
            String responseBody = response.body().string();  
            JSONObject jsonObject = new JSONObject(responseBody);  
            return jsonObject.getString("access_token");  
        }  
    }  
  
    // ... 其他方法  
    
    //获取维护的钉钉号或邮箱。zd.2024.9.9
    public static String getEnumDef(String enumName) 
    {
    	String resultList="";
    	
    	String sql="SELECT LISTAGG(e.ENUMVALUE, ', ') WITHIN GROUP (ORDER BY e.ENUMVALUE) AS ENUMVALUE\r\n" + 
    			" FROM ENUMDEFVALUE e WHERE e.ENUMNAME =:ENUMNAME ";
    	Map<String,Object> bp=new HashMap<>();
    	bp.put("ENUMNAME", enumName);
    	List<Map<String,Object>> sr=IDMFrameServiceProxy.getSqlTemplate().queryForList(sql, bp);  	
    	if(sr.size()>0) 
    	{
    		resultList=sr.get(0).get("ENUMVALUE")==null?"":sr.get(0).get("ENUMVALUE").toString();
    	}
    	return resultList;
    }
}
