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.IDataSourceProvider;
import lombok.extern.slf4j.Slf4j;

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

/**
 * 前端入参数据源提供者实现
 * 从传入的请求数据中提取字段值
 *
 * @author hwc
 */
@Slf4j
public class DocTemplateRequestFieldDataSourceProvider implements IDataSourceProvider {

    private final Map<String, Object> requestData;

    public DocTemplateRequestFieldDataSourceProvider(Map<String, Object> requestData) {
        this.requestData = requestData != null ? requestData : Map.of();
        log.debug("创建前端入参数据源提供者，数据量: {}", this.requestData.size());
    }

    @Override
    public Map<String, Object> getData(Object context, List<String> fieldNames) {
        Map<String, Object> result = new HashMap<>();

        if (requestData == null || requestData.isEmpty()) {
            log.debug("前端入参数据为空");
            return result;
        }

        for (String fieldName : fieldNames) {
            if (requestData.containsKey(fieldName)) {
                Object value = requestData.get(fieldName);
                result.put(fieldName, value);
                log.debug("从前端入参获取字段值: {} = {}", fieldName, value);
            }
        }

        log.debug("前端入参数据源提供结果: {}", result.keySet());
        return result;
    }

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