package com.engine.htsc.zlsbjs.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.zlsbjs.service.UnlockControlService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UnlockControlServiceImpl extends Service implements UnlockControlService {
    private Log log = LogFactory.getLog(this.getClass());

    @Override
    public Map<String, Object> getFormDefaultData(Integer szbm) {
        RecordSet rs = new RecordSet();
        Map<String,Object> dataMap = new HashMap<>();
        ResourceComInfo resourceComInfo = null;
        DepartmentComInfo departmentComInfo = null;
        try {
            departmentComInfo = new DepartmentComInfo();
            resourceComInfo = new ResourceComInfo();
            //String allSupDepId = departmentComInfo.getAllSupDepId(szbm);

            String fgsfzr = "";
            String supdepid = "";
            String sjbmSql = "select supdepid from hrmdepartment where id = ?";
            rs.executeQuery(sjbmSql,szbm);
            if (rs.next()){
                supdepid = Util.null2String(rs.getString("supdepid"));
            }
            //1.获取分公司合规岗
            String hgfkgSql = "select ry from uf_lcgwry where jg = ? and xzlc = 'zldlsbjs'";
            rs.executeQuery(hgfkgSql,supdepid);
            String hgfkg = "";
            if (rs.next()){
                hgfkg = Util.null2String(rs.getString("ry"));
            }

            String hgfkgName = resourceComInfo.getLastname(hgfkg);
            //2.获取分公司负责人
            String fgsfzrSql = "select b.deptdirectors from hrmdepartment a " +
                    " join hrmdepartmentdefined b on a.id = b.deptid and a.id = ?";
            rs.executeQuery(fgsfzrSql,supdepid);
            if (rs.next()){
                fgsfzr = Util.null2String(rs.getString("deptdirectors"));
            }
            String fgsfzrName = resourceComInfo.getLastname(fgsfzr);


            dataMap.put("hgfkgValue",hgfkg);
            dataMap.put("hgfkgName",hgfkgName);
            dataMap.put("fgsfzrValue",fgsfzr);
            dataMap.put("fgsfzrName",fgsfzrName);

        }catch (Exception e){
            e.printStackTrace();
            dataMap.put("error",500);
        }
        return dataMap;
    }

    @Override
    public JSONObject isLock(String account,String type) {
        Map<String,String> headers = new HashMap<>();
        //测试路径
        PropBean propBean = new PropBean();
        String url = propBean.getPropName("UnlockUrl");
        String token = propBean.getPropName("UnlockToken");
        url = url+"/iscs/api/isLock?type="+type+"&account="+account;
        headers.put("user","oa");
        headers.put("token",token);
        String encode = "utf-8";
        String str = httpGetRaw(url, headers, encode);
        JSONObject dataObject = JSONObject.parseObject(str);
        return dataObject;
    }


    /**
     * get请求
     * @param url
     * @param headers
     * @param encode
     * @return
     */
    private String httpGetRaw(String url, Map<String, String> headers, String encode) {
        if (encode == null) {
            encode = "utf-8";
        }
        String content = null;
        CloseableHttpResponse httpResponse = null;
        CloseableHttpClient closeableHttpClient = null;
        try {
            //HttpClients.createDefault()等价于 HttpClientBuilder.create().build();
            closeableHttpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(url);

            //设置header
            httpGet.setHeader("Content-type", "application/json");
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpGet.setHeader(entry.getKey(), entry.getValue());
                }
            }
            //响应信息
            httpResponse = closeableHttpClient.execute(httpGet);
            HttpEntity entity = httpResponse.getEntity();
            content = EntityUtils.toString(entity, encode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {  //关闭连接、释放资源
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }
}
