package com.tzld.piaoquan.ad.engine.service.remote;

import com.tzld.piaoquan.recommend.feature.client.FeatureV2Client;
import com.tzld.piaoquan.recommend.feature.model.feature.FeatureKeyProto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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


/**
 * @author dyp
 */
@Component
@Slf4j
public class FeatureV2RemoteService {

    @Autowired
    private FeatureV2Client client;

    public Map<String, String> getFeature(List<FeatureKeyProto> protos) {
        if (CollectionUtils.isEmpty(protos)) {
            log.debug("FeatureV2RemoteService.getFeature: protos is empty");
            return Collections.emptyMap();
        }
        try {
            long start = System.currentTimeMillis();
            Map<String, String> result = client.multiGetFeature(protos);
            log.debug("FeatureV2RemoteService.getFeature: protos.size={}, result.size={}, costTime={}",
                    protos.size(), result != null ? result.size() : 0, System.currentTimeMillis() - start);
            return result;
        } catch (Exception e) {
            log.error("FeatureV2RemoteService.getFeature: unexpected exception", e);
            return Collections.emptyMap();
        }
    }

}
