package com.jy.flow.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.jy.flow.adapter.WarmFlowAdapter;
import com.jy.flow.model.vo.WarmFlowInteractiveTypeVo;
import com.jy.flow.service.HhDefService;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class HhDefServiceImpl implements HhDefService {

    private static final List<WarmFlowAdapter> WARM_FLOW_ADAPTERS = new ArrayList<>();

    /**
     * 初始化方法
     */
    @PostConstruct
    public void initMethod() {
        String[] beanNamesForType = SpringUtil.getBeanNamesForType(WarmFlowAdapter.class);
        for (String beanName : beanNamesForType) {
            WARM_FLOW_ADAPTERS.add(SpringUtil.getBean(beanName));
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean interactiveType(WarmFlowInteractiveTypeVo warmFlowInteractiveTypeVo) {
        Integer operatorType = warmFlowInteractiveTypeVo.getOperatorType();
        for (WarmFlowAdapter warmFlowAdapter : WARM_FLOW_ADAPTERS) {
            if (warmFlowAdapter.isAdapter(operatorType)) {
                return warmFlowAdapter.adapter(warmFlowInteractiveTypeVo);
            }
        }
        return false;
    }

}
