package com.usoftchina.dingtalk.sdk;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.response.OapiCalendarCreateResponse;
import com.usoftchina.dingtalk.sdk.config.DingTalkProperties;
import com.usoftchina.dingtalk.sdk.dto.AddScheduleReq;

/**
 * DING日程
 *
 * @author yingp
 * @date 2020/2/19
 */
public class ScheduleSdk extends BaseSdk {
    public ScheduleSdk(DingTalkProperties properties) {
        super(properties);
    }

    /**
     * 创建日程
     * 调用该接口可以将企业员工的待办事项写入到钉钉日历并在钉钉日历中展示。
     * 企业可在应用的权限管理页面申请该接口权限
     *
     * @param agentCode
     * @param req
     * @return 日程id
     */
    public String addSchedule(String agentCode, AddScheduleReq req) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/calendar/create");
        OapiCalendarCreateResponse resp = cached(() -> client.execute(req.build(), getAccessToken(agentCode)));
        assertOK(resp);
        return resp.getResult().getDingtalkCalendarId();
    }
}
