package com.friday.custody.context;

import com.friday.custody.exception.RRException;
import com.friday.custody.service.basicService.TablePermissionService;
import com.friday.custody.service.logicService.DataProcessService;
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 javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
@Slf4j
public class DataProcessStrategyContext {
    private final Map<String, DataProcessService> dpStrategyMap = new ConcurrentHashMap<>();
    @Autowired
    private TablePermissionService tablePermissionService;
    @Resource
    public void dpStrategyContext(Map<String, DataProcessService> strategyMap) {
        strategyMap.forEach(this.dpStrategyMap::put);
    }
    public R getQueryDataResult(QueryDataReq vo){
        String service = getServiceNameByType(vo.getType());
        return dpStrategyMap.get(service).queryData(vo);
    }
    private String getServiceNameByType(String type){
        String serviceName = tablePermissionService.getServiceNameById(type);
        if (serviceName == null){
            log.warn("查询数据配置为空, 请配置后再启任务!");
            throw new RRException("查询数据配置为空, 请配置后再启任务! type:" + type);
        }
        return serviceName;
    }
}