package com.uas.platform.b2b.dao;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

@Repository
public class CommonDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<Map<String, Object>> queryForList(String sql) {
		return this.jdbcTemplate.queryForList(sql);
	}

	public <T> List<T> queryForList(String sql, Class<T> elementType) {
		return this.jdbcTemplate.queryForList(sql, elementType);
	}

	public List<Map<String, Object>> queryForList(String sql, Object... args) {
		return this.jdbcTemplate.queryForList(sql, args);
	}

	public <T> List<T> queryForList(String sql, Object[] args, Class<T> elementType) {
		return this.jdbcTemplate.queryForList(sql, args, elementType);
	}

	public <T> List<T> queryForList(String sql, Class<T> elementType, Object... args) {
		return this.jdbcTemplate.queryForList(sql, elementType, args);
	}

	public <T> List<T> query(String sql, Object[] args, Class<T> elementType) {
		return this.jdbcTemplate.query(sql, args, new BeanPropertyRowMapper<T>(elementType));
	}

	public <T> List<T> query(String sql, Class<T> elementType) {
		return this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<T>(elementType));
	}

	public <T> List<T> query(String sql, Class<T> elementType, Object... args) {
		return this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<T>(elementType), args);
	}

	/**
	 * 查找自定义实体对象集合
	 * 
	 * @param sql
	 * @param args
	 * @param elementType
	 * @return
	 */
	public <T> T queryBean(String sql, Object[] args, Class<T> elementType) {
		try {
			return this.jdbcTemplate.queryForObject(sql, args, new BeanPropertyRowMapper<T>(elementType));
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	/**
	 * 查找自定义实体对象集合
	 * 
	 * @param sql
	 * @param elementType
	 * @return
	 */
	public <T> T queryBean(String sql, Class<T> elementType) {
		try {
			return this.jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<T>(elementType));
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	/**
	 * 查找自定义实体对象
	 * 
	 * @param sql
	 * @param elementType
	 * @param args
	 * @return
	 */
	public <T> T queryBean(String sql, Class<T> elementType, Object... args) {
		try {
			return this.jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<T>(elementType), args);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	public <T> T queryForObject(String sql, Object[] args, Class<T> elementType) {
		return this.jdbcTemplate.queryForObject(sql, args, elementType);
	}

	public <T> T queryForObject(String sql, Class<T> elementType) {
		return this.jdbcTemplate.queryForObject(sql, elementType);
	}

	public <T> T queryForObject(String sql, Class<T> elementType, Object... args) {
		return this.jdbcTemplate.queryForObject(sql, elementType, args);
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void save(List<?> beans, String tableName) {
		SqlParameterSource[] sqlSource = new SqlParameterSource[beans.size()];
		int i = 0;
		for (Object obj : beans) {
			sqlSource[i++] = new BeanPropertySqlParameterSource(obj);
		}
		SimpleJdbcInsert insertActor = new SimpleJdbcInsert(getJdbcTemplate());
		insertActor.setTableName(tableName);
		insertActor.executeBatch(sqlSource);
	}

}
