package com.hotwj.platform.flow.listener;

import com.hotwj.platform.flow.event.StartFlowEvent;
import com.hotwj.platform.flow.service.ISysFlowService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 流程事件监听器
 *
 * @author shihongwei
 * @date 2026-01-26
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class FlowEventListener {

    private final ISysFlowService flowService;

    @EventListener
    public void handleStartFlowEvent(StartFlowEvent event) {
        log.info("收到启动流程事件: flowCode={}, businessId={}", event.getFlowCode(), event.getBusinessId());
        try {
            String instanceId = flowService.startFlow(
                    event.getFlowCode(),
                    event.getBusinessId(),
                    event.getCompanyId(),
                    event.getInitiatorId(),
                    event.getApproverId()
            );
            event.setInstanceId(instanceId);
        } catch (Exception e) {
            log.error("启动流程失败: flowCode={}, businessId={}", event.getFlowCode(), event.getBusinessId(), e);
        }
    }
}
