package com.yihu.hos.system.dao;

import com.yihu.hos.system.model.SystemDatasource;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.model.DataGridResult;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;
import java.util.Map;

/**
 * 数据源管理
 * Created by hzp on 2016/1/16.
 */
@Repository("DatasourceDao")
public class DatasourceDao extends SQLGeneralDAO {

    public static final String BEAN_ID = "DatasourceDao";

    /**
     * 测试连接数据源
     */
   
    public void testDatasource(String type,String config) throws Exception
    {
        String driver = "";
        switch(type.toLowerCase())
        {
            case "mysql"://"MYSQL"
            {
                driver = "com.mysql.jdbc.Driver";
                break;
            }
            case "oracle"://ORACLE
            {
                driver = "oracle.jdbc.driver.OracleDriver";
                break;
            }
            case "sqlserver"://SQL SERVER
            {
                driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
                break;
            }
        }

        Class.forName(driver).newInstance();
        Connection con = DriverManager.getConnection(config);
        con.close();

    }

    /**
     * 获取数据源列表
     * @return
     */
   
    public DataGridResult getDatasource(Map<String, Object> conditionMap, Integer page, Integer pageSize) throws Exception
    {
        StringBuilder sb = new StringBuilder();
        sb.append("from SystemDatasource t where 1=1 ");
        if (!StringUtils.isEmpty(conditionMap.get("orgId"))) //组织ID
        {
            sb.append(" and t.orgId = '" + conditionMap.get("orgId") + "'");
        }
        if (!StringUtils.isEmpty(conditionMap.get("type"))) //数据源类型
        {
            sb.append(" and t.type = '" + conditionMap.get("type") + "'");
        }
        if (!StringUtils.isEmpty(conditionMap.get("name"))) {
            sb.append(" and t.name like '%" + conditionMap.get("name") + "%'");
        }
        return getDataGridResult(sb.toString(),page,pageSize);
    }

    /**
     * 获取全部数据源列表
     * @return
     */
   
    public DataGridResult getDatasourceByOrg(String orgId) throws Exception
    {
        DataGridResult re = new DataGridResult();
        String sql = "select * from system_datasource";
        if(orgId!=null&&orgId.length()>0){
            sql += " where org_id ='"+orgId+"'";
        }

        List<SystemDatasource> list = super.queryListBySql(sql,SystemDatasource.class);
        re.setDetailModelList(list);

        return re;
    }

    /**
     * 修改数据源状态(暂弃)
     */
   
    public void validDatasource(String id,String valid)  throws Exception
    {
        String sql = "update system_datasource set activity_Flag='"+valid+"' where Id='"+id+"'";
        super.execute(sql);
    }

   
    public List<SystemDatasource> getDatasources(String orgId) throws Exception {
        String sql = "select * from system_datasource";
        if(orgId!=null&&orgId.length()>0){
            sql += " where org_id ='"+orgId+"'";
        }

        List<SystemDatasource> list = super.queryListBySql(sql,SystemDatasource.class);
        return list;
    }
}
