package com.dhcc.finance.util;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述：OA数据获取工具类
 * 
 * @author 17411
 * @date 2018年12月5日 下午5:21:41 @修改日志：
 */
public class GetDataUtils {
    // 编码方式
    public static final String ALGORITHM_HMAC = "HmacSHA1";
    public static final String ALGORITHMHMAC__MD5 = "HmacMD5";
    public static final String ALGORITHM_MD5 = "MD5";

    public static final String FORMAT_JSON = "json";
    public static final String FORMAT_XML = "xml";
    private static String serverUrl = "http://10.213.103.188/portal/openapi?";// 请求的AWS服务地址;
    private static String accessKey = "SyncUsersInterface";// 私钥;
    private static String secret = "SyncUsersInterface";// 访问凭证;
    private static String format = "json";// 传输格式
    private static int connectTimeout = 3000;
    private static int readTimeout = 15000;

    public GetDataUtils() {

    }

    /**
     * 查询部门下的用户
     * 
     * @param null
     */
    public static String getDepartmentUsers(String departmentId) {
        String uid = "admin";// 调用方法
        Map<String, String> t = new HashMap<String, String>();
        t.put(ApiUtils.TIMESTAMP, Long.toString(System.currentTimeMillis()));
        t.put(ApiUtils.CMD, "org.users.get.department");
        t.put(ApiUtils.FORMAT, format);
        t.put(ApiUtils.ACCESS_KEY, accessKey);
        t.put(ApiUtils.SIG_METHOD, ALGORITHMHMAC__MD5);
        t.put("uid", uid);
        t.put("departmentId", departmentId);// 部门ID
        String sig = ApiUtils.makeSig(t, secret, null);
        t.put(ApiUtils.SIG, sig);
        String charset = ApiUtils.CHARSET_UTF8;
        String ctype = "application/x-www-form-urlencoded;charset=" + charset;
        String query = ApiUtils.buildQuery(t, charset);
        byte[] content = {};
        if (query != null) {
            content = ApiUtils.convertBytes(query);
        }
        try {
            String values = _doPost(serverUrl, ctype, content, connectTimeout, readTimeout, null);
            return values;
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 查询部门ID是否存
     * 
     * @param args
     */
    public static void isExistSubDepartments(String departmentId) {
        String uid = "admin";// 调用方法
        Map<String, String> t = new HashMap<String, String>();
        t.put(ApiUtils.TIMESTAMP, Long.toString(System.currentTimeMillis()));
        t.put(ApiUtils.CMD, "org.department.existSub");
        t.put(ApiUtils.FORMAT, format);
        t.put(ApiUtils.ACCESS_KEY, accessKey);
        t.put(ApiUtils.SIG_METHOD, ALGORITHMHMAC__MD5);
        t.put("uid", uid);
        t.put("id", departmentId);// 部门ID
        String sig = ApiUtils.makeSig(t, secret, null);
        t.put(ApiUtils.SIG, sig);
        String charset = ApiUtils.CHARSET_UTF8;
        String ctype = "application/x-www-form-urlencoded;charset=" + charset;
        String query = ApiUtils.buildQuery(t, charset);
        byte[] content = {};
        if (query != null) {
            content = ApiUtils.convertBytes(query);
            System.out.println("content:" + content);
        }

        try {
            String values = _doPost(serverUrl, ctype, content, connectTimeout, readTimeout, null);
            System.out.println("部门ID是否存在成功了！！！！values" + values);

        } catch (Exception e) {

        }
    }

    /**
     * 查询子部门信息
     * 
     * @param args
     * @return
     */
    public static String getSubDepartments(String departmentId) {
        String uid = "admin";// 调用方法
        Map<String, String> t = new HashMap<String, String>();
        t.put(ApiUtils.TIMESTAMP, Long.toString(System.currentTimeMillis()));
        t.put(ApiUtils.CMD, "org.departments.get.sub");
        t.put(ApiUtils.FORMAT, format);
        t.put(ApiUtils.ACCESS_KEY, accessKey);
        t.put(ApiUtils.SIG_METHOD, ALGORITHMHMAC__MD5);
        t.put("uid", uid);
        t.put("id", departmentId);// 部门ID
        String sig = ApiUtils.makeSig(t, secret, null);
        t.put(ApiUtils.SIG, sig);
        String charset = ApiUtils.CHARSET_UTF8;
        String ctype = "application/x-www-form-urlencoded;charset=" + charset;
        String query = ApiUtils.buildQuery(t, charset);
        byte[] content = {};
        if (query != null) {
            content = ApiUtils.convertBytes(query);
        }
        try {
            String values = _doPost(serverUrl, ctype, content, connectTimeout, readTimeout, null);
            return values;
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 查询部门信息
     * 
     * @param args
     */
    public static String getDepartments(String companyId) {
        String cmd = "org.departments.getdepts";// 调用方法
        String uid = "admin";// 调用方法
        Map<String, String> t = new HashMap<String, String>();
        t.put(ApiUtils.TIMESTAMP, Long.toString(System.currentTimeMillis()));
        t.put(ApiUtils.CMD, cmd);
        t.put(ApiUtils.FORMAT, format);
        t.put(ApiUtils.ACCESS_KEY, accessKey);
        t.put(ApiUtils.SIG_METHOD, ALGORITHMHMAC__MD5);
        t.put("uid", uid);
        t.put("companyId", companyId);// 公司ID
        String sig = ApiUtils.makeSig(t, secret, null);
        t.put(ApiUtils.SIG, sig);
        String charset = ApiUtils.CHARSET_UTF8;
        String ctype = "application/x-www-form-urlencoded;charset=" + charset;
        String query = ApiUtils.buildQuery(t, charset);
        byte[] content = {};
        if (query != null) {
            content = ApiUtils.convertBytes(query);
        }
        try {
            String values = _doPost(serverUrl, ctype, content, connectTimeout, readTimeout, null);
            return values;
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 查询部公司信息
     * 
     * @param args
     */
    public static String getCompanyList() {
        String cmd = "org.companys.get";// 调用方法
        String uid = "admin";// 调用方法
        Map<String, String> t = new HashMap<String, String>();
        t.put(ApiUtils.TIMESTAMP, Long.toString(System.currentTimeMillis()));
        t.put(ApiUtils.CMD, cmd);
        t.put(ApiUtils.FORMAT, format);
        t.put(ApiUtils.ACCESS_KEY, accessKey);
        t.put(ApiUtils.SIG_METHOD, ALGORITHMHMAC__MD5);
        t.put("uid", uid);
        String sig = ApiUtils.makeSig(t, secret, null);
        t.put(ApiUtils.SIG, sig);

        String charset = ApiUtils.CHARSET_UTF8;
        String ctype = "application/x-www-form-urlencoded;charset=" + charset;
        String query = ApiUtils.buildQuery(t, charset);
        byte[] content = {};
        if (query != null) {
            content = ApiUtils.convertBytes(query);
        }
        try {
            String values = _doPost(serverUrl, ctype, content, connectTimeout, readTimeout, null);
            return values;
        } catch (Exception e) {

        }
        return null;
    }

    private static String _doPost(String url, String ctype, byte[] content, int connectTimeout, int readTimeout,
            Map<String, String> headerMap) throws IOException {
        HttpURLConnection conn = null;
        OutputStream out = null;
        String rsp = null;
        try {
            try {
                conn = ApiUtils.getConnection(new URL(url), "POST", ctype, headerMap);
                conn.setConnectTimeout(connectTimeout);
                conn.setReadTimeout(readTimeout);
            } catch (IOException e) {
                throw e;
            }
            try {
                out = conn.getOutputStream();
                out.write(content);
                rsp = ApiUtils.getResponseAsString(conn);
            } catch (IOException e) {
                throw e;
            }
        } finally {
            if (out != null) {
                out.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        return rsp;
    }

}
