package com.yihu.hos.broker.daos;

import com.yihu.hos.broker.common.constants.BrokerConstant;
import com.yihu.hos.broker.models.SystemServiceEndpoint;
import com.yihu.hos.broker.models.SystemServiceFlow;
import com.yihu.hos.broker.models.SystemServiceFlowClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository("BrokerDao")
public class BrokerDao {
    public static final String BEAN_ID = "BrokerDao";

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<SystemServiceFlow> getSystemServiceFlowList() throws Exception {
        List<SystemServiceFlow> systemServiceFlowList = new ArrayList<>();;
        String sql = "select * from system_service_flow t where t.file_type='"+ BrokerConstant.CLASS +"'";
        List list = jdbcTemplate.queryForList(sql);
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Map map = (Map) iterator.next();
            SystemServiceFlow systemServiceFlow = new SystemServiceFlow();
            systemServiceFlow.setId((Integer) map.get("id"));
            systemServiceFlow.setCode((String) map.get("code"));
            systemServiceFlow.setName((String) map.get("name"));
            systemServiceFlow.setDescription((String) map.get("description"));
            systemServiceFlow.setPath((String) map.get("path"));
            systemServiceFlow.setChart((String) map.get("chart"));
            systemServiceFlow.setValid((Integer) map.get("valid"));
            systemServiceFlow.setCreateDate((Date) map.get("create_date"));
            systemServiceFlowList.add(systemServiceFlow);
        }

        return systemServiceFlowList;
    }

    public List<SystemServiceEndpoint> getSystemServiceEndpointList() throws Exception {
        List<SystemServiceEndpoint> systemServieEndpointList = new ArrayList<>();
        String sql = "select * from system_service_endpoint";
        List list = jdbcTemplate.queryForList(sql);
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Map map = (Map) iterator.next();
            SystemServiceEndpoint systemServiceEndpoint = new SystemServiceEndpoint();
            systemServiceEndpoint.setId((String) map.get("id"));
            systemServiceEndpoint.setCode((String) map.get("code"));
            systemServiceEndpoint.setName((String) map.get("name"));
            systemServieEndpointList.add(systemServiceEndpoint);
        }

        return systemServieEndpointList;
    }

    public List<SystemServiceFlowClass> getSystemServiceFlowClassList() throws Exception {
        List<SystemServiceFlowClass> systemServiceFlowClassList = new ArrayList<>();;
        String sql = "select * from system_service_flow_class";
        List list = jdbcTemplate.queryForList(sql);
        Iterator iterator = list.iterator();
        SystemServiceFlowClass systemServiceFlowClass = null;
        while (iterator.hasNext()) {
            Map map = (Map) iterator.next();
            systemServiceFlowClass = new SystemServiceFlowClass();
            systemServiceFlowClass.setId((Integer) map.get("id"));
            systemServiceFlowClass.setClassName((String) map.get("class_name"));
            systemServiceFlowClass.setPackageName((String) map.get("package_name"));
            systemServiceFlowClass.setClassPath((String) map.get("class_path"));
            systemServiceFlowClass.setFlowId((Integer) map.get("flow_id"));
            systemServiceFlowClass.setType((String) map.get("type"));
            systemServiceFlowClassList.add(systemServiceFlowClass);
        }
        return systemServiceFlowClassList;
    }
}
