package com.template.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.template.config.CosConfig;
import com.template.config.DingConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: binguo
 * @Date: 2024/5/8 星期三 9:51
 * @Description: com.template.common.utils
 * @Version: 1.0
 */
@Component
public class DingTalkInterface {
    private static Logger logger = LoggerFactory.getLogger( DingTalkInterface.class);

    @Resource
    private DingConfig dingConfig;

    @Autowired
    private RedisTemplate redisTemplate;

    //获取accesstoken
    public String getAccessToken() throws Exception {
        Map<String, String> headerMap = new HashMap<>();
        String result = HttpClientDingUtils.sendGet(dingConfig.getBaseurl()+"/gettoken",headerMap,"UTF-8");
        JSONObject jsonObject = JSONObject.parseObject(result);
        String status = String.valueOf(jsonObject.get("errcode"));
        if(!status.equals("0")){

        }
        String accessToken = String.valueOf(jsonObject.get("access_token"));
        return accessToken;
    }

    //获取考勤报表列定义
    public JSONObject getattcolumns() throws Exception {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("access_token",this.getAccessToken());
        logger.info("getattcolumns入参:======="+headerMap + "=====");
        String result = HttpClientDingUtils.sendPost(dingConfig.getBaseurl()+"/topapi/attendance/getattcolumns",null,headerMap);
        logger.info("getattcolumns出参:======="+result+"====");
        JSONObject jsonObject = JSONObject.parseObject(result);
        String errcode = String.valueOf(jsonObject.get("errcode"));

        return jsonObject;
    }

    //获取考勤表表列值
    public JSONObject getcolumnval(String userId,String columId,String startTime,String endTime) throws Exception {
        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("access_token",this.getAccessToken());
        JSONObject json = new JSONObject();
        json.put("userid", userId);
        json.put("column_id_list", columId);
        json.put("from_date", startTime);
        json.put("to_date", endTime);
        logger.info("getcolumnval入参:======="+json.toString() + "=====");
        String result = HttpClientDingUtils.sendPost(dingConfig.getBaseurl()+"/topapi/attendance/getcolumnval",json,headerMap);
        logger.info("getcolumnval出参:======="+result+"====");
        JSONObject jsonObject = JSONObject.parseObject(result);
        String errcode = String.valueOf(jsonObject.get("errcode"));

        return jsonObject;
    }
}
