package com.muzhi.dingtalk.test.service.impl;


import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiProcessinstanceGetRequest;
import com.dingtalk.api.request.OapiProcessinstanceListidsRequest;
import com.dingtalk.api.response.OapiProcessinstanceGetResponse;
import com.dingtalk.api.response.OapiProcessinstanceListidsResponse;
import com.muzhi.dingtalk.test.service.DingAccessTokenService;
import com.muzhi.dingtalk.test.service.ShenpiService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShenpiServiceImpl implements ShenpiService {

    @Autowired
    private DingAccessTokenService dingAccessTokenService;

    public static String shenpiIdList;
    public static String shenpiXQ;

    //TODO 分页获取审批实例id列表
    @Override
    public String getShenpiIdList(){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
        OapiProcessinstanceListidsRequest request = new OapiProcessinstanceListidsRequest();
        try {
            request.setProcessCode("PROC-FF6Y2xxxx");
            request.setStartTime(1599148800000L);
            request.setEndTime(1599321599000L);
            request.setSize(10L);
            request.setCursor(0L);
            request.setUseridList("manager1,manager2");
            OapiProcessinstanceListidsResponse response = client.execute(request, dingAccessTokenService.getAccessToken());
            shenpiIdList = response.getBody();
            System.out.println(response.getBody());
        }catch (ApiException e){
            e.printStackTrace();
        }
        return shenpiIdList;
    }

    //TODO 获取审批实例详情
    @Override
    public String getShenpiXQ(){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
        OapiProcessinstanceGetRequest request = new OapiProcessinstanceGetRequest();
        try {
            request.setProcessInstanceId("1a2b-3e4dxxxx");
            OapiProcessinstanceGetResponse response = client.execute(request, dingAccessTokenService.getAccessToken());
            shenpiXQ = response.getBody();
            System.out.println(response.getBody());
        }catch (ApiException e){
            e.printStackTrace();
        }

        return shenpiXQ;
    }
}
