package com.jeeplus.flowable.utils;

import com.jeeplus.flowable.service.FlowTaskService;
import com.jeeplus.sys.utils.SpringContextHolder;
import org.flowable.task.api.Task;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 徐滕
 * @version: 2022-12-08 15:34
 */
public class FlowTaskUtil {

    private FlowTaskService flowTaskService = SpringContextHolder.getBean(FlowTaskService.class);


    public <T> List<T> taskDispose(List<T> list) {

        List<Task> taskList = flowTaskService.getRecords();
        Method procInsIdMethod = null;
        Method processDefinitionIdMethod = null;
        Method statusMethod = null;
        for (T info : list ) {
            Class<? extends Object> tClass = info.getClass();
            //得到所有属性
            Field[] fields = tClass.getDeclaredFields();
            Class<? extends Object> superClass = tClass.getSuperclass();
            //得到所有属性
            Field[] superField = superClass.getDeclaredFields();
            Field[] field = concat(fields,superField);

            for (Task taskInfo : taskList) {
                Integer taskIdInteger = null;
                try {
                    for (Integer i=0; i<field.length; i++ ){
                        //获取属性的名字
                        String name = field[i].getName();
                        //将属性名字的首字母大写
                        name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());

                        if("ProcInsId".equals(name)){
                            field[i].setAccessible(true);
                            //整合出 getId() 属性这个方法
                            procInsIdMethod = tClass.getMethod("get"+name);

                        }else if("ProcessDefinitionId".equals(name)){
                            field[i].setAccessible(true);
                            //整合出 getId() 属性这个方法
                            processDefinitionIdMethod = tClass.getMethod("get"+name);

                        }else if("Status".equals(name)){
                            field[i].setAccessible(true);
                            //整合出 getId() 属性这个方法
                            statusMethod = tClass.getMethod("get"+name);

                        }else if("TaskId".equals(name)){
                            field[i].setAccessible(true);
                            //整合出 getId() 属性这个方法
                            taskIdInteger = i;

                        }
                    }

                    //调用这个整合出来的get方法，强转成自己需要的类型
                    String procInsId = (String)procInsIdMethod.invoke(info);
                    String processDefinitionId = (String)processDefinitionIdMethod.invoke(info);
                    String status = (String)statusMethod.invoke(info);

                    if(procInsId.equals(taskInfo.getProcessInstanceId()) && processDefinitionId.equals(taskInfo.getProcessDefinitionId()) && "4".equals(status)){
                        field[taskIdInteger].set(info,taskInfo.getId());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return list;

    }

    /**
     * 数据整合方法
     * @param a
     * @param b
     * @return
     */
    static Field[] concat(Field[] a, Field[] b) {

        Field[] c= new Field[a.length+b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

}
