package com.weaver.seconddev.chapanda.prop.dao;

import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity;
import com.weaver.seconddev.chapanda.prop.constant.Constants;
import com.weaver.seconddev.chapanda.prop.util.DatabaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class ParamConfigDao {

    private final static Logger log = LoggerFactory.getLogger(ParamConfigDao.class);

    @Autowired
    private DatabaseUtils databaseUtils;

    /**
     *
     * @return
     */
    public String queryEnvironmentValue(String pkey,String envType){
        String groupId = "weaver-ebuilder-form-service";
        String sourceType = "LOGIC";
        String value = "";
        try{
            String dataSql =" select uatvalue,devvalue,prodvalue from uf_config where pkey=? " +
                            " and tenant_key=? \n" +
                            " and delete_type=0" ;

            log.error("dataSql:" + dataSql);
            List<String> paramList = new ArrayList<>(100);
            paramList.add(pkey);
            paramList.add(Constants.TENANT_KEY);
            List<SqlParamEntity> sqlParamList = databaseUtils.querySqlParamEntity(paramList);
            Map<String, Object> result = databaseUtils.executeForQuery(sourceType, groupId, dataSql, sqlParamList);
            List<Map<String, Object>> recordList = databaseUtils.getDataSourceList(result);
            log.error("recordList:{}",recordList.size());
            if(recordList.size() > 0){
                Map<String, Object> recordMap = recordList.get(0);
                value = String.valueOf(recordMap.get(envType+"value"));
            }
        }catch (Exception e){
            log.error("e:{}", e);
        }
        return value;
    }

}
