package net.ferrum.workflow.listener;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.ferrum.flowable.utils.TaskUtils;
import net.ferrum.workflow.service.IWfProcessService;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.springframework.stereotype.Component;

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

/**
 * 发布规格事件监听类
 *
 * @author 26554
 * @version 1.0.0
 * @since 2025/2/18
 */

@Slf4j
@Component("publishSpecificationListener")
@RequiredArgsConstructor
public class PublishSpecificationListener implements ExecutionListener {

    private final IWfProcessService processService;
    private final TaskService taskService;

    @Override
    public void notify(DelegateExecution delegateExecution) {
        TaskQuery taskQuery = taskService.createTaskQuery()
            .active()
            .includeProcessVariables()
            .taskCandidateOrAssigned(TaskUtils.getUserId())
            .taskCandidateGroupIn(TaskUtils.getCandidateGroup())
            .processDefinitionId(delegateExecution.getProcessDefinitionId())
            .orderByTaskCreateTime().desc();
        List<Task> taskList = taskQuery.list();
        taskList.forEach(task -> {
            Field[] fields = task.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    log.info("属性[{}]值为：{}", field.getName(), field.get(task));
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }


}
