package com.ag.secp.platform.common;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.Map;

/**
 * Created by shaoqiang on 16/7/15.
 */
public class DaoUtil {
    private static SqlSessionFactory sqlSessionFactory = null;

    static {
        String resource = "daobase-config.xml";

        Reader reader = null;
        try {
            reader = Resources.getResourceAsReader(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }

        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    }


    public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    /**
     * @param sql
     * @return
     */
    public static String selectOne(String sql, Object param) {

        SqlSession session = DaoUtil.getSqlSessionFactory().openSession();
        String result = "";
        try {
            result = (String) session.selectOne(sql, param);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            session.close();
        }

    }

    /**
     *
     *
     * @param sql
     * @param map
     * @return
     */
    public static List selectList(String sql, Map map)
    {
        SqlSession session = DaoUtil.getSqlSessionFactory().openSession();
        List result;
        try {
            result = session.selectList(sql,map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            session.close();
        }
    }
    /**
     *
     *
     * @param sql
     * @param datastr @return
     */
    public static List selectListforString(String sql, String datastr)
    {
        SqlSession session = DaoUtil.getSqlSessionFactory().openSession();
        List result;
        try {
            result = session.selectList(sql,datastr);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            session.close();
        }
    }
    /**
     *
     *
     * @param sql
     * @param map
     * @return
     */
    public static long selectOneForLong(String sql, Map map)
    {
        SqlSession session = DaoUtil.getSqlSessionFactory().openSession();
        long result;
        try {
            result = ((Long)session.selectOne(sql,map)).longValue();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            session.close();
        }
    }

    /**
     *
     *
     * @param sql
     * @param map
     * @return
     */
    public static double selectOneFordouble(String sql, Map map)
    {
        SqlSession session = DaoUtil.getSqlSessionFactory().openSession();
        double result;
        try {
            result = ((Long) session.selectOne(sql,map)).doubleValue();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            session.close();
        }
    }
    /**
     *
     *
     * @param sql

     * @return
     */
    public static Object selectOneForObject(String sql , Map map)
    {
        SqlSession session = DaoUtil.getSqlSessionFactory().openSession();
        Object result;
        try {
            result = (Object)session.selectOne(sql,map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        } finally {
            session.close();
        }
    }

    public static void update(String sql,Map map)
    {
        SqlSession session = DaoUtil.getSqlSessionFactory().openSession();
        try {
            session.update(sql, map);

            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
    public static void delete(String sql,Object object)
    {
        SqlSession session = DaoUtil.getSqlSessionFactory().openSession();
        try {
            session.delete(sql, object);

            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

    public static void update(String sql,Object object)
    {
        SqlSession session = DaoUtil.getSqlSessionFactory().openSession();
        try {
            session.update(sql, object);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
    public static int insert(String sql,Object object)
    {
        int i=0;;
        SqlSession session = DaoUtil.getSqlSessionFactory().openSession();
        try {
            i=session.insert(sql, object);
            session.commit();
            return i;
        } catch (Exception e) {
            e.printStackTrace();
            return i;
        } finally {
            session.close();
        }
    }
}
