package com.malk.laidi.util;

import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.dingtalkworkflow_1_0.Client;
import com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceHeaders;
import com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceRequest;
import com.aliyun.dingtalkworkflow_1_0.models.StartProcessInstanceResponse;
import com.aliyun.tea.*;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;

import java.util.ArrayList;
import java.util.List;

public class ddutil {
    /**
     * 使用 Token 初始化账号Client
     *
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalkworkflow_1_0.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkworkflow_1_0.Client(config);
    }

    public static com.aliyun.dingtalkoauth2_1_0.Client createClient2() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkoauth2_1_0.Client(config);
    }
    public static String gettoken() throws Exception {
        com.aliyun.dingtalkoauth2_1_0.Client client = ddutil.createClient2();
        com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest getAccessTokenRequest = new com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest()
                .setAppKey("dingyt4ywno34wbxafo9")
                .setAppSecret("pvpAQhLKr3UmZA0nKPQAygycYcFPeR4vmnKKiLuBqFhN2ZU2U6LpOXejT9X-jC5k");
        try {
            GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest);
            return accessToken.body.accessToken;
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        }
        return "";
    }
    public static String startProcessInstance( String fqr,long fqbm,String fkr,List<StartProcessInstanceRequest.StartProcessInstanceRequestFormComponentValues> startProcessInstanceRequestFormComponentValues) throws Exception {
        Client client = ddutil.createClient();
        StartProcessInstanceHeaders startProcessInstanceHeaders = new StartProcessInstanceHeaders();
        startProcessInstanceHeaders.xAcsDingtalkAccessToken = gettoken();


        List<StartProcessInstanceRequest.StartProcessInstanceRequestTargetSelectActioners> startProcessInstanceRequestTargetSelectActioners = new ArrayList<>();
        StartProcessInstanceRequest.StartProcessInstanceRequestTargetSelectActioners Actioners1 = new StartProcessInstanceRequest.StartProcessInstanceRequestTargetSelectActioners();
        Actioners1.setActionerKey("manual_aba6_847d_0790_363d");
        List<String> userids = new ArrayList<>();
        userids.add(fkr);//付款人
        Actioners1.setActionerUserIds(userids);
        startProcessInstanceRequestTargetSelectActioners.add(Actioners1);



        StartProcessInstanceRequest startProcessInstanceRequest = new StartProcessInstanceRequest()
                .setOriginatorUserId(fqr)//发起人
                .setProcessCode("PROC-9D296C27-EF5A-4594-B32C-5AD47EF5CB7B")//表单编码
                .setDeptId(fqbm)//发起部门
                .setTargetSelectActioners(startProcessInstanceRequestTargetSelectActioners)
                .setFormComponentValues(startProcessInstanceRequestFormComponentValues);
        try {
            StartProcessInstanceResponse startProcessInstanceResponse = client.startProcessInstanceWithOptions(startProcessInstanceRequest, startProcessInstanceHeaders, new RuntimeOptions());
            String instanceId = startProcessInstanceResponse.body.getInstanceId();
            return instanceId;
        } catch (TeaException err) {
            if (!Common.empty(err.code) && !Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!Common.empty(err.code) && !Common.empty(err.message)) {
                // err 中含有 code 和 message 属性，可帮助开发定位问题
            }

        }
        return "";
    }

}
