package com.usoftchina.dingtalk.sdk;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiWorkrecordAddRequest;
import com.dingtalk.api.request.OapiWorkrecordGetbyuseridRequest;
import com.dingtalk.api.request.OapiWorkrecordUpdateRequest;
import com.dingtalk.api.response.OapiWorkrecordAddResponse;
import com.dingtalk.api.response.OapiWorkrecordGetbyuseridResponse;
import com.dingtalk.api.response.OapiWorkrecordUpdateResponse;
import com.usoftchina.dingtalk.sdk.config.DingTalkProperties;

/**
 * 待办任务
 *
 * @author yingp
 * @date 2020/2/19
 */
public class WorkRecordSdk extends BaseSdk {
    public WorkRecordSdk(DingTalkProperties properties) {
        super(properties);
    }

    /**
     * 发起待办
     * <p>
     * 企业可以调用该接口发起一个待办事项，该待办事项会出现在钉钉客户端“待办事项”页面，与钉钉审批待办事项并列。
     * 需要注意的是，通过开放接口发起的待办，只会有一个“前往”按钮
     * 目前待办事项有防骚扰控制，具体为：
     * 1、每人每天最多收到一条表单内容相同的待办。触发这个限制，会返回错误码854001
     * 2、每人每天最多收到100条待办。触发这个限制，会返回错误码854002
     * </p>
     *
     * @param agentCode
     * @param req
     * @return 待办事项唯一id
     */
    public String addWorkRecord(String agentCode, OapiWorkrecordAddRequest req) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/workrecord/add");
        OapiWorkrecordAddResponse resp = cached(() -> client.execute(req, getAccessToken(agentCode)));
        assertOK(resp);
        return resp.getBody();
    }

    /**
     * 更新待办
     * <p>
     * 企业可以调用该接口更新待办事项状态，调用成功后，该待办事项在该用户的“待办事项”列表页面中消失。
     * </p>
     *
     * @param agentCode
     * @param userId    待办事项对应的用户id
     * @param recordId  待办事项唯一id。该id可以使用创建待办接口中传入的biz_id，也可以使用创建待办接口返回结果中的record_id
     */
    public void updateWorkRecord(String agentCode, String userId, String recordId) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/workrecord/update");
        OapiWorkrecordUpdateRequest req = new OapiWorkrecordUpdateRequest();
        req.setUserid(userId);
        req.setRecordId(recordId);
        OapiWorkrecordUpdateResponse resp = cached(() -> client.execute(req, getAccessToken(agentCode)));
        assertOK(resp);
    }

    /**
     * 获取用户待办事项
     * <p>
     * 企业可以调用该接口分页获取用户的待办任务列表。
     * </p>
     *
     * @param agentCode
     * @param userId
     * @param offset 分页游标，从0开始，如返回结果中has_more为true，则表示还有数据，offset再传上一次的offset+limit
     * @param limit 分页大小，最多50
     */
    public OapiWorkrecordGetbyuseridResponse.PageResult getWorkRecordList(String agentCode, String userId, long offset, long limit) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/workrecord/getbyuserid");
        OapiWorkrecordGetbyuseridRequest req = new OapiWorkrecordGetbyuseridRequest();
        req.setUserid(userId);
        req.setOffset(offset);
        req.setLimit(limit);
        // 待办事项状态，0表示未完成，1表示完成
        req.setStatus(0L);
        OapiWorkrecordGetbyuseridResponse resp = cached(() -> client.execute(req, getAccessToken(agentCode)));
        assertOK(resp);
        return resp.getRecords();
    }
}
