package com.qqchen.deploy.backend.workflow.listener.business;

import com.qqchen.deploy.backend.workflow.dto.event.WorkflowInstanceStatusChangeEvent;
import com.qqchen.deploy.backend.workflow.service.IWorkflowInstanceService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
public class WorkflowInstanceStatusChangeListener {

    @Resource
    private IWorkflowInstanceService workflowInstanceService;

    @EventListener
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void handleWorkflowStatusChange(WorkflowInstanceStatusChangeEvent event) {
//        log.info("Handling workflow status change event: {}", event);
        workflowInstanceService.updateInstanceStatus(event.getProcessInstanceId(), event.getStatus(), event.getEndTime());
    }
}
