package com.zt.plat.module.base.service.doctemplate.render.provider;

import com.zt.plat.module.base.service.doctemplate.render.DocTemplateDataSourceType;
import com.zt.plat.module.base.service.doctemplate.render.IBusinessDataProvider;
import com.zt.plat.module.base.service.doctemplate.render.IDataSourceProvider;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * 业务数据源提供者实现
 * 包装业务数据提供者，提供统一的数据源接口
 *
 * @author hwc
 */
@Slf4j
public class DocTemplateBusinessFieldDataSourceProvider implements IDataSourceProvider {

    private final IBusinessDataProvider businessDataProvider;

    public DocTemplateBusinessFieldDataSourceProvider(IBusinessDataProvider businessDataProvider) {
        this.businessDataProvider = businessDataProvider;
        log.debug("创建业务数据源提供者: {}", businessDataProvider.getClass().getSimpleName());
    }

    @Override
    public Map<String, Object> getData(Object context, List<String> fieldNames) {
        if (businessDataProvider == null) {
            log.warn("业务数据提供者为空");
            return Map.of();
        }

        try {
            log.debug("调用业务数据提供者获取数据，上下文: {}, 字段数量: {}", context, fieldNames.size());
            Map<String, Object> data = businessDataProvider.provideData(context, fieldNames);
            log.debug("业务数据提供者返回数据: {}", data.keySet());
            return data;
        } catch (Exception e) {
            log.error("业务数据提供者获取数据失败", e);
            return Map.of();
        }
    }

    @Override
    public DocTemplateDataSourceType getType() {
        return DocTemplateDataSourceType.BUSINESS;
    }
}