package com.friday.custody.context;

import com.friday.custody.exception.RRException;
import com.friday.custody.service.basicService.PublicDataBasicService;
import com.friday.custody.service.basicService.impl.PublicDataDirectoryServiceImpl;
import com.friday.custody.req.QueryDataReq;
import com.friday.custody.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
@Slf4j
public class PublicDataStrategyContext {
    private final Map<String, PublicDataBasicService> publicDataStrategyMap = new ConcurrentHashMap<>();
    @Autowired
    private PublicDataDirectoryServiceImpl publicDataDirectoryService;


    @Autowired
    public void dpStrategyContext(Map<String, PublicDataBasicService> strategyMap) {
        strategyMap.forEach(this.publicDataStrategyMap::put);
    }


    public R getPublicData(QueryDataReq vo) {
        String service = getServiceNameByType(vo.getType());
        return publicDataStrategyMap.get(service).queryData(vo);
    }


    private String getServiceNameByType(String type) {
        String serviceName = publicDataDirectoryService.queryByType(type);
        if (serviceName == null) {
            log.warn("查询数据配置为空, 请联系管理员配置后再启任务!");
            throw new RRException("查询数据配置为空, 请联系管理员配置后再启任务! type:" + type);
        }
        return serviceName;
    }
}