package cn.com.ctop.oa.modules.service.impl;

import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.oa.modules.constant.QYWxConstant;
import cn.com.ctop.oa.modules.service.IWechatTokenService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

@Service
public class WechatTokenServiceImpl implements IWechatTokenService {
    /**
     * 获取okr秘钥
     */
    @Override
    public Map<String, Object> getQYWXAccessToken() {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            TreeMap<String, Object> params = new TreeMap<>();
            params.put("corpid", QYWxConstant.QYWX_CORP_ID);
            params.put("corpsecret", QYWxConstant.QYWX_CORP_SECRET);
            String resultStr = HttpUtils.httpGetRequest(QYWxConstant.QYWX_API_URL + QYWxConstant.QYWX_GET_TOKEN, null, params);
            JSONObject resultJson = JSONObject.parseObject(resultStr);
            Integer errcode = resultJson.getInteger("errcode");
            if (errcode == 0) {
                returnMap.put("code", 0);
                returnMap.put("accessToken", resultJson.getString("access_token"));
            } else {
                returnMap.put("code", errcode);
                returnMap.put("message", resultJson.getString("errmsg"));
            }
        } catch (Exception e) {
            returnMap.put("code", -1);
            returnMap.put("message", "系统错误");
        }
        return returnMap;
    }

    /**
     * 打卡信息token
     */
    @Override
    public Map<String, Object> getOAAccessToken() {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            TreeMap<String, Object> params = new TreeMap<>();
            params.put("corpid", QYWxConstant.QYWX_CORP_ID);
            params.put("corpsecret", QYWxConstant.QYWX_OA_CORP_SECRET);
            String resultStr = HttpUtils.httpGetRequest(QYWxConstant.QYWX_API_URL + QYWxConstant.QYWX_GET_TOKEN, null, params);
            JSONObject resultJson = JSONObject.parseObject(resultStr);
            Integer errcode = resultJson.getInteger("errcode");
            if (errcode == 0) {
                returnMap.put("code", 0);
                returnMap.put("accessToken", resultJson.getString("access_token"));
            } else {
                returnMap.put("code", errcode);
                returnMap.put("message", resultJson.getString("errmsg"));
            }
        } catch (Exception e) {
            returnMap.put("code", -1);
            returnMap.put("message", "系统错误");
        }
        return returnMap;
    }

    /**
     * 审批信息token
     */
    @Override
    public Map<String, Object> getApprovaAccessToken() {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            TreeMap<String, Object> params = new TreeMap<>();
            params.put("corpid", QYWxConstant.QYWX_CORP_ID);
            params.put("corpsecret", QYWxConstant.QYWX_APPROVA_CORP_SECRET);
            String resultStr = HttpUtils.httpGetRequest(QYWxConstant.QYWX_API_URL + QYWxConstant.QYWX_GET_TOKEN, null, params);
            JSONObject resultJson = JSONObject.parseObject(resultStr);
            Integer errcode = resultJson.getInteger("errcode");
            if (errcode == 0) {
                returnMap.put("code", 0);
                returnMap.put("accessToken", resultJson.getString("access_token"));
            } else {
                returnMap.put("code", errcode);
                returnMap.put("message", resultJson.getString("errmsg"));
            }
        } catch (Exception e) {
            returnMap.put("code", -1);
            returnMap.put("message", "系统错误");
        }
        return returnMap;
    }

    //--------------↓-天津优容-↓--------------

    /**
     * 获取优容公司秘钥
     */
    @Override
    public Map<String, Object> getYRCompanyToken() {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            TreeMap<String, Object> params = new TreeMap<>();
            params.put("corpid", QYWxConstant.YR_CORP_ID);
            params.put("corpsecret", QYWxConstant.YR_CORP_SECRET);
            String resultStr = HttpUtils.httpGetRequest(QYWxConstant.QYWX_API_URL + QYWxConstant.QYWX_GET_TOKEN, null, params);
            JSONObject resultJson = JSONObject.parseObject(resultStr);
            Integer errcode = resultJson.getInteger("errcode");
            if (errcode == 0) {
                returnMap.put("code", 0);
                returnMap.put("accessToken", resultJson.getString("access_token"));
            } else {
                returnMap.put("code", errcode);
                returnMap.put("message", resultJson.getString("errmsg"));
            }
        } catch (Exception e) {
            returnMap.put("code", -1);
            returnMap.put("message", "系统错误");
        }
        return returnMap;
    }

    /**
     * 优容打卡信息token
     */
    @Override
    public Map<String, Object> getYROAToken() {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            TreeMap<String, Object> params = new TreeMap<>();
            params.put("corpid", QYWxConstant.YR_CORP_ID);
            params.put("corpsecret", QYWxConstant.YR_OA_CORP_SECRET);
            String resultStr = HttpUtils.httpGetRequest(QYWxConstant.QYWX_API_URL + QYWxConstant.QYWX_GET_TOKEN, null, params);
            JSONObject resultJson = JSONObject.parseObject(resultStr);
            Integer errcode = resultJson.getInteger("errcode");
            if (errcode == 0) {
                returnMap.put("code", 0);
                returnMap.put("accessToken", resultJson.getString("access_token"));
            } else {
                returnMap.put("code", errcode);
                returnMap.put("message", resultJson.getString("errmsg"));
            }
        } catch (Exception e) {
            returnMap.put("code", -1);
            returnMap.put("message", "系统错误");
        }
        return returnMap;
    }

    /**
     * 优容审批信息token
     */
    @Override
    public Map<String, Object> getYRApprovaToken() {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            TreeMap<String, Object> params = new TreeMap<>();
            params.put("corpid", QYWxConstant.YR_CORP_ID);
            params.put("corpsecret", QYWxConstant.YR_APPROVA_CORP_SECRET);
            String resultStr = HttpUtils.httpGetRequest(QYWxConstant.QYWX_API_URL + QYWxConstant.QYWX_GET_TOKEN, null, params);
            JSONObject resultJson = JSONObject.parseObject(resultStr);
            Integer errcode = resultJson.getInteger("errcode");
            if (errcode == 0) {
                returnMap.put("code", 0);
                returnMap.put("accessToken", resultJson.getString("access_token"));
            } else {
                returnMap.put("code", errcode);
                returnMap.put("message", resultJson.getString("errmsg"));
            }
        } catch (Exception e) {
            returnMap.put("code", -1);
            returnMap.put("message", "系统错误");
        }
        return returnMap;
    }


}
