package com.usoftchina.qywx.sdk;

import com.usoftchina.qywx.sdk.config.QywxProperties;
import com.usoftchina.qywx.sdk.dto.*;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;

import java.util.List;

/**
 * OA数据接口
 *
 * @author yingp
 */
public class OaSdk extends BaseSdk {

    public final static String CHECKIN_AGENT_CODE = "Checkin";

    public final static String APPROVAL_AGENT_CODE = "Approval";

    public OaSdk(QywxProperties properties) {
        super(properties);
    }

    /**
     * 获取打卡数据
     *
     * @param req
     */
    public List<GetCheckinDataResp.CheckinData> getCheckinData(GetCheckinDataReq req) {
        ResponseEntity<GetCheckinDataResp> resp = restTemplate.postForEntity(
                baseUrl + "/cgi-bin/checkin/getcheckindata?access_token={access_token}",
                req.build(),
                GetCheckinDataResp.class,
                new ModelMap("access_token", getAccessToken(CHECKIN_AGENT_CODE)));
        assertOK(resp);
        return resp.getBody().getCheckindata();
    }

    /**
     * 获取打卡规则
     *
     * @param req
     */
    public List<GetCheckinOptionResp.CheckinOption> getCheckinOption(GetCheckinOptionReq req) {
        ResponseEntity<GetCheckinOptionResp> resp = restTemplate.postForEntity(
                baseUrl + "/cgi-bin/checkin/getcheckinoption?access_token={access_token}",
                req.build(),
                GetCheckinOptionResp.class,
                new ModelMap("access_token", getAccessToken(CHECKIN_AGENT_CODE)));
        assertOK(resp);
        return resp.getBody().getInfo();
    }

    /**
     * 获取审批模板详情
     * 企业可通过审批应用或自建应用Secret调用本接口，获取企业微信“审批应用”内指定审批模板的详情
     * <p>
     * 1.审批应用的Secret可获取企业自建模板及第三方服务商添加的模板详情；自建应用的Secret可获取企业自建模板的模板详情。
     * 2.接口调用频率限制为600次/分钟。
     * </p>
     *
     * @param templateId 模板的唯一标识id 可在“获取审批单据详情”、“审批状态变化回调通知”中获得，也可在审批模板的模板编辑页面浏览器Url链接中获得
     * @return
     */
    public GetApprovalTemplateResp getApprovalTemplate(String templateId) {
        ResponseEntity<GetApprovalTemplateResp> resp = restTemplate.postForEntity(
                baseUrl + "/cgi-bin/oa/gettemplatedetail?access_token={access_token}",
                new ModelMap("template_id", templateId),
                GetApprovalTemplateResp.class,
                new ModelMap("access_token", getAccessToken(APPROVAL_AGENT_CODE)));
        assertOK(resp);
        return resp.getBody();
    }

    public void applyApproval(ApplyApprovalReq req) {

    }
}
