[temp]钉钉考勤

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sie.util.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DingtalkUtils {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private String corpid = "";
    private String agentid = "";
    private String appkey = "";
    private String appSecret = "";

    private String accessToken;

    public DingtalkUtils() {
        try {
            accessToken = DingtalkTokenUtils.getDingtalkToken(appkey, agentid, appSecret);
        } catch (Exception e) {
            logger.info("获取钉钉accessToken失败", e);
        }
    }

    /**
     * 考勤排班
     *
     * @param workDate
     * @param offset
     * @param size
     */
    public Map scheduleList(String workDate, String offset, String size) {
        String url = "https://oapi.dingtalk.com/topapi/attendance/listschedule?access_token=" + accessToken;
        JSONObject data = new JSONObject();
        data.put("workDate", workDate);
        data.put("offset", offset);
        data.put("size", size);// 默认200
        try {
            //{"errcode":0,
            // "result":{"has_more":false,
            // "schedules":[{"check_type":"OnDuty","group_id":269960068,"plan_check_time":"2019-05-03 10:06:14","plan_id":47501754824,"userid":"manager4093"}]},
            // "request_id":"417pjgqwxopk"}
            JSONObject resutJSON = JSONObject.parseObject(HttpUtils.postBody(url, null, "application/json", data.toJSONString()));
            String errcode = String.valueOf(resutJSON.get("errcode"));
            Map map = new HashMap();
            if ("0".equals(errcode)) {
                JSONObject result = resutJSON.getJSONObject("result");
                boolean more = result.getBoolean("has_more");
                JSONArray schedules = result.getJSONArray("schedules");
                for (Object o : schedules) {
                    JSONObject schedule = (JSONObject) o;
                    //OnDuty, OffDuty
                    map.put(schedule.getString("check_type"), schedule.getString("plan_check_time"));
                }
                return map;
            }
        } catch (Exception e) {
            logger.info("");
        }
        return null;
    }

    /**
     * 考勤组详情
     *
     * @param offset
     * @param size
     */
    public List scheduleGroupsList(String offset, String size) {
        String url = "https://oapi.dingtalk.com/topapi/attendance/getsimplegroups?access_token=" + accessToken;
        JSONObject data = new JSONObject();
        data.put("offset", offset);
        data.put("size", size);// 默认10
        try {
            /*{"errcode":0,"errmsg":"ok",
        "result":{"has_more":true,
            "groups":[{ "group_id":20015047,
                        "is_default":false,
                        "group_name":"固定排班",
                        "selected_class":[
                            {"setting":{
                                    "class_setting_id":1,
                                    "rest_begin_time":{"check_time":"2017-04-11 11:11:11"},
                                    "permit_late_minutes":10,
                                    "work_time_minutes":-1,
                                    "rest_end_time":{"check_time":"2017-04-11 11:11:11" },
                                    "absenteeism_late_minutes":30,
                                    "serious_late_minutes":20,
                                    "is_off_duty_free_check":"Y"
                                },
                                "class_id":20008010,
                                "sections":[{times":[{"check_time":"2017-04-11 11:11:11","check_type":"OnDuty","across":0}]} ],
                                "class_name":"A"}],
                    "type":"FIXED",
                    "member_count":1,
                    "default_class_id":111,
                    "work_day_list":"1",
                    "classes_list":"周一、二 班次A:09:00-18:00, 周六、周日 休息",
                    "manager_list":"1,2",
                    "dept_name_list":"部门1,部门2"}] }}*/
            JSONObject resutJSON = JSONObject.parseObject(HttpUtils.postBody(url, null, "application/json", data.toJSONString()));
            String errcode = String.valueOf(resutJSON.get("errcode"));
            if ("0".equals(errcode)) {
                JSONObject result = resutJSON.getJSONObject("result");
                boolean more = result.getBoolean("has_more");
                JSONArray groups = result.getJSONArray("groups");
                return groups;
            }
        } catch (Exception e) {
            logger.info("");
        }
        return null;
    }

    /**
     * 用户考勤记录
     *
     * @param usersList
     * @param workDateFrom
     * @param workDateTo
     * @param offset
     * @param limit
     * @return
     */
    public JSONArray getAttendanceList(List usersList, String workDateFrom, String workDateTo, String offset, String limit) {
        String url = "https://oapi.dingtalk.com/attendance/list?access_token=" + accessToken;
        JSONObject data = new JSONObject();
        data.put("userIdList", usersList);
        data.put("workDateFrom", workDateFrom);
        data.put("workDateTo", workDateTo);
        data.put("offset", offset);
        data.put("limit", limit);
        try {
            String result = HttpUtils.postBody(url, null, "application/json", data.toJSONString());
            JSONObject resutJSON = JSONObject.parseObject(result);
            String errcode = String.valueOf(resutJSON.get("errcode"));
            JSONArray jsonArray = null;
            if ("0".equals(errcode)) {
                jsonArray = (JSONArray) resutJSON.get("recordresult");
            }
            return jsonArray;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 部门列表
     *
     * @return
     */
    public List getDepartList() {
        String url = "https://oapi.dingtalk.com/department/list?access_token=" + accessToken;
        try {
            JSONObject result = JSONObject.parseObject(HttpUtils.get(url, null));
            //{"errmsg":"ok","department":[{"id":1,"createDeptGroup":true,"name":"南京童心舞动网络科技有限公司","autoAddUser":true}],"errcode":0}
            if ("errcode".equals(String.valueOf(result.getString("errcode")))) {
                JSONArray department = result.getJSONArray("department");
                return department;
            }
        } catch (Exception e) {
            logger.info("getDepartList", e);
        }
        return null;
    }

    /**
     * 根据部门ID获取用户信息(userid和name)
     *
     * @param departmentId
     */
    public List getSimpleUsersByDeptId(Long departmentId) {
        String url = "https://oapi.dingtalk.com/user/simplelist?access_token=" + accessToken + "&department_id=" + departmentId;
        try {
            JSONObject result = JSONObject.parseObject(HttpUtils.get(url, null));
            //{"errcode":0,"userlist":[{"name":"XXX","userid":"XXX"},{"name":"XXX","userid":"XXX"}],"errmsg":"ok"}
            if ("errcode".equals(String.valueOf(result.getString("errcode")))) {
                JSONArray userlist = result.getJSONArray("userlist");
                return userlist;
            }
        } catch (Exception e) {
            logger.info("getDepartList", e);
        }
        return null;
    }

    /**
     * 获取员工的打卡记录
     *
     * @param userIds       企业内的员工id列表,最多不能超过50个
     * @param checkDateFrom 查询考勤打卡记录的起始工作日,格式为“yyyy-MM-dd hh:mm:ss”
     * @param checkDateTo   查询考勤打卡记录的结束工作日。注意,起始与结束工作日最多相隔7天
     */
    public JSONArray getRecordInfo(List userIds, String checkDateFrom, String checkDateTo) {
        String url = "https://oapi.dingtalk.com/attendance/listRecord?access_token=" + accessToken;
        try {
            JSONObject data = new JSONObject();
            data.put("userIds", String.valueOf(userIds));
            data.put("checkDateFrom", checkDateFrom);
            data.put("checkDateTo", checkDateTo);
            data.put("isI18n", "false");
            JSONObject result = JSONObject.parseObject(HttpUtils.postBody(url, null, "application/json", data.toJSONString()));
            System.out.println(result);
            if (result != null && "0".equals(result.get("errcode").toString())) {
                JSONArray recordResult = result.getJSONArray("recordresult");
                return recordResult;
            }
        } catch (Exception e) {
            logger.info("");
        }
        return null;
    }

    private static String getToken(String appkey, String appSecret) throws Exception {
        String validateUrl = "https://oapi.dingtalk.com/gettoken?appkey=" + appkey + "&appsecret=" + appSecret;
        JSONObject result = (JSONObject) JSONObject.parse(HttpUtils.get(validateUrl, null));
        if ("0".equals(String.valueOf(result.get("errcode")))) {
            return result.getString("access_token");
        }
        return null;
    }

    public static void main(String[] args) {
        String workDateFrom = "2019-05-03 08:00:00";
        String workDateTo = "2019-05-03 09:00:00";
        List usersList = new ArrayList();
        usersList.add("userid");
        usersList.add("273844474026776057");
        String offset = "0";//分页获取数据,0表示第一页
        String limit = "10";//每页10条数据
        DingtalkUtils dingtalkUtils = new DingtalkUtils();

//        List list = dingtalkUtils.scheduleList(workDateFrom, offset, limit);
//        System.out.println(list);
    }
}

 

你可能感兴趣的:(Java,钉钉)